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

from django.conf import settings
from django.core.urlresolvers import reverse

from mtp.apps.accounts.factories import TrainerFactory

from ..factories import WorkshopStepImageFactory

from .test_cases import WorkshopTestCase


class WorkshopDeleteViewTestCase(WorkshopTestCase):
    """Test case that tests the ``WorkshopDeleteView`` view."""

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

        cls.workshop_delete_url = cls.workshop.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.workshop_delete_url, {})

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

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

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

        """

        self.login_as_trainer()

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

    def test_quiz_is_soft_deleted(self):
        """Test that the quiz is soft deleted.

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

        """

        self.login_as_trainer()

        self.client.post(self.workshop_delete_url, {})
        self.workshop.refresh_from_db()
        self.assertTrue(self.workshop.is_deleted)


class XLSQuizImportViewTestCase(WorkshopTestCase):
    """Test the ``XLSQuizImportView`` view."""

    def test_template_example_file(self):
        """Test the ``template_example_file`` variable exists."""

        self.login_as_trainer()
        response = self.client.get(reverse('workshops:import-xls', kwargs={
            'training_uuid': self.training.id,
        }))

        self.assertIn('template_example_file', response.context)
        self.assertIsNotNone(response.context['template_example_file'])


class XLSQuizExportViewTestCase(WorkshopTestCase):
    """Test the ``XLSQuizExportView`` view."""

    def test_the_view_should_raise_a_404_if_the_workshop_is_not_exportable(self):
        """Test that the view should raise a 404 if the workshop is not exportable"""

        self.login_as_trainer()

        # We remove the "image" step, so we can fetch the export url
        self.workshop_question_mc_image.delete()

        export_url = self.workshop.get_export_url()

        # The route is working
        response = self.client.get(export_url)
        self.assertEqual(response.status_code, 200)

        # We add a new "image" step
        WorkshopStepImageFactory(
            workshop=self.workshop
        )

        # The route should raise a 404, because we do have an "image" step
        response = self.client.get(export_url)
        self.assertEqual(response.status_code, 404)
