/
test_recipient_validation.py
460 lines (406 loc) · 14.1 KB
/
test_recipient_validation.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import pytest
from notifications_utils.recipients import (
InvalidEmailError,
InvalidPhoneError,
allowed_to_send_to,
format_phone_number_human_readable,
format_recipient,
get_international_phone_info,
international_phone_info,
is_uk_phone_number,
normalise_phone_number,
try_validate_and_format_phone_number,
validate_and_format_phone_number,
validate_email_address,
validate_phone_number,
)
valid_uk_phone_numbers = [
"7123456789",
"07123456789",
"07123 456789",
"07123-456-789",
"00447123456789",
"00 44 7123456789",
"+447123456789",
"+44 7123 456 789",
"+44 (0)7123 456 789",
"\u200B\t\t+44 (0)7123 \uFEFF 456 789 \r\n",
]
valid_international_phone_numbers = [
"71234567890", # Russia
"1-202-555-0104", # USA
"+12025550104", # USA
"0012025550104", # USA
"+0012025550104", # USA
"23051234567", # Mauritius,
"+682 12345", # Cook islands
"+3312345678",
"003312345678",
"1-2345-12345-12345", # 15 digits
]
valid_phone_numbers = valid_uk_phone_numbers + valid_international_phone_numbers
invalid_uk_phone_numbers = sum(
[
[(phone_number, error) for phone_number in group]
for error, group in [
(
"Mobile number is too long",
(
"712345678910",
"0712345678910",
"0044712345678910",
"0044712345678910",
"+44 (0)7123 456 789 10",
),
),
(
"Mobile number is too short",
(
"0712345678",
"004471234567",
"00447123456",
"+44 (0)7123 456 78",
),
),
(
"This does not look like a UK mobile number – double check the mobile number you entered",
(
"08081 570364",
"+44 8081 570364",
"0117 496 0860",
"+44 117 496 0860",
"020 7946 0991",
"+44 20 7946 0991",
),
),
(
"Mobile numbers can only include: 0 1 2 3 4 5 6 7 8 9 ( ) + -",
(
"07890x32109",
"07123 456789...",
"07123 ☟☜⬇⬆☞☝",
"07123☟☜⬇⬆☞☝",
'07";DROP TABLE;"',
"+44 07ab cde fgh",
"ALPHANUM3R1C",
),
),
]
],
[],
)
invalid_phone_numbers = list(
filter(
lambda number: number[0]
not in {
"712345678910", # Could be Russia
},
invalid_uk_phone_numbers,
)
) + [
("800000000000", "Country code not found - double check the mobile number you entered"),
("1234567", "Mobile number is too short"),
("+682 1234", "Mobile number is too short"), # Cook Islands phone numbers can be 5 digits
("+12345 12345 12345 6", "Mobile number is too long"),
]
valid_email_addresses = (
"email@domain.com",
"email@domain.COM",
"firstname.lastname@domain.com",
"firstname.o'lastname@domain.com",
"email@subdomain.domain.com",
"firstname+lastname@domain.com",
"1234567890@domain.com",
"email@domain-one.com",
"_______@domain.com",
"email@domain.name",
"email@domain.superlongtld",
"email@domain.co.jp",
"firstname-lastname@domain.com",
"info@german-financial-services.vermögensberatung",
"info@german-financial-services.reallylongarbitrarytldthatiswaytoohugejustincase",
"japanese-info@例え.テスト",
"email@double--hyphen.com",
)
invalid_email_addresses = (
"email@123.123.123.123",
"email@[123.123.123.123]",
"plainaddress",
"@no-local-part.com",
"Outlook Contact <outlook-contact@domain.com>",
"no-at.domain.com",
"no-tld@domain",
";beginning-semicolon@domain.co.uk",
"middle-semicolon@domain.co;uk",
"trailing-semicolon@domain.com;",
'"email+leading-quotes@domain.com',
'email+middle"-quotes@domain.com',
'"quoted-local-part"@domain.com',
'"quoted@domain.com"',
"lots-of-dots@domain..gov..uk",
"two-dots..in-local@domain.com",
"multiple@domains@domain.com",
"spaces in local@domain.com",
"spaces-in-domain@dom ain.com",
"underscores-in-domain@dom_ain.com",
"pipe-in-domain@example.com|gov.uk",
"comma,in-local@gov.uk",
"comma-in-domain@domain,gov.uk",
"pound-sign-in-local£@domain.com",
"local-with-’-apostrophe@domain.com",
"local-with-”-quotes@domain.com",
"domain-starts-with-a-dot@.domain.com",
"brackets(in)local@domain.com",
f"email-too-long-{'a' * 320}@example.com",
"incorrect-punycode@xn---something.com",
)
@pytest.mark.parametrize("phone_number", valid_international_phone_numbers)
def test_detect_international_phone_numbers(phone_number):
assert is_uk_phone_number(phone_number) is False
@pytest.mark.parametrize("phone_number", valid_uk_phone_numbers)
def test_detect_uk_phone_numbers(phone_number):
assert is_uk_phone_number(phone_number) is True
@pytest.mark.parametrize(
"phone_number, expected_info",
[
(
"07900900123",
international_phone_info(
international=False,
crown_dependency=False,
country_prefix="44", # UK
billable_units=1,
),
),
(
"07700900123",
international_phone_info(
international=False,
crown_dependency=False,
country_prefix="44", # Number in TV range
billable_units=1,
),
),
(
"07700800123",
international_phone_info(
international=True,
crown_dependency=True,
country_prefix="44", # UK Crown dependency, so prefix same as UK
billable_units=1,
),
),
(
"20-12-1234-1234",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="20", # Egypt
billable_units=3,
),
),
(
"00201212341234",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="20", # Egypt
billable_units=3,
),
),
(
"1664000000000",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="1664", # Montserrat
billable_units=3,
),
),
(
"71234567890",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="7", # Russia
billable_units=4,
),
),
(
"1-202-555-0104",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="1", # USA
billable_units=1,
),
),
(
"+23051234567",
international_phone_info(
international=True,
crown_dependency=False,
country_prefix="230", # Mauritius
billable_units=2,
),
),
],
)
def test_get_international_info(phone_number, expected_info):
assert get_international_phone_info(phone_number) == expected_info
@pytest.mark.parametrize(
"phone_number",
[
"abcd",
"079OO900123",
pytest.param("", marks=pytest.mark.xfail),
pytest.param("12345", marks=pytest.mark.xfail),
pytest.param("+12345", marks=pytest.mark.xfail),
pytest.param("1-2-3-4-5", marks=pytest.mark.xfail),
pytest.param("1 2 3 4 5", marks=pytest.mark.xfail),
pytest.param("(1)2345", marks=pytest.mark.xfail),
],
)
def test_normalise_phone_number_raises_if_unparseable_characters(phone_number):
with pytest.raises(InvalidPhoneError):
normalise_phone_number(phone_number)
@pytest.mark.parametrize(
"phone_number",
[
"+21 4321 0987",
"00997 1234 7890",
"801234-7890",
"(8-0)-1234-7890",
],
)
def test_get_international_info_raises(phone_number):
with pytest.raises(InvalidPhoneError) as error:
get_international_phone_info(phone_number)
assert str(error.value) == "Country code not found - double check the mobile number you entered"
@pytest.mark.parametrize("phone_number", valid_uk_phone_numbers)
@pytest.mark.parametrize(
"extra_args",
[
{},
{"international": False},
],
)
def test_phone_number_accepts_valid_values(extra_args, phone_number):
try:
validate_phone_number(phone_number, **extra_args)
except InvalidPhoneError:
pytest.fail("Unexpected InvalidPhoneError")
@pytest.mark.parametrize("phone_number", valid_phone_numbers)
def test_phone_number_accepts_valid_international_values(phone_number):
try:
validate_phone_number(phone_number, international=True)
except InvalidPhoneError:
pytest.fail("Unexpected InvalidPhoneError")
@pytest.mark.parametrize("phone_number", valid_uk_phone_numbers)
def test_valid_uk_phone_number_can_be_formatted_consistently(phone_number):
assert validate_and_format_phone_number(phone_number) == "447123456789"
@pytest.mark.parametrize(
"phone_number, expected_formatted",
[
("71234567890", "71234567890"),
("1-202-555-0104", "12025550104"),
("+12025550104", "12025550104"),
("0012025550104", "12025550104"),
("+0012025550104", "12025550104"),
("23051234567", "23051234567"),
],
)
def test_valid_international_phone_number_can_be_formatted_consistently(phone_number, expected_formatted):
assert validate_and_format_phone_number(phone_number, international=True) == expected_formatted
@pytest.mark.parametrize("phone_number, error_message", invalid_uk_phone_numbers)
@pytest.mark.parametrize(
"extra_args",
[
{},
{"international": False},
],
)
def test_phone_number_rejects_invalid_values(extra_args, phone_number, error_message):
with pytest.raises(InvalidPhoneError) as e:
validate_phone_number(phone_number, **extra_args)
assert error_message == str(e.value)
@pytest.mark.parametrize("phone_number, error_message", invalid_phone_numbers)
def test_phone_number_rejects_invalid_international_values(phone_number, error_message):
with pytest.raises(InvalidPhoneError) as e:
validate_phone_number(phone_number, international=True)
assert error_message == str(e.value)
@pytest.mark.parametrize("email_address", valid_email_addresses)
def test_validate_email_address_accepts_valid(email_address):
try:
assert validate_email_address(email_address) == email_address
except InvalidEmailError:
pytest.fail("Unexpected InvalidEmailError")
@pytest.mark.parametrize(
"email",
[
" email@domain.com ",
"\temail@domain.com",
"\temail@domain.com\n",
"\u200Bemail@domain.com\u200B",
],
)
def test_validate_email_address_strips_whitespace(email):
assert validate_email_address(email) == "email@domain.com"
@pytest.mark.parametrize("email_address", invalid_email_addresses)
def test_validate_email_address_raises_for_invalid(email_address):
with pytest.raises(InvalidEmailError) as e:
validate_email_address(email_address)
assert str(e.value) == "Not a valid email address"
@pytest.mark.parametrize("phone_number", valid_uk_phone_numbers)
def test_validates_against_guestlist_of_phone_numbers(phone_number):
assert allowed_to_send_to(phone_number, ["07123456789", "07700900460", "test@example.com"])
assert not allowed_to_send_to(phone_number, ["07700900460", "07700900461", "test@example.com"])
@pytest.mark.parametrize(
"recipient_number, allowlist_number",
[
["1-202-555-0104", "0012025550104"],
["0012025550104", "1-202-555-0104"],
],
)
def test_validates_against_guestlist_of_international_phone_numbers(recipient_number, allowlist_number):
assert allowed_to_send_to(recipient_number, [allowlist_number])
@pytest.mark.parametrize("email_address", valid_email_addresses)
def test_validates_against_guestlist_of_email_addresses(email_address):
assert not allowed_to_send_to(email_address, ["very_special_and_unique@example.com"])
@pytest.mark.parametrize(
"phone_number, expected_formatted",
[
("07900900123", "07900 900123"), # UK
("+44(0)7900900123", "07900 900123"), # UK
("447900900123", "07900 900123"), # UK
("20-12-1234-1234", "+20 12 12341234"), # Egypt
("00201212341234", "+20 12 12341234"), # Egypt
("1664 0000000", "+1 664-000-0000"), # Montserrat
("7 499 1231212", "+7 499 123-12-12"), # Moscow (Russia)
("1-202-555-0104", "+1 202-555-0104"), # Washington DC (USA)
("+23051234567", "+230 5123 4567"), # Mauritius
("33(0)1 12345678", "+33 1 12 34 56 78"), # Paris (France)
],
)
def test_format_uk_and_international_phone_numbers(phone_number, expected_formatted):
assert format_phone_number_human_readable(phone_number) == expected_formatted
@pytest.mark.parametrize(
"recipient, expected_formatted",
[
(True, ""),
(False, ""),
(0, ""),
(0.1, ""),
(None, ""),
("foo", "foo"),
("TeSt@ExAmPl3.com", "test@exampl3.com"),
("+4407900 900 123", "447900900123"),
("+1 800 555 5555", "18005555555"),
],
)
def test_format_recipient(recipient, expected_formatted):
assert format_recipient(recipient) == expected_formatted
def test_try_format_recipient_doesnt_throw():
assert try_validate_and_format_phone_number("ALPHANUM3R1C") == "ALPHANUM3R1C"
def test_format_phone_number_human_readable_doenst_throw():
assert format_phone_number_human_readable("ALPHANUM3R1C") == "ALPHANUM3R1C"