/
tests.py
289 lines (250 loc) · 9.75 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
277
278
279
280
281
282
283
284
285
286
287
288
289
from django.test import TestCase
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.core import mail
from humans.models import User
from datetime import timedelta
from .models import Box, Message
from .forms import CreateBoxForm, SubmitBoxForm, MAX_MESSAGE_SIZE
from .tasks import process_email
from .test_constants import ENCRYPTED_MESSAGE
from humans.test_constants import EXPIRED_KEY, EXPIRED_KEY_FINGERPRINT, \
REVOKED_KEY, REVOKED_KEY_FINGERPRINT, VALID_KEY, VALID_KEY_FINGERPRINT
import random
import string
def create_boxes(user):
not_expired = timezone.now() + timedelta(hours=3)
expired = timezone.now() - timedelta(hours=3)
user.own_boxes.create(
name="open", expires_at=not_expired, status=Box.OPEN)
user.own_boxes.create(
name="closed", expires_at=not_expired, status=Box.CLOSED)
user.own_boxes.create(
name="sent", expires_at=not_expired, status=Box.DONE)
user.own_boxes.create(
name="expired", expires_at=expired, status=Box.EXPIRED)
def create_open_box(user):
expires_at = timezone.now() + timedelta(hours=3)
return user.own_boxes.create(name="open",
expires_at=expires_at,
status=Box.OPEN)
def create_and_login_user(client):
username = ''.join(random.choice(string.ascii_uppercase) for _ in range(5))
user = User.objects.create_user(username=username,
email="{}@example.com".format(username))
client.force_login(user)
return user
class BoxFormTests(TestCase):
def test_invalid_expiration_date(self):
"""
Expiration must be submitted provided and with valid format
If one of this the above statments are not true form must be
invalid
"""
data = {
"name": "some name",
"description": "some text",
"expires_at": "31/31/2000 24:00",
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), False)
def test_valid_expiration_date(self):
"""
If expiration date is valid and in the future form is valid
"""
data = {
"name": "some name",
"description": "some text",
"expires_at": "12/12/2020 23:00",
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), True)
def test_expiration_date_from_past(self):
"""
If date already belongs to the past, the form must be invalid
"""
data = {
"name": "some name",
"description": "some text",
"expires_at": "12/12/2012 23:00",
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), False)
def test_empty_name(self):
"""
Name must be present
"""
data = {
"name": "",
"description": "some text",
"expires_at": "12/12/2020 23:00",
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), False)
def test_empty_desription(self):
"""
Description is optional
"""
data = {
"name": "some name",
"description": "",
"expires_at": "12/12/2020 23:00",
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), True)
def test_invalid_max_messages(self):
"""
Description is optional
"""
data = {
"name": "some name",
"description": "",
"expires_at": "12/12/2020 23:00",
"max_messages": 0
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), False)
def test_never_expires(self):
data = {
"name": "some name",
"description": "",
"never_expires": True,
"max_messages": 1
}
form = CreateBoxForm(data)
self.assertEqual(form.is_valid(), True)
class SubmitBoxFormTests(TestCase):
def test_encrypted_content(self):
"""
Form is valid if content is encrypted
"""
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE})
self.assertEqual(form.is_valid(), True)
def test_clear_text_content(self):
"""
If not PGP message, form is invalid
"""
form = SubmitBoxForm({"message": "some clear text message"})
self.assertEqual(form.is_valid(), False)
def test_message_too_big(self):
form = SubmitBoxForm({"message": "m" * (MAX_MESSAGE_SIZE + 1)})
self.assertEqual(form.is_valid(), False)
def test_encrypted_file(self):
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE,
"file_name": "test"})
self.assertEqual(form.is_valid(), True)
def test_add_reply_to_is_present(self):
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE,
"add_reply_to": "on"})
self.assertEqual(form.is_valid(), True)
self.assertTrue(form.cleaned_data.get("add_reply_to"))
def test_add_reply_to_is_not_present(self):
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE})
self.assertEqual(form.is_valid(), True)
self.assertFalse(form.cleaned_data.get("add_reply_to"))
class BoxListViewTests(TestCase):
def test_open_boxes_are_default_list(self):
"""
Base page shows list of open boxes
"""
user = create_and_login_user(self.client)
create_boxes(user)
response = self.client.get(reverse("boxes_list"))
for box in response.context["object_list"]:
self.assertEqual(box.status, Box.OPEN)
def test_expired_boxes_list(self):
"""
With expired query param expired boxes are shown
"""
user = create_and_login_user(self.client)
create_boxes(user)
response = self.client.get(reverse("boxes_list"),
{'display': 'Expired'})
for box in response.context["object_list"]:
self.assertEqual(box.status, Box.EXPIRED)
def test_closed_boxes_list(self):
"""
With closed query param closed boxes are shown
"""
user = create_and_login_user(self.client)
create_boxes(user)
response = self.client.get(reverse("boxes_list"),
{'display': 'Closed'})
for box in response.context["object_list"]:
self.assertEqual(box.status, Box.CLOSED)
def test_sent_boxes_list(self):
"""
With sent query param sent boxes are shown
"""
user = create_and_login_user(self.client)
create_boxes(user)
response = self.client.get(reverse("boxes_list"),
{'display': 'Done'})
for box in response.context["object_list"]:
self.assertEqual(box.status, Box.DONE)
class BoxSubmitViewTests(TestCase):
def test_valid_owner_key(self):
user = create_and_login_user(self.client)
user.public_key = VALID_KEY
user.fingerprint = VALID_KEY_FINGERPRINT
user.save()
box = create_open_box(user)
response = self.client.get(reverse("boxes_show", args=(box.uuid,)))
self.assertEqual(response.template_name[0], 'boxes/box_submit.html')
def test_revoked_owner_key(self):
user = create_and_login_user(self.client)
user.public_key = REVOKED_KEY
user.fingerprint = REVOKED_KEY_FINGERPRINT
user.save()
box = create_open_box(user)
response = self.client.get(reverse("boxes_show", args=(box.uuid,)))
self.assertEqual(response.template_name, 'boxes/closed.html')
def test_expired_owner_key(self):
user = create_and_login_user(self.client)
user.public_key = EXPIRED_KEY
user.fingerprint = EXPIRED_KEY_FINGERPRINT
user.save()
box = create_open_box(user)
response = self.client.get(reverse("boxes_show", args=(box.uuid,)))
self.assertEqual(response.template_name, 'boxes/closed.html')
def test_no_owner_key(self):
user = create_and_login_user(self.client)
box = create_open_box(user)
user.save()
response = self.client.get(reverse("boxes_show", args=(box.uuid,)))
self.assertEqual(response.template_name, 'boxes/closed.html')
class MailTaskTests(TestCase):
def test_email_sending(self):
"""
With a valid message_id an email is sent and the Message status
is changed to sent
"""
user = create_and_login_user(self.client)
create_boxes(user)
initial_box = user.own_boxes.all()[0]
message = initial_box.messages.create()
process_email(message.id, {"message": ENCRYPTED_MESSAGE})
after_box = user.own_boxes.get(id=initial_box.id)
after_msg = after_box.messages.get(id=message.id)
message.refresh_from_db()
self.assertEqual(message.status, Message.SENT)
self.assertEqual(after_msg.sent_at, after_box.last_sent_at)
self.assertEqual(len(mail.outbox[0].reply_to), 0)
def test_email_sending_with_reply_to(self):
mail.outbox = []
user = create_and_login_user(self.client)
create_boxes(user)
initial_box = user.own_boxes.all()[0]
message = initial_box.messages.create()
process_email(
message.id, {"message": ENCRYPTED_MESSAGE}, sent_by=user.email
)
message.refresh_from_db()
self.assertEqual(len(mail.outbox), 1)
self.assertIn(user.email, mail.outbox[0].reply_to)
self.assertEqual(message.status, Message.SENT)