bgneal@811: """ bgneal@811: Unit tests for the messages application views. bgneal@811: bgneal@811: """ bgneal@812: import datetime bgneal@812: bgneal@811: from django.test import TestCase bgneal@811: from django.core.urlresolvers import reverse bgneal@812: from django.contrib.auth.models import User bgneal@813: from django.core import mail bgneal@811: bgneal@811: from messages.models import Message, Options, Flag bgneal@811: bgneal@812: # TODO TODO TODO TODO bgneal@812: # TODO TODO TODO TODO bgneal@812: # TODO TODO TODO TODO bgneal@812: # bgneal@812: # 1) Test report functionality; ensure email sent to admin bgneal@812: # 2) Test email sent when PM sent if options allow it bgneal@812: # bgneal@812: # TODO TODO TODO TODO bgneal@812: # TODO TODO TODO TODO bgneal@812: # TODO TODO TODO TODO bgneal@811: bgneal@811: bgneal@811: class NotLoggedInTestCase(TestCase): bgneal@811: """Ensure we are redirected to the login page before we can do anything with bgneal@811: this application. bgneal@811: bgneal@811: """ bgneal@811: LOGIN_URL = reverse('accounts-login') bgneal@811: bgneal@811: def _test_get(self, url_name, **kwargs): bgneal@811: url = reverse(url_name, **kwargs) bgneal@811: response = self.client.get(url, follow=True) bgneal@811: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@811: bgneal@811: def _test_post(self, url_name, **kwargs): bgneal@811: url = reverse(url_name, **kwargs) bgneal@811: response = self.client.post(url, follow=True) bgneal@811: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@811: bgneal@811: def test_inbox(self): bgneal@811: self._test_get('messages-inbox') bgneal@811: bgneal@811: def test_compose(self): bgneal@811: url_name = 'messages-compose' bgneal@811: self._test_get(url_name) bgneal@811: self._test_post(url_name) bgneal@811: bgneal@811: def test_outbox(self): bgneal@811: self._test_get('messages-outbox') bgneal@811: bgneal@811: def test_trash(self): bgneal@811: self._test_get('messages-trash') bgneal@811: bgneal@811: def test_options(self): bgneal@811: url_name = 'messages-options' bgneal@811: self._test_get(url_name) bgneal@811: self._test_post(url_name) bgneal@811: bgneal@811: def test_delete(self): bgneal@811: self._test_get('messages-delete') bgneal@811: bgneal@811: def test_undelete(self): bgneal@811: self._test_get('messages-undelete') bgneal@811: bgneal@811: def test_view(self): bgneal@811: url_name = 'messages-view' bgneal@811: self._test_get(url_name, args=[123]) bgneal@811: self._test_post(url_name, args=[123]) bgneal@811: bgneal@811: def test_report(self): bgneal@811: url_name = 'messages-report' bgneal@811: self._test_get(url_name, args=[123]) bgneal@811: self._test_post(url_name, args=[123]) bgneal@812: bgneal@812: bgneal@812: class BasicTestCase(TestCase): bgneal@812: """Testing a logged in user visiting all the views with no messages.""" bgneal@812: fixtures = ['messages_test_users.json'] bgneal@812: bgneal@812: def setUp(self): bgneal@812: self.users = {} bgneal@812: self.users['pj'] = User.objects.get(username='pj') bgneal@812: self.users['pj'].set_password('12345') bgneal@812: self.users['pj'].save() bgneal@812: self.assertTrue(self.client.login(username='pj', password='12345')) bgneal@812: bgneal@812: self.users['eddie'] = User.objects.get(username='eddie') bgneal@812: self.users['eddie'].set_password('12345') bgneal@812: self.users['eddie'].save() bgneal@812: bgneal@812: self.users['richard'] = User.objects.get(username='richard') bgneal@813: self.users['richard'].set_password('12345') bgneal@813: self.users['richard'].save() bgneal@812: bgneal@812: def test_simple_gets(self): bgneal@812: view_names = [ bgneal@812: 'messages-inbox', bgneal@812: 'messages-compose', bgneal@812: 'messages-outbox', bgneal@812: 'messages-trash', bgneal@812: 'messages-options', bgneal@812: ] bgneal@812: for view_name in view_names: bgneal@812: response = self.client.get(reverse(view_name)) bgneal@812: self.assertEqual(response.status_code, 200) bgneal@812: bgneal@812: def test_wrong_method(self): bgneal@812: view_names = [ bgneal@812: 'messages-delete', bgneal@812: 'messages-undelete', bgneal@812: ] bgneal@812: for view_name in view_names: bgneal@812: response = self.client.get(reverse(view_name)) bgneal@812: self.assertEqual(response.status_code, 405) bgneal@812: bgneal@812: def test_nonexistent_pms(self): bgneal@812: view_names = [ bgneal@812: 'messages-view', bgneal@812: 'messages-report', bgneal@812: ] bgneal@812: for view_name in view_names: bgneal@812: response = self.client.get(reverse(view_name, args=[42])) bgneal@812: self.assertEqual(response.status_code, 404) bgneal@812: bgneal@812: response = self.client.post(reverse(view_name, args=[42])) bgneal@812: self.assertEqual(response.status_code, 404) bgneal@812: bgneal@812: def test_options(self): bgneal@812: view_name = 'messages-options' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertEqual(response.status_code, 200) bgneal@812: bgneal@812: post_data = { bgneal@812: 'opts-attach_signature': 'on', bgneal@812: 'opts-notify_email': 'on', bgneal@812: } bgneal@812: response = self.client.post(url, data=post_data, follow=True) bgneal@812: self.assertRedirects(response, url) bgneal@812: bgneal@812: opts = Options.objects.for_user(self.users['pj']) bgneal@812: self.assertTrue(opts.attach_signature) bgneal@812: self.assertTrue(opts.notify_email) bgneal@812: bgneal@812: post_data = {} bgneal@812: response = self.client.post(url, data=post_data, follow=True) bgneal@812: self.assertRedirects(response, url) bgneal@812: bgneal@812: opts = Options.objects.for_user(self.users['pj']) bgneal@812: self.assertFalse(opts.attach_signature) bgneal@812: self.assertFalse(opts.notify_email) bgneal@812: bgneal@812: def test_send_bogus_user(self): bgneal@812: post_data = { bgneal@812: 'receiver': 'unknown_user', bgneal@812: 'subject': 'hi', bgneal@812: 'message': 'test', bgneal@812: } bgneal@812: view_name = 'messages-compose' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=post_data) bgneal@812: self.assertContains(response, 'username does not exist', status_code=200) bgneal@812: bgneal@812: qs = Message.objects.filter(sender=self.users['pj']) bgneal@812: self.assertEqual(qs.count(), 0) bgneal@812: bgneal@812: def test_send_to_self(self): bgneal@812: post_data = { bgneal@812: 'receiver': 'pj', bgneal@812: 'subject': 'hi', bgneal@812: 'message': 'test', bgneal@812: } bgneal@812: view_name = 'messages-compose' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=post_data) bgneal@812: self.assertContains(response, "You can't send a message to yourself", status_code=200) bgneal@812: bgneal@812: qs = Message.objects.filter(sender=self.users['pj']) bgneal@812: self.assertEqual(qs.count(), 0) bgneal@812: bgneal@812: def test_message_cycle(self): bgneal@812: # pj sends message to eddie bgneal@812: now = datetime.datetime.now() bgneal@812: post_data = { bgneal@812: 'receiver': 'eddie', bgneal@812: 'subject': 'Mr. Moto Demo', bgneal@812: 'message': 'Gig at Newport High School', bgneal@812: } bgneal@812: view_name = 'messages-compose' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=post_data) bgneal@812: self.assertContains(response, "Message sent", status_code=200) bgneal@812: bgneal@812: # see if message got created in database bgneal@812: msg = Message.objects.get(sender=self.users['pj']) bgneal@812: bgneal@812: self.assertEqual(msg.sender, self.users['pj']) bgneal@812: self.assertEqual(msg.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg.read_date) bgneal@812: self.assertIsNone(msg.reply_date) bgneal@812: self.assertEqual(msg.subject, post_data['subject']) bgneal@812: self.assertEqual(msg.message, post_data['message']) bgneal@812: self.assertIsNone(msg.sender_delete_date) bgneal@812: self.assertIsNone(msg.receiver_delete_date) bgneal@812: bgneal@812: # see if shows up in outbox view bgneal@812: view_name = 'messages-outbox' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: bgneal@812: # see if we can view it bgneal@812: view_name = 'messages-view' bgneal@812: url = reverse(view_name, args=[msg.pk]) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: self.assertContains(response, post_data['message'], status_code=200) bgneal@812: bgneal@812: # eddie logs in and checks various views bgneal@812: self.assertTrue(self.client.login(username='eddie', password='12345')) bgneal@812: view_name = 'messages-inbox' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: view_name = 'messages-outbox' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=200) bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=200) bgneal@812: bgneal@812: # eddie reads it bgneal@812: view_name = 'messages-view' bgneal@812: url = reverse(view_name, args=[msg.pk]) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: self.assertContains(response, post_data['message'], status_code=200) bgneal@812: bgneal@812: # see if message got updated in database bgneal@812: msg = Message.objects.get(receiver=self.users['eddie']) bgneal@812: bgneal@812: self.assertEqual(msg.sender, self.users['pj']) bgneal@812: self.assertEqual(msg.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg.reply_date) bgneal@812: self.assertEqual(msg.subject, post_data['subject']) bgneal@812: self.assertEqual(msg.message, post_data['message']) bgneal@812: self.assertIsNone(msg.sender_delete_date) bgneal@812: self.assertIsNone(msg.receiver_delete_date) bgneal@812: bgneal@812: # eddie replies bgneal@812: post_data2 = { bgneal@812: 'receiver': 'pj', bgneal@812: 'subject': 'Re: Mr. Moto Demo', bgneal@812: 'message': 'South Bay Sound', bgneal@812: 'parent_id': msg.pk, bgneal@812: } bgneal@812: view_name = 'messages-compose' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=post_data2) bgneal@812: self.assertContains(response, "Message sent", status_code=200) bgneal@812: bgneal@812: self.assertEqual(Message.objects.all().count(), 2) bgneal@812: bgneal@812: msg1 = Message.objects.get(receiver=self.users['eddie']) bgneal@812: bgneal@812: self.assertEqual(msg1.sender, self.users['pj']) bgneal@812: self.assertEqual(msg1.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertEqual(msg1.subject, post_data['subject']) bgneal@812: self.assertEqual(msg1.message, post_data['message']) bgneal@812: self.assertIsNone(msg1.sender_delete_date) bgneal@812: self.assertIsNone(msg1.receiver_delete_date) bgneal@812: bgneal@812: msg2 = Message.objects.get(receiver=self.users['pj']) bgneal@812: bgneal@812: self.assertEqual(msg2.sender, self.users['eddie']) bgneal@812: self.assertEqual(msg2.receiver, self.users['pj']) bgneal@812: self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg2.read_date) bgneal@812: self.assertIsNone(msg2.reply_date) bgneal@812: self.assertEqual(msg2.subject, post_data2['subject']) bgneal@812: self.assertEqual(msg2.message, post_data2['message']) bgneal@812: self.assertIsNone(msg2.sender_delete_date) bgneal@812: self.assertIsNone(msg2.receiver_delete_date) bgneal@812: bgneal@812: # pj logs in and checks various views bgneal@812: self.assertTrue(self.client.login(username='pj', password='12345')) bgneal@812: view_name = 'messages-inbox' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data2['subject'], status_code=200) bgneal@812: view_name = 'messages-outbox' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=200) bgneal@812: self.assertNotContains(response, post_data2['subject'], status_code=200) bgneal@812: bgneal@812: # pj reads reply bgneal@812: view_name = 'messages-view' bgneal@812: url = reverse(view_name, args=[msg2.pk]) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data2['subject'], status_code=200) bgneal@812: self.assertContains(response, post_data2['message'], status_code=200) bgneal@812: bgneal@812: msg = Message.objects.get(receiver=self.users['eddie']) bgneal@812: bgneal@812: self.assertEqual(msg.sender, self.users['pj']) bgneal@812: self.assertEqual(msg.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg.reply_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertEqual(msg.subject, post_data['subject']) bgneal@812: self.assertEqual(msg.message, post_data['message']) bgneal@812: self.assertIsNone(msg.sender_delete_date) bgneal@812: self.assertIsNone(msg.receiver_delete_date) bgneal@812: bgneal@812: msg2 = Message.objects.get(receiver=self.users['pj']) bgneal@812: bgneal@812: self.assertEqual(msg2.sender, self.users['eddie']) bgneal@812: self.assertEqual(msg2.receiver, self.users['pj']) bgneal@812: self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg2.reply_date) bgneal@812: self.assertEqual(msg2.subject, post_data2['subject']) bgneal@812: self.assertEqual(msg2.message, post_data2['message']) bgneal@812: self.assertIsNone(msg2.sender_delete_date) bgneal@812: self.assertIsNone(msg2.receiver_delete_date) bgneal@812: bgneal@812: # pj deletes message 2 bgneal@812: self.assertEqual(Message.objects.all().count(), 2) bgneal@812: bgneal@812: delete_post_data = { bgneal@812: 'pm_ids': [msg2.pk], bgneal@812: } bgneal@812: view_name = 'messages-delete' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=delete_post_data, follow=True) bgneal@812: self.assertContains(response, "1 message deleted", status_code=200) bgneal@812: bgneal@812: self.assertEqual(Message.objects.all().count(), 2) bgneal@812: bgneal@812: msg2 = Message.objects.get(receiver=self.users['pj']) bgneal@812: bgneal@812: self.assertEqual(msg2.sender, self.users['eddie']) bgneal@812: self.assertEqual(msg2.receiver, self.users['pj']) bgneal@812: self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg2.reply_date) bgneal@812: self.assertEqual(msg2.subject, post_data2['subject']) bgneal@812: self.assertEqual(msg2.message, post_data2['message']) bgneal@812: self.assertIsNone(msg2.sender_delete_date) bgneal@812: self.assertTrue(now - msg2.receiver_delete_date < datetime.timedelta(seconds=5)) bgneal@812: bgneal@812: # should be in pj's trash now bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data2['subject'], status_code=200) bgneal@812: bgneal@812: # eddie logs in and deletes his copy of message 2 bgneal@812: self.assertTrue(self.client.login(username='eddie', password='12345')) bgneal@812: delete_post_data = { bgneal@812: 'pm_ids': [msg2.pk], bgneal@812: } bgneal@812: view_name = 'messages-delete' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=delete_post_data, follow=True) bgneal@812: self.assertContains(response, "1 message deleted", status_code=200) bgneal@812: bgneal@812: # should be really deleted now bgneal@812: self.assertEqual(Message.objects.all().count(), 1) bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data2['subject'], status_code=200) bgneal@812: bgneal@812: # eddie deletes then undeletes message 1 bgneal@812: delete_post_data = { bgneal@812: 'pm_ids': [msg1.pk], bgneal@812: } bgneal@812: view_name = 'messages-delete' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=delete_post_data, follow=True) bgneal@812: self.assertContains(response, "1 message deleted", status_code=200) bgneal@812: bgneal@812: # should not be really deleted now bgneal@812: self.assertEqual(Message.objects.all().count(), 1) bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertContains(response, post_data['subject'], status_code=200) bgneal@812: bgneal@812: msg1 = Message.objects.get(receiver=self.users['eddie']) bgneal@812: bgneal@812: self.assertEqual(msg1.sender, self.users['pj']) bgneal@812: self.assertEqual(msg1.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertEqual(msg1.subject, post_data['subject']) bgneal@812: self.assertEqual(msg1.message, post_data['message']) bgneal@812: self.assertIsNone(msg1.sender_delete_date) bgneal@812: self.assertTrue(now - msg1.receiver_delete_date < datetime.timedelta(seconds=5)) bgneal@812: bgneal@812: delete_post_data = { bgneal@812: 'pm_ids': [msg1.pk], bgneal@812: } bgneal@812: view_name = 'messages-undelete' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=delete_post_data, follow=True) bgneal@812: self.assertContains(response, "1 message undeleted", status_code=200) bgneal@812: bgneal@812: # should not be really deleted now bgneal@812: self.assertEqual(Message.objects.all().count(), 1) bgneal@812: view_name = 'messages-trash' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=200) bgneal@812: bgneal@812: msg1 = Message.objects.get(receiver=self.users['eddie']) bgneal@812: bgneal@812: self.assertEqual(msg1.sender, self.users['pj']) bgneal@812: self.assertEqual(msg1.receiver, self.users['eddie']) bgneal@812: self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertEqual(msg1.subject, post_data['subject']) bgneal@812: self.assertEqual(msg1.message, post_data['message']) bgneal@812: self.assertIsNone(msg1.sender_delete_date) bgneal@812: self.assertIsNone(msg1.receiver_delete_date) bgneal@812: bgneal@812: def test_read_permission(self): bgneal@812: bgneal@812: now = datetime.datetime.now() bgneal@812: # pj sends a PM to richard bgneal@812: post_data = { bgneal@812: 'receiver': 'richard', bgneal@812: 'subject': 'Mr. Moto Demo', bgneal@812: 'message': 'Gig at Newport High School', bgneal@812: } bgneal@812: view_name = 'messages-compose' bgneal@812: url = reverse(view_name) bgneal@812: response = self.client.post(url, data=post_data) bgneal@812: self.assertContains(response, "Message sent", status_code=200) bgneal@812: bgneal@812: msg = Message.objects.get(sender=self.users['pj']) bgneal@812: bgneal@812: # eddie should not be able to read it bgneal@812: self.assertTrue(self.client.login(username='eddie', password='12345')) bgneal@812: view_name = 'messages-view' bgneal@812: url = reverse(view_name, args=[msg.pk]) bgneal@812: response = self.client.get(url) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=302) bgneal@812: self.assertNotContains(response, post_data['message'], status_code=302) bgneal@812: bgneal@812: response = self.client.get(url, follow=True) bgneal@812: self.assertNotContains(response, post_data['subject'], status_code=200) bgneal@812: self.assertNotContains(response, post_data['message'], status_code=200) bgneal@812: bgneal@812: msg = Message.objects.get(sender=self.users['pj']) bgneal@812: bgneal@812: self.assertEqual(msg.sender, self.users['pj']) bgneal@812: self.assertEqual(msg.receiver, self.users['richard']) bgneal@812: self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5)) bgneal@812: self.assertIsNone(msg.read_date) bgneal@812: self.assertIsNone(msg.reply_date) bgneal@812: self.assertEqual(msg.subject, post_data['subject']) bgneal@812: self.assertEqual(msg.message, post_data['message']) bgneal@812: self.assertIsNone(msg.sender_delete_date) bgneal@812: self.assertIsNone(msg.receiver_delete_date) bgneal@813: bgneal@813: def test_report_message(self): bgneal@813: post_data = { bgneal@813: 'receiver': 'eddie', bgneal@813: 'subject': 'Mr. Moto Demo', bgneal@813: 'message': 'Gig at Newport High School', bgneal@813: } bgneal@813: view_name = 'messages-compose' bgneal@813: url = reverse(view_name) bgneal@813: response = self.client.post(url, data=post_data) bgneal@813: self.assertContains(response, "Message sent", status_code=200) bgneal@813: bgneal@813: msg = Message.objects.get(sender=self.users['pj']) bgneal@813: bgneal@813: # Ensure non-recipients can't report a message bgneal@813: view_name = 'messages-report' bgneal@813: url = reverse(view_name, args=[msg.pk]) bgneal@813: response = self.client.post(url, data={}, follow=True) bgneal@813: self.assertContains(response, "You can't report this message", status_code=200) bgneal@813: self.assertEqual(Flag.objects.count(), 0) bgneal@813: bgneal@813: self.assertTrue(self.client.login(username='richard', password='12345')) bgneal@813: response = self.client.post(url, data={}, follow=True) bgneal@813: self.assertContains(response, "You can't report this message", status_code=200) bgneal@813: self.assertEqual(Flag.objects.count(), 0) bgneal@813: bgneal@813: # Test bogus report bgneal@813: self.assertTrue(self.client.login(username='eddie', password='12345')) bgneal@813: bad_url = reverse(view_name, args=[msg.pk + 1]) bgneal@813: response = self.client.post(bad_url, data={}) bgneal@813: self.assertEqual(response.status_code, 404) bgneal@813: self.assertEqual(Flag.objects.count(), 0) bgneal@813: bgneal@813: # Test get of report view bgneal@813: response = self.client.get(url) bgneal@813: self.assertEqual(response.status_code, 200) bgneal@813: bgneal@813: # Test posting to report view bgneal@813: post_data = { bgneal@813: 'comments': 'This is abuse!', bgneal@813: } bgneal@813: now = datetime.datetime.now() bgneal@813: response = self.client.post(url, data=post_data, follow=True) bgneal@813: self.assertContains(response, "Message reported", status_code=200) bgneal@813: bgneal@813: self.assertEqual(Flag.objects.count(), 1) bgneal@813: msg = Message.objects.get(receiver=self.users['eddie']) bgneal@813: bgneal@813: try: bgneal@813: flag = msg.flag bgneal@813: except Flag.DoesNotExist: bgneal@813: self.fail("msg.flag does not exist!") bgneal@813: bgneal@813: self.assertEqual(flag.message, msg) bgneal@813: self.assertTrue(now - flag.flag_date < datetime.timedelta(seconds=2)) bgneal@813: self.assertEqual(flag.comments, post_data['comments']) bgneal@813: bgneal@813: # Admin should have been sent an email bgneal@813: self.assertEqual(len(mail.outbox), 1) bgneal@813: email = mail.outbox[0] bgneal@813: self.assertEqual(len(email.recipients()), 1) bgneal@813: self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') bgneal@813: self.assertTrue(email.subject.endswith('A user has flagged a private message')) bgneal@813: bgneal@813: bgneal@813: class EmailTestCase(TestCase): bgneal@813: """Testing to ensure email is sent when PM is sent if options allow.""" bgneal@813: fixtures = ['messages_test_users.json'] bgneal@813: bgneal@813: def setUp(self): bgneal@813: self.users = {} bgneal@813: self.users['pj'] = User.objects.get(username='pj') bgneal@813: self.users['pj'].set_password('12345') bgneal@813: self.users['pj'].save() bgneal@813: self.assertTrue(self.client.login(username='pj', password='12345')) bgneal@813: bgneal@813: self.users['eddie'] = User.objects.get(username='eddie') bgneal@813: self.users['eddie'].set_password('12345') bgneal@813: self.users['eddie'].save() bgneal@813: bgneal@813: def _test_email(self, email_expected): bgneal@813: # pj sends message to eddie bgneal@813: post_data = { bgneal@813: 'receiver': 'eddie', bgneal@813: 'subject': 'Mr. Moto Demo', bgneal@813: 'message': 'Gig at Newport High School', bgneal@813: } bgneal@813: view_name = 'messages-compose' bgneal@813: url = reverse(view_name) bgneal@813: response = self.client.post(url, data=post_data) bgneal@813: self.assertContains(response, "Message sent", status_code=200) bgneal@813: bgneal@813: # Ensure notification email sent bgneal@813: count = 1 if email_expected else 0 bgneal@813: self.assertEqual(len(mail.outbox), count) bgneal@813: bgneal@813: if count == 1: bgneal@813: email = mail.outbox[0] bgneal@813: self.assertEqual(len(email.recipients()), 1) bgneal@813: self.assertEqual(email.recipients()[0], 'eddie@example.com') bgneal@813: self.assertTrue(email.subject.startswith('New private message')) bgneal@813: bgneal@813: def test_email_sent(self): bgneal@813: # eddie elects to receive email notifications bgneal@813: # Note: testing the options view is done above. bgneal@813: opts = Options.objects.for_user(self.users['eddie']) bgneal@813: opts.notify_email = True bgneal@813: opts.save() bgneal@813: self._test_email(True) bgneal@813: bgneal@813: def test_no_email(self): bgneal@813: # eddie does not to elect to receive email notifications bgneal@813: opts = Options.objects.for_user(self.users['eddie']) bgneal@813: opts.notify_email = False bgneal@813: opts.save() bgneal@813: self._test_email(False)