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