Commit 14f239a9 authored by Aron Carroll's avatar Aron Carroll Committed by Randall Leeds

Add unit tests for the AccountManagement controller

Also moved the code out of the directives module and into it's own file.
parent 4980f0e7
......@@ -3,6 +3,7 @@ imports = [
'ngRoute'
'h.auth'
'h.controllers'
'h.controllers.AccountManagement'
'h.directives'
'h.directives.annotation'
'h.filters'
......
class AccountManagement
@inject = ['$scope, $filter', 'flash', 'profile', 'util']
constructor: ($scope, $filter, flash, profile, util) ->
persona_filter = $filter('persona')
onSuccess = (response) ->
# Fire flash messages.
for type, msgs of response.flash
flash(type, msgs)
onError = (form, data) ->
if 400 >= data.status < 500
util.applyValidationErrors(form, data.errors)
else
flash('error', 'Sorry, we were unable to perform your request')
$scope.deleteAccount = (form) ->
# If the password is correct, the account is deleted.
# The extension is then removed from the page.
# Confirmation of success is given.
return unless form.$valid
username = persona_filter $scope.session.persona
packet =
username: username
pwd: form.deleteaccountpassword.$modelValue
promise = profile.disable_user(packet)
promise.$promise.then(onSuccess, angular.bind(null, onError, form))
$scope.submit = (form) ->
# In the frontend change_email and change_password are two different
# forms. However, in the backend it is just one: edit_profile
return unless form.$valid
username = persona_filter $scope.session.persona
if form.$name is 'editProfile'
packet =
username: username
email: form.email.$modelValue
pwd: form.password.$modelValue
else
packet =
username: username
pwd: form.oldpassword.$modelValue
password: form.newpassword.$modelValue
promise = profile.edit_profile(packet)
promise.$promise.then(onSuccess, angular.bind(null, onError, form))
angular.module('h.controllers.AccountManagement', [])
.controller('AccountManagement', AccountManagement)
......@@ -373,79 +373,6 @@ match = ->
restrict: 'A'
require: 'ngModel'
confirmPasswordCheck = ['$resource', ($resource)->
link: (scope, elem, attr, ctrl) ->
return unless ctrl?
# Check password against server.
restrict: 'A'
require: 'ngModel'
]
accountManagement = ['$filter', 'flash', 'profile', 'util', ($filter, flash, profile, util) ->
link: (scope, elem, attr, ctrl) ->
scope.emailCheck = ->
# Checks to see if email is duplicate.
return
controller: ($scope, $filter) ->
persona_filter = $filter('persona')
onSuccess = (response) ->
# Fire flash messages.
for type, msgs of response.flash
flash(type, msgs)
onError = (form, data) ->
if 400 >= data.status < 500
util.applyValidationErrors(form, data.errors)
else
flash('error', 'Sorry, we were unable to perform your request')
$scope.confirmDelete = false
$scope.toggleConfirmDelete = ->
$scope.confirmDelete = !$scope.confirmDelete
$scope.deleteAccount = (form) ->
# If the password is correct, the account is deleted.
# The extension is then removed from the page.
# Confirmation of success is given.
return unless form.$valid
username = persona_filter $scope.session.persona
packet =
username: username
pwd: form.deleteaccountpassword.$modelValue
promise = profile.disable_user packet
promise.$promise.then(onSuccess, onError.bind(null, form))
$scope.submit = (form) ->
# In the frontend change_email and change_password are two different
# forms. However, in the backend it is just one: edit_profile
return unless form.$valid
username = persona_filter $scope.session.persona
if form.$name is 'edit_profile'
packet =
username: username
email: form.email.$modelValue
pwd: form.password.$modelValue
else
packet =
username: username
pwd: form.oldpassword.$modelValue
password: form.newpassword.$modelValue
promise = profile.edit_profile packet
promise.$promise.then(onSuccess, onError.bind(null, form))
# Jake's Note: there is an addional piece of UI I would like to implement. The basic idea being
# to give some visual indication that the changes they have made have been applied successfully.
# If they change their email, it would be nice to have an event (or something) to tell the front end that
# the request was successful.
restrict: 'C'
templateUrl: 'account_management.html'
]
angular.module('h.directives', ['ngSanitize', 'ngTagsInput'])
.directive('formValidate', formValidate)
......@@ -460,4 +387,3 @@ angular.module('h.directives', ['ngSanitize', 'ngTagsInput'])
.directive('repeatAnim', repeatAnim)
.directive('whenscrolled', whenscrolled)
.directive('match', match)
.directive('accountManagement', accountManagement)
assert = chai.assert
sinon.assert.expose assert, prefix: null
sandbox = sinon.sandbox.create()
describe 'h.controllers.AccountManagement', ->
$scope = null
fakeUtil = null
fakeFlash = null
fakeProfile = null
editProfilePromise = null
disableUserPromise = null
createController = null
beforeEach module ($provide, $filterProvider) ->
fakeProfile = {}
fakeFlash = sandbox.spy()
fakeUtil =
applyValidationErrors: sandbox.spy()
$filterProvider.register 'persona', ->
sandbox.stub().returns('STUBBED_PERSONA_FILTER')
$provide.value 'profile', fakeProfile
$provide.value 'flash', fakeFlash
$provide.value 'util', fakeUtil
return
beforeEach module('h.controllers.AccountManagement')
beforeEach inject ($rootScope, $q, $controller) ->
$scope = $rootScope.$new()
$scope.session = persona: 'egon@columbia.edu'
disableUserPromise = {then: sandbox.stub()}
editProfilePromise = {then: sandbox.stub()}
fakeProfile.edit_profile = sandbox.stub().returns($promise: editProfilePromise)
fakeProfile.disable_user = sandbox.stub().returns($promise: disableUserPromise)
createController = ->
$controller('AccountManagement', {$scope: $scope})
describe '.submit', ->
it 'updates the email address on the backend', ->
fakeForm =
$name: 'editProfile'
$valid: true
email: $modelValue: 'egon@columbia.edu'
password: $modelValue: 'paranormal'
controller = createController()
$scope.submit(fakeForm)
assert.calledWith(fakeProfile.edit_profile, {
username: 'STUBBED_PERSONA_FILTER'
email: 'egon@columbia.edu'
pwd: 'paranormal'
})
it 'updates the password on the backend', ->
fakeForm =
$name: 'changePassword'
$valid: true
oldpassword: $modelValue: 'gozer'
newpassword: $modelValue: 'paranormal'
controller = createController()
$scope.submit(fakeForm)
assert.calledWith(fakeProfile.edit_profile, {
username: 'STUBBED_PERSONA_FILTER'
pwd: 'gozer'
password: 'paranormal'
})
it 'displays a flash message on success', ->
fakeForm =
$name: 'changePassword'
$valid: true
oldpassword: $modelValue: 'gozer'
newpassword: $modelValue: 'paranormal'
# 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 'updates the error fields on bad response', ->
fakeForm =
$name: 'changePassword'
$valid: true
oldpassword: $modelValue: 'gozer'
newpassword: $modelValue: 'paranormal'
controller = createController()
$scope.submit(fakeForm)
# Resolve the request.
editProfilePromise.then.callArg 1,
status: 400
errors:
oldpassword: 'this is wrong'
assert.calledWith fakeUtil.applyValidationErrors, fakeForm,
oldpassword: 'this is wrong'
it 'displays a flash message if a server error occurs', ->
fakeForm =
$name: 'changePassword'
$valid: true
oldpassword: $modelValue: 'gozer'
newpassword: $modelValue: 'paranormal'
controller = createController()
$scope.submit(fakeForm)
# Resolve the request.
editProfilePromise.then.callArg 1,
status: 500
assert.calledWith(fakeFlash, 'error')
describe '.deleteAccount', ->
it 'disables the user account', ->
fakeForm =
$name: 'deleteAccount'
$valid: true
deleteaccountpassword: $modelValue: 'paranormal'
controller = createController()
$scope.deleteAccount(fakeForm)
assert.calledWith fakeProfile.disable_user,
username: 'STUBBED_PERSONA_FILTER'
pwd: 'paranormal'
it 'logs the user out of the application'
it 'updates the error fields on bad response', ->
fakeForm =
$name: 'deleteAccount'
$valid: true
deleteaccountpassword: $modelValue: 'paranormal'
controller = createController()
$scope.deleteAccount(fakeForm)
# Resolve the request.
disableUserPromise.then.callArg 1,
status: 400
errors:
oldpassword: 'this is wrong'
assert.calledWith fakeUtil.applyValidationErrors, fakeForm,
oldpassword: 'this is wrong'
it 'displays a flash message if a server error ocurrs', ->
fakeForm =
$name: 'deleteAccount'
$valid: true
deleteaccountpassword: $modelValue: 'paranormal'
controller = createController()
$scope.deleteAccount(fakeForm)
# Resolve the request.
disableUserPromise.then.callArg 1,
status: 500
assert.calledWith(fakeFlash, 'error')
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