|
1
|
|
|
from rest_framework import status |
|
2
|
|
|
from rest_framework.test import APITestCase, force_authenticate |
|
3
|
|
|
|
|
4
|
|
|
from sigma_core.tests.factories import UserFactory, AdminUserFactory |
|
5
|
|
|
|
|
6
|
|
|
from sigma_chat.tests.factories import ChatFactory, ChatMemberFactory |
|
7
|
|
|
|
|
8
|
|
|
from sigma_chat.models.chat import Chat |
|
9
|
|
|
from sigma_chat.serializers.chat import ChatSerializer |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def reload(obj): |
|
13
|
|
|
return obj.__class__.objects.get(pk=obj.pk) |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class ChatTests(APITestCase): |
|
17
|
|
|
@classmethod |
|
18
|
|
|
def setUpTestData(self): |
|
19
|
|
|
# Summary: 4 users + 1 admin, 1 chat |
|
20
|
|
|
# User #5 is Sigma admin |
|
21
|
|
|
# User #1 is in chat |
|
22
|
|
|
|
|
23
|
|
|
super(ChatTests, self).setUpTestData() |
|
24
|
|
|
self.users = UserFactory.create_batch(3) + [AdminUserFactory()] |
|
25
|
|
|
self.chats = ChatFactory.create_batch(1) |
|
26
|
|
|
self.chatmember = ChatMemberFactory(is_creator=True, is_admin=True, chat=self.chats[0], user=self.users[0]) |
|
27
|
|
|
|
|
28
|
|
|
self.chats_url = "/chat/" |
|
29
|
|
|
self.chat_url = self.chats_url + "%d/" |
|
30
|
|
|
|
|
31
|
|
|
self.new_chat_data = {"name": "New chat"} |
|
32
|
|
|
self.add_member_data = {"user_id": self.users[0].id} |
|
33
|
|
|
|
|
34
|
|
|
#### Model methods test |
|
35
|
|
|
def test_model_chat(self): |
|
36
|
|
|
self.assertEqual(len(self.chats), 1) |
|
37
|
|
|
self.assertEqual(len(self.chats[0].chatmember.all()), 1) |
|
38
|
|
|
self.assertTrue(self.chats[0].chatmember.get(pk=1).is_creator) |
|
39
|
|
|
self.assertTrue(self.chats[0].chatmember.get(pk=1).is_admin) |
|
40
|
|
|
self.assertTrue(self.chats[0].chatmember.get(pk=1).is_member) |
|
41
|
|
|
self.assertFalse(self.chats[0].chatmember.get(pk=1).is_banned) |
|
42
|
|
|
|
|
43
|
|
|
#### List requests |
|
44
|
|
|
def test_get_chats_list_unauthed(self): |
|
45
|
|
|
# Client not authenticated |
|
46
|
|
|
response = self.client.get(self.chats_url) |
|
47
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
48
|
|
|
|
|
49
|
|
|
def test_get_chats_list_limited(self): |
|
50
|
|
|
# Client authenticated and can see limited list of chats |
|
51
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
52
|
|
|
response = self.client.get(self.chats_url) |
|
53
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
54
|
|
|
self.assertEqual(len(response.data), 0) |
|
55
|
|
|
|
|
56
|
|
|
def test_get_chats_list_creator(self): |
|
57
|
|
|
# Client authenticated and can see limited list of chats |
|
58
|
|
|
self.client.force_authenticate(user=self.chats[0].chatmember.get(pk=1).user) |
|
59
|
|
|
response = self.client.get(self.chats_url) |
|
60
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
61
|
|
|
self.assertEqual(len(response.data), 1) |
|
62
|
|
|
|
|
63
|
|
|
def test_get_chats_list_admin(self): |
|
64
|
|
|
# Client authenticated and can see limited list of chats |
|
65
|
|
|
self.client.force_authenticate(user=self.users[-1]) |
|
66
|
|
|
response = self.client.get(self.chats_url) |
|
67
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
68
|
|
|
self.assertEqual(len(response.data), 0) |
|
69
|
|
|
|
|
70
|
|
|
#### Get requests |
|
71
|
|
|
def test_get_chat_unauthed(self): |
|
72
|
|
|
# Client is not authenticated |
|
73
|
|
|
response = self.client.get(self.chat_url % self.chats[0].id) |
|
74
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
75
|
|
|
|
|
76
|
|
|
def test_get_chat_creator(self): |
|
77
|
|
|
# Client can see chat |
|
78
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
79
|
|
|
response = self.client.get(self.chat_url % self.chats[0].id) |
|
80
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
81
|
|
|
self.assertEqual(len(response.data), 2) |
|
82
|
|
|
|
|
83
|
|
|
def test_get_chat_not_member(self): |
|
84
|
|
|
# Client cannot see chat if he's not a member |
|
85
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
86
|
|
|
response = self.client.get(self.chat_url % self.chats[0].id) |
|
87
|
|
|
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
|
88
|
|
|
|
|
89
|
|
|
#### Create requests |
|
90
|
|
|
def test_create_unauthed(self): |
|
91
|
|
|
# Client is not authenticated |
|
92
|
|
|
response = self.client.post(self.chats_url, self.new_chat_data) |
|
93
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
def test_create_chat(self): |
|
|
|
|
|
|
96
|
|
|
# Everybody can create a chat |
|
97
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
98
|
|
|
response = self.client.post(self.chats_url, self.new_chat_data) |
|
99
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
|
100
|
|
|
self.assertEqual(response.data['name'], self.new_chat_data['name']) |
|
101
|
|
|
Chat.objects.get(pk=response.data['id']).delete() |
|
102
|
|
|
|
|
103
|
|
|
#### Modification requests |
|
104
|
|
|
def test_update_unauthed(self): |
|
105
|
|
|
# Unauthed client cannot update a chat |
|
106
|
|
|
update_chat_data = ChatSerializer(self.chats[0]).data |
|
107
|
|
|
response = self.client.put(self.chat_url % self.chats[0].id, update_chat_data) |
|
108
|
|
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
|
109
|
|
|
|
|
110
|
|
|
def test_update_forbidden(self): |
|
111
|
|
|
# Client cannot update a chat if he isn't admin of |
|
112
|
|
|
self.client.force_authenticate(user=self.users[1]) |
|
113
|
|
|
response = self.client.put(self.chat_url % self.chats[0].id, {'name': "new name"}) |
|
114
|
|
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
def test_update_ok(self): |
|
|
|
|
|
|
117
|
|
|
# Client can update a chat if he has clearance |
|
118
|
|
|
update_chat_data = ChatSerializer(self.chats[0]).data |
|
119
|
|
|
self.client.force_authenticate(user=self.users[0]) |
|
120
|
|
|
old_name = update_chat_data['name'] |
|
121
|
|
|
update_chat_data['name'] = "A new name" |
|
122
|
|
|
response = self.client.put(self.chat_url % self.chats[0].id, update_chat_data) |
|
123
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK) |
|
124
|
|
|
self.assertEqual(reload(self.chats[0]).name, update_chat_data['name']) |
|
125
|
|
|
# Guarantee independance of tests |
|
126
|
|
|
self.chats[0].name = old_name |
|
127
|
|
|
self.chats[0].save() |
|
128
|
|
|
|