"""Test cases for the ``documents`` views."""

from ..models import Document

from .test_cases import DocumentTestCase


class DocumentViewsTestCase(DocumentTestCase):
    """Test case that tests the ``Document`` related views."""

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        cls.urls_to_test = [
            Document.get_create_url(cls.training),
            cls.document.get_detail_url(),
            cls.document.get_edit_url(),
            cls.document.get_delete_url(),
        ]

        cls.training.version_state = cls.training.VERSION_STATES.DRAFT
        cls.training.save()

    def test_non_training_owner_should_not_have_access(self):
        """Test that a user not owner shouldn't be allowed to access document."""

        self.login_as_admin()

        for url in self.urls_to_test:
            with self.subTest(url=url):
                response = self.client.get(url)
                self.assertEqual(response.status_code, 404)

    def test_training_owner_should_have_access(self):
        """Test that user of document should be allowed to access the document."""

        self.login_as_trainer()

        for url in self.urls_to_test:
            with self.subTest(url=url):
                response = self.client.get(url)
                self.assertEqual(response.status_code, 200)

    def test_delete_document_view_should_work(self):
        """Test that the document is soft deleted.

        Notes
        -----
        The training of the workshop should be in a ``DRAFT`` state.

        """

        self.login_as_trainer()

        self.client.post(self.document.get_delete_url(), {})
        self.document.refresh_from_db()
        self.assertTrue(self.document.is_deleted)
