Commit 7bb363b4 authored by Aron Carroll's avatar Aron Carroll Committed by Randall Leeds

Hook up response error messages to the account form

parent cb79e75c
......@@ -18,9 +18,12 @@ class AccountManagement
identity.logout()
onSuccess(form, response)
onError = (form, data) ->
if 400 >= data.status < 500
util.applyValidationErrors(form, data.errors)
onError = (form, response) ->
if response.status >= 400 and response.status < 500
util.applyValidationErrors(form, response.data.errors)
else
if response.data.flash
flash(type, msgs) for own type, msgs of response.data.flash
else
flash('error', 'Sorry, we were unable to perform your request')
......
......@@ -44,14 +44,17 @@ describe 'h.controllers.AccountManagement', ->
$controller('AccountManagement', {$scope: $scope})
describe '.submit', ->
it 'updates the password on the backend', ->
fakeForm =
createFakeForm = (overrides={}) ->
defaults =
$name: 'changePasswordForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'gozer'
password: $modelValue: 'paranormal'
angular.extend(defaults, overrides)
it 'updates the password on the backend', ->
fakeForm = createFakeForm()
controller = createController()
$scope.submit(fakeForm)
......@@ -61,37 +64,10 @@ describe 'h.controllers.AccountManagement', ->
password: 'paranormal'
})
it 'displays a flash message on success', ->
fakeForm =
$name: 'changePasswordForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'gozer'
password: $modelValue: 'paranormal'
# Resolve the request.
editProfilePromise.then.yields(flash: {
success: ['Your profile has been updated.']
})
it 'clears the fields', ->
controller = createController()
$scope.submit(fakeForm)
assert.calledWith(fakeFlash, 'success', [
'Your profile has been updated.'
])
it 'clears the password field', ->
controller = createController()
$scope.changePassword = {pwd: 'password', password: 'password'}
fakeForm =
$name: 'changePasswordForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'gozer'
password: $modelValue: 'paranormal'
fakeForm = createFakeForm()
# Resolve the request.
editProfilePromise.then.yields(flash: {
......@@ -102,50 +78,72 @@ describe 'h.controllers.AccountManagement', ->
assert.deepEqual($scope.changePassword, {})
it 'updates the error fields on bad response', ->
fakeForm =
$name: 'changePasswordForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'gozer'
password: $modelValue: 'paranormal'
fakeForm = createFakeForm()
controller = createController()
$scope.submit(fakeForm)
# Resolve the request.
editProfilePromise.then.callArg 1,
status: 400
data:
errors:
pwd: 'this is wrong'
assert.calledWith fakeUtil.applyValidationErrors, fakeForm,
pwd: 'this is wrong'
it 'displays a flash message on success', ->
fakeForm = createFakeForm()
# Resolve the request.
editProfilePromise.then.yields(flash: {
success: ['Your profile has been updated.']
})
controller = createController()
$scope.submit(fakeForm)
assert.calledWith(fakeFlash, 'success', [
'Your profile has been updated.'
])
it 'displays a flash message if a server error occurs', ->
fakeForm =
$name: 'changePasswordForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'gozer'
password: $modelValue: 'paranormal'
fakeForm = createFakeForm()
controller = createController()
$scope.submit(fakeForm)
# Resolve the request.
editProfilePromise.then.callArg 1,
status: 500
data:
flash:
error: ['Something bad happened']
assert.calledWith(fakeFlash, 'error', ['Something bad happened'])
it 'displays a fallback flash message if none are present', ->
fakeForm = createFakeForm()
controller = createController()
$scope.submit(fakeForm)
# Resolve the request.
editProfilePromise.then.callArg 1,
status: 500
data: {}
assert.calledWith(fakeFlash, 'error')
assert.calledWith(fakeFlash, 'error', 'Sorry, we were unable to perform your request')
describe '.delete', ->
it 'disables the user account', ->
fakeForm =
createFakeForm = (overrides={}) ->
defaults =
$name: 'deleteAccountForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'paranormal'
angular.extend(defaults, overrides)
it 'disables the user account', ->
fakeForm = createFakeForm()
controller = createController()
$scope.delete(fakeForm)
......@@ -154,12 +152,7 @@ describe 'h.controllers.AccountManagement', ->
pwd: 'paranormal'
it 'logs the user out of the application', ->
fakeForm =
$name: 'deleteAccountForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'paranormal'
fakeForm = createFakeForm()
controller = createController()
$scope.delete(fakeForm)
......@@ -172,12 +165,7 @@ describe 'h.controllers.AccountManagement', ->
it 'clears the password field', ->
controller = createController()
fakeForm =
$name: 'deleteAccountForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'paranormal'
fakeForm = createFakeForm()
$scope.deleteAccount = {pwd: ''}
$scope.delete(fakeForm)
disableUserPromise.then.callArg 0,
......@@ -186,37 +174,42 @@ describe 'h.controllers.AccountManagement', ->
assert.deepEqual($scope.deleteAccount, {})
it 'updates the error fields on bad response', ->
fakeForm =
$name: 'deleteAccountForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'paranormal'
fakeForm = createFakeForm()
controller = createController()
$scope.delete(fakeForm)
# Resolve the request.
disableUserPromise.then.callArg 1,
status: 400
data:
errors:
pwd: 'this is wrong'
assert.calledWith fakeUtil.applyValidationErrors, fakeForm,
pwd: 'this is wrong'
it 'displays a flash message if a server error ocurrs', ->
fakeForm =
$name: 'deleteAccountForm'
$valid: true
$setPristine: sandbox.spy()
pwd: $modelValue: 'paranormal'
it 'displays a flash message if a server error occurs', ->
fakeForm = createFakeForm()
controller = createController()
$scope.delete(fakeForm)
# Resolve the request.
disableUserPromise.then.callArg 1,
status: 500
data:
flash:
error: ['Something bad happened']
assert.calledWith(fakeFlash, 'error', ['Something bad happened'])
assert.calledWith(fakeFlash, 'error')
it 'displays a fallback flash message if none are present', ->
fakeForm = createFakeForm()
controller = createController()
$scope.delete(fakeForm)
# Resolve the request.
disableUserPromise.then.callArg 1,
status: 500
data: {}
assert.calledWith(fakeFlash, 'error', 'Sorry, we were unable to perform your request')
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