Mercurial > public > sg101
comparison messages/tests/test_views.py @ 812:42436d674ba8
Private message refactor: add unit tests for message cycle.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 07 Sep 2014 16:53:05 -0500 |
parents | 56b30c79f10e |
children | eca0c17ff9c8 |
comparison
equal
deleted
inserted
replaced
811:56b30c79f10e | 812:42436d674ba8 |
---|---|
1 """ | 1 """ |
2 Unit tests for the messages application views. | 2 Unit tests for the messages application views. |
3 | 3 |
4 """ | 4 """ |
5 import datetime | |
6 | |
5 from django.test import TestCase | 7 from django.test import TestCase |
6 from django.core.urlresolvers import reverse | 8 from django.core.urlresolvers import reverse |
9 from django.contrib.auth.models import User | |
7 | 10 |
8 from messages.models import Message, Options, Flag | 11 from messages.models import Message, Options, Flag |
9 | 12 |
10 | 13 # TODO TODO TODO TODO |
11 VIEW_NAMES = [ | 14 # TODO TODO TODO TODO |
12 'messages-inbox', | 15 # TODO TODO TODO TODO |
13 'messages-compose', | 16 # |
14 'messages-outbox', | 17 # 1) Test report functionality; ensure email sent to admin |
15 'messages-trash', | 18 # 2) Test email sent when PM sent if options allow it |
16 'messages-options', | 19 # |
17 'messages-delete', | 20 # TODO TODO TODO TODO |
18 'messages-undelete', | 21 # TODO TODO TODO TODO |
19 'messages-view', | 22 # TODO TODO TODO TODO |
20 'messages-report', | |
21 ] | |
22 | 23 |
23 | 24 |
24 class NotLoggedInTestCase(TestCase): | 25 class NotLoggedInTestCase(TestCase): |
25 """Ensure we are redirected to the login page before we can do anything with | 26 """Ensure we are redirected to the login page before we can do anything with |
26 this application. | 27 this application. |
70 | 71 |
71 def test_report(self): | 72 def test_report(self): |
72 url_name = 'messages-report' | 73 url_name = 'messages-report' |
73 self._test_get(url_name, args=[123]) | 74 self._test_get(url_name, args=[123]) |
74 self._test_post(url_name, args=[123]) | 75 self._test_post(url_name, args=[123]) |
76 | |
77 | |
78 class BasicTestCase(TestCase): | |
79 """Testing a logged in user visiting all the views with no messages.""" | |
80 fixtures = ['messages_test_users.json'] | |
81 | |
82 def setUp(self): | |
83 self.users = {} | |
84 self.users['pj'] = User.objects.get(username='pj') | |
85 self.users['pj'].set_password('12345') | |
86 self.users['pj'].save() | |
87 self.assertTrue(self.client.login(username='pj', password='12345')) | |
88 | |
89 self.users['eddie'] = User.objects.get(username='eddie') | |
90 self.users['eddie'].set_password('12345') | |
91 self.users['eddie'].save() | |
92 | |
93 self.users['richard'] = User.objects.get(username='richard') | |
94 | |
95 def test_simple_gets(self): | |
96 view_names = [ | |
97 'messages-inbox', | |
98 'messages-compose', | |
99 'messages-outbox', | |
100 'messages-trash', | |
101 'messages-options', | |
102 ] | |
103 for view_name in view_names: | |
104 response = self.client.get(reverse(view_name)) | |
105 self.assertEqual(response.status_code, 200) | |
106 | |
107 def test_wrong_method(self): | |
108 view_names = [ | |
109 'messages-delete', | |
110 'messages-undelete', | |
111 ] | |
112 for view_name in view_names: | |
113 response = self.client.get(reverse(view_name)) | |
114 self.assertEqual(response.status_code, 405) | |
115 | |
116 def test_nonexistent_pms(self): | |
117 view_names = [ | |
118 'messages-view', | |
119 'messages-report', | |
120 ] | |
121 for view_name in view_names: | |
122 response = self.client.get(reverse(view_name, args=[42])) | |
123 self.assertEqual(response.status_code, 404) | |
124 | |
125 response = self.client.post(reverse(view_name, args=[42])) | |
126 self.assertEqual(response.status_code, 404) | |
127 | |
128 def test_options(self): | |
129 view_name = 'messages-options' | |
130 url = reverse(view_name) | |
131 response = self.client.get(url) | |
132 self.assertEqual(response.status_code, 200) | |
133 | |
134 post_data = { | |
135 'opts-attach_signature': 'on', | |
136 'opts-notify_email': 'on', | |
137 } | |
138 response = self.client.post(url, data=post_data, follow=True) | |
139 self.assertRedirects(response, url) | |
140 | |
141 opts = Options.objects.for_user(self.users['pj']) | |
142 self.assertTrue(opts.attach_signature) | |
143 self.assertTrue(opts.notify_email) | |
144 | |
145 post_data = {} | |
146 response = self.client.post(url, data=post_data, follow=True) | |
147 self.assertRedirects(response, url) | |
148 | |
149 opts = Options.objects.for_user(self.users['pj']) | |
150 self.assertFalse(opts.attach_signature) | |
151 self.assertFalse(opts.notify_email) | |
152 | |
153 def test_send_bogus_user(self): | |
154 post_data = { | |
155 'receiver': 'unknown_user', | |
156 'subject': 'hi', | |
157 'message': 'test', | |
158 } | |
159 view_name = 'messages-compose' | |
160 url = reverse(view_name) | |
161 response = self.client.post(url, data=post_data) | |
162 self.assertContains(response, 'username does not exist', status_code=200) | |
163 | |
164 qs = Message.objects.filter(sender=self.users['pj']) | |
165 self.assertEqual(qs.count(), 0) | |
166 | |
167 def test_send_to_self(self): | |
168 post_data = { | |
169 'receiver': 'pj', | |
170 'subject': 'hi', | |
171 'message': 'test', | |
172 } | |
173 view_name = 'messages-compose' | |
174 url = reverse(view_name) | |
175 response = self.client.post(url, data=post_data) | |
176 self.assertContains(response, "You can't send a message to yourself", status_code=200) | |
177 | |
178 qs = Message.objects.filter(sender=self.users['pj']) | |
179 self.assertEqual(qs.count(), 0) | |
180 | |
181 def test_message_cycle(self): | |
182 # pj sends message to eddie | |
183 now = datetime.datetime.now() | |
184 post_data = { | |
185 'receiver': 'eddie', | |
186 'subject': 'Mr. Moto Demo', | |
187 'message': 'Gig at Newport High School', | |
188 } | |
189 view_name = 'messages-compose' | |
190 url = reverse(view_name) | |
191 response = self.client.post(url, data=post_data) | |
192 self.assertContains(response, "Message sent", status_code=200) | |
193 | |
194 # see if message got created in database | |
195 msg = Message.objects.get(sender=self.users['pj']) | |
196 | |
197 self.assertEqual(msg.sender, self.users['pj']) | |
198 self.assertEqual(msg.receiver, self.users['eddie']) | |
199 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) | |
200 self.assertIsNone(msg.read_date) | |
201 self.assertIsNone(msg.reply_date) | |
202 self.assertEqual(msg.subject, post_data['subject']) | |
203 self.assertEqual(msg.message, post_data['message']) | |
204 self.assertIsNone(msg.sender_delete_date) | |
205 self.assertIsNone(msg.receiver_delete_date) | |
206 | |
207 # see if shows up in outbox view | |
208 view_name = 'messages-outbox' | |
209 url = reverse(view_name) | |
210 response = self.client.get(url) | |
211 self.assertContains(response, post_data['subject'], status_code=200) | |
212 | |
213 # see if we can view it | |
214 view_name = 'messages-view' | |
215 url = reverse(view_name, args=[msg.pk]) | |
216 response = self.client.get(url) | |
217 self.assertContains(response, post_data['subject'], status_code=200) | |
218 self.assertContains(response, post_data['message'], status_code=200) | |
219 | |
220 # eddie logs in and checks various views | |
221 self.assertTrue(self.client.login(username='eddie', password='12345')) | |
222 view_name = 'messages-inbox' | |
223 url = reverse(view_name) | |
224 response = self.client.get(url) | |
225 self.assertContains(response, post_data['subject'], status_code=200) | |
226 view_name = 'messages-outbox' | |
227 url = reverse(view_name) | |
228 response = self.client.get(url) | |
229 self.assertNotContains(response, post_data['subject'], status_code=200) | |
230 view_name = 'messages-trash' | |
231 url = reverse(view_name) | |
232 response = self.client.get(url) | |
233 self.assertNotContains(response, post_data['subject'], status_code=200) | |
234 | |
235 # eddie reads it | |
236 view_name = 'messages-view' | |
237 url = reverse(view_name, args=[msg.pk]) | |
238 response = self.client.get(url) | |
239 self.assertContains(response, post_data['subject'], status_code=200) | |
240 self.assertContains(response, post_data['message'], status_code=200) | |
241 | |
242 # see if message got updated in database | |
243 msg = Message.objects.get(receiver=self.users['eddie']) | |
244 | |
245 self.assertEqual(msg.sender, self.users['pj']) | |
246 self.assertEqual(msg.receiver, self.users['eddie']) | |
247 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) | |
248 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5)) | |
249 self.assertIsNone(msg.reply_date) | |
250 self.assertEqual(msg.subject, post_data['subject']) | |
251 self.assertEqual(msg.message, post_data['message']) | |
252 self.assertIsNone(msg.sender_delete_date) | |
253 self.assertIsNone(msg.receiver_delete_date) | |
254 | |
255 # eddie replies | |
256 post_data2 = { | |
257 'receiver': 'pj', | |
258 'subject': 'Re: Mr. Moto Demo', | |
259 'message': 'South Bay Sound', | |
260 'parent_id': msg.pk, | |
261 } | |
262 view_name = 'messages-compose' | |
263 url = reverse(view_name) | |
264 response = self.client.post(url, data=post_data2) | |
265 self.assertContains(response, "Message sent", status_code=200) | |
266 | |
267 self.assertEqual(Message.objects.all().count(), 2) | |
268 | |
269 msg1 = Message.objects.get(receiver=self.users['eddie']) | |
270 | |
271 self.assertEqual(msg1.sender, self.users['pj']) | |
272 self.assertEqual(msg1.receiver, self.users['eddie']) | |
273 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) | |
274 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) | |
275 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) | |
276 self.assertEqual(msg1.subject, post_data['subject']) | |
277 self.assertEqual(msg1.message, post_data['message']) | |
278 self.assertIsNone(msg1.sender_delete_date) | |
279 self.assertIsNone(msg1.receiver_delete_date) | |
280 | |
281 msg2 = Message.objects.get(receiver=self.users['pj']) | |
282 | |
283 self.assertEqual(msg2.sender, self.users['eddie']) | |
284 self.assertEqual(msg2.receiver, self.users['pj']) | |
285 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) | |
286 self.assertIsNone(msg2.read_date) | |
287 self.assertIsNone(msg2.reply_date) | |
288 self.assertEqual(msg2.subject, post_data2['subject']) | |
289 self.assertEqual(msg2.message, post_data2['message']) | |
290 self.assertIsNone(msg2.sender_delete_date) | |
291 self.assertIsNone(msg2.receiver_delete_date) | |
292 | |
293 # pj logs in and checks various views | |
294 self.assertTrue(self.client.login(username='pj', password='12345')) | |
295 view_name = 'messages-inbox' | |
296 url = reverse(view_name) | |
297 response = self.client.get(url) | |
298 self.assertContains(response, post_data2['subject'], status_code=200) | |
299 view_name = 'messages-outbox' | |
300 url = reverse(view_name) | |
301 response = self.client.get(url) | |
302 self.assertContains(response, post_data['subject'], status_code=200) | |
303 view_name = 'messages-trash' | |
304 url = reverse(view_name) | |
305 response = self.client.get(url) | |
306 self.assertNotContains(response, post_data['subject'], status_code=200) | |
307 self.assertNotContains(response, post_data2['subject'], status_code=200) | |
308 | |
309 # pj reads reply | |
310 view_name = 'messages-view' | |
311 url = reverse(view_name, args=[msg2.pk]) | |
312 response = self.client.get(url) | |
313 self.assertContains(response, post_data2['subject'], status_code=200) | |
314 self.assertContains(response, post_data2['message'], status_code=200) | |
315 | |
316 msg = Message.objects.get(receiver=self.users['eddie']) | |
317 | |
318 self.assertEqual(msg.sender, self.users['pj']) | |
319 self.assertEqual(msg.receiver, self.users['eddie']) | |
320 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) | |
321 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5)) | |
322 self.assertTrue(now - msg.reply_date < datetime.timedelta(seconds=5)) | |
323 self.assertEqual(msg.subject, post_data['subject']) | |
324 self.assertEqual(msg.message, post_data['message']) | |
325 self.assertIsNone(msg.sender_delete_date) | |
326 self.assertIsNone(msg.receiver_delete_date) | |
327 | |
328 msg2 = Message.objects.get(receiver=self.users['pj']) | |
329 | |
330 self.assertEqual(msg2.sender, self.users['eddie']) | |
331 self.assertEqual(msg2.receiver, self.users['pj']) | |
332 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) | |
333 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5)) | |
334 self.assertIsNone(msg2.reply_date) | |
335 self.assertEqual(msg2.subject, post_data2['subject']) | |
336 self.assertEqual(msg2.message, post_data2['message']) | |
337 self.assertIsNone(msg2.sender_delete_date) | |
338 self.assertIsNone(msg2.receiver_delete_date) | |
339 | |
340 # pj deletes message 2 | |
341 self.assertEqual(Message.objects.all().count(), 2) | |
342 | |
343 delete_post_data = { | |
344 'pm_ids': [msg2.pk], | |
345 } | |
346 view_name = 'messages-delete' | |
347 url = reverse(view_name) | |
348 response = self.client.post(url, data=delete_post_data, follow=True) | |
349 self.assertContains(response, "1 message deleted", status_code=200) | |
350 | |
351 self.assertEqual(Message.objects.all().count(), 2) | |
352 | |
353 msg2 = Message.objects.get(receiver=self.users['pj']) | |
354 | |
355 self.assertEqual(msg2.sender, self.users['eddie']) | |
356 self.assertEqual(msg2.receiver, self.users['pj']) | |
357 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) | |
358 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5)) | |
359 self.assertIsNone(msg2.reply_date) | |
360 self.assertEqual(msg2.subject, post_data2['subject']) | |
361 self.assertEqual(msg2.message, post_data2['message']) | |
362 self.assertIsNone(msg2.sender_delete_date) | |
363 self.assertTrue(now - msg2.receiver_delete_date < datetime.timedelta(seconds=5)) | |
364 | |
365 # should be in pj's trash now | |
366 view_name = 'messages-trash' | |
367 url = reverse(view_name) | |
368 response = self.client.get(url) | |
369 self.assertContains(response, post_data2['subject'], status_code=200) | |
370 | |
371 # eddie logs in and deletes his copy of message 2 | |
372 self.assertTrue(self.client.login(username='eddie', password='12345')) | |
373 delete_post_data = { | |
374 'pm_ids': [msg2.pk], | |
375 } | |
376 view_name = 'messages-delete' | |
377 url = reverse(view_name) | |
378 response = self.client.post(url, data=delete_post_data, follow=True) | |
379 self.assertContains(response, "1 message deleted", status_code=200) | |
380 | |
381 # should be really deleted now | |
382 self.assertEqual(Message.objects.all().count(), 1) | |
383 view_name = 'messages-trash' | |
384 url = reverse(view_name) | |
385 response = self.client.get(url) | |
386 self.assertNotContains(response, post_data2['subject'], status_code=200) | |
387 | |
388 # eddie deletes then undeletes message 1 | |
389 delete_post_data = { | |
390 'pm_ids': [msg1.pk], | |
391 } | |
392 view_name = 'messages-delete' | |
393 url = reverse(view_name) | |
394 response = self.client.post(url, data=delete_post_data, follow=True) | |
395 self.assertContains(response, "1 message deleted", status_code=200) | |
396 | |
397 # should not be really deleted now | |
398 self.assertEqual(Message.objects.all().count(), 1) | |
399 view_name = 'messages-trash' | |
400 url = reverse(view_name) | |
401 response = self.client.get(url) | |
402 self.assertContains(response, post_data['subject'], status_code=200) | |
403 | |
404 msg1 = Message.objects.get(receiver=self.users['eddie']) | |
405 | |
406 self.assertEqual(msg1.sender, self.users['pj']) | |
407 self.assertEqual(msg1.receiver, self.users['eddie']) | |
408 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) | |
409 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) | |
410 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) | |
411 self.assertEqual(msg1.subject, post_data['subject']) | |
412 self.assertEqual(msg1.message, post_data['message']) | |
413 self.assertIsNone(msg1.sender_delete_date) | |
414 self.assertTrue(now - msg1.receiver_delete_date < datetime.timedelta(seconds=5)) | |
415 | |
416 delete_post_data = { | |
417 'pm_ids': [msg1.pk], | |
418 } | |
419 view_name = 'messages-undelete' | |
420 url = reverse(view_name) | |
421 response = self.client.post(url, data=delete_post_data, follow=True) | |
422 self.assertContains(response, "1 message undeleted", status_code=200) | |
423 | |
424 # should not be really deleted now | |
425 self.assertEqual(Message.objects.all().count(), 1) | |
426 view_name = 'messages-trash' | |
427 url = reverse(view_name) | |
428 response = self.client.get(url) | |
429 self.assertNotContains(response, post_data['subject'], status_code=200) | |
430 | |
431 msg1 = Message.objects.get(receiver=self.users['eddie']) | |
432 | |
433 self.assertEqual(msg1.sender, self.users['pj']) | |
434 self.assertEqual(msg1.receiver, self.users['eddie']) | |
435 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) | |
436 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) | |
437 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) | |
438 self.assertEqual(msg1.subject, post_data['subject']) | |
439 self.assertEqual(msg1.message, post_data['message']) | |
440 self.assertIsNone(msg1.sender_delete_date) | |
441 self.assertIsNone(msg1.receiver_delete_date) | |
442 | |
443 def test_read_permission(self): | |
444 | |
445 now = datetime.datetime.now() | |
446 # pj sends a PM to richard | |
447 post_data = { | |
448 'receiver': 'richard', | |
449 'subject': 'Mr. Moto Demo', | |
450 'message': 'Gig at Newport High School', | |
451 } | |
452 view_name = 'messages-compose' | |
453 url = reverse(view_name) | |
454 response = self.client.post(url, data=post_data) | |
455 self.assertContains(response, "Message sent", status_code=200) | |
456 | |
457 msg = Message.objects.get(sender=self.users['pj']) | |
458 | |
459 # eddie should not be able to read it | |
460 self.assertTrue(self.client.login(username='eddie', password='12345')) | |
461 view_name = 'messages-view' | |
462 url = reverse(view_name, args=[msg.pk]) | |
463 response = self.client.get(url) | |
464 self.assertNotContains(response, post_data['subject'], status_code=302) | |
465 self.assertNotContains(response, post_data['message'], status_code=302) | |
466 | |
467 response = self.client.get(url, follow=True) | |
468 self.assertNotContains(response, post_data['subject'], status_code=200) | |
469 self.assertNotContains(response, post_data['message'], status_code=200) | |
470 | |
471 msg = Message.objects.get(sender=self.users['pj']) | |
472 | |
473 self.assertEqual(msg.sender, self.users['pj']) | |
474 self.assertEqual(msg.receiver, self.users['richard']) | |
475 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) | |
476 self.assertIsNone(msg.read_date) | |
477 self.assertIsNone(msg.reply_date) | |
478 self.assertEqual(msg.subject, post_data['subject']) | |
479 self.assertEqual(msg.message, post_data['message']) | |
480 self.assertIsNone(msg.sender_delete_date) | |
481 self.assertIsNone(msg.receiver_delete_date) |