annotate messages/tests/test_views.py @ 1178:a584caef1576

Remove Catch a Wave Radio link. Google gives a big scary warning if you try to follow this link.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Apr 2019 14:38:40 -0500
parents cf486a8e8b43
children
rev   line source
bgneal@811 1 """
bgneal@811 2 Unit tests for the messages application views.
bgneal@811 3
bgneal@811 4 """
bgneal@812 5 import datetime
bgneal@812 6
bgneal@811 7 from django.test import TestCase
bgneal@811 8 from django.core.urlresolvers import reverse
bgneal@812 9 from django.contrib.auth.models import User
bgneal@813 10 from django.core import mail
bgneal@811 11
bgneal@814 12 import messages
bgneal@811 13 from messages.models import Message, Options, Flag
bgneal@811 14
bgneal@811 15
bgneal@811 16 class NotLoggedInTestCase(TestCase):
bgneal@811 17 """Ensure we are redirected to the login page before we can do anything with
bgneal@811 18 this application.
bgneal@811 19
bgneal@811 20 """
bgneal@811 21 LOGIN_URL = reverse('accounts-login')
bgneal@811 22
bgneal@811 23 def _test_get(self, url_name, **kwargs):
bgneal@811 24 url = reverse(url_name, **kwargs)
bgneal@811 25 response = self.client.get(url, follow=True)
bgneal@811 26 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@811 27
bgneal@811 28 def _test_post(self, url_name, **kwargs):
bgneal@811 29 url = reverse(url_name, **kwargs)
bgneal@811 30 response = self.client.post(url, follow=True)
bgneal@811 31 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@811 32
bgneal@811 33 def test_inbox(self):
bgneal@811 34 self._test_get('messages-inbox')
bgneal@811 35
bgneal@811 36 def test_compose(self):
bgneal@811 37 url_name = 'messages-compose'
bgneal@811 38 self._test_get(url_name)
bgneal@811 39 self._test_post(url_name)
bgneal@811 40
bgneal@811 41 def test_outbox(self):
bgneal@811 42 self._test_get('messages-outbox')
bgneal@811 43
bgneal@811 44 def test_trash(self):
bgneal@811 45 self._test_get('messages-trash')
bgneal@811 46
bgneal@811 47 def test_options(self):
bgneal@811 48 url_name = 'messages-options'
bgneal@811 49 self._test_get(url_name)
bgneal@811 50 self._test_post(url_name)
bgneal@811 51
bgneal@811 52 def test_delete(self):
bgneal@818 53 self._test_get('messages-bulk')
bgneal@811 54
bgneal@811 55 def test_undelete(self):
bgneal@811 56 self._test_get('messages-undelete')
bgneal@811 57
bgneal@811 58 def test_view(self):
bgneal@811 59 url_name = 'messages-view'
bgneal@811 60 self._test_get(url_name, args=[123])
bgneal@811 61 self._test_post(url_name, args=[123])
bgneal@811 62
bgneal@811 63 def test_report(self):
bgneal@811 64 url_name = 'messages-report'
bgneal@811 65 self._test_get(url_name, args=[123])
bgneal@811 66 self._test_post(url_name, args=[123])
bgneal@812 67
bgneal@812 68
bgneal@812 69 class BasicTestCase(TestCase):
bgneal@812 70 """Testing a logged in user visiting all the views with no messages."""
bgneal@812 71 fixtures = ['messages_test_users.json']
bgneal@814 72 MSG_BOX_LIMIT = 3
bgneal@812 73
bgneal@812 74 def setUp(self):
bgneal@812 75 self.users = {}
bgneal@812 76 self.users['pj'] = User.objects.get(username='pj')
bgneal@812 77 self.users['pj'].set_password('12345')
bgneal@812 78 self.users['pj'].save()
bgneal@812 79 self.assertTrue(self.client.login(username='pj', password='12345'))
bgneal@812 80
bgneal@812 81 self.users['eddie'] = User.objects.get(username='eddie')
bgneal@812 82 self.users['eddie'].set_password('12345')
bgneal@812 83 self.users['eddie'].save()
bgneal@812 84
bgneal@812 85 self.users['richard'] = User.objects.get(username='richard')
bgneal@813 86 self.users['richard'].set_password('12345')
bgneal@813 87 self.users['richard'].save()
bgneal@812 88
bgneal@814 89 # To reduce test duration, reduce MSG_BOX_LIMIT
bgneal@814 90 self.saved_msg_box_limit = messages.MSG_BOX_LIMIT
bgneal@814 91 messages.MSG_BOX_LIMIT = self.MSG_BOX_LIMIT
bgneal@814 92
bgneal@814 93 def tearDown(self):
bgneal@814 94 messages.MSG_BOX_LIMIT = self.saved_msg_box_limit
bgneal@814 95
bgneal@812 96 def test_simple_gets(self):
bgneal@812 97 view_names = [
bgneal@812 98 'messages-inbox',
bgneal@812 99 'messages-compose',
bgneal@812 100 'messages-outbox',
bgneal@812 101 'messages-trash',
bgneal@812 102 'messages-options',
bgneal@812 103 ]
bgneal@812 104 for view_name in view_names:
bgneal@812 105 response = self.client.get(reverse(view_name))
bgneal@812 106 self.assertEqual(response.status_code, 200)
bgneal@812 107
bgneal@812 108 def test_wrong_method(self):
bgneal@812 109 view_names = [
bgneal@818 110 'messages-bulk',
bgneal@812 111 'messages-undelete',
bgneal@812 112 ]
bgneal@812 113 for view_name in view_names:
bgneal@812 114 response = self.client.get(reverse(view_name))
bgneal@812 115 self.assertEqual(response.status_code, 405)
bgneal@812 116
bgneal@812 117 def test_nonexistent_pms(self):
bgneal@812 118 view_names = [
bgneal@812 119 'messages-view',
bgneal@812 120 'messages-report',
bgneal@812 121 ]
bgneal@812 122 for view_name in view_names:
bgneal@812 123 response = self.client.get(reverse(view_name, args=[42]))
bgneal@812 124 self.assertEqual(response.status_code, 404)
bgneal@812 125
bgneal@812 126 response = self.client.post(reverse(view_name, args=[42]))
bgneal@812 127 self.assertEqual(response.status_code, 404)
bgneal@812 128
bgneal@812 129 def test_options(self):
bgneal@812 130 view_name = 'messages-options'
bgneal@812 131 url = reverse(view_name)
bgneal@812 132 response = self.client.get(url)
bgneal@812 133 self.assertEqual(response.status_code, 200)
bgneal@812 134
bgneal@812 135 post_data = {
bgneal@812 136 'opts-attach_signature': 'on',
bgneal@812 137 'opts-notify_email': 'on',
bgneal@812 138 }
bgneal@812 139 response = self.client.post(url, data=post_data, follow=True)
bgneal@812 140 self.assertRedirects(response, url)
bgneal@812 141
bgneal@812 142 opts = Options.objects.for_user(self.users['pj'])
bgneal@812 143 self.assertTrue(opts.attach_signature)
bgneal@812 144 self.assertTrue(opts.notify_email)
bgneal@812 145
bgneal@812 146 post_data = {}
bgneal@812 147 response = self.client.post(url, data=post_data, follow=True)
bgneal@812 148 self.assertRedirects(response, url)
bgneal@812 149
bgneal@812 150 opts = Options.objects.for_user(self.users['pj'])
bgneal@812 151 self.assertFalse(opts.attach_signature)
bgneal@812 152 self.assertFalse(opts.notify_email)
bgneal@812 153
bgneal@812 154 def test_send_bogus_user(self):
bgneal@812 155 post_data = {
bgneal@812 156 'receiver': 'unknown_user',
bgneal@812 157 'subject': 'hi',
bgneal@812 158 'message': 'test',
bgneal@812 159 }
bgneal@812 160 view_name = 'messages-compose'
bgneal@812 161 url = reverse(view_name)
bgneal@812 162 response = self.client.post(url, data=post_data)
bgneal@812 163 self.assertContains(response, 'username does not exist', status_code=200)
bgneal@812 164
bgneal@812 165 qs = Message.objects.filter(sender=self.users['pj'])
bgneal@812 166 self.assertEqual(qs.count(), 0)
bgneal@812 167
bgneal@812 168 def test_send_to_self(self):
bgneal@812 169 post_data = {
bgneal@812 170 'receiver': 'pj',
bgneal@812 171 'subject': 'hi',
bgneal@812 172 'message': 'test',
bgneal@812 173 }
bgneal@812 174 view_name = 'messages-compose'
bgneal@812 175 url = reverse(view_name)
bgneal@812 176 response = self.client.post(url, data=post_data)
bgneal@812 177 self.assertContains(response, "You can&#39;t send a message to yourself", status_code=200)
bgneal@812 178
bgneal@812 179 qs = Message.objects.filter(sender=self.users['pj'])
bgneal@812 180 self.assertEqual(qs.count(), 0)
bgneal@812 181
bgneal@812 182 def test_message_cycle(self):
bgneal@812 183 # pj sends message to eddie
bgneal@812 184 now = datetime.datetime.now()
bgneal@812 185 post_data = {
bgneal@812 186 'receiver': 'eddie',
bgneal@812 187 'subject': 'Mr. Moto Demo',
bgneal@812 188 'message': 'Gig at Newport High School',
bgneal@812 189 }
bgneal@812 190 view_name = 'messages-compose'
bgneal@812 191 url = reverse(view_name)
bgneal@812 192 response = self.client.post(url, data=post_data)
bgneal@812 193 self.assertContains(response, "Message sent", status_code=200)
bgneal@812 194
bgneal@812 195 # see if message got created in database
bgneal@812 196 msg = Message.objects.get(sender=self.users['pj'])
bgneal@812 197
bgneal@812 198 self.assertEqual(msg.sender, self.users['pj'])
bgneal@812 199 self.assertEqual(msg.receiver, self.users['eddie'])
bgneal@812 200 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
bgneal@812 201 self.assertIsNone(msg.read_date)
bgneal@812 202 self.assertIsNone(msg.reply_date)
bgneal@812 203 self.assertEqual(msg.subject, post_data['subject'])
bgneal@812 204 self.assertEqual(msg.message, post_data['message'])
bgneal@812 205 self.assertIsNone(msg.sender_delete_date)
bgneal@812 206 self.assertIsNone(msg.receiver_delete_date)
bgneal@812 207
bgneal@812 208 # see if shows up in outbox view
bgneal@812 209 view_name = 'messages-outbox'
bgneal@812 210 url = reverse(view_name)
bgneal@812 211 response = self.client.get(url)
bgneal@812 212 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 213
bgneal@812 214 # see if we can view it
bgneal@812 215 view_name = 'messages-view'
bgneal@812 216 url = reverse(view_name, args=[msg.pk])
bgneal@812 217 response = self.client.get(url)
bgneal@812 218 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 219 self.assertContains(response, post_data['message'], status_code=200)
bgneal@812 220
bgneal@812 221 # eddie logs in and checks various views
bgneal@812 222 self.assertTrue(self.client.login(username='eddie', password='12345'))
bgneal@812 223 view_name = 'messages-inbox'
bgneal@812 224 url = reverse(view_name)
bgneal@812 225 response = self.client.get(url)
bgneal@812 226 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 227 view_name = 'messages-outbox'
bgneal@812 228 url = reverse(view_name)
bgneal@812 229 response = self.client.get(url)
bgneal@812 230 self.assertNotContains(response, post_data['subject'], status_code=200)
bgneal@812 231 view_name = 'messages-trash'
bgneal@812 232 url = reverse(view_name)
bgneal@812 233 response = self.client.get(url)
bgneal@812 234 self.assertNotContains(response, post_data['subject'], status_code=200)
bgneal@812 235
bgneal@812 236 # eddie reads it
bgneal@812 237 view_name = 'messages-view'
bgneal@812 238 url = reverse(view_name, args=[msg.pk])
bgneal@812 239 response = self.client.get(url)
bgneal@812 240 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 241 self.assertContains(response, post_data['message'], status_code=200)
bgneal@812 242
bgneal@812 243 # see if message got updated in database
bgneal@812 244 msg = Message.objects.get(receiver=self.users['eddie'])
bgneal@812 245
bgneal@812 246 self.assertEqual(msg.sender, self.users['pj'])
bgneal@812 247 self.assertEqual(msg.receiver, self.users['eddie'])
bgneal@812 248 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
bgneal@812 249 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5))
bgneal@812 250 self.assertIsNone(msg.reply_date)
bgneal@812 251 self.assertEqual(msg.subject, post_data['subject'])
bgneal@812 252 self.assertEqual(msg.message, post_data['message'])
bgneal@812 253 self.assertIsNone(msg.sender_delete_date)
bgneal@812 254 self.assertIsNone(msg.receiver_delete_date)
bgneal@812 255
bgneal@812 256 # eddie replies
bgneal@812 257 post_data2 = {
bgneal@812 258 'receiver': 'pj',
bgneal@812 259 'subject': 'Re: Mr. Moto Demo',
bgneal@812 260 'message': 'South Bay Sound',
bgneal@812 261 'parent_id': msg.pk,
bgneal@812 262 }
bgneal@812 263 view_name = 'messages-compose'
bgneal@812 264 url = reverse(view_name)
bgneal@812 265 response = self.client.post(url, data=post_data2)
bgneal@812 266 self.assertContains(response, "Message sent", status_code=200)
bgneal@812 267
bgneal@812 268 self.assertEqual(Message.objects.all().count(), 2)
bgneal@812 269
bgneal@812 270 msg1 = Message.objects.get(receiver=self.users['eddie'])
bgneal@812 271
bgneal@812 272 self.assertEqual(msg1.sender, self.users['pj'])
bgneal@812 273 self.assertEqual(msg1.receiver, self.users['eddie'])
bgneal@812 274 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
bgneal@812 275 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
bgneal@812 276 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
bgneal@812 277 self.assertEqual(msg1.subject, post_data['subject'])
bgneal@812 278 self.assertEqual(msg1.message, post_data['message'])
bgneal@812 279 self.assertIsNone(msg1.sender_delete_date)
bgneal@812 280 self.assertIsNone(msg1.receiver_delete_date)
bgneal@812 281
bgneal@812 282 msg2 = Message.objects.get(receiver=self.users['pj'])
bgneal@812 283
bgneal@812 284 self.assertEqual(msg2.sender, self.users['eddie'])
bgneal@812 285 self.assertEqual(msg2.receiver, self.users['pj'])
bgneal@812 286 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
bgneal@812 287 self.assertIsNone(msg2.read_date)
bgneal@812 288 self.assertIsNone(msg2.reply_date)
bgneal@812 289 self.assertEqual(msg2.subject, post_data2['subject'])
bgneal@812 290 self.assertEqual(msg2.message, post_data2['message'])
bgneal@812 291 self.assertIsNone(msg2.sender_delete_date)
bgneal@812 292 self.assertIsNone(msg2.receiver_delete_date)
bgneal@812 293
bgneal@812 294 # pj logs in and checks various views
bgneal@812 295 self.assertTrue(self.client.login(username='pj', password='12345'))
bgneal@812 296 view_name = 'messages-inbox'
bgneal@812 297 url = reverse(view_name)
bgneal@812 298 response = self.client.get(url)
bgneal@812 299 self.assertContains(response, post_data2['subject'], status_code=200)
bgneal@812 300 view_name = 'messages-outbox'
bgneal@812 301 url = reverse(view_name)
bgneal@812 302 response = self.client.get(url)
bgneal@812 303 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 304 view_name = 'messages-trash'
bgneal@812 305 url = reverse(view_name)
bgneal@812 306 response = self.client.get(url)
bgneal@812 307 self.assertNotContains(response, post_data['subject'], status_code=200)
bgneal@812 308 self.assertNotContains(response, post_data2['subject'], status_code=200)
bgneal@812 309
bgneal@812 310 # pj reads reply
bgneal@812 311 view_name = 'messages-view'
bgneal@812 312 url = reverse(view_name, args=[msg2.pk])
bgneal@812 313 response = self.client.get(url)
bgneal@812 314 self.assertContains(response, post_data2['subject'], status_code=200)
bgneal@812 315 self.assertContains(response, post_data2['message'], status_code=200)
bgneal@812 316
bgneal@812 317 msg = Message.objects.get(receiver=self.users['eddie'])
bgneal@812 318
bgneal@812 319 self.assertEqual(msg.sender, self.users['pj'])
bgneal@812 320 self.assertEqual(msg.receiver, self.users['eddie'])
bgneal@812 321 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
bgneal@812 322 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5))
bgneal@812 323 self.assertTrue(now - msg.reply_date < datetime.timedelta(seconds=5))
bgneal@812 324 self.assertEqual(msg.subject, post_data['subject'])
bgneal@812 325 self.assertEqual(msg.message, post_data['message'])
bgneal@812 326 self.assertIsNone(msg.sender_delete_date)
bgneal@812 327 self.assertIsNone(msg.receiver_delete_date)
bgneal@812 328
bgneal@812 329 msg2 = Message.objects.get(receiver=self.users['pj'])
bgneal@812 330
bgneal@812 331 self.assertEqual(msg2.sender, self.users['eddie'])
bgneal@812 332 self.assertEqual(msg2.receiver, self.users['pj'])
bgneal@812 333 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
bgneal@812 334 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5))
bgneal@812 335 self.assertIsNone(msg2.reply_date)
bgneal@812 336 self.assertEqual(msg2.subject, post_data2['subject'])
bgneal@812 337 self.assertEqual(msg2.message, post_data2['message'])
bgneal@812 338 self.assertIsNone(msg2.sender_delete_date)
bgneal@812 339 self.assertIsNone(msg2.receiver_delete_date)
bgneal@812 340
bgneal@812 341 # pj deletes message 2
bgneal@812 342 self.assertEqual(Message.objects.all().count(), 2)
bgneal@812 343
bgneal@812 344 delete_post_data = {
bgneal@812 345 'pm_ids': [msg2.pk],
bgneal@818 346 'action': 'Delete',
bgneal@812 347 }
bgneal@818 348 view_name = 'messages-bulk'
bgneal@812 349 url = reverse(view_name)
bgneal@812 350 response = self.client.post(url, data=delete_post_data, follow=True)
bgneal@812 351 self.assertContains(response, "1 message deleted", status_code=200)
bgneal@812 352
bgneal@812 353 self.assertEqual(Message.objects.all().count(), 2)
bgneal@812 354
bgneal@812 355 msg2 = Message.objects.get(receiver=self.users['pj'])
bgneal@812 356
bgneal@812 357 self.assertEqual(msg2.sender, self.users['eddie'])
bgneal@812 358 self.assertEqual(msg2.receiver, self.users['pj'])
bgneal@812 359 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
bgneal@812 360 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5))
bgneal@812 361 self.assertIsNone(msg2.reply_date)
bgneal@812 362 self.assertEqual(msg2.subject, post_data2['subject'])
bgneal@812 363 self.assertEqual(msg2.message, post_data2['message'])
bgneal@812 364 self.assertIsNone(msg2.sender_delete_date)
bgneal@812 365 self.assertTrue(now - msg2.receiver_delete_date < datetime.timedelta(seconds=5))
bgneal@812 366
bgneal@812 367 # should be in pj's trash now
bgneal@812 368 view_name = 'messages-trash'
bgneal@812 369 url = reverse(view_name)
bgneal@812 370 response = self.client.get(url)
bgneal@812 371 self.assertContains(response, post_data2['subject'], status_code=200)
bgneal@812 372
bgneal@812 373 # eddie logs in and deletes his copy of message 2
bgneal@812 374 self.assertTrue(self.client.login(username='eddie', password='12345'))
bgneal@812 375 delete_post_data = {
bgneal@812 376 'pm_ids': [msg2.pk],
bgneal@818 377 'action': 'Delete',
bgneal@812 378 }
bgneal@818 379 view_name = 'messages-bulk'
bgneal@812 380 url = reverse(view_name)
bgneal@812 381 response = self.client.post(url, data=delete_post_data, follow=True)
bgneal@812 382 self.assertContains(response, "1 message deleted", status_code=200)
bgneal@812 383
bgneal@812 384 # should be really deleted now
bgneal@812 385 self.assertEqual(Message.objects.all().count(), 1)
bgneal@812 386 view_name = 'messages-trash'
bgneal@812 387 url = reverse(view_name)
bgneal@812 388 response = self.client.get(url)
bgneal@812 389 self.assertNotContains(response, post_data2['subject'], status_code=200)
bgneal@812 390
bgneal@812 391 # eddie deletes then undeletes message 1
bgneal@812 392 delete_post_data = {
bgneal@812 393 'pm_ids': [msg1.pk],
bgneal@818 394 'action': 'Delete',
bgneal@812 395 }
bgneal@818 396 view_name = 'messages-bulk'
bgneal@812 397 url = reverse(view_name)
bgneal@812 398 response = self.client.post(url, data=delete_post_data, follow=True)
bgneal@812 399 self.assertContains(response, "1 message deleted", status_code=200)
bgneal@812 400
bgneal@812 401 # should not be really deleted now
bgneal@812 402 self.assertEqual(Message.objects.all().count(), 1)
bgneal@812 403 view_name = 'messages-trash'
bgneal@812 404 url = reverse(view_name)
bgneal@812 405 response = self.client.get(url)
bgneal@812 406 self.assertContains(response, post_data['subject'], status_code=200)
bgneal@812 407
bgneal@812 408 msg1 = Message.objects.get(receiver=self.users['eddie'])
bgneal@812 409
bgneal@812 410 self.assertEqual(msg1.sender, self.users['pj'])
bgneal@812 411 self.assertEqual(msg1.receiver, self.users['eddie'])
bgneal@812 412 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
bgneal@812 413 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
bgneal@812 414 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
bgneal@812 415 self.assertEqual(msg1.subject, post_data['subject'])
bgneal@812 416 self.assertEqual(msg1.message, post_data['message'])
bgneal@812 417 self.assertIsNone(msg1.sender_delete_date)
bgneal@812 418 self.assertTrue(now - msg1.receiver_delete_date < datetime.timedelta(seconds=5))
bgneal@812 419
bgneal@812 420 delete_post_data = {
bgneal@812 421 'pm_ids': [msg1.pk],
bgneal@812 422 }
bgneal@812 423 view_name = 'messages-undelete'
bgneal@812 424 url = reverse(view_name)
bgneal@812 425 response = self.client.post(url, data=delete_post_data, follow=True)
bgneal@812 426 self.assertContains(response, "1 message undeleted", status_code=200)
bgneal@812 427
bgneal@812 428 # should not be really deleted now
bgneal@812 429 self.assertEqual(Message.objects.all().count(), 1)
bgneal@812 430 view_name = 'messages-trash'
bgneal@812 431 url = reverse(view_name)
bgneal@812 432 response = self.client.get(url)
bgneal@812 433 self.assertNotContains(response, post_data['subject'], status_code=200)
bgneal@812 434
bgneal@812 435 msg1 = Message.objects.get(receiver=self.users['eddie'])
bgneal@812 436
bgneal@812 437 self.assertEqual(msg1.sender, self.users['pj'])
bgneal@812 438 self.assertEqual(msg1.receiver, self.users['eddie'])
bgneal@812 439 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
bgneal@812 440 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
bgneal@812 441 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
bgneal@812 442 self.assertEqual(msg1.subject, post_data['subject'])
bgneal@812 443 self.assertEqual(msg1.message, post_data['message'])
bgneal@812 444 self.assertIsNone(msg1.sender_delete_date)
bgneal@812 445 self.assertIsNone(msg1.receiver_delete_date)
bgneal@812 446
bgneal@812 447 def test_read_permission(self):
bgneal@812 448
bgneal@812 449 now = datetime.datetime.now()
bgneal@812 450 # pj sends a PM to richard
bgneal@812 451 post_data = {
bgneal@812 452 'receiver': 'richard',
bgneal@812 453 'subject': 'Mr. Moto Demo',
bgneal@812 454 'message': 'Gig at Newport High School',
bgneal@812 455 }
bgneal@812 456 view_name = 'messages-compose'
bgneal@812 457 url = reverse(view_name)
bgneal@812 458 response = self.client.post(url, data=post_data)
bgneal@812 459 self.assertContains(response, "Message sent", status_code=200)
bgneal@812 460
bgneal@812 461 msg = Message.objects.get(sender=self.users['pj'])
bgneal@812 462
bgneal@812 463 # eddie should not be able to read it
bgneal@812 464 self.assertTrue(self.client.login(username='eddie', password='12345'))
bgneal@812 465 view_name = 'messages-view'
bgneal@812 466 url = reverse(view_name, args=[msg.pk])
bgneal@812 467 response = self.client.get(url)
bgneal@812 468 self.assertNotContains(response, post_data['subject'], status_code=302)
bgneal@812 469 self.assertNotContains(response, post_data['message'], status_code=302)
bgneal@812 470
bgneal@812 471 response = self.client.get(url, follow=True)
bgneal@812 472 self.assertNotContains(response, post_data['subject'], status_code=200)
bgneal@812 473 self.assertNotContains(response, post_data['message'], status_code=200)
bgneal@812 474
bgneal@812 475 msg = Message.objects.get(sender=self.users['pj'])
bgneal@812 476
bgneal@812 477 self.assertEqual(msg.sender, self.users['pj'])
bgneal@812 478 self.assertEqual(msg.receiver, self.users['richard'])
bgneal@812 479 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
bgneal@812 480 self.assertIsNone(msg.read_date)
bgneal@812 481 self.assertIsNone(msg.reply_date)
bgneal@812 482 self.assertEqual(msg.subject, post_data['subject'])
bgneal@812 483 self.assertEqual(msg.message, post_data['message'])
bgneal@812 484 self.assertIsNone(msg.sender_delete_date)
bgneal@812 485 self.assertIsNone(msg.receiver_delete_date)
bgneal@813 486
bgneal@813 487 def test_report_message(self):
bgneal@813 488 post_data = {
bgneal@813 489 'receiver': 'eddie',
bgneal@813 490 'subject': 'Mr. Moto Demo',
bgneal@813 491 'message': 'Gig at Newport High School',
bgneal@813 492 }
bgneal@813 493 view_name = 'messages-compose'
bgneal@813 494 url = reverse(view_name)
bgneal@813 495 response = self.client.post(url, data=post_data)
bgneal@813 496 self.assertContains(response, "Message sent", status_code=200)
bgneal@813 497
bgneal@813 498 msg = Message.objects.get(sender=self.users['pj'])
bgneal@813 499
bgneal@813 500 # Ensure non-recipients can't report a message
bgneal@813 501 view_name = 'messages-report'
bgneal@813 502 url = reverse(view_name, args=[msg.pk])
bgneal@813 503 response = self.client.post(url, data={}, follow=True)
bgneal@813 504 self.assertContains(response, "You can&#39;t report this message", status_code=200)
bgneal@813 505 self.assertEqual(Flag.objects.count(), 0)
bgneal@813 506
bgneal@813 507 self.assertTrue(self.client.login(username='richard', password='12345'))
bgneal@813 508 response = self.client.post(url, data={}, follow=True)
bgneal@813 509 self.assertContains(response, "You can&#39;t report this message", status_code=200)
bgneal@813 510 self.assertEqual(Flag.objects.count(), 0)
bgneal@813 511
bgneal@813 512 # Test bogus report
bgneal@813 513 self.assertTrue(self.client.login(username='eddie', password='12345'))
bgneal@813 514 bad_url = reverse(view_name, args=[msg.pk + 1])
bgneal@813 515 response = self.client.post(bad_url, data={})
bgneal@813 516 self.assertEqual(response.status_code, 404)
bgneal@813 517 self.assertEqual(Flag.objects.count(), 0)
bgneal@813 518
bgneal@813 519 # Test get of report view
bgneal@813 520 response = self.client.get(url)
bgneal@813 521 self.assertEqual(response.status_code, 200)
bgneal@813 522
bgneal@813 523 # Test posting to report view
bgneal@813 524 post_data = {
bgneal@813 525 'comments': 'This is abuse!',
bgneal@813 526 }
bgneal@813 527 now = datetime.datetime.now()
bgneal@813 528 response = self.client.post(url, data=post_data, follow=True)
bgneal@813 529 self.assertContains(response, "Message reported", status_code=200)
bgneal@813 530
bgneal@813 531 self.assertEqual(Flag.objects.count(), 1)
bgneal@813 532 msg = Message.objects.get(receiver=self.users['eddie'])
bgneal@813 533
bgneal@813 534 try:
bgneal@813 535 flag = msg.flag
bgneal@813 536 except Flag.DoesNotExist:
bgneal@813 537 self.fail("msg.flag does not exist!")
bgneal@813 538
bgneal@813 539 self.assertEqual(flag.message, msg)
bgneal@813 540 self.assertTrue(now - flag.flag_date < datetime.timedelta(seconds=2))
bgneal@813 541 self.assertEqual(flag.comments, post_data['comments'])
bgneal@813 542
bgneal@813 543 # Admin should have been sent an email
bgneal@813 544 self.assertEqual(len(mail.outbox), 1)
bgneal@813 545 email = mail.outbox[0]
bgneal@813 546 self.assertEqual(len(email.recipients()), 1)
bgneal@813 547 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com')
bgneal@813 548 self.assertTrue(email.subject.endswith('A user has flagged a private message'))
bgneal@813 549
bgneal@814 550 def test_inbox_full(self):
bgneal@814 551
bgneal@814 552 post_data = {
bgneal@814 553 'receiver': 'eddie',
bgneal@814 554 'subject': 'Mr. Moto Demo',
bgneal@814 555 'message': 'Gig at Newport High School',
bgneal@814 556 }
bgneal@814 557 view_name = 'messages-compose'
bgneal@814 558 url = reverse(view_name)
bgneal@814 559 for n in range(self.MSG_BOX_LIMIT):
bgneal@814 560 response = self.client.post(url, data=post_data)
bgneal@814 561 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 562
bgneal@814 563 # pj deletes a message so we can make sure we are testing inbox full
bgneal@814 564 msg = Message.objects.filter(receiver=self.users['eddie']).last()
bgneal@814 565 msg.sender_delete_date = datetime.datetime.now()
bgneal@814 566 msg.save()
bgneal@814 567 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 568 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
bgneal@814 569 self.MSG_BOX_LIMIT - 1)
bgneal@814 570 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
bgneal@814 571 self.MSG_BOX_LIMIT)
bgneal@814 572
bgneal@814 573 # pj should not be able to send another
bgneal@814 574 response = self.client.post(url, data=post_data)
bgneal@814 575 self.assertNotContains(response, "Message sent", status_code=200)
bgneal@814 576 self.assertContains(response, "inbox is full", status_code=200)
bgneal@814 577 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 578
bgneal@814 579 def test_outbox_full(self):
bgneal@814 580
bgneal@814 581 post_data = {
bgneal@814 582 'receiver': 'eddie',
bgneal@814 583 'subject': 'Mr. Moto Demo',
bgneal@814 584 'message': 'Gig at Newport High School',
bgneal@814 585 }
bgneal@814 586 view_name = 'messages-compose'
bgneal@814 587 url = reverse(view_name)
bgneal@814 588 for n in range(self.MSG_BOX_LIMIT):
bgneal@814 589 response = self.client.post(url, data=post_data)
bgneal@814 590 self.assertContains(response, "Message sent", status_code=200)
bgneal@814 591 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 592
bgneal@814 593 # eddie deletes a message so we can make sure we are testing outbox full
bgneal@814 594 msg = Message.objects.filter(receiver=self.users['eddie']).last()
bgneal@814 595 msg.read_date = datetime.datetime.now()
bgneal@814 596 msg.receiver_delete_date = msg.read_date
bgneal@814 597 msg.save()
bgneal@814 598 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 599 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
bgneal@814 600 self.MSG_BOX_LIMIT)
bgneal@814 601 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
bgneal@814 602 self.MSG_BOX_LIMIT - 1)
bgneal@814 603
bgneal@814 604 # pj should not be able to send another
bgneal@814 605 response = self.client.post(url, data=post_data)
bgneal@814 606 self.assertNotContains(response, "Message sent", status_code=200)
bgneal@814 607 self.assertContains(response, "Your outbox is full", status_code=200)
bgneal@814 608 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
bgneal@814 609
bgneal@818 610 def test_email_export(self):
bgneal@818 611
bgneal@818 612 post_data = {
bgneal@818 613 'receiver': 'eddie',
bgneal@818 614 'subject': 'Mr. Moto Demo',
bgneal@818 615 'message': 'Gig at Newport High School',
bgneal@818 616 }
bgneal@818 617 view_name = 'messages-compose'
bgneal@818 618 url = reverse(view_name)
bgneal@818 619 response = self.client.post(url, data=post_data)
bgneal@818 620 self.assertContains(response, "Message sent", status_code=200)
bgneal@818 621
bgneal@818 622 msg = Message.objects.get(receiver=self.users['eddie'])
bgneal@818 623 post_data = {
bgneal@818 624 'pm_ids': [msg.id],
bgneal@818 625 'action': 'Email',
bgneal@818 626 }
bgneal@818 627 view_name = 'messages-bulk'
bgneal@818 628 url = reverse(view_name)
bgneal@818 629 response = self.client.post(url, data=post_data, follow=True)
bgneal@818 630 self.assertContains(response, "1 message sent to email", status_code=200)
bgneal@818 631
bgneal@818 632 # Ensure email sent
bgneal@818 633 self.assertEqual(len(mail.outbox), 1)
bgneal@818 634
bgneal@818 635 if len(mail.outbox) == 1:
bgneal@818 636 email = mail.outbox[0]
bgneal@818 637 self.assertEqual(len(email.recipients()), 1)
bgneal@818 638 self.assertEqual(email.recipients()[0], 'pj@example.com')
bgneal@818 639 self.assertTrue(email.subject.startswith('Private messages from'))
bgneal@818 640
bgneal@813 641
bgneal@813 642 class EmailTestCase(TestCase):
bgneal@813 643 """Testing to ensure email is sent when PM is sent if options allow."""
bgneal@813 644 fixtures = ['messages_test_users.json']
bgneal@813 645
bgneal@813 646 def setUp(self):
bgneal@813 647 self.users = {}
bgneal@813 648 self.users['pj'] = User.objects.get(username='pj')
bgneal@813 649 self.users['pj'].set_password('12345')
bgneal@813 650 self.users['pj'].save()
bgneal@813 651 self.assertTrue(self.client.login(username='pj', password='12345'))
bgneal@813 652
bgneal@813 653 self.users['eddie'] = User.objects.get(username='eddie')
bgneal@813 654 self.users['eddie'].set_password('12345')
bgneal@813 655 self.users['eddie'].save()
bgneal@813 656
bgneal@813 657 def _test_email(self, email_expected):
bgneal@813 658 # pj sends message to eddie
bgneal@813 659 post_data = {
bgneal@813 660 'receiver': 'eddie',
bgneal@813 661 'subject': 'Mr. Moto Demo',
bgneal@813 662 'message': 'Gig at Newport High School',
bgneal@813 663 }
bgneal@813 664 view_name = 'messages-compose'
bgneal@813 665 url = reverse(view_name)
bgneal@813 666 response = self.client.post(url, data=post_data)
bgneal@813 667 self.assertContains(response, "Message sent", status_code=200)
bgneal@813 668
bgneal@813 669 # Ensure notification email sent
bgneal@813 670 count = 1 if email_expected else 0
bgneal@813 671 self.assertEqual(len(mail.outbox), count)
bgneal@813 672
bgneal@813 673 if count == 1:
bgneal@813 674 email = mail.outbox[0]
bgneal@813 675 self.assertEqual(len(email.recipients()), 1)
bgneal@813 676 self.assertEqual(email.recipients()[0], 'eddie@example.com')
bgneal@813 677 self.assertTrue(email.subject.startswith('New private message'))
bgneal@813 678
bgneal@813 679 def test_email_sent(self):
bgneal@813 680 # eddie elects to receive email notifications
bgneal@813 681 # Note: testing the options view is done above.
bgneal@813 682 opts = Options.objects.for_user(self.users['eddie'])
bgneal@813 683 opts.notify_email = True
bgneal@813 684 opts.save()
bgneal@813 685 self._test_email(True)
bgneal@813 686
bgneal@813 687 def test_no_email(self):
bgneal@813 688 # eddie does not to elect to receive email notifications
bgneal@813 689 opts = Options.objects.for_user(self.users['eddie'])
bgneal@813 690 opts.notify_email = False
bgneal@813 691 opts.save()
bgneal@813 692 self._test_email(False)