Mercurial > public > sg101
comparison messages/tests/test_views.py @ 813:eca0c17ff9c8
Private message refactor: reports should email admins.
Added tests for reporting messages and ensuring emails are sent
when messages are sent (when user options allow it).
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 08 Sep 2014 20:50:37 -0500 |
parents | 42436d674ba8 |
children | 999a71b81111 |
comparison
equal
deleted
inserted
replaced
812:42436d674ba8 | 813:eca0c17ff9c8 |
---|---|
5 import datetime | 5 import datetime |
6 | 6 |
7 from django.test import TestCase | 7 from django.test import TestCase |
8 from django.core.urlresolvers import reverse | 8 from django.core.urlresolvers import reverse |
9 from django.contrib.auth.models import User | 9 from django.contrib.auth.models import User |
10 from django.core import mail | |
10 | 11 |
11 from messages.models import Message, Options, Flag | 12 from messages.models import Message, Options, Flag |
12 | 13 |
13 # TODO TODO TODO TODO | 14 # TODO TODO TODO TODO |
14 # TODO TODO TODO TODO | 15 # TODO TODO TODO TODO |
89 self.users['eddie'] = User.objects.get(username='eddie') | 90 self.users['eddie'] = User.objects.get(username='eddie') |
90 self.users['eddie'].set_password('12345') | 91 self.users['eddie'].set_password('12345') |
91 self.users['eddie'].save() | 92 self.users['eddie'].save() |
92 | 93 |
93 self.users['richard'] = User.objects.get(username='richard') | 94 self.users['richard'] = User.objects.get(username='richard') |
95 self.users['richard'].set_password('12345') | |
96 self.users['richard'].save() | |
94 | 97 |
95 def test_simple_gets(self): | 98 def test_simple_gets(self): |
96 view_names = [ | 99 view_names = [ |
97 'messages-inbox', | 100 'messages-inbox', |
98 'messages-compose', | 101 'messages-compose', |
477 self.assertIsNone(msg.reply_date) | 480 self.assertIsNone(msg.reply_date) |
478 self.assertEqual(msg.subject, post_data['subject']) | 481 self.assertEqual(msg.subject, post_data['subject']) |
479 self.assertEqual(msg.message, post_data['message']) | 482 self.assertEqual(msg.message, post_data['message']) |
480 self.assertIsNone(msg.sender_delete_date) | 483 self.assertIsNone(msg.sender_delete_date) |
481 self.assertIsNone(msg.receiver_delete_date) | 484 self.assertIsNone(msg.receiver_delete_date) |
485 | |
486 def test_report_message(self): | |
487 post_data = { | |
488 'receiver': 'eddie', | |
489 'subject': 'Mr. Moto Demo', | |
490 'message': 'Gig at Newport High School', | |
491 } | |
492 view_name = 'messages-compose' | |
493 url = reverse(view_name) | |
494 response = self.client.post(url, data=post_data) | |
495 self.assertContains(response, "Message sent", status_code=200) | |
496 | |
497 msg = Message.objects.get(sender=self.users['pj']) | |
498 | |
499 # Ensure non-recipients can't report a message | |
500 view_name = 'messages-report' | |
501 url = reverse(view_name, args=[msg.pk]) | |
502 response = self.client.post(url, data={}, follow=True) | |
503 self.assertContains(response, "You can't report this message", status_code=200) | |
504 self.assertEqual(Flag.objects.count(), 0) | |
505 | |
506 self.assertTrue(self.client.login(username='richard', password='12345')) | |
507 response = self.client.post(url, data={}, follow=True) | |
508 self.assertContains(response, "You can't report this message", status_code=200) | |
509 self.assertEqual(Flag.objects.count(), 0) | |
510 | |
511 # Test bogus report | |
512 self.assertTrue(self.client.login(username='eddie', password='12345')) | |
513 bad_url = reverse(view_name, args=[msg.pk + 1]) | |
514 response = self.client.post(bad_url, data={}) | |
515 self.assertEqual(response.status_code, 404) | |
516 self.assertEqual(Flag.objects.count(), 0) | |
517 | |
518 # Test get of report view | |
519 response = self.client.get(url) | |
520 self.assertEqual(response.status_code, 200) | |
521 | |
522 # Test posting to report view | |
523 post_data = { | |
524 'comments': 'This is abuse!', | |
525 } | |
526 now = datetime.datetime.now() | |
527 response = self.client.post(url, data=post_data, follow=True) | |
528 self.assertContains(response, "Message reported", status_code=200) | |
529 | |
530 self.assertEqual(Flag.objects.count(), 1) | |
531 msg = Message.objects.get(receiver=self.users['eddie']) | |
532 | |
533 try: | |
534 flag = msg.flag | |
535 except Flag.DoesNotExist: | |
536 self.fail("msg.flag does not exist!") | |
537 | |
538 self.assertEqual(flag.message, msg) | |
539 self.assertTrue(now - flag.flag_date < datetime.timedelta(seconds=2)) | |
540 self.assertEqual(flag.comments, post_data['comments']) | |
541 | |
542 # Admin should have been sent an email | |
543 self.assertEqual(len(mail.outbox), 1) | |
544 email = mail.outbox[0] | |
545 self.assertEqual(len(email.recipients()), 1) | |
546 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') | |
547 self.assertTrue(email.subject.endswith('A user has flagged a private message')) | |
548 | |
549 | |
550 class EmailTestCase(TestCase): | |
551 """Testing to ensure email is sent when PM is sent if options allow.""" | |
552 fixtures = ['messages_test_users.json'] | |
553 | |
554 def setUp(self): | |
555 self.users = {} | |
556 self.users['pj'] = User.objects.get(username='pj') | |
557 self.users['pj'].set_password('12345') | |
558 self.users['pj'].save() | |
559 self.assertTrue(self.client.login(username='pj', password='12345')) | |
560 | |
561 self.users['eddie'] = User.objects.get(username='eddie') | |
562 self.users['eddie'].set_password('12345') | |
563 self.users['eddie'].save() | |
564 | |
565 def _test_email(self, email_expected): | |
566 # pj sends message to eddie | |
567 post_data = { | |
568 'receiver': 'eddie', | |
569 'subject': 'Mr. Moto Demo', | |
570 'message': 'Gig at Newport High School', | |
571 } | |
572 view_name = 'messages-compose' | |
573 url = reverse(view_name) | |
574 response = self.client.post(url, data=post_data) | |
575 self.assertContains(response, "Message sent", status_code=200) | |
576 | |
577 # Ensure notification email sent | |
578 count = 1 if email_expected else 0 | |
579 self.assertEqual(len(mail.outbox), count) | |
580 | |
581 if count == 1: | |
582 email = mail.outbox[0] | |
583 self.assertEqual(len(email.recipients()), 1) | |
584 self.assertEqual(email.recipients()[0], 'eddie@example.com') | |
585 self.assertTrue(email.subject.startswith('New private message')) | |
586 | |
587 def test_email_sent(self): | |
588 # eddie elects to receive email notifications | |
589 # Note: testing the options view is done above. | |
590 opts = Options.objects.for_user(self.users['eddie']) | |
591 opts.notify_email = True | |
592 opts.save() | |
593 self._test_email(True) | |
594 | |
595 def test_no_email(self): | |
596 # eddie does not to elect to receive email notifications | |
597 opts = Options.objects.for_user(self.users['eddie']) | |
598 opts.notify_email = False | |
599 opts.save() | |
600 self._test_email(False) |