示例#1
0
def test_email_validation(email_mock):
    email_mock.return_value = None
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert not form.is_valid()
    assert 'email' in form.errors

    email_mock.return_value = '*****@*****.**'
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors

    # should result in whatever email process_email returns
    email_mock.return_value = '*****@*****.**'
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['email'] == '*****@*****.**'
示例#2
0
def test_newsletters_validation(email_mock):
    # comma separated in just one field
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude, walter"],
            "email": "*****@*****.**",
            "privacy": "true",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["newsletters"] == ["dude", "walter"]

    # separate fields
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude", "walter"],
            "email": "*****@*****.**",
            "privacy": "true",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["newsletters"] == ["dude", "walter"]

    # combo of comma separated and non
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude", "walter,donnie"],
            "email": "*****@*****.**",
            "privacy": "true",
        }, )
    form.fields["newsletters"].choices = (
        ("dude", "dude"),
        ("walter", "walter"),
        ("donnie", "donnie"),
    )
    assert form.is_valid(), form.errors
    assert form.cleaned_data["newsletters"] == ["dude", "walter", "donnie"]

    # invalid newsletter
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude, walter"],
            "email": "*****@*****.**",
            "privacy": "true",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), )
    assert not form.is_valid()
    assert "newsletters" in form.errors
示例#3
0
def test_privacy_required(email_mock):
    form = forms.SubscribeForm({
        'newsletters': ['dude, walter'],
        'email': '*****@*****.**',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), )
    assert not form.is_valid()
    assert 'privacy' in form.errors

    form = forms.SubscribeForm({
        'newsletters': ['dude, walter'],
        'email': '*****@*****.**',
        'privacy': 'false',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), )
    assert not form.is_valid()
    assert 'privacy' in form.errors
示例#4
0
def test_newsletters_validation(email_mock):
    # comma separated in just one field
    form = forms.SubscribeForm({
        'newsletters': ['dude, walter'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['newsletters'] == ['dude', 'walter']

    # separate fields
    form = forms.SubscribeForm({
        'newsletters': ['dude', 'walter'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['newsletters'] == ['dude', 'walter']

    # combo of comma separated and non
    form = forms.SubscribeForm({
        'newsletters': ['dude', 'walter,donnie'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'),
                                          ('walter', 'walter'), ('donnie',
                                                                 'donnie'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['newsletters'] == ['dude', 'walter', 'donnie']

    # invalid newsletter
    form = forms.SubscribeForm({
        'newsletters': ['dude, walter'],
        'email': '*****@*****.**',
        'privacy': 'true',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), )
    assert not form.is_valid()
    assert 'newsletters' in form.errors
示例#5
0
def test_privacy_required(email_mock):
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude, walter"],
            "email": "*****@*****.**"
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), )
    assert not form.is_valid()
    assert "privacy" in form.errors

    form = forms.SubscribeForm(
        {
            "newsletters": ["dude, walter"],
            "email": "*****@*****.**",
            "privacy": "false",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), )
    assert not form.is_valid()
    assert "privacy" in form.errors
示例#6
0
def test_source_url_clean(email_mock):
    # HTTP URL
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true",
            "source_url": "http://example.com",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["source_url"] == "http://example.com"

    # HTTPS URL
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true",
            "source_url": "https://example.com",
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["source_url"] == "https://example.com"

    # JavaScript URL
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true",
            "source_url": 'javascript:alert("dude!")',
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["source_url"] == ""
示例#7
0
def test_source_url_clean(email_mock):
    # HTTP URL
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
        'source_url': 'http://example.com',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['source_url'] == 'http://example.com'

    # HTTPS URL
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
        'source_url': 'https://example.com',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['source_url'] == 'https://example.com'

    # JavaScript URL
    form = forms.SubscribeForm({
        'newsletters': ['dude'],
        'email': '*****@*****.**',
        'privacy': 'true',
        'source_url': 'javascript:alert("dude!")',
    })
    form.fields['newsletters'].choices = (('dude', 'dude'), ('walter',
                                                             'walter'))
    assert form.is_valid(), form.errors
    assert form.cleaned_data['source_url'] == ''
示例#8
0
def test_email_validation(email_mock):
    email_mock.return_value = None
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true"
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert not form.is_valid()
    assert "email" in form.errors

    email_mock.return_value = "*****@*****.**"
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true"
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors

    # should result in whatever email process_email returns
    email_mock.return_value = "*****@*****.**"
    form = forms.SubscribeForm(
        {
            "newsletters": ["dude"],
            "email": "*****@*****.**",
            "privacy": "true"
        }, )
    form.fields["newsletters"].choices = (("dude", "dude"), ("walter",
                                                             "walter"))
    assert form.is_valid(), form.errors
    assert form.cleaned_data["email"] == "*****@*****.**"