Skip to content
Snippets Groups Projects
test_hosts.py 4.3 KiB
Newer Older
from rest_framework.test import APITransactionTestCase

from program.tests import HostMixin, ImageMixin, SteeringTestCaseMixin, UserMixin


class HostViewTestCase(
    HostMixin, ImageMixin, UserMixin, SteeringTestCaseMixin, APITransactionTestCase
):
    reset_sequences = True

    def test_create_host(self):
        client = self._get_client(self.user_admin)
        url = self._url("hosts")
        host_data = {"name": "NAME"}

        response = client.post(url, data=host_data)

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data["name"], host_data["name"])

    def test_create_host_forbidden_for_common_user(self):
        client = self._get_client()
        url = self._url("hosts")

        response = client.post(url, data={"name": "NAME"})

        self.assertEqual(response.status_code, 403)

    def test_destroy_host(self):
        host = self._create_host()
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)

        response = client.delete(url)

        self.assertEqual(response.status_code, 204)

    def test_destroy_host_forbidden_for_common_user(self):
        host = self._create_host()
        client = self._get_client(self.user_common)
        url = self._url("hosts", host.id)

        response = client.delete(url)

        self.assertEqual(response.status_code, 403)

    def test_list_hosts(self):
        client = self._get_client()
        url = self._url("hosts")

        for _ in range(3):
            self._create_host()

        response = client.get(url)

        self.assertEqual(len(response.data), 3)

    def test_retrieve_host(self):
        data = {"biography": "BIOGRAPHY", "email": "host@aura.radio", "name": "NAME"}
        host = self._create_host(**data)
        client = self._get_client()
        url = self._url("hosts", host.id)

        response = client.get(url)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["biography"], data["biography"])
        self.assertEqual(response.data["email"], data["email"])
        self.assertEqual(response.data["name"], data["name"])

    def test_update_biography(self):
        host = self._create_host()
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)
        update = {"biography": "BIOGRAPHY"}

        response = client.patch(url, data=update)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["biography"], update["biography"])

    def test_update_email(self):
        host = self._create_host()
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)
        update = {"email": "host@aura.radio"}

        response = client.patch(url, data=update)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["email"], update["email"])

    def test_update_image(self):
        host = self._create_host()
        image = self._create_image(owner=self.user_admin)
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)
        update = {"image": image.id}

        response = client.patch(url, data=update)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["image"], image.id)

    def test_update_is_active(self):
        host = self._create_host()
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)
        update = {"is_active": False}

        response = client.patch(url, data=update)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["is_active"], update["is_active"])

    def test_update_name(self):
        host = self._create_host()
        client = self._get_client(self.user_admin)
        url = self._url("hosts", host.id)
        update = {"name": "HOST"}

        response = client.patch(url, data=update)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data["name"], update["name"])

    def test_update_host_forbidden_for_common_user(self):
        host = self._create_host()
        client = self._get_client(self.user_common)
        url = self._url("hosts", host.id)

        response = client.patch(url, data={"email": "host@aura.radio"})

        self.assertEqual(response.status_code, 403)