Projet

Général

Profil

0002-tests-test-email-validator-40989.patch

Benjamin Dauvergne, 27 mars 2020 12:51

Télécharger (5,43 ko)

Voir les différences:

Subject: [PATCH 2/2] tests: test email validator (#40989)

 tests/test_all.py        |  25 ----------
 tests/test_validators.py | 103 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 103 insertions(+), 25 deletions(-)
 create mode 100644 tests/test_validators.py
tests/test_all.py
204 204
            '/coin?nonce=xxx&next=/zob/')
205 205

  
206 206

  
207
class ValidatorsTest(TestCase):
208
    def test_validate_password_(self):
209
        from authentic2.validators import validate_password
210
        from django.core.exceptions import ValidationError
211
        with self.assertRaises(ValidationError):
212
            validate_password('aaaaaZZZZZZ')
213
        with self.assertRaises(ValidationError):
214
            validate_password('00000aaaaaa')
215
        with self.assertRaises(ValidationError):
216
            validate_password('00000ZZZZZZ')
217
        validate_password('000aaaaZZZZ')
218

  
219
    @override_settings(A2_PASSWORD_POLICY_REGEX='^[0-9]{8}$',
220
                       A2_PASSWORD_POLICY_REGEX_ERROR_MSG='pasbon',
221
                       A2_PASSWORD_POLICY_MIN_LENGTH=0,
222
                       A2_PASSWORD_POLICY_MIN_CLASSES=0)
223
    def test_digits_password_policy(self):
224
        from authentic2.validators import validate_password
225
        from django.core.exceptions import ValidationError
226

  
227
        with pytest.raises(ValidationError):
228
            validate_password('aaa')
229
        validate_password('12345678')
230

  
231

  
232 207
class UserProfileTests(TestCase):
233 208
    def setUp(self):
234 209
        User = get_user_model()
tests/test_validators.py
1
# -*- coding: utf-8 -*-
2
# authentic2 - versatile identity manager
3
# Copyright (C) 2010-2019 Entr'ouvert
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17

  
18
from __future__ import unicode_literals
19

  
20
import smtplib
21

  
22
import mock
23
import pytest
24

  
25
from django.core.exceptions import ValidationError
26

  
27
from authentic2.validators import validate_password, EmailValidator
28

  
29

  
30
def test_validate_password():
31
    with pytest.raises(ValidationError):
32
        validate_password('aaaaaZZZZZZ')
33
    with pytest.raises(ValidationError):
34
        validate_password('00000aaaaaa')
35
    with pytest.raises(ValidationError):
36
        validate_password('00000ZZZZZZ')
37
    validate_password('000aaaaZZZZ')
38

  
39

  
40
def test_digits_password_policy(settings):
41
    settings.A2_PASSWORD_POLICY_REGEX = '^[0-9]{8}$'
42
    settings.A2_PASSWORD_POLICY_REGEX_ERROR_MSG = 'pasbon'
43
    settings.A2_PASSWORD_POLICY_MIN_LENGTH = 0
44
    settings.A2_PASSWORD_POLICY_MIN_CLASSES = 0
45

  
46
    with pytest.raises(ValidationError):
47
        validate_password('aaa')
48
    validate_password('12345678')
49

  
50

  
51
def test_email_validator():
52
    with pytest.raises(ValidationError):
53
        EmailValidator()('nok')
54
    with pytest.raises(ValidationError):
55
        EmailValidator()('@nok.com')
56
    with pytest.raises(ValidationError):
57
        EmailValidator()('foo@bar\x00')
58
    EmailValidator()('ok@ok.com')
59

  
60

  
61
def test_email_validator_domain(settings):
62
    settings.A2_VALIDATE_EMAIL_DOMAIN = True
63
    with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=[]) as check_mxs:
64
        with pytest.raises(ValidationError):
65
            EmailValidator()('ok@ok.com')
66
    assert check_mxs.call_count == 1
67
    with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=['ok']) as check_mxs:
68
        EmailValidator()('ok@ok.com')
69
    assert check_mxs.call_count == 1
70

  
71

  
72
@pytest.fixture
73
def smtp():
74
    smtp = mock.Mock()
75
    smtp.helo.return_value = 250, None
76
    with mock.patch('smtplib.SMTP', return_value=smtp):
77
        yield smtp
78

  
79

  
80
def test_email_validator_rcpt_check(settings, smtp):
81
    settings.A2_VALIDATE_EMAIL_DOMAIN = True
82
    settings.A2_VALIDATE_EMAIL = True
83

  
84
    validator = EmailValidator(rcpt_check=True)
85

  
86
    with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=['ok']):
87
        smtp.rcpt.return_value = 100, None
88
        validator('ok@ok.com')
89

  
90
        smtp.rcpt.return_value = 500, None
91
        with pytest.raises(ValidationError):
92
            validator('ok@ok.com')
93

  
94
        smtp.rcpt.return_value = 100, None
95
        smtp.rcpt.side_effect = smtplib.SMTPServerDisconnected
96
        validator('ok@ok.com')
97

  
98
        smtp.rcpt.return_value = 100, None
99
        smtp.connect.side_effect = smtplib.SMTPConnectError(1,2)
100
        validator('ok@ok.com')
101

  
102
    assert smtp.connect.call_count == 4
103
    assert smtp.rcpt.call_count == 3
0
-