test_need_validate_cgu_rgpd.py 1.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from django.test import TestCase
from rest_framework.test import APIClient

from base_app.models import User


class HasAccessTestCase(TestCase):
    """
    Test to check that validation of the CGU and RGPD is Required
    """

    @classmethod
    def setUpTestData(cls):
        password = "123456"

        cls.has_validated_user = User.objects.create_user(
            username="yolo", email="osef@master.fr", password=password
        )
19
        cls.has_validated_user.has_validated_cgu_rgpd = True
20 21 22 23 24
        cls.has_validated_user.save()

        cls.has_not_validated_user = User.objects.create_user(
            username="yolo2", email="osef@master.fr", password=password
        )
25
        cls.has_not_validated_user.has_validated_cgu_rgpd = False
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        cls.has_not_validated_user.save()

        cls.has_validated_client = APIClient()
        cls.has_validated_client.login(
            username=cls.has_validated_user.username, password=password
        )

        cls.has_not_validated_client = APIClient()
        cls.has_not_validated_client.login(
            username=cls.has_not_validated_user.username, password=password
        )

    def test_accepted_has_access(self):
        response = self.has_validated_client.get("/api/")
        self.assertEqual(response.status_code, 200)

    def test_not_accepted_refused(self):
        response = self.has_not_validated_client.get("/api/")
        self.assertEqual(response.status_code, 302)  # We have a redirect
45
        self.assertEqual(response.url, "/cgu-rgpd/?next=/api/")