File: test_email_validator.py

package info (click to toggle)
wtforms-components 0.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: python: 1,582; makefile: 135; sh: 11
file content (87 lines) | stat: -rw-r--r-- 2,399 bytes parent folder | download
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
import pytest
from wtforms.validators import ValidationError

from wtforms_components import Email


class DummyTranslations:
    def gettext(self, string):
        return string

    def ngettext(self, singular, plural, n):
        if n == 1:
            return singular

        return plural


class DummyForm(dict):
    pass


class DummyField:
    _translations = DummyTranslations()

    def __init__(self, data, errors=(), raw_data=None):
        self.data = data
        self.errors = list(errors)
        self.raw_data = raw_data

    def gettext(self, string):
        return self._translations.gettext(string)

    def ngettext(self, singular, plural, n):
        return self._translations.ngettext(singular, plural, n)


class TestEmailValidator:
    def setup_method(self, method):
        self.form = DummyForm()

    @pytest.mark.parametrize(
        "email",
        [
            "email@here.com",
            "weirder-email@here.and.there.com",
            "example@valid-----hyphens.com",
            "example@valid-with-hyphens.com",
            "test@domain.with.idn.tld.उदाहरण.परीक्षा",
            '"\\\011"@here.com',
        ],
    )
    def test_returns_none_on_valid_email(self, email):
        validate_email = Email()
        validate_email(self.form, DummyField(email))

    @pytest.mark.parametrize(
        ("email",),
        [
            (None,),
            ("",),
            ("abc",),
            ("abc@",),
            ("abc@bar",),
            ("a @x.cz",),
            ("abc@.com",),
            ("something@@somewhere.com",),
            ("email@127.0.0.1",),
            ("example@invalid-.com",),
            ("example@-invalid.com",),
            ("example@inv-.alid-.com",),
            ("example@inv-.-alid.com",),
            # Quoted-string format (CR not allowed)
            ('"\\\012"@here.com',),
        ],
    )
    def test_raises_validationerror_on_invalid_email(self, email):
        validate_email = Email()
        with pytest.raises(ValidationError):
            validate_email(self.form, DummyField(email))

    def test_default_validation_error_message(self):
        validate_email = Email()
        try:
            validate_email(self.form, DummyField("@@@"))
            assert False, "No validation error thrown."
        except ValidationError as e:
            assert str(e) == "Invalid email address."