forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unread.py
252 lines (212 loc) · 11.4 KB
/
test_unread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-AA
from __future__ import absolute_import
from typing import Any, Dict, List
from zerver.models import (
get_user_profile_by_email, Recipient, UserMessage
)
from zerver.lib.test_helpers import ZulipTestCase, tornado_redirected_to_list
import ujson
class PointerTest(ZulipTestCase):
def test_update_pointer(self):
# type: () -> None
"""
Posting a pointer to /update (in the form {"pointer": pointer}) changes
the pointer we store for your UserProfile.
"""
self.login("[email protected]")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
msg_id = self.send_message("[email protected]", "Verona", Recipient.STREAM)
result = self.client_put("/json/users/me/pointer", {"pointer": msg_id})
self.assert_json_success(result)
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, msg_id)
def test_api_update_pointer(self):
# type: () -> None
"""
Same as above, but for the API view
"""
email = "[email protected]"
self.assertEqual(get_user_profile_by_email(email).pointer, -1)
msg_id = self.send_message("[email protected]", "Verona", Recipient.STREAM)
result = self.client_put("/api/v1/users/me/pointer", {"pointer": msg_id},
**self.api_auth(email))
self.assert_json_success(result)
self.assertEqual(get_user_profile_by_email(email).pointer, msg_id)
def test_missing_pointer(self):
# type: () -> None
"""
Posting json to /json/users/me/pointer which does not contain a pointer key/value pair
returns a 400 and error message.
"""
self.login("[email protected]")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
result = self.client_put("/json/users/me/pointer", {"foo": 1})
self.assert_json_error(result, "Missing 'pointer' argument")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
def test_invalid_pointer(self):
# type: () -> None
"""
Posting json to /json/users/me/pointer with an invalid pointer returns a 400 and error
message.
"""
self.login("[email protected]")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
result = self.client_put("/json/users/me/pointer", {"pointer": "foo"})
self.assert_json_error(result, "Bad value for 'pointer': foo")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
def test_pointer_out_of_range(self):
# type: () -> None
"""
Posting json to /json/users/me/pointer with an out of range (< 0) pointer returns a 400
and error message.
"""
self.login("[email protected]")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
result = self.client_put("/json/users/me/pointer", {"pointer": -2})
self.assert_json_error(result, "Bad value for 'pointer': -2")
self.assertEqual(get_user_profile_by_email("[email protected]").pointer, -1)
class UnreadCountTests(ZulipTestCase):
def setUp(self):
# type: () -> None
self.unread_msg_ids = [self.send_message(
"[email protected]", "[email protected]", Recipient.PERSONAL, "hello"),
self.send_message(
"[email protected]", "[email protected]", Recipient.PERSONAL, "hello2")]
# Sending a new message results in unread UserMessages being created
def test_new_message(self):
# type: () -> None
self.login("[email protected]")
content = "Test message for unset read bit"
last_msg = self.send_message("[email protected]", "Verona", Recipient.STREAM, content)
user_messages = list(UserMessage.objects.filter(message=last_msg))
self.assertEqual(len(user_messages) > 0, True)
for um in user_messages:
self.assertEqual(um.message.content, content)
if um.user_profile.email != "[email protected]":
self.assertFalse(um.flags.read)
def test_update_flags(self):
# type: () -> None
self.login("[email protected]")
result = self.client_post("/json/messages/flags",
{"messages": ujson.dumps(self.unread_msg_ids),
"op": "add",
"flag": "read"})
self.assert_json_success(result)
# Ensure we properly set the flags
found = 0
for msg in self.get_old_messages():
if msg['id'] in self.unread_msg_ids:
self.assertEqual(msg['flags'], ['read'])
found += 1
self.assertEqual(found, 2)
result = self.client_post("/json/messages/flags",
{"messages": ujson.dumps([self.unread_msg_ids[1]]),
"op": "remove", "flag": "read"})
self.assert_json_success(result)
# Ensure we properly remove just one flag
for msg in self.get_old_messages():
if msg['id'] == self.unread_msg_ids[0]:
self.assertEqual(msg['flags'], ['read'])
elif msg['id'] == self.unread_msg_ids[1]:
self.assertEqual(msg['flags'], [])
def test_update_all_flags(self):
# type: () -> None
self.login("[email protected]")
message_ids = [self.send_message("[email protected]", "[email protected]",
Recipient.PERSONAL, "test"),
self.send_message("[email protected]", "[email protected]",
Recipient.PERSONAL, "test2")]
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps(message_ids),
"op": "add",
"flag": "read"})
self.assert_json_success(result)
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps([]),
"op": "remove",
"flag": "read",
"all": ujson.dumps(True)})
self.assert_json_success(result)
for msg in self.get_old_messages():
self.assertEqual(msg['flags'], [])
def test_mark_all_in_stream_read(self):
# type: () -> None
self.login("[email protected]")
user_profile = get_user_profile_by_email("[email protected]")
self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm)
message_id = self.send_message("[email protected]", "test_stream", Recipient.STREAM, "hello")
unrelated_message_id = self.send_message("[email protected]", "Denmark", Recipient.STREAM, "hello")
events = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps([]),
"op": "add",
"flag": "read",
"stream_name": "test_stream"})
self.assert_json_success(result)
self.assertTrue(len(events) == 1)
event = events[0]['event']
expected = dict(operation='add',
messages=[message_id],
flag='read',
type='update_message_flags',
all=False)
differences = [key for key in expected if expected[key] != event[key]]
self.assertTrue(len(differences) == 0)
um = list(UserMessage.objects.filter(message=message_id))
for msg in um:
if msg.user_profile.email == "[email protected]":
self.assertTrue(msg.flags.read)
else:
self.assertFalse(msg.flags.read)
unrelated_messages = list(UserMessage.objects.filter(message=unrelated_message_id))
for msg in unrelated_messages:
if msg.user_profile.email == "[email protected]":
self.assertFalse(msg.flags.read)
def test_mark_all_in_invalid_stream_read(self):
# type: () -> None
self.login("[email protected]")
invalid_stream_name = ""
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps([]),
"op": "add",
"flag": "read",
"stream_name": invalid_stream_name})
self.assert_json_error(result, 'No such stream \'\'')
def test_mark_all_in_stream_topic_read(self):
# type: () -> None
self.login("[email protected]")
user_profile = get_user_profile_by_email("[email protected]")
self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm)
message_id = self.send_message("[email protected]", "test_stream", Recipient.STREAM, "hello", "test_topic")
unrelated_message_id = self.send_message("[email protected]", "Denmark", Recipient.STREAM, "hello", "Denmark2")
events = [] # type: List[Dict[str, Any]]
with tornado_redirected_to_list(events):
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps([]),
"op": "add",
"flag": "read",
"topic_name": "test_topic",
"stream_name": "test_stream"})
self.assert_json_success(result)
self.assertTrue(len(events) == 1)
event = events[0]['event']
expected = dict(operation='add',
messages=[message_id],
flag='read',
type='update_message_flags',
all=False)
differences = [key for key in expected if expected[key] != event[key]]
self.assertTrue(len(differences) == 0)
um = list(UserMessage.objects.filter(message=message_id))
for msg in um:
if msg.user_profile.email == "[email protected]":
self.assertTrue(msg.flags.read)
unrelated_messages = list(UserMessage.objects.filter(message=unrelated_message_id))
for msg in unrelated_messages:
if msg.user_profile.email == "[email protected]":
self.assertFalse(msg.flags.read)
def test_mark_all_in_invalid_topic_read(self):
# type: () -> None
self.login("[email protected]")
invalid_topic_name = "abc"
result = self.client_post("/json/messages/flags", {"messages": ujson.dumps([]),
"op": "add",
"flag": "read",
"topic_name": invalid_topic_name,
"stream_name": "Denmark"})
self.assert_json_error(result, 'No such topic \'abc\'')