comparison messages/tests/test_views.py @ 818:cf486a8e8b43

Added the ability to export private messages to email.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Sep 2014 16:19:46 -0500
parents 999a71b81111
children
comparison
equal deleted inserted replaced
817:74e84f5fc948 818:cf486a8e8b43
48 url_name = 'messages-options' 48 url_name = 'messages-options'
49 self._test_get(url_name) 49 self._test_get(url_name)
50 self._test_post(url_name) 50 self._test_post(url_name)
51 51
52 def test_delete(self): 52 def test_delete(self):
53 self._test_get('messages-delete') 53 self._test_get('messages-bulk')
54 54
55 def test_undelete(self): 55 def test_undelete(self):
56 self._test_get('messages-undelete') 56 self._test_get('messages-undelete')
57 57
58 def test_view(self): 58 def test_view(self):
105 response = self.client.get(reverse(view_name)) 105 response = self.client.get(reverse(view_name))
106 self.assertEqual(response.status_code, 200) 106 self.assertEqual(response.status_code, 200)
107 107
108 def test_wrong_method(self): 108 def test_wrong_method(self):
109 view_names = [ 109 view_names = [
110 'messages-delete', 110 'messages-bulk',
111 'messages-undelete', 111 'messages-undelete',
112 ] 112 ]
113 for view_name in view_names: 113 for view_name in view_names:
114 response = self.client.get(reverse(view_name)) 114 response = self.client.get(reverse(view_name))
115 self.assertEqual(response.status_code, 405) 115 self.assertEqual(response.status_code, 405)
341 # pj deletes message 2 341 # pj deletes message 2
342 self.assertEqual(Message.objects.all().count(), 2) 342 self.assertEqual(Message.objects.all().count(), 2)
343 343
344 delete_post_data = { 344 delete_post_data = {
345 'pm_ids': [msg2.pk], 345 'pm_ids': [msg2.pk],
346 } 346 'action': 'Delete',
347 view_name = 'messages-delete' 347 }
348 view_name = 'messages-bulk'
348 url = reverse(view_name) 349 url = reverse(view_name)
349 response = self.client.post(url, data=delete_post_data, follow=True) 350 response = self.client.post(url, data=delete_post_data, follow=True)
350 self.assertContains(response, "1 message deleted", status_code=200) 351 self.assertContains(response, "1 message deleted", status_code=200)
351 352
352 self.assertEqual(Message.objects.all().count(), 2) 353 self.assertEqual(Message.objects.all().count(), 2)
371 372
372 # eddie logs in and deletes his copy of message 2 373 # eddie logs in and deletes his copy of message 2
373 self.assertTrue(self.client.login(username='eddie', password='12345')) 374 self.assertTrue(self.client.login(username='eddie', password='12345'))
374 delete_post_data = { 375 delete_post_data = {
375 'pm_ids': [msg2.pk], 376 'pm_ids': [msg2.pk],
376 } 377 'action': 'Delete',
377 view_name = 'messages-delete' 378 }
379 view_name = 'messages-bulk'
378 url = reverse(view_name) 380 url = reverse(view_name)
379 response = self.client.post(url, data=delete_post_data, follow=True) 381 response = self.client.post(url, data=delete_post_data, follow=True)
380 self.assertContains(response, "1 message deleted", status_code=200) 382 self.assertContains(response, "1 message deleted", status_code=200)
381 383
382 # should be really deleted now 384 # should be really deleted now
387 self.assertNotContains(response, post_data2['subject'], status_code=200) 389 self.assertNotContains(response, post_data2['subject'], status_code=200)
388 390
389 # eddie deletes then undeletes message 1 391 # eddie deletes then undeletes message 1
390 delete_post_data = { 392 delete_post_data = {
391 'pm_ids': [msg1.pk], 393 'pm_ids': [msg1.pk],
392 } 394 'action': 'Delete',
393 view_name = 'messages-delete' 395 }
396 view_name = 'messages-bulk'
394 url = reverse(view_name) 397 url = reverse(view_name)
395 response = self.client.post(url, data=delete_post_data, follow=True) 398 response = self.client.post(url, data=delete_post_data, follow=True)
396 self.assertContains(response, "1 message deleted", status_code=200) 399 self.assertContains(response, "1 message deleted", status_code=200)
397 400
398 # should not be really deleted now 401 # should not be really deleted now
602 response = self.client.post(url, data=post_data) 605 response = self.client.post(url, data=post_data)
603 self.assertNotContains(response, "Message sent", status_code=200) 606 self.assertNotContains(response, "Message sent", status_code=200)
604 self.assertContains(response, "Your outbox is full", status_code=200) 607 self.assertContains(response, "Your outbox is full", status_code=200)
605 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT) 608 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
606 609
610 def test_email_export(self):
611
612 post_data = {
613 'receiver': 'eddie',
614 'subject': 'Mr. Moto Demo',
615 'message': 'Gig at Newport High School',
616 }
617 view_name = 'messages-compose'
618 url = reverse(view_name)
619 response = self.client.post(url, data=post_data)
620 self.assertContains(response, "Message sent", status_code=200)
621
622 msg = Message.objects.get(receiver=self.users['eddie'])
623 post_data = {
624 'pm_ids': [msg.id],
625 'action': 'Email',
626 }
627 view_name = 'messages-bulk'
628 url = reverse(view_name)
629 response = self.client.post(url, data=post_data, follow=True)
630 self.assertContains(response, "1 message sent to email", status_code=200)
631
632 # Ensure email sent
633 self.assertEqual(len(mail.outbox), 1)
634
635 if len(mail.outbox) == 1:
636 email = mail.outbox[0]
637 self.assertEqual(len(email.recipients()), 1)
638 self.assertEqual(email.recipients()[0], 'pj@example.com')
639 self.assertTrue(email.subject.startswith('Private messages from'))
640
607 641
608 class EmailTestCase(TestCase): 642 class EmailTestCase(TestCase):
609 """Testing to ensure email is sent when PM is sent if options allow.""" 643 """Testing to ensure email is sent when PM is sent if options allow."""
610 fixtures = ['messages_test_users.json'] 644 fixtures = ['messages_test_users.json']
611 645