56 |
56 |
validate_password('12345678')
|
57 |
57 |
|
58 |
58 |
|
59 |
|
@pytest.mark.parametrize(
|
60 |
|
'email',
|
61 |
|
[
|
62 |
|
'nok',
|
63 |
|
'@nok.com',
|
64 |
|
'foo@bar\x00',
|
65 |
|
'foo&@bar',
|
66 |
|
'|a@nok.com',
|
67 |
|
'a/../b@nok.com',
|
68 |
|
'a%b@nok.com',
|
69 |
|
'a!b@nok.com',
|
70 |
|
'a#b@nok.com',
|
71 |
|
'a&b@nok.com',
|
72 |
|
'a?b@nok.com',
|
73 |
|
],
|
74 |
|
)
|
75 |
|
def test_email_validator_nok(email):
|
76 |
|
with pytest.raises(ValidationError):
|
77 |
|
EmailValidator()(email)
|
78 |
|
|
79 |
|
|
80 |
|
@pytest.mark.parametrize('email', ['ok@ok.com', 'a|b@ok.com'])
|
81 |
|
def test_email_validator_ok(email):
|
82 |
|
EmailValidator()(email)
|
83 |
|
|
84 |
|
|
85 |
|
def test_email_validator_domain(settings):
|
86 |
|
settings.A2_VALIDATE_EMAIL_DOMAIN = True
|
87 |
|
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=[]) as query_mxs:
|
|
59 |
class TestEmailValidator:
|
|
60 |
@pytest.mark.parametrize(
|
|
61 |
'bad_email',
|
|
62 |
[
|
|
63 |
'nok',
|
|
64 |
'@nok.com',
|
|
65 |
'foo@bar\x00',
|
|
66 |
'foo&@bar',
|
|
67 |
'|a@nok.com',
|
|
68 |
'a/../b@nok.com',
|
|
69 |
'a%b@nok.com',
|
|
70 |
'a!b@nok.com',
|
|
71 |
'a#b@nok.com',
|
|
72 |
'a&b@nok.com',
|
|
73 |
'a?b@nok.com',
|
|
74 |
],
|
|
75 |
)
|
|
76 |
def test_bad_email(self, bad_email):
|
88 |
77 |
with pytest.raises(ValidationError):
|
|
78 |
EmailValidator()(bad_email)
|
|
79 |
|
|
80 |
@pytest.mark.parametrize('good_email', ['ok@ok.com', 'a|b@ok.com'])
|
|
81 |
def test_good_email(self, good_email):
|
|
82 |
EmailValidator()(good_email)
|
|
83 |
|
|
84 |
def test_validate_email_domain(self, settings):
|
|
85 |
settings.A2_VALIDATE_EMAIL_DOMAIN = True
|
|
86 |
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=[]) as query_mxs:
|
|
87 |
with pytest.raises(ValidationError):
|
|
88 |
EmailValidator()('ok@ok.com')
|
|
89 |
assert query_mxs.call_count == 1
|
|
90 |
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=['ok']) as query_mxs:
|
89 |
91 |
EmailValidator()('ok@ok.com')
|
90 |
|
assert query_mxs.call_count == 1
|
91 |
|
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=['ok']) as query_mxs:
|
92 |
|
EmailValidator()('ok@ok.com')
|
93 |
|
assert query_mxs.call_count == 1
|
|
92 |
assert query_mxs.call_count == 1
|
94 |
93 |
|
|
94 |
@pytest.fixture
|
|
95 |
def smtp(self):
|
|
96 |
def __enter__(self):
|
|
97 |
return self
|
95 |
98 |
|
96 |
|
@pytest.fixture
|
97 |
|
def smtp():
|
98 |
|
def __enter__(self):
|
99 |
|
return self
|
|
99 |
def __exit__(self, *args):
|
|
100 |
return None
|
100 |
101 |
|
101 |
|
def __exit__(self, *args):
|
102 |
|
return None
|
|
102 |
smtp = mock.Mock()
|
|
103 |
smtp.helo.return_value = 250, None
|
|
104 |
smtp.__enter__ = __enter__
|
|
105 |
smtp.__exit__ = __exit__
|
|
106 |
with mock.patch('smtplib.SMTP', return_value=smtp):
|
|
107 |
yield smtp
|
103 |
108 |
|
104 |
|
smtp = mock.Mock()
|
105 |
|
smtp.helo.return_value = 250, None
|
106 |
|
smtp.__enter__ = __enter__
|
107 |
|
smtp.__exit__ = __exit__
|
108 |
|
with mock.patch('smtplib.SMTP', return_value=smtp):
|
109 |
|
yield smtp
|
|
109 |
def test_rcpt_check(self, settings, smtp):
|
|
110 |
settings.A2_VALIDATE_EMAIL_DOMAIN = True
|
|
111 |
settings.A2_VALIDATE_EMAIL = True
|
110 |
112 |
|
|
113 |
validator = EmailValidator(rcpt_check=True)
|
111 |
114 |
|
112 |
|
def test_email_validator_rcpt_check(settings, smtp):
|
113 |
|
settings.A2_VALIDATE_EMAIL_DOMAIN = True
|
114 |
|
settings.A2_VALIDATE_EMAIL = True
|
115 |
|
|
116 |
|
validator = EmailValidator(rcpt_check=True)
|
|
115 |
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=['ok']):
|
|
116 |
smtp.rcpt.return_value = 100, None
|
|
117 |
validator('ok@ok.com')
|
117 |
118 |
|
118 |
|
with mock.patch('authentic2.validators.EmailValidator.query_mxs', return_value=['ok']):
|
119 |
|
smtp.rcpt.return_value = 100, None
|
120 |
|
validator('ok@ok.com')
|
|
119 |
smtp.rcpt.return_value = 500, None
|
|
120 |
with pytest.raises(ValidationError):
|
|
121 |
validator('ok@ok.com')
|
121 |
122 |
|
122 |
|
smtp.rcpt.return_value = 500, None
|
123 |
|
with pytest.raises(ValidationError):
|
|
123 |
smtp.rcpt.return_value = 100, None
|
|
124 |
smtp.rcpt.side_effect = smtplib.SMTPServerDisconnected
|
124 |
125 |
validator('ok@ok.com')
|
125 |
126 |
|
126 |
|
smtp.rcpt.return_value = 100, None
|
127 |
|
smtp.rcpt.side_effect = smtplib.SMTPServerDisconnected
|
128 |
|
validator('ok@ok.com')
|
129 |
|
|
130 |
|
smtp.rcpt.return_value = 100, None
|
131 |
|
smtp.connect.side_effect = smtplib.SMTPConnectError(1, 2)
|
132 |
|
validator('ok@ok.com')
|
|
127 |
smtp.rcpt.return_value = 100, None
|
|
128 |
smtp.connect.side_effect = smtplib.SMTPConnectError(1, 2)
|
|
129 |
validator('ok@ok.com')
|
133 |
130 |
|
134 |
|
assert smtp.connect.call_count == 4
|
135 |
|
assert smtp.rcpt.call_count == 3
|
|
131 |
assert smtp.connect.call_count == 4
|
|
132 |
assert smtp.rcpt.call_count == 3
|
136 |
|
-
|