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

from django.conf import settings

from mtp.apps.accounts.factories import TrainerFactory

from .test_cases import DocumentTestCase


class DocumentDeleteViewTestCase(DocumentTestCase):
    """Test case that tests the ``DocumentDeleteView`` view."""

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

        cls.document_delete_url = cls.document.get_delete_url()
        cls.training.version_state = cls.training.VERSION_STATES.DRAFT
        cls.training.save()

    def test_user_not_owner_should_be_rejected(self):
        """Test that a user not owner should't be allowed to soft delete quiz."""

        trainer_not_owner = TrainerFactory()  # this trainer is not owner of ``self.quiz``
        self.login_as_trainer(username=trainer_not_owner.username)
        response = self.client.post(self.document_delete_url, {})

        self.assertRedirects(response, settings.LOGIN_URL + '?next=' + self.document_delete_url)

    def test_owner_should_be_accepted(self):
        """Test that user of document should be allowed to soft delete the document.

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

        """

        self.login_as_trainer()

        response = self.client.post(self.document_delete_url, {}, follow=True)
        self.assertEqual(response.status_code, 200)

    def test_document_is_soft_deleted(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_delete_url, {})
        self.document.refresh_from_db()
        self.assertTrue(self.document.is_deleted)
