changeset 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 95462f4adb94
files messages/forms.py messages/tests/test_views.py
diffstat 2 files changed, 72 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/messages/forms.py	Mon Sep 08 20:50:37 2014 -0500
+++ b/messages/forms.py	Tue Sep 09 20:07:55 2014 -0500
@@ -11,8 +11,8 @@
 
 from core.functions import send_mail
 from core.widgets import AutoCompleteUserInput
+import messages
 from messages.models import Flag, Message, Options
-from messages import MSG_BOX_LIMIT
 
 
 # Maximum size of a private message in characters
@@ -62,13 +62,13 @@
             # Can we send a message? Is our outbox full?
 
             count = Message.objects.outbox(self.user).count()
-            if count >= MSG_BOX_LIMIT:
+            if count >= messages.MSG_BOX_LIMIT:
                 raise forms.ValidationError(
                         "Your outbox is full. Please delete some messages.")
 
             # Is the receiver's inbox full?
             count = Message.objects.inbox(self.rcvr_user).count()
-            if count >= MSG_BOX_LIMIT:
+            if count >= messages.MSG_BOX_LIMIT:
                 raise forms.ValidationError(
                     "Sorry, %s's inbox is full. This message cannot be sent." %
                     self.rcvr_user.username)
--- a/messages/tests/test_views.py	Mon Sep 08 20:50:37 2014 -0500
+++ b/messages/tests/test_views.py	Tue Sep 09 20:07:55 2014 -0500
@@ -9,19 +9,9 @@
 from django.contrib.auth.models import User
 from django.core import mail
 
+import messages
 from messages.models import Message, Options, Flag
 
-# TODO TODO TODO TODO
-# TODO TODO TODO TODO
-# TODO TODO TODO TODO
-#
-# 1) Test report functionality; ensure email sent to admin
-# 2) Test email sent when PM sent if options allow it
-#
-# TODO TODO TODO TODO
-# TODO TODO TODO TODO
-# TODO TODO TODO TODO
-
 
 class NotLoggedInTestCase(TestCase):
     """Ensure we are redirected to the login page before we can do anything with
@@ -79,6 +69,7 @@
 class BasicTestCase(TestCase):
     """Testing a logged in user visiting all the views with no messages."""
     fixtures = ['messages_test_users.json']
+    MSG_BOX_LIMIT = 3
 
     def setUp(self):
         self.users = {}
@@ -95,6 +86,13 @@
         self.users['richard'].set_password('12345')
         self.users['richard'].save()
 
+        # To reduce test duration, reduce MSG_BOX_LIMIT
+        self.saved_msg_box_limit = messages.MSG_BOX_LIMIT
+        messages.MSG_BOX_LIMIT = self.MSG_BOX_LIMIT
+
+    def tearDown(self):
+        messages.MSG_BOX_LIMIT = self.saved_msg_box_limit
+
     def test_simple_gets(self):
         view_names = [
             'messages-inbox',
@@ -546,6 +544,66 @@
         self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com')
         self.assertTrue(email.subject.endswith('A user has flagged a private message'))
 
+    def test_inbox_full(self):
+
+        post_data = {
+            'receiver': 'eddie',
+            'subject': 'Mr. Moto Demo',
+            'message': 'Gig at Newport High School',
+        }
+        view_name = 'messages-compose'
+        url = reverse(view_name)
+        for n in range(self.MSG_BOX_LIMIT):
+            response = self.client.post(url, data=post_data)
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+
+        # pj deletes a message so we can make sure we are testing inbox full
+        msg = Message.objects.filter(receiver=self.users['eddie']).last()
+        msg.sender_delete_date = datetime.datetime.now()
+        msg.save()
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+        self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
+                self.MSG_BOX_LIMIT - 1)
+        self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
+                self.MSG_BOX_LIMIT)
+
+        # pj should not be able to send another
+        response = self.client.post(url, data=post_data)
+        self.assertNotContains(response, "Message sent", status_code=200)
+        self.assertContains(response, "inbox is full", status_code=200)
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+
+    def test_outbox_full(self):
+
+        post_data = {
+            'receiver': 'eddie',
+            'subject': 'Mr. Moto Demo',
+            'message': 'Gig at Newport High School',
+        }
+        view_name = 'messages-compose'
+        url = reverse(view_name)
+        for n in range(self.MSG_BOX_LIMIT):
+            response = self.client.post(url, data=post_data)
+            self.assertContains(response, "Message sent", status_code=200)
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+
+        # eddie deletes a message so we can make sure we are testing outbox full
+        msg = Message.objects.filter(receiver=self.users['eddie']).last()
+        msg.read_date = datetime.datetime.now()
+        msg.receiver_delete_date = msg.read_date
+        msg.save()
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+        self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
+                self.MSG_BOX_LIMIT)
+        self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
+                self.MSG_BOX_LIMIT - 1)
+
+        # pj should not be able to send another
+        response = self.client.post(url, data=post_data)
+        self.assertNotContains(response, "Message sent", status_code=200)
+        self.assertContains(response, "Your outbox is full", status_code=200)
+        self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
+
 
 class EmailTestCase(TestCase):
     """Testing to ensure email is sent when PM is sent if options allow."""