From a776fb13da758f7d4d636a3294abeb616ed51244 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 25 Mar 2020 10:33:01 +0100 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 diff --git a/tests/test_all.py b/tests/test_all.py index 88254267..22e8fc25 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -204,31 +204,6 @@ class UtilsTests(Authentic2TestCase): '/coin?nonce=xxx&next=/zob/') -class ValidatorsTest(TestCase): - def test_validate_password_(self): - from authentic2.validators import validate_password - from django.core.exceptions import ValidationError - with self.assertRaises(ValidationError): - validate_password('aaaaaZZZZZZ') - with self.assertRaises(ValidationError): - validate_password('00000aaaaaa') - with self.assertRaises(ValidationError): - validate_password('00000ZZZZZZ') - validate_password('000aaaaZZZZ') - - @override_settings(A2_PASSWORD_POLICY_REGEX='^[0-9]{8}$', - A2_PASSWORD_POLICY_REGEX_ERROR_MSG='pasbon', - A2_PASSWORD_POLICY_MIN_LENGTH=0, - A2_PASSWORD_POLICY_MIN_CLASSES=0) - def test_digits_password_policy(self): - from authentic2.validators import validate_password - from django.core.exceptions import ValidationError - - with pytest.raises(ValidationError): - validate_password('aaa') - validate_password('12345678') - - class UserProfileTests(TestCase): def setUp(self): User = get_user_model() diff --git a/tests/test_validators.py b/tests/test_validators.py new file mode 100644 index 00000000..b87f4486 --- /dev/null +++ b/tests/test_validators.py @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- +# authentic2 - versatile identity manager +# Copyright (C) 2010-2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from __future__ import unicode_literals + +import smtplib + +import mock +import pytest + +from django.core.exceptions import ValidationError + +from authentic2.validators import validate_password, EmailValidator + + +def test_validate_password(): + with pytest.raises(ValidationError): + validate_password('aaaaaZZZZZZ') + with pytest.raises(ValidationError): + validate_password('00000aaaaaa') + with pytest.raises(ValidationError): + validate_password('00000ZZZZZZ') + validate_password('000aaaaZZZZ') + + +def test_digits_password_policy(settings): + settings.A2_PASSWORD_POLICY_REGEX = '^[0-9]{8}$' + settings.A2_PASSWORD_POLICY_REGEX_ERROR_MSG = 'pasbon' + settings.A2_PASSWORD_POLICY_MIN_LENGTH = 0 + settings.A2_PASSWORD_POLICY_MIN_CLASSES = 0 + + with pytest.raises(ValidationError): + validate_password('aaa') + validate_password('12345678') + + +def test_email_validator(): + with pytest.raises(ValidationError): + EmailValidator()('nok') + with pytest.raises(ValidationError): + EmailValidator()('@nok.com') + with pytest.raises(ValidationError): + EmailValidator()('foo@bar\x00') + EmailValidator()('ok@ok.com') + + +def test_email_validator_domain(settings): + settings.A2_VALIDATE_EMAIL_DOMAIN = True + with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=[]) as check_mxs: + with pytest.raises(ValidationError): + EmailValidator()('ok@ok.com') + assert check_mxs.call_count == 1 + with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=['ok']) as check_mxs: + EmailValidator()('ok@ok.com') + assert check_mxs.call_count == 1 + + +@pytest.fixture +def smtp(): + smtp = mock.Mock() + smtp.helo.return_value = 250, None + with mock.patch('smtplib.SMTP', return_value=smtp): + yield smtp + + +def test_email_validator_rcpt_check(settings, smtp): + settings.A2_VALIDATE_EMAIL_DOMAIN = True + settings.A2_VALIDATE_EMAIL = True + + validator = EmailValidator(rcpt_check=True) + + with mock.patch('authentic2.validators.EmailValidator.check_mxs', return_value=['ok']): + smtp.rcpt.return_value = 100, None + validator('ok@ok.com') + + smtp.rcpt.return_value = 500, None + with pytest.raises(ValidationError): + validator('ok@ok.com') + + smtp.rcpt.return_value = 100, None + smtp.rcpt.side_effect = smtplib.SMTPServerDisconnected + validator('ok@ok.com') + + smtp.rcpt.return_value = 100, None + smtp.connect.side_effect = smtplib.SMTPConnectError(1,2) + validator('ok@ok.com') + + assert smtp.connect.call_count == 4 + assert smtp.rcpt.call_count == 3 -- 2.24.0