utils.py 2.64 KB
Newer Older
1
2
from django.contrib.auth.models import Group
from django.test import TestCase
3
4
5
from rest_framework.test import APIClient

from base_app.models import User
6
7
8


class WithUserTestCase(TestCase):
Florent Chehab's avatar
Florent Chehab committed
9
10
    @classmethod
    def setUpTestData(cls):
11
        password = "123456"
12

Florent Chehab's avatar
Florent Chehab committed
13
        cls.staff_user = User.objects.create_user(
14
            username="staff_member", email="master@master.fr", password=password
15
        )
Florent Chehab's avatar
Florent Chehab committed
16
17
        cls.staff_user.is_staff = True
        cls.staff_user.save()
18

Florent Chehab's avatar
Florent Chehab committed
19
        cls.moderator_user = User.objects.create_user(
20
21
22
            username="moderator_member",
            email="moderator@moderator.fr",
            password=password,
23
        )
24
        cls.moderator_group = Group.objects.get_or_create(name="Moderators")[0]
Florent Chehab's avatar
Florent Chehab committed
25
26
        cls.moderator_group.user_set.add(cls.moderator_user)
        cls.moderator_group.save()
27

28
        cls.dri_user = User.objects.create_user(
29
            username="dri_member", email="dri@dri.fr", password=password
30
        )
31
        cls.dri_group = Group.objects.get_or_create(name="DRI")[0]
32
33
34
        cls.dri_group.user_set.add(cls.dri_user)
        cls.dri_group.save()

Florent Chehab's avatar
Florent Chehab committed
35
        cls.authenticated_user = User.objects.create_user(
36
37
38
            username="authenticated_user",
            email="authenticated@authenticated.fr",
            password=password,
39
40
        )

Florent Chehab's avatar
Florent Chehab committed
41
        cls.authenticated_user_2 = User.objects.create_user(
42
43
44
            username="authenticated_user_2",
            email="authenticated_2@authenticated.fr",
            password=password,
45
46
        )

Florent Chehab's avatar
Florent Chehab committed
47
        cls.staff_client = APIClient()
48
        cls.staff_client.login(username=cls.staff_user.username, password=password)
49

Florent Chehab's avatar
Florent Chehab committed
50
51
        cls.moderator_client = APIClient()
        cls.moderator_client.login(
52
            username=cls.moderator_user.username, password=password
53
54
        )

55
        cls.dri_client = APIClient()
56
        cls.dri_client.login(username=cls.dri_user.username, password=password)
57

Florent Chehab's avatar
Florent Chehab committed
58
59
        cls.authenticated_client = APIClient()
        cls.authenticated_client.login(
60
            username=cls.authenticated_user.username, password=password
Florent Chehab's avatar
Florent Chehab committed
61
62
63
64
        )

        cls.authenticated_client_2 = APIClient()
        cls.authenticated_client_2.login(
65
            username=cls.authenticated_user_2.username, password=password
Florent Chehab's avatar
Florent Chehab committed
66
67
        )

68
69
70
71
72
73
74
75
76
77
78
        for user in [
            cls.staff_user,
            cls.moderator_user,
            cls.dri_user,
            cls.authenticated_user,
            cls.authenticated_user_2,
        ]:
            user.has_validated_cgu = True
            user.has_validated_rgpd = True
            user.save()

Florent Chehab's avatar
Florent Chehab committed
79
        cls.unauthenticated_client = APIClient()
80

Florent Chehab's avatar
Florent Chehab committed
81
        cls.setUpMoreTestData()
82

Florent Chehab's avatar
Florent Chehab committed
83
84
    @classmethod
    def setUpMoreTestData(cls):
85
        pass