|
1
|
|
|
from rest_framework import status |
|
2
|
|
|
from rest_framework.test import APITestCase, force_authenticate |
|
3
|
|
|
|
|
4
|
|
|
from sigma_core.models.group import Group |
|
5
|
|
|
from sigma_core.models.school import School |
|
6
|
|
|
from sigma_core.serializers.group import GroupSerializer |
|
7
|
|
|
from sigma_core.serializers.school import SchoolSerializer |
|
8
|
|
|
from sigma_core.tests.factories import UserFactory, GroupFactory, SchoolFactory, GroupMemberFactory |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def reload(obj): |
|
12
|
|
|
return obj.__class__.objects.get(pk=obj.pk) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class SchoolTests(APITestCase): |
|
16
|
|
|
@classmethod |
|
17
|
|
|
def setUpTestData(self): |
|
18
|
|
|
super(SchoolTests, self).setUpTestData() |
|
19
|
|
|
|
|
20
|
|
|
# Schools |
|
21
|
|
|
self.schools = SchoolFactory.create_batch(2) |
|
22
|
|
|
|
|
23
|
|
|
# Users |
|
24
|
|
|
self.users = UserFactory.create_batch(4) |
|
25
|
|
|
self.users[2].is_staff = True # Sigma admin |
|
26
|
|
|
self.users[2].save() |
|
27
|
|
|
|
|
28
|
|
|
# Memberships |
|
29
|
|
|
self.member1 = GroupMemberFactory(user=self.users[0], group=self.schools[0], perm_rank=Group.ADMINISTRATOR_RANK) |
|
30
|
|
|
self.member2 = GroupMemberFactory(user=self.users[1], group=self.schools[0], perm_rank=1) |
|
31
|
|
|
|
|
32
|
|
|
serializer = SchoolSerializer(self.schools[0]) |
|
33
|
|
|
self.school_data = serializer.data |
|
34
|
|
|
self.schools_url = "/school/" |
|
35
|
|
|
self.school_url = self.schools_url + "%d/" |
|
36
|
|
|
|
|
37
|
|
|
self.new_school_data = {"name": "Ecole polytechnique", "design": "default"} |
|
38
|
|
|
# self.invite_data = {"user": self.users[0].id} |
|
39
|
|
|
|
|
40
|
|
|
#### List requests |
|
41
|
|
|
def test_get_schools_list_unauthed(self): |
|
42
|
|
|
# Client not authenticated but can see schools list |
|
43
|
|
|
response = self.client.get(self.schools_url) |
|
44
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
45
|
|
|
self.assertEqual(len(response.data), len(self.schools)) |
|
46
|
|
|
|
|
47
|
|
|
def test_get_schools_list_ok(self): |
|
48
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
49
|
|
|
response = self.client.get(self.schools_url) |
|
50
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
51
|
|
|
self.assertEqual(len(response.data), len(self.schools)) |
|
52
|
|
|
|
|
53
|
|
|
#### Get requests |
|
54
|
|
|
def test_get_school_unauthed(self): |
|
55
|
|
|
# Client is not authenticated and cannot see school details (especially members) |
|
56
|
|
|
response = self.client.get(self.school_url % self.schools[0].id) |
|
57
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
58
|
|
|
|
|
59
|
|
|
def test_get_school_forbidden(self): |
|
60
|
|
|
# Client wants to see a a school whose he is not member of |
|
61
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
62
|
|
|
response = self.client.get(self.school_url % self.schools[1].id) |
|
63
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
64
|
|
|
|
|
65
|
|
|
def test_get_school_ok(self): |
|
66
|
|
|
# Client wants to see a school to which he belongs |
|
67
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
68
|
|
|
response = self.client.get(self.school_url % self.schools[0].id) |
|
69
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
70
|
|
|
self.assertEqual(response.data, self.school_data) |
|
71
|
|
|
|
|
72
|
|
|
#### Create requests |
|
73
|
|
|
def test_create_school_unauthed(self): |
|
74
|
|
|
response = self.client.post(self.schools_url, self.new_school_data) |
|
75
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
76
|
|
|
|
|
77
|
|
|
def test_create_school_forbidden(self): |
|
78
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
79
|
|
|
response = self.client.post(self.schools_url, self.new_school_data) |
|
80
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
81
|
|
|
|
|
82
|
|
|
def test_create_school_wrong_data(self): |
|
83
|
|
|
self.client.force_authenticate(user=self.users[2]) |
|
84
|
|
|
response = self.client.post(self.schools_url, {"name": ""}) |
|
85
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
|
86
|
|
|
|
|
87
|
|
|
def test_create_school_ok(self): |
|
88
|
|
|
self.client.force_authenticate(user=self.users[2]) |
|
89
|
|
|
response = self.client.post(self.schools_url, self.new_school_data) |
|
90
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
|
91
|
|
|
self.assertEqual(response.data['name'], "Ecole polytechnique") |
|
92
|
|
|
self.assertEqual(response.data['visibility'], Group.VIS_PUBLIC) |
|
93
|
|
|
self.assertEqual(response.data['default_member_rank'], -1) |
|
94
|
|
|
self.assertEqual(response.data['req_rank_invite'], Group.ADMINISTRATOR_RANK) |
|
95
|
|
|
|
|
96
|
|
|
#### Modification requests |
|
97
|
|
|
def test_update_school_unauthed(self): |
|
98
|
|
|
self.school_data['name'] = "Ecole polytechnique" |
|
99
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
100
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
101
|
|
|
|
|
102
|
|
|
def test_update_school_forbidden_1(self): |
|
103
|
|
|
self.client.force_authenticate(user=self.users[3]) |
|
104
|
|
|
self.school_data['name'] = "Ecole polytechnique" |
|
105
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
106
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
107
|
|
|
|
|
108
|
|
|
def test_update_school_forbidden_2(self): |
|
109
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
110
|
|
|
self.school_data['name'] = "Ecole polytechnique" |
|
111
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
112
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
113
|
|
|
|
|
114
|
|
|
def test_update_school_wrong_data(self): |
|
115
|
|
|
self.client.force_authenticate(user=self.users[2]) |
|
116
|
|
|
self.school_data['name'] = "" |
|
117
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
118
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
|
119
|
|
|
|
|
120
|
|
|
def test_update_school_ok_staff(self): |
|
|
|
|
|
|
121
|
|
|
self.client.force_authenticate(user=self.users[2]) |
|
122
|
|
|
self.school_data['name'] = "Ecole polytechnique" |
|
123
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
124
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
125
|
|
|
self.assertEqual(response.data['name'], "Ecole polytechnique") |
|
126
|
|
|
|
|
127
|
|
|
def test_update_school_ok_school_admin(self): |
|
|
|
|
|
|
128
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
129
|
|
|
self.school_data['name'] = "Ecole polytechnique" |
|
130
|
|
|
response = self.client.put(self.school_url % self.school_data['id'], self.school_data) |
|
131
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
132
|
|
|
self.assertEqual(response.data['name'], "Ecole polytechnique") |
|
133
|
|
|
|
|
134
|
|
|
#### Invitation process |
|
135
|
|
|
|
|
136
|
|
|
#### Deletion requests |
|
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.