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'] == '*****@*****.**'
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
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
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
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
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"] == ""
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'] == ''
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"] == "*****@*****.**"