"""This module contains the tests for the views of the ``brainstormers`` app"""

from ..factories import BrainstormerFactory, BrainstormerPropositionGroupFactory

from .test_cases import BrainstormerTestCase


class BrainstormerViewTestCase(BrainstormerTestCase):
    """Test the views related to the ``Brainstormer`` model"""

    @classmethod
    def setUpTestData(cls):
        """Set up the test data"""
        super().setUpTestData()

        # We will need a brainstormer for the session
        cls.brainstormer = BrainstormerFactory()
        cls.group = BrainstormerPropositionGroupFactory(brainstormer=cls.brainstormer)

    def setUp(self):
        """SetUp data for each method"""

        self.training_session.refresh_from_db()

    def test_all_routes_should_work(self):
        """Test the response_code of all the brainstormer related routes"""

        self.login_as_trainer()

        urls_to_test = [
            self.training_session.get_brainstormer_list_url(),
            self.training_session.get_brainstormer_import_url(),
            self.training_session.get_brainstormer_create_url(),
            self.brainstormer.get_detail_url(),
            self.brainstormer.get_update_url(),
            self.brainstormer.get_delete_url(),
            self.brainstormer.get_export_xls_url(),
            self.brainstormer.get_export_csv_url(),
            self.group.get_create_proposition_url(),
        ]

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

    def test_create_brainstormer_not_allowed_on_closed_session(self):
        """A metaplan can not be created on a closed session"""

        self.training_session.close()

        create_url = self.training_session.get_brainstormer_create_url()

        # The GET method for creation should raise 403 on closed session
        response = self.client.get(create_url)
        self.assertEqual(response.status_code, 403)

        # The POST method for creation should raise 403 on closed session
        response = self.client.post(create_url, {})
        self.assertEqual(response.status_code, 403)

    def test_import_brainstormer_not_allowed_on_closed_session(self):
        """A metaplan can not be created on a closed session"""

        self.training_session.close()

        import_url = self.training_session.get_brainstormer_import_url()

        # The GET method for import should raise 403 on closed session
        response = self.client.get(import_url)
        self.assertEqual(response.status_code, 403)

        # The POST method for import should raise 403 on closed session
        response = self.client.post(import_url, {})
        self.assertEqual(response.status_code, 403)
