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@811
|
53 self._test_get('messages-delete')
|
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@812
|
110 'messages-delete',
|
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'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@812
|
346 }
|
bgneal@812
|
347 view_name = 'messages-delete'
|
bgneal@812
|
348 url = reverse(view_name)
|
bgneal@812
|
349 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
350 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
351
|
bgneal@812
|
352 self.assertEqual(Message.objects.all().count(), 2)
|
bgneal@812
|
353
|
bgneal@812
|
354 msg2 = Message.objects.get(receiver=self.users['pj'])
|
bgneal@812
|
355
|
bgneal@812
|
356 self.assertEqual(msg2.sender, self.users['eddie'])
|
bgneal@812
|
357 self.assertEqual(msg2.receiver, self.users['pj'])
|
bgneal@812
|
358 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
359 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
360 self.assertIsNone(msg2.reply_date)
|
bgneal@812
|
361 self.assertEqual(msg2.subject, post_data2['subject'])
|
bgneal@812
|
362 self.assertEqual(msg2.message, post_data2['message'])
|
bgneal@812
|
363 self.assertIsNone(msg2.sender_delete_date)
|
bgneal@812
|
364 self.assertTrue(now - msg2.receiver_delete_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
365
|
bgneal@812
|
366 # should be in pj's trash now
|
bgneal@812
|
367 view_name = 'messages-trash'
|
bgneal@812
|
368 url = reverse(view_name)
|
bgneal@812
|
369 response = self.client.get(url)
|
bgneal@812
|
370 self.assertContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
371
|
bgneal@812
|
372 # eddie logs in and deletes his copy of message 2
|
bgneal@812
|
373 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@812
|
374 delete_post_data = {
|
bgneal@812
|
375 'pm_ids': [msg2.pk],
|
bgneal@812
|
376 }
|
bgneal@812
|
377 view_name = 'messages-delete'
|
bgneal@812
|
378 url = reverse(view_name)
|
bgneal@812
|
379 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
380 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
381
|
bgneal@812
|
382 # should be really deleted now
|
bgneal@812
|
383 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
384 view_name = 'messages-trash'
|
bgneal@812
|
385 url = reverse(view_name)
|
bgneal@812
|
386 response = self.client.get(url)
|
bgneal@812
|
387 self.assertNotContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
388
|
bgneal@812
|
389 # eddie deletes then undeletes message 1
|
bgneal@812
|
390 delete_post_data = {
|
bgneal@812
|
391 'pm_ids': [msg1.pk],
|
bgneal@812
|
392 }
|
bgneal@812
|
393 view_name = 'messages-delete'
|
bgneal@812
|
394 url = reverse(view_name)
|
bgneal@812
|
395 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
396 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
397
|
bgneal@812
|
398 # should not be really deleted now
|
bgneal@812
|
399 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
400 view_name = 'messages-trash'
|
bgneal@812
|
401 url = reverse(view_name)
|
bgneal@812
|
402 response = self.client.get(url)
|
bgneal@812
|
403 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
404
|
bgneal@812
|
405 msg1 = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
406
|
bgneal@812
|
407 self.assertEqual(msg1.sender, self.users['pj'])
|
bgneal@812
|
408 self.assertEqual(msg1.receiver, self.users['eddie'])
|
bgneal@812
|
409 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
410 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
411 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
412 self.assertEqual(msg1.subject, post_data['subject'])
|
bgneal@812
|
413 self.assertEqual(msg1.message, post_data['message'])
|
bgneal@812
|
414 self.assertIsNone(msg1.sender_delete_date)
|
bgneal@812
|
415 self.assertTrue(now - msg1.receiver_delete_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
416
|
bgneal@812
|
417 delete_post_data = {
|
bgneal@812
|
418 'pm_ids': [msg1.pk],
|
bgneal@812
|
419 }
|
bgneal@812
|
420 view_name = 'messages-undelete'
|
bgneal@812
|
421 url = reverse(view_name)
|
bgneal@812
|
422 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
423 self.assertContains(response, "1 message undeleted", status_code=200)
|
bgneal@812
|
424
|
bgneal@812
|
425 # should not be really deleted now
|
bgneal@812
|
426 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
427 view_name = 'messages-trash'
|
bgneal@812
|
428 url = reverse(view_name)
|
bgneal@812
|
429 response = self.client.get(url)
|
bgneal@812
|
430 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
431
|
bgneal@812
|
432 msg1 = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
433
|
bgneal@812
|
434 self.assertEqual(msg1.sender, self.users['pj'])
|
bgneal@812
|
435 self.assertEqual(msg1.receiver, self.users['eddie'])
|
bgneal@812
|
436 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
437 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
438 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
439 self.assertEqual(msg1.subject, post_data['subject'])
|
bgneal@812
|
440 self.assertEqual(msg1.message, post_data['message'])
|
bgneal@812
|
441 self.assertIsNone(msg1.sender_delete_date)
|
bgneal@812
|
442 self.assertIsNone(msg1.receiver_delete_date)
|
bgneal@812
|
443
|
bgneal@812
|
444 def test_read_permission(self):
|
bgneal@812
|
445
|
bgneal@812
|
446 now = datetime.datetime.now()
|
bgneal@812
|
447 # pj sends a PM to richard
|
bgneal@812
|
448 post_data = {
|
bgneal@812
|
449 'receiver': 'richard',
|
bgneal@812
|
450 'subject': 'Mr. Moto Demo',
|
bgneal@812
|
451 'message': 'Gig at Newport High School',
|
bgneal@812
|
452 }
|
bgneal@812
|
453 view_name = 'messages-compose'
|
bgneal@812
|
454 url = reverse(view_name)
|
bgneal@812
|
455 response = self.client.post(url, data=post_data)
|
bgneal@812
|
456 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@812
|
457
|
bgneal@812
|
458 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@812
|
459
|
bgneal@812
|
460 # eddie should not be able to read it
|
bgneal@812
|
461 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@812
|
462 view_name = 'messages-view'
|
bgneal@812
|
463 url = reverse(view_name, args=[msg.pk])
|
bgneal@812
|
464 response = self.client.get(url)
|
bgneal@812
|
465 self.assertNotContains(response, post_data['subject'], status_code=302)
|
bgneal@812
|
466 self.assertNotContains(response, post_data['message'], status_code=302)
|
bgneal@812
|
467
|
bgneal@812
|
468 response = self.client.get(url, follow=True)
|
bgneal@812
|
469 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
470 self.assertNotContains(response, post_data['message'], status_code=200)
|
bgneal@812
|
471
|
bgneal@812
|
472 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@812
|
473
|
bgneal@812
|
474 self.assertEqual(msg.sender, self.users['pj'])
|
bgneal@812
|
475 self.assertEqual(msg.receiver, self.users['richard'])
|
bgneal@812
|
476 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
477 self.assertIsNone(msg.read_date)
|
bgneal@812
|
478 self.assertIsNone(msg.reply_date)
|
bgneal@812
|
479 self.assertEqual(msg.subject, post_data['subject'])
|
bgneal@812
|
480 self.assertEqual(msg.message, post_data['message'])
|
bgneal@812
|
481 self.assertIsNone(msg.sender_delete_date)
|
bgneal@812
|
482 self.assertIsNone(msg.receiver_delete_date)
|
bgneal@813
|
483
|
bgneal@813
|
484 def test_report_message(self):
|
bgneal@813
|
485 post_data = {
|
bgneal@813
|
486 'receiver': 'eddie',
|
bgneal@813
|
487 'subject': 'Mr. Moto Demo',
|
bgneal@813
|
488 'message': 'Gig at Newport High School',
|
bgneal@813
|
489 }
|
bgneal@813
|
490 view_name = 'messages-compose'
|
bgneal@813
|
491 url = reverse(view_name)
|
bgneal@813
|
492 response = self.client.post(url, data=post_data)
|
bgneal@813
|
493 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@813
|
494
|
bgneal@813
|
495 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@813
|
496
|
bgneal@813
|
497 # Ensure non-recipients can't report a message
|
bgneal@813
|
498 view_name = 'messages-report'
|
bgneal@813
|
499 url = reverse(view_name, args=[msg.pk])
|
bgneal@813
|
500 response = self.client.post(url, data={}, follow=True)
|
bgneal@813
|
501 self.assertContains(response, "You can't report this message", status_code=200)
|
bgneal@813
|
502 self.assertEqual(Flag.objects.count(), 0)
|
bgneal@813
|
503
|
bgneal@813
|
504 self.assertTrue(self.client.login(username='richard', password='12345'))
|
bgneal@813
|
505 response = self.client.post(url, data={}, follow=True)
|
bgneal@813
|
506 self.assertContains(response, "You can't report this message", status_code=200)
|
bgneal@813
|
507 self.assertEqual(Flag.objects.count(), 0)
|
bgneal@813
|
508
|
bgneal@813
|
509 # Test bogus report
|
bgneal@813
|
510 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@813
|
511 bad_url = reverse(view_name, args=[msg.pk + 1])
|
bgneal@813
|
512 response = self.client.post(bad_url, data={})
|
bgneal@813
|
513 self.assertEqual(response.status_code, 404)
|
bgneal@813
|
514 self.assertEqual(Flag.objects.count(), 0)
|
bgneal@813
|
515
|
bgneal@813
|
516 # Test get of report view
|
bgneal@813
|
517 response = self.client.get(url)
|
bgneal@813
|
518 self.assertEqual(response.status_code, 200)
|
bgneal@813
|
519
|
bgneal@813
|
520 # Test posting to report view
|
bgneal@813
|
521 post_data = {
|
bgneal@813
|
522 'comments': 'This is abuse!',
|
bgneal@813
|
523 }
|
bgneal@813
|
524 now = datetime.datetime.now()
|
bgneal@813
|
525 response = self.client.post(url, data=post_data, follow=True)
|
bgneal@813
|
526 self.assertContains(response, "Message reported", status_code=200)
|
bgneal@813
|
527
|
bgneal@813
|
528 self.assertEqual(Flag.objects.count(), 1)
|
bgneal@813
|
529 msg = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@813
|
530
|
bgneal@813
|
531 try:
|
bgneal@813
|
532 flag = msg.flag
|
bgneal@813
|
533 except Flag.DoesNotExist:
|
bgneal@813
|
534 self.fail("msg.flag does not exist!")
|
bgneal@813
|
535
|
bgneal@813
|
536 self.assertEqual(flag.message, msg)
|
bgneal@813
|
537 self.assertTrue(now - flag.flag_date < datetime.timedelta(seconds=2))
|
bgneal@813
|
538 self.assertEqual(flag.comments, post_data['comments'])
|
bgneal@813
|
539
|
bgneal@813
|
540 # Admin should have been sent an email
|
bgneal@813
|
541 self.assertEqual(len(mail.outbox), 1)
|
bgneal@813
|
542 email = mail.outbox[0]
|
bgneal@813
|
543 self.assertEqual(len(email.recipients()), 1)
|
bgneal@813
|
544 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com')
|
bgneal@813
|
545 self.assertTrue(email.subject.endswith('A user has flagged a private message'))
|
bgneal@813
|
546
|
bgneal@814
|
547 def test_inbox_full(self):
|
bgneal@814
|
548
|
bgneal@814
|
549 post_data = {
|
bgneal@814
|
550 'receiver': 'eddie',
|
bgneal@814
|
551 'subject': 'Mr. Moto Demo',
|
bgneal@814
|
552 'message': 'Gig at Newport High School',
|
bgneal@814
|
553 }
|
bgneal@814
|
554 view_name = 'messages-compose'
|
bgneal@814
|
555 url = reverse(view_name)
|
bgneal@814
|
556 for n in range(self.MSG_BOX_LIMIT):
|
bgneal@814
|
557 response = self.client.post(url, data=post_data)
|
bgneal@814
|
558 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
559
|
bgneal@814
|
560 # pj deletes a message so we can make sure we are testing inbox full
|
bgneal@814
|
561 msg = Message.objects.filter(receiver=self.users['eddie']).last()
|
bgneal@814
|
562 msg.sender_delete_date = datetime.datetime.now()
|
bgneal@814
|
563 msg.save()
|
bgneal@814
|
564 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
565 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
|
bgneal@814
|
566 self.MSG_BOX_LIMIT - 1)
|
bgneal@814
|
567 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
|
bgneal@814
|
568 self.MSG_BOX_LIMIT)
|
bgneal@814
|
569
|
bgneal@814
|
570 # pj should not be able to send another
|
bgneal@814
|
571 response = self.client.post(url, data=post_data)
|
bgneal@814
|
572 self.assertNotContains(response, "Message sent", status_code=200)
|
bgneal@814
|
573 self.assertContains(response, "inbox is full", status_code=200)
|
bgneal@814
|
574 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
575
|
bgneal@814
|
576 def test_outbox_full(self):
|
bgneal@814
|
577
|
bgneal@814
|
578 post_data = {
|
bgneal@814
|
579 'receiver': 'eddie',
|
bgneal@814
|
580 'subject': 'Mr. Moto Demo',
|
bgneal@814
|
581 'message': 'Gig at Newport High School',
|
bgneal@814
|
582 }
|
bgneal@814
|
583 view_name = 'messages-compose'
|
bgneal@814
|
584 url = reverse(view_name)
|
bgneal@814
|
585 for n in range(self.MSG_BOX_LIMIT):
|
bgneal@814
|
586 response = self.client.post(url, data=post_data)
|
bgneal@814
|
587 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@814
|
588 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
589
|
bgneal@814
|
590 # eddie deletes a message so we can make sure we are testing outbox full
|
bgneal@814
|
591 msg = Message.objects.filter(receiver=self.users['eddie']).last()
|
bgneal@814
|
592 msg.read_date = datetime.datetime.now()
|
bgneal@814
|
593 msg.receiver_delete_date = msg.read_date
|
bgneal@814
|
594 msg.save()
|
bgneal@814
|
595 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
596 self.assertEqual(Message.objects.outbox(self.users['pj']).count(),
|
bgneal@814
|
597 self.MSG_BOX_LIMIT)
|
bgneal@814
|
598 self.assertEqual(Message.objects.inbox(self.users['eddie']).count(),
|
bgneal@814
|
599 self.MSG_BOX_LIMIT - 1)
|
bgneal@814
|
600
|
bgneal@814
|
601 # pj should not be able to send another
|
bgneal@814
|
602 response = self.client.post(url, data=post_data)
|
bgneal@814
|
603 self.assertNotContains(response, "Message sent", status_code=200)
|
bgneal@814
|
604 self.assertContains(response, "Your outbox is full", status_code=200)
|
bgneal@814
|
605 self.assertEqual(Message.objects.all().count(), self.MSG_BOX_LIMIT)
|
bgneal@814
|
606
|
bgneal@813
|
607
|
bgneal@813
|
608 class EmailTestCase(TestCase):
|
bgneal@813
|
609 """Testing to ensure email is sent when PM is sent if options allow."""
|
bgneal@813
|
610 fixtures = ['messages_test_users.json']
|
bgneal@813
|
611
|
bgneal@813
|
612 def setUp(self):
|
bgneal@813
|
613 self.users = {}
|
bgneal@813
|
614 self.users['pj'] = User.objects.get(username='pj')
|
bgneal@813
|
615 self.users['pj'].set_password('12345')
|
bgneal@813
|
616 self.users['pj'].save()
|
bgneal@813
|
617 self.assertTrue(self.client.login(username='pj', password='12345'))
|
bgneal@813
|
618
|
bgneal@813
|
619 self.users['eddie'] = User.objects.get(username='eddie')
|
bgneal@813
|
620 self.users['eddie'].set_password('12345')
|
bgneal@813
|
621 self.users['eddie'].save()
|
bgneal@813
|
622
|
bgneal@813
|
623 def _test_email(self, email_expected):
|
bgneal@813
|
624 # pj sends message to eddie
|
bgneal@813
|
625 post_data = {
|
bgneal@813
|
626 'receiver': 'eddie',
|
bgneal@813
|
627 'subject': 'Mr. Moto Demo',
|
bgneal@813
|
628 'message': 'Gig at Newport High School',
|
bgneal@813
|
629 }
|
bgneal@813
|
630 view_name = 'messages-compose'
|
bgneal@813
|
631 url = reverse(view_name)
|
bgneal@813
|
632 response = self.client.post(url, data=post_data)
|
bgneal@813
|
633 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@813
|
634
|
bgneal@813
|
635 # Ensure notification email sent
|
bgneal@813
|
636 count = 1 if email_expected else 0
|
bgneal@813
|
637 self.assertEqual(len(mail.outbox), count)
|
bgneal@813
|
638
|
bgneal@813
|
639 if count == 1:
|
bgneal@813
|
640 email = mail.outbox[0]
|
bgneal@813
|
641 self.assertEqual(len(email.recipients()), 1)
|
bgneal@813
|
642 self.assertEqual(email.recipients()[0], 'eddie@example.com')
|
bgneal@813
|
643 self.assertTrue(email.subject.startswith('New private message'))
|
bgneal@813
|
644
|
bgneal@813
|
645 def test_email_sent(self):
|
bgneal@813
|
646 # eddie elects to receive email notifications
|
bgneal@813
|
647 # Note: testing the options view is done above.
|
bgneal@813
|
648 opts = Options.objects.for_user(self.users['eddie'])
|
bgneal@813
|
649 opts.notify_email = True
|
bgneal@813
|
650 opts.save()
|
bgneal@813
|
651 self._test_email(True)
|
bgneal@813
|
652
|
bgneal@813
|
653 def test_no_email(self):
|
bgneal@813
|
654 # eddie does not to elect to receive email notifications
|
bgneal@813
|
655 opts = Options.objects.for_user(self.users['eddie'])
|
bgneal@813
|
656 opts.notify_email = False
|
bgneal@813
|
657 opts.save()
|
bgneal@813
|
658 self._test_email(False)
|