Skip to content

Commit

Permalink
Add tests for ajax_form
Browse files Browse the repository at this point in the history
  • Loading branch information
nickstenning committed Jun 3, 2015
1 parent 4f2e1c5 commit 3756ebe
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions h/accounts/test/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from h.accounts import schemas
from h.accounts import views
from h.accounts.views import ajax_form
from h.accounts.views import validate_form
from h.accounts.views import RegisterController
from h.accounts.views import ProfileController
Expand Down Expand Up @@ -49,6 +50,104 @@ def configure(config):
FakeInvalid = namedtuple('FakeInvalid', 'children')


def test_ajax_form_handles_http_redirect_as_success():
request = DummyRequest()
result = ajax_form(request, httpexceptions.HTTPFound())

assert result['status'] == 'okay'
assert request.response.status_code == 200


def test_ajax_form_handles_http_error_as_error():
request = DummyRequest()
result = ajax_form(request, httpexceptions.HTTPInsufficientStorage())

assert result['status'] == 'failure'
assert result['reason'] == 'There was not enough space to save the resource'
assert request.response.status_code == 507


def test_ajax_form_sets_failure_status_on_errors():
request = DummyRequest()
result = ajax_form(request, {'errors': 'data'})

assert result['status'] == 'failure'


def test_ajax_form_sets_status_code_400_on_errors():
request = DummyRequest()
result = ajax_form(request, {'errors': 'data'})

assert request.response.status_code == 400


def test_ajax_form_sets_status_code_from_input_on_errors():
request = DummyRequest()
result = ajax_form(request, {'errors': 'data', 'code': 418})

assert request.response.status_code == 418


def test_ajax_form_aggregates_errors_on_success():
request = DummyRequest()
errors = [
{'name': 'Name is too weird'},
{'email': 'Email must be @hotmail.com'},
]
result = ajax_form(request, {'errors': errors})

assert result['errors'] == {'name': 'Name is too weird',
'email': 'Email must be @hotmail.com'}


def test_ajax_form_passes_data_through_on_success():
request = DummyRequest()
result = ajax_form(request, {'some': 'data', 'no': 'errors'})

assert result['some'] == 'data'
assert result['no'] == 'errors'
assert request.response.status_code == 200


def test_ajax_form_ignores_status_code_from_input_on_success():
request = DummyRequest()
result = ajax_form(request, {'some': 'data', 'code': 418})

assert request.response.status_code == 200


def test_ajax_form_includes_flash_data(pop_flash):
request = DummyRequest()
pop_flash.return_value = {'success': ['Well done!']}
result = ajax_form(request, {'some': 'data'})

assert result['flash'] == {'success': ['Well done!']}


def test_ajax_form_sets_status_code_400_on_flash_error(pop_flash):
request = DummyRequest()
pop_flash.return_value = {'error': ['I asplode!']}
result = ajax_form(request, {'some': 'data'})

assert request.response.status_code == 400


def test_ajax_form_sets_status_failure_on_flash_error(pop_flash):
request = DummyRequest()
pop_flash.return_value = {'error': ['I asplode!']}
result = ajax_form(request, {'some': 'data'})

assert result['status'] == 'failure'


def test_ajax_form_sets_reason_on_flash_error(pop_flash):
request = DummyRequest()
pop_flash.return_value = {'error': ['I asplode!']}
result = ajax_form(request, {'some': 'data'})

assert result['reason'] == 'I asplode!'


def test_validate_form_passes_data_to_validate():
idata = {}
form = MagicMock()
Expand Down Expand Up @@ -264,6 +363,13 @@ def test_registration_does_not_autologin(config, authn_policy):
assert not authn_policy.remember.called


@pytest.fixture
def pop_flash(request):
patcher = patch('h.accounts.views.session.pop_flash', autospec=True)
request.addfinalizer(patcher.stop)
return patcher.start()


@pytest.fixture
def subscriptions_model(request):
patcher = patch('h.accounts.views.Subscriptions', autospec=True)
Expand Down

0 comments on commit 3756ebe

Please sign in to comment.