This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from AusDTO/sharing-is-caring
Put more reusable code into utils repo from buyer-frontend
- Loading branch information
Showing
6 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<form> | ||
{{ form.csrf_token }} | ||
{{ form.stripped_field }} | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from dmutils.forms import DmForm, email_regex, FakeCsrf, render_template_with_csrf, StripWhitespaceStringField | ||
|
||
from helpers import BaseApplicationTest | ||
|
||
|
||
class TestForm(DmForm): | ||
stripped_field = StripWhitespaceStringField('Stripped', id='stripped_field') | ||
|
||
|
||
class TestFormHandling(BaseApplicationTest): | ||
|
||
def test_whitespace_stripping(self): | ||
with self.flask.app_context(): | ||
form = TestForm(stripped_field=' asdf ', csrf_token=FakeCsrf.valid_token) | ||
assert form.validate() | ||
assert form.stripped_field.data == 'asdf' | ||
|
||
def test_csrf_protection(self): | ||
with self.flask.app_context(): | ||
form = TestForm(stripped_field='asdf', csrf_token='bad') | ||
assert not form.validate() | ||
assert 'csrf_token' in form.errors | ||
|
||
def test_does_not_crash_on_missing_csrf_token(self): | ||
with self.flask.app_context(): | ||
form = TestForm(stripped_field='asdf') | ||
assert not form.validate() | ||
assert 'csrf_token' in form.errors | ||
|
||
def test_render_template_with_csrf(self): | ||
with self.flask.app_context(): | ||
response, status_code = render_template_with_csrf('test_form.html', 123) | ||
assert status_code == 123 | ||
assert response.cache_control.private | ||
assert response.cache_control.max_age == self.flask.config['CSRF_TIME_LIMIT'] | ||
assert FakeCsrf.valid_token in response.data | ||
|
||
|
||
def test_valid_email_formats(): | ||
cases = [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]', | ||
] | ||
for address in cases: | ||
assert email_regex.regex.match(address) is not None, address | ||
|
||
|
||
def test_invalid_email_formats(): | ||
cases = [ | ||
'', | ||
'bad', | ||
'bad@@example.com', | ||
'bad @example.com', | ||
'[email protected]', | ||
'bad.example.com', | ||
'@', | ||
'@example.com', | ||
'bad@', | ||
'[email protected],[email protected]', | ||
'[email protected] [email protected]', | ||
'[email protected],other.example.com', | ||
] | ||
for address in cases: | ||
assert email_regex.regex.match(address) is None, address |