-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathtests.py
276 lines (242 loc) · 11.1 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.client import Client, RequestFactory
from django.utils import timezone
from django.utils.encoding import force_text
from django.contrib.auth.models import AnonymousUser
from django.template import Template, Context
from django_messages.forms import ComposeForm
from django_messages.models import Message
from django_messages.utils import format_subject, format_quote
from django_messages.context_processors import inbox
from .utils import get_user_model
User = get_user_model()
class SendTestCase(TestCase):
def setUp(self):
self.user1 = User.objects.create_user(
'user1', 'user1@example.com', '123456')
self.user2 = User.objects.create_user(
'user2', 'user2@example.com', '123456')
self.msg1 = Message(sender=self.user1, recipient=self.user2,
subject='Subject Text', body='Body Text')
self.msg1.save()
def testBasic(self):
self.assertEqual(self.msg1.sender, self.user1)
self.assertEqual(self.msg1.recipient, self.user2)
self.assertEqual(self.msg1.subject, 'Subject Text')
self.assertEqual(self.msg1.body, 'Body Text')
self.assertEqual(self.user1.sent_messages.count(), 1)
self.assertEqual(self.user1.received_messages.count(), 0)
self.assertEqual(self.user2.received_messages.count(), 1)
self.assertEqual(self.user2.sent_messages.count(), 0)
class DeleteTestCase(TestCase):
def setUp(self):
self.user1 = User.objects.create_user(
'user3', 'user3@example.com', '123456')
self.user2 = User.objects.create_user(
'user4', 'user4@example.com', '123456')
self.msg1 = Message(sender=self.user1, recipient=self.user2,
subject='Subject Text 1', body='Body Text 1')
self.msg2 = Message(sender=self.user1, recipient=self.user2,
subject='Subject Text 2', body='Body Text 2')
self.msg1.sender_deleted_at = timezone.now()
self.msg2.recipient_deleted_at = timezone.now()
self.msg1.save()
self.msg2.save()
def testBasic(self):
self.assertEqual(Message.objects.outbox_for(self.user1).count(), 1)
self.assertEqual(
Message.objects.outbox_for(self.user1)[0].subject,
'Subject Text 2'
)
self.assertEqual(Message.objects.inbox_for(self.user2).count(), 1)
self.assertEqual(
Message.objects.inbox_for(self.user2)[0].subject,
'Subject Text 1'
)
#undelete
self.msg1.sender_deleted_at = None
self.msg2.recipient_deleted_at = None
self.msg1.save()
self.msg2.save()
self.assertEqual(Message.objects.outbox_for(self.user1).count(), 2)
self.assertEqual(Message.objects.inbox_for(self.user2).count(), 2)
class IntegrationTestCase(TestCase):
"""
Test the app from a user perpective using Django's Test-Client.
"""
T_USER_DATA = [{'username': 'user_1', 'password': '123456',
'email': 'user_1@example.com'},
{'username': 'user_2', 'password': '123456',
'email': 'user_2@example.com'}]
T_MESSAGE_DATA = [{'subject': 'Test Subject 1',
'body': 'Lorem ipsum\ndolor sit amet\n\nconsectur.'}]
def setUp(self):
""" create 2 users and a test-client logged in as user_1 """
self.user_1 = User.objects.create_user(**self.T_USER_DATA[0])
self.user_2 = User.objects.create_user(**self.T_USER_DATA[1])
self.c = Client()
self.c.login(username=self.T_USER_DATA[0]['username'],
password=self.T_USER_DATA[0]['password'])
def testInboxEmpty(self):
""" request the empty inbox """
response = self.c.get(reverse('messages_inbox'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/inbox.html')
self.assertEqual(len(response.context['message_list']), 0)
def testOutboxEmpty(self):
""" request the empty outbox """
response = self.c.get(reverse('messages_outbox'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/outbox.html')
self.assertEqual(len(response.context['message_list']), 0)
def testTrashEmpty(self):
""" request the empty trash """
response = self.c.get(reverse('messages_trash'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/trash.html')
self.assertEqual(len(response.context['message_list']), 0)
def testCompose(self):
""" compose a message step by step """
response = self.c.get(reverse('messages_compose'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/compose.html')
response = self.c.post(
reverse('messages_compose'),
{
'recipient': self.T_USER_DATA[1]['username'],
'subject': self.T_MESSAGE_DATA[0]['subject'],
'body': self.T_MESSAGE_DATA[0]['body']
})
# successfull sending should redirect to inbox
self.assertEqual(response.status_code, 302)
if 'http' in response['Location']:
self.assertEqual(response['Location'],
"http://testserver%s" % reverse('messages_inbox'))
else:
self.assertEqual(response['Location'], reverse('messages_inbox'))
# make sure the message exists in the outbox after sending
response = self.c.get(reverse('messages_outbox'))
self.assertEqual(len(response.context['message_list']), 1)
def testReply(self):
""" test that user_2 can reply """
# create a message for this test
Message.objects.create(sender=self.user_1,
recipient=self.user_2,
subject=self.T_MESSAGE_DATA[0]['subject'],
body=self.T_MESSAGE_DATA[0]['body'])
# log the user_2 in and check the inbox
self.c.login(username=self.T_USER_DATA[1]['username'],
password=self.T_USER_DATA[1]['password'])
response = self.c.get(reverse('messages_inbox'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/inbox.html')
self.assertEqual(len(response.context['message_list']), 1)
pk = getattr(response.context['message_list'][0], 'pk')
# reply to the first message
response = self.c.get(reverse('messages_reply',
kwargs={'message_id': pk}))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.templates[0].name,
'django_messages/compose.html')
self.assertEqual(
response.context['form'].initial['body'],
format_quote(self.user_1, self.T_MESSAGE_DATA[0]['body'])
)
self.assertEqual(
response.context['form'].initial['subject'],
u"Re: %(subject)s" % {'subject': self.T_MESSAGE_DATA[0]['subject']}
)
class FormatTestCase(TestCase):
""" some tests for helper functions """
def testSubject(self):
""" test that reply counting works as expected """
self.assertEqual(format_subject(u"foo bar"), u"Re: foo bar")
self.assertEqual(format_subject(u"Re: foo bar"), u"Re[2]: foo bar")
self.assertEqual(format_subject(u"Re[2]: foo bar"), u"Re[3]: foo bar")
self.assertEqual(format_subject(u"Re[10]: foo bar"),
u"Re[11]: foo bar")
class InboxCountTestCase(TestCase):
"""Test inbox-count content processor and templatetag."""
def setUp(self):
self.factory = RequestFactory()
self.anonymous_user = AnonymousUser()
self.user = User.objects.create_user(
username='test_user', email='test@example.com', password='secret'
)
self.user_2 = User.objects.create_user(
username='test_user_2', email='test2@example.de', password='secret'
)
Message.objects.create(
recipient=self.user_2,
subject="Subject",
body="Body",
sender=self.user
)
self.template = Template("""{% load inbox %}{% inbox_count %}""")
def test_content_processor_anon(self):
"""Test message count for anonymous user."""
r = self.factory.get('/')
r.user = self.anonymous_user
self.assertEquals(inbox(r), {})
def test_context_processor_user_empty(self):
"""Test message count for user with empty inbox."""
r = self.factory.get('/')
r.user = self.user
self.assertEquals(inbox(r), {'messages_inbox_count': 0})
def test_context_processor_user_count(self):
"""Test message count for user with one unread message."""
r = self.factory.get('/')
r.user = self.user_2
self.assertEquals(inbox(r), {'messages_inbox_count': 1})
def test_template_tag_anon(self):
"""Test message count for anonymous user."""
html = self.template.render(Context({'user': self.anonymous_user}))
self.assertEquals(html, "")
def test_template_tag_user_empty(self):
"""Test message count for user with empty inbox."""
html = self.template.render(Context({'user': self.user}))
self.assertEquals(html, "0")
def test_template_tag_user_count(self):
"""Test message count for user with one unread message."""
html = self.template.render(Context({'user': self.user_2}))
self.assertEquals(html, "1")
class RecipientFilterTestCase(TestCase):
def setUp(self):
self.user1 = User.objects.create_user(
'user1', 'user1@example.com', '123456')
self.user2 = User.objects.create_user(
'user2', 'user2@example.com', '123456')
self.user2.is_active = False
self.user2.save()
self.f = lambda u: u.is_active
def testRecipientFiterIsActive(self):
form = ComposeForm(
{"recipient": "user1", "subject": "S", "body": "B"},
recipient_filter=self.f
)
assert form.is_valid()
assert self.user1 in form.cleaned_data["recipient"]
def testRecipientFilterNotActive(self):
form = ComposeForm(
{"recipient": "user2", "subject": "S", "body": "B"},
recipient_filter=self.f
)
assert not form.is_valid()
assert self.user2.username in force_text(form.errors)
def testRecipientFilterMixed(self):
form = ComposeForm(
{"recipient": "user1,user2", "subject": "S", "body": "B"},
recipient_filter=self.f
)
assert not form.is_valid()
assert self.user2.username in force_text(form.errors)