|
1
|
|
|
import json |
|
2
|
|
|
|
|
3
|
|
|
from rest_framework import status |
|
4
|
|
|
from rest_framework.test import APITestCase, force_authenticate |
|
5
|
|
|
|
|
6
|
|
|
from sigma_core.tests.factories import UserFactory, GroupFactory |
|
7
|
|
|
from sigma_core.models.group import Group |
|
8
|
|
|
from sigma_core.serializers.group import GroupSerializer |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class GroupTests(APITestCase): |
|
12
|
|
|
@classmethod |
|
|
|
|
|
|
13
|
|
|
def setUpTestData(self): |
|
14
|
|
|
super(GroupTests, self).setUpTestData() |
|
15
|
|
|
|
|
16
|
|
|
self.group = GroupFactory() |
|
17
|
|
|
self.user = UserFactory() |
|
18
|
|
|
|
|
19
|
|
|
serializer = GroupSerializer(self.group) |
|
20
|
|
|
self.group_data = serializer.data |
|
21
|
|
|
self.group_url = '/group/%d/' % self.group.id |
|
22
|
|
|
|
|
23
|
|
|
self.groups_list = [self.group] |
|
24
|
|
|
|
|
25
|
|
|
self.new_group_data = {'name': 'New group'} |
|
26
|
|
|
|
|
27
|
|
|
#### List requests |
|
28
|
|
|
def test_get_groups_list_unauthed(self): |
|
29
|
|
|
# Client not authenticated |
|
30
|
|
|
response = self.client.get('/group/') |
|
31
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
32
|
|
|
|
|
33
|
|
|
# def test_get_groups_list_forbidden(self): |
|
34
|
|
|
# # Client authenticated but has no permission |
|
35
|
|
|
# self.client.force_authenticate(group=self.user) |
|
36
|
|
|
# response = self.client.get('/group/') |
|
37
|
|
|
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
38
|
|
|
|
|
39
|
|
|
def test_get_groups_list_ok(self): |
|
40
|
|
|
# Client has permissions |
|
41
|
|
|
self.client.force_authenticate(user=self.user) |
|
42
|
|
|
response = self.client.get('/group/') |
|
43
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
44
|
|
|
self.assertEqual(len(response.data), len(self.groups_list)) |
|
45
|
|
|
|
|
46
|
|
|
#### Get requests |
|
47
|
|
|
def test_get_group_unauthed(self): |
|
48
|
|
|
# Client is not authenticated |
|
49
|
|
|
response = self.client.get(self.group_url) |
|
50
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
51
|
|
|
|
|
52
|
|
|
# def test_get_group_forbidden(self): |
|
53
|
|
|
# response = self.client.get(self.group_url) |
|
54
|
|
|
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
55
|
|
|
|
|
56
|
|
|
def test_get_group_ok(self): |
|
57
|
|
|
# Client has permissions |
|
58
|
|
|
self.client.force_authenticate(user=self.user) |
|
59
|
|
|
response = self.client.get(self.group_url) |
|
60
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
61
|
|
|
self.assertEqual(response.data, self.group_data) |
|
62
|
|
|
|
|
63
|
|
|
#### Create requests |
|
64
|
|
|
|
|
65
|
|
|
#### Modification requests |
|
66
|
|
|
|
|
67
|
|
|
#### Deletion requests |
|
68
|
|
|
|
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.