Completed
Push — master ( ebb95c...951ec2 )
by Camille
01:25
created

GroupFieldTests.test_list_not_authed()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 3
c 2
b 0
f 0
rs 10
cc 1
1
import json
2
3
from rest_framework import status
4
from rest_framework.test import APITestCase
5
6
from sigma_core.models.user import User
7
from sigma_core.models.group import Group
8
from sigma_core.models.group_member import GroupMember
9
from sigma_core.models.group_field import GroupField
10
from sigma_core.models.validator import Validator
11
from sigma_core.tests.factories import UserFactory, GroupFieldFactory, GroupFactory, GroupMemberFactory
12
13
14
class GroupFieldTests(APITestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable APITestCase does not seem to be defined.
Loading history...
15
    fixtures = ['fixtures_prod.json']
16
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
17
    def setUpTestData(self):
18
        super(APITestCase, self).setUpTestData()
19
20
        # Routes
21
        self.group_field_url = "/group-field/"
22
23
        # Group open to anyone
24
        self.group = GroupFactory()
25
26
        # Users already in group
27
        # User[0]: Not in Group
28
        # User[1]: Requested join, not accepted
29
        # User[2]: Group member
30
        # User[3]: Group admin
31
        self.users = [UserFactory(), UserFactory(), UserFactory(), UserFactory()]
32
        # Associated GroupMember
33
        self.group_member = [
34
                None,
35
                GroupMemberFactory(user=self.users[1], group=self.group, perm_rank=0),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
36
                GroupMemberFactory(user=self.users[2], group=self.group, perm_rank=1),
37
                GroupMemberFactory(user=self.users[3], group=self.group, perm_rank=Group.ADMINISTRATOR_RANK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Group does not seem to be defined.
Loading history...
38
            ]
39
        self.validator_none = Validator.objects.all().get(html_name=Validator.VALIDATOR_NONE)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Validator does not seem to be defined.
Loading history...
40
        self.validator_text = Validator.objects.all().get(html_name=Validator.VALIDATOR_TEXT)
41
        self.group_field = GroupFieldFactory(group=self.group, validator=self.validator_none, validator_values={})
42
43
        # Misc
44
        # If you need to test validators more in deep, see test_validators.py
45
        self.new_field_data = {"group": self.group.id,
46
            "name": "Example Group Field",
47
            "validator": Validator.VALIDATOR_NONE,
48
            "validator_values": {}}
49
        self.new_field_data_invalid = {"group": self.group.id,
50
            "name": "I am invaliiiid !",
51
            "validator": Validator.VALIDATOR_TEXT,
52
            "validator_values": {"regex": "zek$er$z$)!~", "message": ""}}
53
        self.new_field_data_email_validator = {"group": self.group.id,
54
            "name": "Email verification",
55
            "validator": Validator.VALIDATOR_TEXT,
56
            "validator_values": {"regex": "[^@]+@[^@]+\.[^@]+", "message": "Invalid email"}}
57
58
    def test_imported_validators(self):
59
        self.assertTrue(Validator.objects.all().filter(html_name=Validator.VALIDATOR_NONE).exists())
60
61
    #################### TEST GROUP FIELD CREATION ########################
62
    def try_create(self, user, data=None):
63
        if data is None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable data does not seem to be defined.
Loading history...
64
            data = self.new_field_data
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
65
        self.client.force_authenticate(user=user)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable user does not seem to be defined.
Loading history...
66
        resp = self.client.post(self.group_field_url, data)
67
        return resp.status_code
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
68
69
    def test_create_not_authed(self):
70
        self.assertEqual(self.try_create(None), status.HTTP_401_UNAUTHORIZED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
71
72
    def test_create_not_group_member(self):
73
        self.assertEqual(self.try_create(self.users[0]), status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
74
75
    def test_create_not_group_accepted(self):
76
        self.assertEqual(self.try_create(self.users[1]), status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
77
78
    def test_create_not_group_admin(self):
79
        self.assertEqual(self.try_create(self.users[2]), status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
80
81
    def test_create_ok(self):
82
        self.assertEqual(self.try_create(self.users[3]), status.HTTP_201_CREATED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
83
84
    #################### TEST GROUP FIELD DELETION ########################
85
    def try_delete(self, user):
86
        self.client.force_authenticate(user=user)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable user does not seem to be defined.
Loading history...
87
        resp = self.client.delete(self.group_field_url + str(self.group_field.id) + "/")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
88
        return resp.status_code
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
89
90
    def test_delete_not_authed(self):
91
        self.assertEqual(self.try_delete(None), status.HTTP_401_UNAUTHORIZED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
92
        self.assertTrue(GroupField.objects.all().filter(id=self.group_field.id).exists())
93
94
    def test_delete_not_group_member(self):
95
        self.assertEqual(self.try_delete(self.users[0]), status.HTTP_404_NOT_FOUND)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
96
        self.assertTrue(GroupField.objects.all().filter(id=self.group_field.id).exists())
97
98
    def test_delete_not_group_accepted(self):
99
        self.assertEqual(self.try_delete(self.users[1]), status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
100
        self.assertTrue(GroupField.objects.all().filter(id=self.group_field.id).exists())
101
102
    def test_delete_not_group_admin(self):
103
        self.assertEqual(self.try_delete(self.users[2]), status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
104
        self.assertTrue(GroupField.objects.all().filter(id=self.group_field.id).exists())
105
106
    def test_delete_ok(self):
107
        self.assertEqual(self.try_delete(self.users[3]), status.HTTP_204_NO_CONTENT)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
108
        self.assertFalse(GroupField.objects.all().filter(id=self.group_field.id).exists())
109
110
    #################### TEST GROUP FIELD LIST    ########################
111
    def test_list_not_authed(self):
112
        resp = self.client.get(self.group_field_url)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
113
        self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
114
115
    def test_list_no_group(self):
116
        self.client.force_authenticate(user=self.users[0])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
117
        resp = self.client.get(self.group_field_url)
118
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
119
        self.assertFalse(any(resp.data))
120
121
    def test_list_in_group_not_accepted(self):
122
        from sigma_core.serializers.group_field import GroupFieldSerializer
123
        self.client.force_authenticate(user=self.users[1])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
124
        resp = self.client.get(self.group_field_url)
125
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
126
        self.assertEqual(len(resp.data), 1)
127
        self.assertEqual(resp.data[0], GroupFieldSerializer(self.group_field).data)
128
129
    def test_list_in_group(self):
130
        from sigma_core.serializers.group_field import GroupFieldSerializer
131
        self.client.force_authenticate(user=self.users[2])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
132
        resp = self.client.get(self.group_field_url)
133
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
134
        self.assertEqual(len(resp.data), 1)
135
        self.assertEqual(resp.data[0], GroupFieldSerializer(self.group_field).data)
136
137
    #################### TEST GROUP FIELD UPDATE ########################
138
    def try_update(self, user, allow):
139
        from sigma_core.serializers.group_field import GroupFieldSerializer
140
        group_field = GroupFieldFactory(group=self.group, validator=self.validator_none, validator_values={}, name="AAA")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
141
        group_field_old = GroupFieldSerializer(group_field).data
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable group_field does not seem to be defined.
Loading history...
142
        group_field_new = GroupFieldSerializer(group_field).data
143
        group_field_new["name"] = "BBB"
144
145
        self.client.force_authenticate(user=user)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable user does not seem to be defined.
Loading history...
146
        resp = self.client.put("%s%d/" % (self.group_field_url, group_field.id), group_field_new)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable group_field_new does not seem to be defined.
Loading history...
147
        group_field = GroupField.objects.all().get(id=group_field.id)
148
        if allow:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable allow does not seem to be defined.
Loading history...
149
            self.assertEqual(GroupFieldSerializer(group_field).data, group_field_new)
150
        else:
151
            self.assertEqual(GroupFieldSerializer(group_field).data, group_field_old)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable group_field_old does not seem to be defined.
Loading history...
152
        return resp
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
153
154
    def test_update_not_authed(self):
155
        r = self.try_update(None, False)
156
        self.assertEqual(r.status_code, status.HTTP_401_UNAUTHORIZED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable r does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
157
158
    def test_update_not_group_member(self):
159
        r = self.try_update(self.users[0], False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
160
        self.assertEqual(r.status_code, status.HTTP_404_NOT_FOUND)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable r does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
161
162
    def test_update_not_accepted(self):
163
        r = self.try_update(self.users[1], False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
164
        self.assertEqual(r.status_code, status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable r does not seem to be defined.
Loading history...
165
166
    def test_update_not_admin(self):
167
        r = self.try_update(self.users[2], False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
168
        self.assertEqual(r.status_code, status.HTTP_403_FORBIDDEN)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable r does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
169
170
    def test_update_ok(self):
171
        r = self.try_update(self.users[3], True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
172
        self.assertEqual(r.status_code, status.HTTP_200_OK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable r does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
173
174
175
    #################### A FEW TESTS FOR VALIDATION ########################
176
    def test_create_invalid_regex(self):
177
        self.assertEqual(self.try_create(self.users[3], self.new_field_data_invalid),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
178
            status.HTTP_400_BAD_REQUEST)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
179
180
    def test_create_valid_regex(self):
181
        self.assertEqual(self.try_create(self.users[3], self.new_field_data_email_validator),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
182
            status.HTTP_201_CREATED)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
183
184
185
class GroupFieldValidatorTests(APITestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable APITestCase does not seem to be defined.
Loading history...
186
    fixtures = ['fixtures_prod.json']
187
    @classmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable classmethod does not seem to be defined.
Loading history...
188
    def setUpTestData(self):
189
        super(APITestCase, self).setUpTestData()
190
191
        # Routes
192
        self.group_field_url = "/group-field/"
193
194
        # Group open to anyone
195
        self.group = GroupFactory()
196
197
        # Users already in group
198
        # User[0]: Not in Group
199
        # User[1]: Requested join, not accepted
200
        # User[2]: Group member
201
        # User[3]: Group admin
202
        self.users = [UserFactory(), UserFactory(), UserFactory(), UserFactory()]
203
        # Associated GroupMember
204
        self.group_member = [
205
                None,
206
                GroupMemberFactory(user=self.users[1], group=self.group, perm_rank=0),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
207
                GroupMemberFactory(user=self.users[2], group=self.group, perm_rank=1),
208
                GroupMemberFactory(user=self.users[3], group=self.group, perm_rank=Group.ADMINISTRATOR_RANK)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Group does not seem to be defined.
Loading history...
209
            ]
210
        self.validator_none = Validator.objects.all().get(html_name=Validator.VALIDATOR_NONE)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Validator does not seem to be defined.
Loading history...
211
212
        # If you need to test validators more in deep, see test_validators.py
213
        self.validator_text = Validator.objects.all().get(html_name=Validator.VALIDATOR_TEXT)
214
        self.email_vdtor = GroupFieldFactory(group=self.group,
215
                validator=self.validator_text,
216
                validator_values={"regex": "[a-z0-9]*@[a-z0-9]*.[a-z]{2,3}", "message": "Invalid email"})
217
218
    #################### ../{pk}/validate ########################
219
    def _test_validate_input(self, user, validatorId, input, expectHttp, isInputValid):
220
        self.client.force_authenticate(user=user)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable user does not seem to be defined.
Loading history...
221
        resp = self.client.post("%s%d/validate/" % (self.group_field_url, validatorId), {"value": input})
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable input does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable validatorId does not seem to be defined.
Loading history...
222
        self.assertEqual(resp.status_code, expectHttp)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable resp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable expectHttp does not seem to be defined.
Loading history...
223
        if resp.status_code == status.HTTP_200_OK:
224
            if isInputValid:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable isInputValid does not seem to be defined.
Loading history...
225
                self.assertEqual(resp.data['status'], "ok")
226
            else:
227
                self.assertEqual(resp.data['status'], "ko")
228
229
    def test_validate_route_not_authed(self):
230
        self._test_validate_input(None, self.email_vdtor.id, "[email protected]", status.HTTP_401_UNAUTHORIZED, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
231
232
    def test_validate_route_not_group_member(self):
233
        self._test_validate_input(self.users[0], self.email_vdtor.id, "[email protected]", status.HTTP_404_NOT_FOUND, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
234
235
    def test_validate_route_not_accepted_ok(self):
236
        self._test_validate_input(self.users[1], self.email_vdtor.id, "[email protected]", status.HTTP_200_OK, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
237
238
    def test_validate_route_not_admin_ok(self):
239
        self._test_validate_input(self.users[2], self.email_vdtor.id, "[email protected]", status.HTTP_200_OK, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
240
241
    def test_validate_route_admin_ok(self):
242
        self._test_validate_input(self.users[3], self.email_vdtor.id, "[email protected]", status.HTTP_200_OK, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
243
244
    def test_validate_route_bad_email(self):
245
        self._test_validate_input(self.users[3], self.email_vdtor.id, "ThisIsNoAnEmail", status.HTTP_200_OK, False)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
246
247
    def test_validate_route_bad_validator(self):
248
        self._test_validate_input(self.users[3], -1, "[email protected]", status.HTTP_404_NOT_FOUND, True)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable status does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
249