comparison messages/tests/test_views.py @ 814:999a71b81111

Private messages refactor: test inbox/outbox limits.
author Brian Neal <bgneal@gmail.com>
date Tue, 09 Sep 2014 20:07:55 -0500
parents eca0c17ff9c8
children cf486a8e8b43
comparison
equal deleted inserted replaced
813:eca0c17ff9c8 814:999a71b81111
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 from django.core import mail
11 11
12 import messages
12 from messages.models import Message, Options, Flag 13 from messages.models import Message, Options, Flag
13
14 # TODO TODO TODO TODO
15 # TODO TODO TODO TODO
16 # TODO TODO TODO TODO
17 #
18 # 1) Test report functionality; ensure email sent to admin
19 # 2) Test email sent when PM sent if options allow it
20 #
21 # TODO TODO TODO TODO
22 # TODO TODO TODO TODO
23 # TODO TODO TODO TODO
24 14
25 15
26 class NotLoggedInTestCase(TestCase): 16 class NotLoggedInTestCase(TestCase):
27 """Ensure we are redirected to the login page before we can do anything with 17 """Ensure we are redirected to the login page before we can do anything with
28 this application. 18 this application.
77 67
78 68
79 class BasicTestCase(TestCase): 69 class BasicTestCase(TestCase):
80 """Testing a logged in user visiting all the views with no messages.""" 70 """Testing a logged in user visiting all the views with no messages."""
81 fixtures = ['messages_test_users.json'] 71 fixtures = ['messages_test_users.json']
72 MSG_BOX_LIMIT = 3
82 73
83 def setUp(self): 74 def setUp(self):
84 self.users = {} 75 self.users = {}
85 self.users['pj'] = User.objects.get(username='pj') 76 self.users['pj'] = User.objects.get(username='pj')
86 self.users['pj'].set_password('12345') 77 self.users['pj'].set_password('12345')
92 self.users['eddie'].save() 83 self.users['eddie'].save()
93 84
94 self.users['richard'] = User.objects.get(username='richard') 85 self.users['richard'] = User.objects.get(username='richard')
95 self.users['richard'].set_password('12345') 86 self.users['richard'].set_password('12345')
96 self.users['richard'].save() 87 self.users['richard'].save()
88
89 # To reduce test duration, reduce MSG_BOX_LIMIT
90 self.saved_msg_box_limit = messages.MSG_BOX_LIMIT
91 messages.MSG_BOX_LIMIT = self.MSG_BOX_LIMIT
92
93 def tearDown(self):
94 messages.MSG_BOX_LIMIT = self.saved_msg_box_limit
97 95
98 def test_simple_gets(self): 96 def test_simple_gets(self):
99 view_names = [ 97 view_names = [
100 'messages-inbox', 98 'messages-inbox',
101 'messages-compose', 99 'messages-compose',
544 email = mail.outbox[0] 542 email = mail.outbox[0]
545 self.assertEqual(len(email.recipients()), 1) 543 self.assertEqual(len(email.recipients()), 1)
546 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') 544 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com')
547 self.assertTrue(email.subject.endswith('A user has flagged a private message')) 545 self.assertTrue(email.subject.endswith('A user has flagged a private message'))
548 546
547 def test_inbox_full(self):
548
549 post_data = {
550 'receiver': 'eddie',
551 'subject': 'Mr. Moto Demo',
552 'message': 'Gig at Newport High School',
553 }
554 view_name = 'messages-compose'
555 url = reverse(view_name)
556 for n in range(self.MSG_BOX_LIMIT):
557 response = self.client.post(url, data=post_data)
558 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
559
560 # pj deletes a message so we can make sure we are testing inbox full
561 msg = Message.objects.filter(receiver=self.users['eddie']).last()
562 msg.sender_delete_date = datetime.datetime.now()
563 msg.save()
564 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
565 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
566 self.MSG_BOX_LIMIT - 1)
567 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
568 self.MSG_BOX_LIMIT)
569
570 # pj should not be able to send another
571 response = self.client.post(url, data=post_data)
572 self.assertNotContains(response, "Message sent", status_code=200)
573 self.assertContains(response, "inbox is full", status_code=200)
574 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
575
576 def test_outbox_full(self):
577
578 post_data = {
579 'receiver': 'eddie',
580 'subject': 'Mr. Moto Demo',
581 'message': 'Gig at Newport High School',
582 }
583 view_name = 'messages-compose'
584 url = reverse(view_name)
585 for n in range(self.MSG_BOX_LIMIT):
586 response = self.client.post(url, data=post_data)
587 self.assertContains(response, "Message sent", status_code=200)
588 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
589
590 # eddie deletes a message so we can make sure we are testing outbox full
591 msg = Message.objects.filter(receiver=self.users['eddie']).last()
592 msg.read_date = datetime.datetime.now()
593 msg.receiver_delete_date = msg.read_date
594 msg.save()
595 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
596 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
597 self.MSG_BOX_LIMIT)
598 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
599 self.MSG_BOX_LIMIT - 1)
600
601 # pj should not be able to send another
602 response = self.client.post(url, data=post_data)
603 self.assertNotContains(response, "Message sent", status_code=200)
604 self.assertContains(response, "Your outbox is full", status_code=200)
605 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
606
549 607
550 class EmailTestCase(TestCase): 608 class EmailTestCase(TestCase):
551 """Testing to ensure email is sent when PM is sent if options allow.""" 609 """Testing to ensure email is sent when PM is sent if options allow."""
552 fixtures = ['messages_test_users.json'] 610 fixtures = ['messages_test_users.json']
553 611