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@811
|
10
|
bgneal@811
|
11 from messages.models import Message, Options, Flag
|
bgneal@811
|
12
|
bgneal@812
|
13 # TODO TODO TODO TODO
|
bgneal@812
|
14 # TODO TODO TODO TODO
|
bgneal@812
|
15 # TODO TODO TODO TODO
|
bgneal@812
|
16 #
|
bgneal@812
|
17 # 1) Test report functionality; ensure email sent to admin
|
bgneal@812
|
18 # 2) Test email sent when PM sent if options allow it
|
bgneal@812
|
19 #
|
bgneal@812
|
20 # TODO TODO TODO TODO
|
bgneal@812
|
21 # TODO TODO TODO TODO
|
bgneal@812
|
22 # TODO TODO TODO TODO
|
bgneal@811
|
23
|
bgneal@811
|
24
|
bgneal@811
|
25 class NotLoggedInTestCase(TestCase):
|
bgneal@811
|
26 """Ensure we are redirected to the login page before we can do anything with
|
bgneal@811
|
27 this application.
|
bgneal@811
|
28
|
bgneal@811
|
29 """
|
bgneal@811
|
30 LOGIN_URL = reverse('accounts-login')
|
bgneal@811
|
31
|
bgneal@811
|
32 def _test_get(self, url_name, **kwargs):
|
bgneal@811
|
33 url = reverse(url_name, **kwargs)
|
bgneal@811
|
34 response = self.client.get(url, follow=True)
|
bgneal@811
|
35 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
|
bgneal@811
|
36
|
bgneal@811
|
37 def _test_post(self, url_name, **kwargs):
|
bgneal@811
|
38 url = reverse(url_name, **kwargs)
|
bgneal@811
|
39 response = self.client.post(url, follow=True)
|
bgneal@811
|
40 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
|
bgneal@811
|
41
|
bgneal@811
|
42 def test_inbox(self):
|
bgneal@811
|
43 self._test_get('messages-inbox')
|
bgneal@811
|
44
|
bgneal@811
|
45 def test_compose(self):
|
bgneal@811
|
46 url_name = 'messages-compose'
|
bgneal@811
|
47 self._test_get(url_name)
|
bgneal@811
|
48 self._test_post(url_name)
|
bgneal@811
|
49
|
bgneal@811
|
50 def test_outbox(self):
|
bgneal@811
|
51 self._test_get('messages-outbox')
|
bgneal@811
|
52
|
bgneal@811
|
53 def test_trash(self):
|
bgneal@811
|
54 self._test_get('messages-trash')
|
bgneal@811
|
55
|
bgneal@811
|
56 def test_options(self):
|
bgneal@811
|
57 url_name = 'messages-options'
|
bgneal@811
|
58 self._test_get(url_name)
|
bgneal@811
|
59 self._test_post(url_name)
|
bgneal@811
|
60
|
bgneal@811
|
61 def test_delete(self):
|
bgneal@811
|
62 self._test_get('messages-delete')
|
bgneal@811
|
63
|
bgneal@811
|
64 def test_undelete(self):
|
bgneal@811
|
65 self._test_get('messages-undelete')
|
bgneal@811
|
66
|
bgneal@811
|
67 def test_view(self):
|
bgneal@811
|
68 url_name = 'messages-view'
|
bgneal@811
|
69 self._test_get(url_name, args=[123])
|
bgneal@811
|
70 self._test_post(url_name, args=[123])
|
bgneal@811
|
71
|
bgneal@811
|
72 def test_report(self):
|
bgneal@811
|
73 url_name = 'messages-report'
|
bgneal@811
|
74 self._test_get(url_name, args=[123])
|
bgneal@811
|
75 self._test_post(url_name, args=[123])
|
bgneal@812
|
76
|
bgneal@812
|
77
|
bgneal@812
|
78 class BasicTestCase(TestCase):
|
bgneal@812
|
79 """Testing a logged in user visiting all the views with no messages."""
|
bgneal@812
|
80 fixtures = ['messages_test_users.json']
|
bgneal@812
|
81
|
bgneal@812
|
82 def setUp(self):
|
bgneal@812
|
83 self.users = {}
|
bgneal@812
|
84 self.users['pj'] = User.objects.get(username='pj')
|
bgneal@812
|
85 self.users['pj'].set_password('12345')
|
bgneal@812
|
86 self.users['pj'].save()
|
bgneal@812
|
87 self.assertTrue(self.client.login(username='pj', password='12345'))
|
bgneal@812
|
88
|
bgneal@812
|
89 self.users['eddie'] = User.objects.get(username='eddie')
|
bgneal@812
|
90 self.users['eddie'].set_password('12345')
|
bgneal@812
|
91 self.users['eddie'].save()
|
bgneal@812
|
92
|
bgneal@812
|
93 self.users['richard'] = User.objects.get(username='richard')
|
bgneal@812
|
94
|
bgneal@812
|
95 def test_simple_gets(self):
|
bgneal@812
|
96 view_names = [
|
bgneal@812
|
97 'messages-inbox',
|
bgneal@812
|
98 'messages-compose',
|
bgneal@812
|
99 'messages-outbox',
|
bgneal@812
|
100 'messages-trash',
|
bgneal@812
|
101 'messages-options',
|
bgneal@812
|
102 ]
|
bgneal@812
|
103 for view_name in view_names:
|
bgneal@812
|
104 response = self.client.get(reverse(view_name))
|
bgneal@812
|
105 self.assertEqual(response.status_code, 200)
|
bgneal@812
|
106
|
bgneal@812
|
107 def test_wrong_method(self):
|
bgneal@812
|
108 view_names = [
|
bgneal@812
|
109 'messages-delete',
|
bgneal@812
|
110 'messages-undelete',
|
bgneal@812
|
111 ]
|
bgneal@812
|
112 for view_name in view_names:
|
bgneal@812
|
113 response = self.client.get(reverse(view_name))
|
bgneal@812
|
114 self.assertEqual(response.status_code, 405)
|
bgneal@812
|
115
|
bgneal@812
|
116 def test_nonexistent_pms(self):
|
bgneal@812
|
117 view_names = [
|
bgneal@812
|
118 'messages-view',
|
bgneal@812
|
119 'messages-report',
|
bgneal@812
|
120 ]
|
bgneal@812
|
121 for view_name in view_names:
|
bgneal@812
|
122 response = self.client.get(reverse(view_name, args=[42]))
|
bgneal@812
|
123 self.assertEqual(response.status_code, 404)
|
bgneal@812
|
124
|
bgneal@812
|
125 response = self.client.post(reverse(view_name, args=[42]))
|
bgneal@812
|
126 self.assertEqual(response.status_code, 404)
|
bgneal@812
|
127
|
bgneal@812
|
128 def test_options(self):
|
bgneal@812
|
129 view_name = 'messages-options'
|
bgneal@812
|
130 url = reverse(view_name)
|
bgneal@812
|
131 response = self.client.get(url)
|
bgneal@812
|
132 self.assertEqual(response.status_code, 200)
|
bgneal@812
|
133
|
bgneal@812
|
134 post_data = {
|
bgneal@812
|
135 'opts-attach_signature': 'on',
|
bgneal@812
|
136 'opts-notify_email': 'on',
|
bgneal@812
|
137 }
|
bgneal@812
|
138 response = self.client.post(url, data=post_data, follow=True)
|
bgneal@812
|
139 self.assertRedirects(response, url)
|
bgneal@812
|
140
|
bgneal@812
|
141 opts = Options.objects.for_user(self.users['pj'])
|
bgneal@812
|
142 self.assertTrue(opts.attach_signature)
|
bgneal@812
|
143 self.assertTrue(opts.notify_email)
|
bgneal@812
|
144
|
bgneal@812
|
145 post_data = {}
|
bgneal@812
|
146 response = self.client.post(url, data=post_data, follow=True)
|
bgneal@812
|
147 self.assertRedirects(response, url)
|
bgneal@812
|
148
|
bgneal@812
|
149 opts = Options.objects.for_user(self.users['pj'])
|
bgneal@812
|
150 self.assertFalse(opts.attach_signature)
|
bgneal@812
|
151 self.assertFalse(opts.notify_email)
|
bgneal@812
|
152
|
bgneal@812
|
153 def test_send_bogus_user(self):
|
bgneal@812
|
154 post_data = {
|
bgneal@812
|
155 'receiver': 'unknown_user',
|
bgneal@812
|
156 'subject': 'hi',
|
bgneal@812
|
157 'message': 'test',
|
bgneal@812
|
158 }
|
bgneal@812
|
159 view_name = 'messages-compose'
|
bgneal@812
|
160 url = reverse(view_name)
|
bgneal@812
|
161 response = self.client.post(url, data=post_data)
|
bgneal@812
|
162 self.assertContains(response, 'username does not exist', status_code=200)
|
bgneal@812
|
163
|
bgneal@812
|
164 qs = Message.objects.filter(sender=self.users['pj'])
|
bgneal@812
|
165 self.assertEqual(qs.count(), 0)
|
bgneal@812
|
166
|
bgneal@812
|
167 def test_send_to_self(self):
|
bgneal@812
|
168 post_data = {
|
bgneal@812
|
169 'receiver': 'pj',
|
bgneal@812
|
170 'subject': 'hi',
|
bgneal@812
|
171 'message': 'test',
|
bgneal@812
|
172 }
|
bgneal@812
|
173 view_name = 'messages-compose'
|
bgneal@812
|
174 url = reverse(view_name)
|
bgneal@812
|
175 response = self.client.post(url, data=post_data)
|
bgneal@812
|
176 self.assertContains(response, "You can't send a message to yourself", status_code=200)
|
bgneal@812
|
177
|
bgneal@812
|
178 qs = Message.objects.filter(sender=self.users['pj'])
|
bgneal@812
|
179 self.assertEqual(qs.count(), 0)
|
bgneal@812
|
180
|
bgneal@812
|
181 def test_message_cycle(self):
|
bgneal@812
|
182 # pj sends message to eddie
|
bgneal@812
|
183 now = datetime.datetime.now()
|
bgneal@812
|
184 post_data = {
|
bgneal@812
|
185 'receiver': 'eddie',
|
bgneal@812
|
186 'subject': 'Mr. Moto Demo',
|
bgneal@812
|
187 'message': 'Gig at Newport High School',
|
bgneal@812
|
188 }
|
bgneal@812
|
189 view_name = 'messages-compose'
|
bgneal@812
|
190 url = reverse(view_name)
|
bgneal@812
|
191 response = self.client.post(url, data=post_data)
|
bgneal@812
|
192 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@812
|
193
|
bgneal@812
|
194 # see if message got created in database
|
bgneal@812
|
195 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@812
|
196
|
bgneal@812
|
197 self.assertEqual(msg.sender, self.users['pj'])
|
bgneal@812
|
198 self.assertEqual(msg.receiver, self.users['eddie'])
|
bgneal@812
|
199 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
200 self.assertIsNone(msg.read_date)
|
bgneal@812
|
201 self.assertIsNone(msg.reply_date)
|
bgneal@812
|
202 self.assertEqual(msg.subject, post_data['subject'])
|
bgneal@812
|
203 self.assertEqual(msg.message, post_data['message'])
|
bgneal@812
|
204 self.assertIsNone(msg.sender_delete_date)
|
bgneal@812
|
205 self.assertIsNone(msg.receiver_delete_date)
|
bgneal@812
|
206
|
bgneal@812
|
207 # see if shows up in outbox view
|
bgneal@812
|
208 view_name = 'messages-outbox'
|
bgneal@812
|
209 url = reverse(view_name)
|
bgneal@812
|
210 response = self.client.get(url)
|
bgneal@812
|
211 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
212
|
bgneal@812
|
213 # see if we can view it
|
bgneal@812
|
214 view_name = 'messages-view'
|
bgneal@812
|
215 url = reverse(view_name, args=[msg.pk])
|
bgneal@812
|
216 response = self.client.get(url)
|
bgneal@812
|
217 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
218 self.assertContains(response, post_data['message'], status_code=200)
|
bgneal@812
|
219
|
bgneal@812
|
220 # eddie logs in and checks various views
|
bgneal@812
|
221 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@812
|
222 view_name = 'messages-inbox'
|
bgneal@812
|
223 url = reverse(view_name)
|
bgneal@812
|
224 response = self.client.get(url)
|
bgneal@812
|
225 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
226 view_name = 'messages-outbox'
|
bgneal@812
|
227 url = reverse(view_name)
|
bgneal@812
|
228 response = self.client.get(url)
|
bgneal@812
|
229 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
230 view_name = 'messages-trash'
|
bgneal@812
|
231 url = reverse(view_name)
|
bgneal@812
|
232 response = self.client.get(url)
|
bgneal@812
|
233 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
234
|
bgneal@812
|
235 # eddie reads it
|
bgneal@812
|
236 view_name = 'messages-view'
|
bgneal@812
|
237 url = reverse(view_name, args=[msg.pk])
|
bgneal@812
|
238 response = self.client.get(url)
|
bgneal@812
|
239 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
240 self.assertContains(response, post_data['message'], status_code=200)
|
bgneal@812
|
241
|
bgneal@812
|
242 # see if message got updated in database
|
bgneal@812
|
243 msg = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
244
|
bgneal@812
|
245 self.assertEqual(msg.sender, self.users['pj'])
|
bgneal@812
|
246 self.assertEqual(msg.receiver, self.users['eddie'])
|
bgneal@812
|
247 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
248 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
249 self.assertIsNone(msg.reply_date)
|
bgneal@812
|
250 self.assertEqual(msg.subject, post_data['subject'])
|
bgneal@812
|
251 self.assertEqual(msg.message, post_data['message'])
|
bgneal@812
|
252 self.assertIsNone(msg.sender_delete_date)
|
bgneal@812
|
253 self.assertIsNone(msg.receiver_delete_date)
|
bgneal@812
|
254
|
bgneal@812
|
255 # eddie replies
|
bgneal@812
|
256 post_data2 = {
|
bgneal@812
|
257 'receiver': 'pj',
|
bgneal@812
|
258 'subject': 'Re: Mr. Moto Demo',
|
bgneal@812
|
259 'message': 'South Bay Sound',
|
bgneal@812
|
260 'parent_id': msg.pk,
|
bgneal@812
|
261 }
|
bgneal@812
|
262 view_name = 'messages-compose'
|
bgneal@812
|
263 url = reverse(view_name)
|
bgneal@812
|
264 response = self.client.post(url, data=post_data2)
|
bgneal@812
|
265 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@812
|
266
|
bgneal@812
|
267 self.assertEqual(Message.objects.all().count(), 2)
|
bgneal@812
|
268
|
bgneal@812
|
269 msg1 = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
270
|
bgneal@812
|
271 self.assertEqual(msg1.sender, self.users['pj'])
|
bgneal@812
|
272 self.assertEqual(msg1.receiver, self.users['eddie'])
|
bgneal@812
|
273 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
274 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
275 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
276 self.assertEqual(msg1.subject, post_data['subject'])
|
bgneal@812
|
277 self.assertEqual(msg1.message, post_data['message'])
|
bgneal@812
|
278 self.assertIsNone(msg1.sender_delete_date)
|
bgneal@812
|
279 self.assertIsNone(msg1.receiver_delete_date)
|
bgneal@812
|
280
|
bgneal@812
|
281 msg2 = Message.objects.get(receiver=self.users['pj'])
|
bgneal@812
|
282
|
bgneal@812
|
283 self.assertEqual(msg2.sender, self.users['eddie'])
|
bgneal@812
|
284 self.assertEqual(msg2.receiver, self.users['pj'])
|
bgneal@812
|
285 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
286 self.assertIsNone(msg2.read_date)
|
bgneal@812
|
287 self.assertIsNone(msg2.reply_date)
|
bgneal@812
|
288 self.assertEqual(msg2.subject, post_data2['subject'])
|
bgneal@812
|
289 self.assertEqual(msg2.message, post_data2['message'])
|
bgneal@812
|
290 self.assertIsNone(msg2.sender_delete_date)
|
bgneal@812
|
291 self.assertIsNone(msg2.receiver_delete_date)
|
bgneal@812
|
292
|
bgneal@812
|
293 # pj logs in and checks various views
|
bgneal@812
|
294 self.assertTrue(self.client.login(username='pj', password='12345'))
|
bgneal@812
|
295 view_name = 'messages-inbox'
|
bgneal@812
|
296 url = reverse(view_name)
|
bgneal@812
|
297 response = self.client.get(url)
|
bgneal@812
|
298 self.assertContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
299 view_name = 'messages-outbox'
|
bgneal@812
|
300 url = reverse(view_name)
|
bgneal@812
|
301 response = self.client.get(url)
|
bgneal@812
|
302 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
303 view_name = 'messages-trash'
|
bgneal@812
|
304 url = reverse(view_name)
|
bgneal@812
|
305 response = self.client.get(url)
|
bgneal@812
|
306 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
307 self.assertNotContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
308
|
bgneal@812
|
309 # pj reads reply
|
bgneal@812
|
310 view_name = 'messages-view'
|
bgneal@812
|
311 url = reverse(view_name, args=[msg2.pk])
|
bgneal@812
|
312 response = self.client.get(url)
|
bgneal@812
|
313 self.assertContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
314 self.assertContains(response, post_data2['message'], status_code=200)
|
bgneal@812
|
315
|
bgneal@812
|
316 msg = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
317
|
bgneal@812
|
318 self.assertEqual(msg.sender, self.users['pj'])
|
bgneal@812
|
319 self.assertEqual(msg.receiver, self.users['eddie'])
|
bgneal@812
|
320 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
321 self.assertTrue(now - msg.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
322 self.assertTrue(now - msg.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
323 self.assertEqual(msg.subject, post_data['subject'])
|
bgneal@812
|
324 self.assertEqual(msg.message, post_data['message'])
|
bgneal@812
|
325 self.assertIsNone(msg.sender_delete_date)
|
bgneal@812
|
326 self.assertIsNone(msg.receiver_delete_date)
|
bgneal@812
|
327
|
bgneal@812
|
328 msg2 = Message.objects.get(receiver=self.users['pj'])
|
bgneal@812
|
329
|
bgneal@812
|
330 self.assertEqual(msg2.sender, self.users['eddie'])
|
bgneal@812
|
331 self.assertEqual(msg2.receiver, self.users['pj'])
|
bgneal@812
|
332 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
333 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
334 self.assertIsNone(msg2.reply_date)
|
bgneal@812
|
335 self.assertEqual(msg2.subject, post_data2['subject'])
|
bgneal@812
|
336 self.assertEqual(msg2.message, post_data2['message'])
|
bgneal@812
|
337 self.assertIsNone(msg2.sender_delete_date)
|
bgneal@812
|
338 self.assertIsNone(msg2.receiver_delete_date)
|
bgneal@812
|
339
|
bgneal@812
|
340 # pj deletes message 2
|
bgneal@812
|
341 self.assertEqual(Message.objects.all().count(), 2)
|
bgneal@812
|
342
|
bgneal@812
|
343 delete_post_data = {
|
bgneal@812
|
344 'pm_ids': [msg2.pk],
|
bgneal@812
|
345 }
|
bgneal@812
|
346 view_name = 'messages-delete'
|
bgneal@812
|
347 url = reverse(view_name)
|
bgneal@812
|
348 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
349 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
350
|
bgneal@812
|
351 self.assertEqual(Message.objects.all().count(), 2)
|
bgneal@812
|
352
|
bgneal@812
|
353 msg2 = Message.objects.get(receiver=self.users['pj'])
|
bgneal@812
|
354
|
bgneal@812
|
355 self.assertEqual(msg2.sender, self.users['eddie'])
|
bgneal@812
|
356 self.assertEqual(msg2.receiver, self.users['pj'])
|
bgneal@812
|
357 self.assertTrue(now - msg2.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
358 self.assertTrue(now - msg2.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
359 self.assertIsNone(msg2.reply_date)
|
bgneal@812
|
360 self.assertEqual(msg2.subject, post_data2['subject'])
|
bgneal@812
|
361 self.assertEqual(msg2.message, post_data2['message'])
|
bgneal@812
|
362 self.assertIsNone(msg2.sender_delete_date)
|
bgneal@812
|
363 self.assertTrue(now - msg2.receiver_delete_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
364
|
bgneal@812
|
365 # should be in pj's trash now
|
bgneal@812
|
366 view_name = 'messages-trash'
|
bgneal@812
|
367 url = reverse(view_name)
|
bgneal@812
|
368 response = self.client.get(url)
|
bgneal@812
|
369 self.assertContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
370
|
bgneal@812
|
371 # eddie logs in and deletes his copy of message 2
|
bgneal@812
|
372 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@812
|
373 delete_post_data = {
|
bgneal@812
|
374 'pm_ids': [msg2.pk],
|
bgneal@812
|
375 }
|
bgneal@812
|
376 view_name = 'messages-delete'
|
bgneal@812
|
377 url = reverse(view_name)
|
bgneal@812
|
378 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
379 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
380
|
bgneal@812
|
381 # should be really deleted now
|
bgneal@812
|
382 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
383 view_name = 'messages-trash'
|
bgneal@812
|
384 url = reverse(view_name)
|
bgneal@812
|
385 response = self.client.get(url)
|
bgneal@812
|
386 self.assertNotContains(response, post_data2['subject'], status_code=200)
|
bgneal@812
|
387
|
bgneal@812
|
388 # eddie deletes then undeletes message 1
|
bgneal@812
|
389 delete_post_data = {
|
bgneal@812
|
390 'pm_ids': [msg1.pk],
|
bgneal@812
|
391 }
|
bgneal@812
|
392 view_name = 'messages-delete'
|
bgneal@812
|
393 url = reverse(view_name)
|
bgneal@812
|
394 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
395 self.assertContains(response, "1 message deleted", status_code=200)
|
bgneal@812
|
396
|
bgneal@812
|
397 # should not be really deleted now
|
bgneal@812
|
398 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
399 view_name = 'messages-trash'
|
bgneal@812
|
400 url = reverse(view_name)
|
bgneal@812
|
401 response = self.client.get(url)
|
bgneal@812
|
402 self.assertContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
403
|
bgneal@812
|
404 msg1 = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
405
|
bgneal@812
|
406 self.assertEqual(msg1.sender, self.users['pj'])
|
bgneal@812
|
407 self.assertEqual(msg1.receiver, self.users['eddie'])
|
bgneal@812
|
408 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
409 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
410 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
411 self.assertEqual(msg1.subject, post_data['subject'])
|
bgneal@812
|
412 self.assertEqual(msg1.message, post_data['message'])
|
bgneal@812
|
413 self.assertIsNone(msg1.sender_delete_date)
|
bgneal@812
|
414 self.assertTrue(now - msg1.receiver_delete_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
415
|
bgneal@812
|
416 delete_post_data = {
|
bgneal@812
|
417 'pm_ids': [msg1.pk],
|
bgneal@812
|
418 }
|
bgneal@812
|
419 view_name = 'messages-undelete'
|
bgneal@812
|
420 url = reverse(view_name)
|
bgneal@812
|
421 response = self.client.post(url, data=delete_post_data, follow=True)
|
bgneal@812
|
422 self.assertContains(response, "1 message undeleted", status_code=200)
|
bgneal@812
|
423
|
bgneal@812
|
424 # should not be really deleted now
|
bgneal@812
|
425 self.assertEqual(Message.objects.all().count(), 1)
|
bgneal@812
|
426 view_name = 'messages-trash'
|
bgneal@812
|
427 url = reverse(view_name)
|
bgneal@812
|
428 response = self.client.get(url)
|
bgneal@812
|
429 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
430
|
bgneal@812
|
431 msg1 = Message.objects.get(receiver=self.users['eddie'])
|
bgneal@812
|
432
|
bgneal@812
|
433 self.assertEqual(msg1.sender, self.users['pj'])
|
bgneal@812
|
434 self.assertEqual(msg1.receiver, self.users['eddie'])
|
bgneal@812
|
435 self.assertTrue(now - msg1.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
436 self.assertTrue(now - msg1.read_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
437 self.assertTrue(now - msg1.reply_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
438 self.assertEqual(msg1.subject, post_data['subject'])
|
bgneal@812
|
439 self.assertEqual(msg1.message, post_data['message'])
|
bgneal@812
|
440 self.assertIsNone(msg1.sender_delete_date)
|
bgneal@812
|
441 self.assertIsNone(msg1.receiver_delete_date)
|
bgneal@812
|
442
|
bgneal@812
|
443 def test_read_permission(self):
|
bgneal@812
|
444
|
bgneal@812
|
445 now = datetime.datetime.now()
|
bgneal@812
|
446 # pj sends a PM to richard
|
bgneal@812
|
447 post_data = {
|
bgneal@812
|
448 'receiver': 'richard',
|
bgneal@812
|
449 'subject': 'Mr. Moto Demo',
|
bgneal@812
|
450 'message': 'Gig at Newport High School',
|
bgneal@812
|
451 }
|
bgneal@812
|
452 view_name = 'messages-compose'
|
bgneal@812
|
453 url = reverse(view_name)
|
bgneal@812
|
454 response = self.client.post(url, data=post_data)
|
bgneal@812
|
455 self.assertContains(response, "Message sent", status_code=200)
|
bgneal@812
|
456
|
bgneal@812
|
457 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@812
|
458
|
bgneal@812
|
459 # eddie should not be able to read it
|
bgneal@812
|
460 self.assertTrue(self.client.login(username='eddie', password='12345'))
|
bgneal@812
|
461 view_name = 'messages-view'
|
bgneal@812
|
462 url = reverse(view_name, args=[msg.pk])
|
bgneal@812
|
463 response = self.client.get(url)
|
bgneal@812
|
464 self.assertNotContains(response, post_data['subject'], status_code=302)
|
bgneal@812
|
465 self.assertNotContains(response, post_data['message'], status_code=302)
|
bgneal@812
|
466
|
bgneal@812
|
467 response = self.client.get(url, follow=True)
|
bgneal@812
|
468 self.assertNotContains(response, post_data['subject'], status_code=200)
|
bgneal@812
|
469 self.assertNotContains(response, post_data['message'], status_code=200)
|
bgneal@812
|
470
|
bgneal@812
|
471 msg = Message.objects.get(sender=self.users['pj'])
|
bgneal@812
|
472
|
bgneal@812
|
473 self.assertEqual(msg.sender, self.users['pj'])
|
bgneal@812
|
474 self.assertEqual(msg.receiver, self.users['richard'])
|
bgneal@812
|
475 self.assertTrue(now - msg.send_date < datetime.timedelta(seconds=5))
|
bgneal@812
|
476 self.assertIsNone(msg.read_date)
|
bgneal@812
|
477 self.assertIsNone(msg.reply_date)
|
bgneal@812
|
478 self.assertEqual(msg.subject, post_data['subject'])
|
bgneal@812
|
479 self.assertEqual(msg.message, post_data['message'])
|
bgneal@812
|
480 self.assertIsNone(msg.sender_delete_date)
|
bgneal@812
|
481 self.assertIsNone(msg.receiver_delete_date)
|