|
1
|
1 |
|
import json |
|
2
|
|
|
|
|
3
|
1 |
|
from rest_framework import status |
|
4
|
1 |
|
from rest_framework.test import APITestCase, force_authenticate |
|
5
|
|
|
|
|
6
|
1 |
|
from sigma_core.models.group import Group |
|
7
|
1 |
|
from sigma_core.serializers.group import GroupSerializer |
|
8
|
1 |
|
from sigma_core.tests.factories import UserFactory, GroupFactory, GroupMemberFactory |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
def reload(obj): |
|
12
|
1 |
|
return obj.__class__.objects.get(pk=obj.pk) |
|
13
|
|
|
|
|
14
|
1 |
|
|
|
15
|
|
|
class GroupTests(APITestCase): |
|
16
|
1 |
|
@classmethod |
|
17
|
1 |
|
def setUpTestData(self): |
|
18
|
|
|
super(GroupTests, self).setUpTestData() |
|
19
|
1 |
|
|
|
20
|
1 |
|
# Groups |
|
21
|
1 |
|
self.groups = GroupFactory.create_batch(2) |
|
22
|
|
|
self.groups[0].visibility = Group.VIS_PUBLIC |
|
23
|
1 |
|
self.groups[0].save() |
|
24
|
|
|
self.groups[1].visibility = Group.VIS_PRIVATE |
|
25
|
1 |
|
self.groups[1].req_rank_invite = 5 |
|
26
|
|
|
self.groups[1].save() |
|
27
|
|
|
|
|
28
|
1 |
|
# Users |
|
29
|
|
|
self.users = UserFactory.create_batch(3) |
|
30
|
1 |
|
|
|
31
|
1 |
|
# Memberships |
|
32
|
|
|
self.member1 = GroupMemberFactory(user=self.users[1], group=self.groups[1], perm_rank=1) |
|
33
|
|
|
self.member2 = GroupMemberFactory(user=self.users[2], group=self.groups[1], perm_rank=Group.ADMINISTRATOR_RANK) |
|
34
|
|
|
|
|
35
|
|
|
serializer = GroupSerializer(self.groups[0]) |
|
36
|
|
|
self.group_data = serializer.data |
|
37
|
|
|
self.groups_url = "/group/" |
|
38
|
|
|
self.group_url = self.groups_url + "%d/" |
|
39
|
1 |
|
|
|
40
|
|
|
self.new_group_data = {"name": "New group"} |
|
41
|
1 |
|
self.invite_data = {"user": self.users[0].id} |
|
42
|
1 |
|
|
|
43
|
1 |
|
#### List requests |
|
44
|
1 |
|
def test_get_groups_list_unauthed(self): |
|
45
|
|
|
# Client not authenticated |
|
46
|
|
|
response = self.client.get(self.groups_url) |
|
47
|
1 |
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
48
|
|
|
|
|
49
|
1 |
|
# def test_get_groups_list_forbidden(self): |
|
50
|
1 |
|
# # Client authenticated but has no permission |
|
51
|
|
|
# self.client.force_authenticate(group=self.users[0]) |
|
52
|
|
|
# response = self.client.get(self.group_url % self.groups[1].id) |
|
53
|
|
|
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
54
|
|
|
|
|
55
|
|
|
def test_get_groups_list_ok(self): |
|
56
|
1 |
|
# Client has permissions |
|
57
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
58
|
1 |
|
response = self.client.get(self.groups_url) |
|
59
|
1 |
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
60
|
1 |
|
self.assertEqual(len(response.data), len(self.groups)) |
|
61
|
1 |
|
|
|
62
|
|
|
#### Get requests |
|
63
|
|
|
def test_get_group_unauthed(self): |
|
64
|
|
|
# Client is not authenticated |
|
65
|
|
|
response = self.client.get(self.group_url % self.groups[0].id) |
|
66
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
67
|
|
|
|
|
68
|
|
|
def test_get_group_forbidden(self): |
|
69
|
|
|
# Non-member wants to see a private group |
|
70
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
71
|
|
|
response = self.client.get(self.group_url % self.groups[1].id) |
|
72
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
73
|
|
|
|
|
74
|
|
|
def test_get_group_ok(self): |
|
75
|
|
|
# Client wants to see a public group |
|
76
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
77
|
|
|
response = self.client.get(self.group_url % self.groups[0].id) |
|
78
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
79
|
|
|
self.assertEqual(response.data, self.group_data) |
|
80
|
|
|
|
|
81
|
|
|
#### Invitation requests |
|
82
|
|
|
def test_invite_unauthed(self): |
|
83
|
|
|
response = self.client.put((self.group_url + "invite/") % self.groups[1].id, self.invite_data) |
|
84
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
85
|
|
|
|
|
86
|
|
|
# def test_invite_forbidden(self): |
|
87
|
|
|
# # Client has not perms to invite |
|
88
|
|
|
# self.client.force_authenticate(user=self.users[1]) |
|
89
|
|
|
# response = self.client.put((self.group_url + "invite/") % self.groups[1].id, self.invite_data) |
|
90
|
|
|
# self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
91
|
|
|
|
|
92
|
|
|
def test_invite_ok(self): |
|
93
|
|
|
# Client has perms to invite |
|
94
|
|
|
self.client.force_authenticate(user=self.users[2]) |
|
95
|
|
|
response = self.client.put((self.group_url + "invite/") % self.groups[1].id, self.invite_data) |
|
96
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
97
|
|
|
self.assertIn(self.groups[1], reload(self.users[0]).invited_to_groups.all()) |
|
98
|
|
|
|
|
99
|
|
|
#### Create requests |
|
100
|
|
|
|
|
101
|
|
|
#### Modification requests |
|
102
|
|
|
|
|
103
|
|
|
#### Deletion requests |
|
104
|
|
|
|