Commit 48a182a4 authored by Randall Leeds's avatar Randall Leeds

Merge pull request #2103 from hypothesis/1755-handle-internal-server-error-in-registration-form

Handle internal server errors in the registration form
parents 298ea35d eff949c0
...@@ -18,7 +18,11 @@ class AuthController ...@@ -18,7 +18,11 @@ class AuthController
$scope.form?.$setPristine() $scope.form?.$setPristine()
failure = (form, response) -> failure = (form, response) ->
try
{errors, reason} = response.data {errors, reason} = response.data
catch
reason = "Oops, something went wrong on the server. Please try again
later!"
formRespond(form, errors, reason) formRespond(form, errors, reason)
this.submit = (form) -> this.submit = (form) ->
......
...@@ -26,6 +26,7 @@ describe 'h:AuthController', -> ...@@ -26,6 +26,7 @@ describe 'h:AuthController', ->
$timeout = null $timeout = null
auth = null auth = null
session = null session = null
$controller = null
before -> before ->
angular.module('h', []) angular.module('h', [])
...@@ -41,9 +42,10 @@ describe 'h:AuthController', -> ...@@ -41,9 +42,10 @@ describe 'h:AuthController', ->
$provide.value 'formRespond', mockFormRespond $provide.value 'formRespond', mockFormRespond
return return
beforeEach inject ($controller, $rootScope, _$timeout_, _session_) -> beforeEach inject (_$controller_, $rootScope, _$timeout_, _session_) ->
$scope = $rootScope.$new() $scope = $rootScope.$new()
$timeout = _$timeout_ $timeout = _$timeout_
$controller = _$controller_
auth = $controller 'AuthController', {$scope} auth = $controller 'AuthController', {$scope}
session = _session_ session = _session_
sandbox.spy session, 'login' sandbox.spy session, 'login'
...@@ -85,6 +87,44 @@ describe 'h:AuthController', -> ...@@ -85,6 +87,44 @@ describe 'h:AuthController', ->
{username: 'taken'}, {username: 'taken'},
'registration error' 'registration error'
it 'should apply reason-only validation errors from the server', ->
# Make a mock session that returns an error response with a "reason" but
# no "errors" in the JSON object.
reason = 'Argh, crashed! :|'
myMockSession = new MockSession()
myMockSession.register = (data, callback, errback) ->
errback({data: {reason: reason}})
$promise: {finally: sandbox.stub()}
# Get an AuthController object with our mock session.
authCtrl = $controller(
'AuthController', {$scope:$scope, session:myMockSession})
form = {$name: 'register', $valid: true}
authCtrl.submit(form)
assert.calledWith(mockFormRespond, form, undefined, reason)
it 'should handle invalid error responses from the server', ->
# A mock session that returns an error that isn't a valid JSON object
# in the form that the frontend expects. This happens if there's an
# uncaught exception on the server.
myMockSession = new MockSession()
myMockSession.register = (data, callback, errback) ->
errback('Oh no!!')
$promise: {finally: sandbox.stub()}
authCtrl = $controller(
'AuthController', {$scope:$scope, session:myMockSession})
form = {$name: 'register', $valid: true}
authCtrl.submit(form)
assert.calledWith(mockFormRespond, form, undefined,
"Oops, something went wrong on the server. Please try again later!")
it 'should emit an auth event once authenticated', -> it 'should emit an auth event once authenticated', ->
form = form =
$name: 'login' $name: 'login'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment