Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Commit

Permalink
test(ResetTwoFactorToken): added tests and fixed missing dependency i…
Browse files Browse the repository at this point in the history
…n alerts service.
  • Loading branch information
Sjors committed Jan 20, 2016
1 parent b7e2126 commit b2419f1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 21 deletions.
11 changes: 2 additions & 9 deletions assets/js/controllers/resetTwoFactorToken.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ function ResetTwoFactorTokenCtrl($scope, WalletNetwork, $stateParams, $state, Al

$scope.checkingToken = false

$translate(['SUCCESS']).then(translations => {
$state.go("public.login-uid", {uid: obj.guid}).then(() =>{
$rootScope.$emit('showNotification', {
type: 'verified-email',
icon: 'ti-email',
heading: translations.SUCCESS,
msg: obj.message
});
});
$state.go("public.login-uid", {uid: obj.guid}).then(() =>{
Alerts.displayResetTwoFactor(obj.message)
});
}

Expand Down
6 changes: 2 additions & 4 deletions assets/js/controllers/verifyEmail.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ angular

function VerifyEmailCtrl($scope, WalletNetwork, $stateParams, $state, Alerts, $translate, $rootScope) {
const success = (uid) => {
$translate(['SUCCESS', 'EMAIL_VERIFIED_SUCCESS']).then(translations => {
$state.go("public.login-uid", {uid: uid}).then(() =>{
Alerts.displayVerifiedEmail()
});
$state.go("public.login-uid", {uid: uid}).then(() =>{
Alerts.displayVerifiedEmail()
});
}

Expand Down
30 changes: 22 additions & 8 deletions assets/js/services/alerts.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ angular
.module('walletApp')
.factory('Alerts', Alerts);

Alerts.$inject = ['$timeout', '$rootScope'];
Alerts.$inject = ['$timeout', '$rootScope', '$translate'];

function Alerts($timeout, $rootScope) {
function Alerts($timeout, $rootScope, $translate) {
const service = {
alerts : [],
close : close,
Expand All @@ -14,7 +14,8 @@ function Alerts($timeout, $rootScope) {
displayWarning : display.bind(null, ''),
displayError : display.bind(null, 'danger'),
displayReceivedBitcoin : display.bind(null, 'received-bitcoin'),
displayVerifiedEmail : displayVerifiedEmail
displayVerifiedEmail : displayVerifiedEmail,
displayResetTwoFactor : displayResetTwoFactor
};

function close(alert, context=service.alerts) {
Expand All @@ -36,11 +37,24 @@ function Alerts($timeout, $rootScope) {
}

function displayVerifiedEmail() {
$rootScope.$emit('showNotification', {
type: 'verified-email',
icon: 'ti-email',
heading: translations.SUCCESS,
msg: translations.EMAIL_VERIFIED_SUCCESS
$translate(['SUCCESS', 'EMAIL_VERIFIED_SUCCESS']).then(translations => {
$rootScope.$emit('showNotification', {
type: 'verified-email',
icon: 'ti-email',
heading: translations.SUCCESS,
msg: translations.EMAIL_VERIFIED_SUCCESS
});
});
}

function displayResetTwoFactor(message) {
$translate(['SUCCESS']).then(translations => {
$rootScope.$emit('showNotification', {
type: 'verified-email',
icon: 'ti-email',
heading: translations.SUCCESS,
msg: message
});
});
}

Expand Down
78 changes: 78 additions & 0 deletions tests/controllers/reset_two_factor_token_ctrl_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
ddescribe "ResetTwoFactorTokenController", ->
scope = undefined

beforeEach angular.mock.module("walletApp")

beforeEach ->
angular.mock.inject ($injector, $rootScope, $controller) ->
WalletNetwork = $injector.get("WalletNetwork")
$state = $injector.get("$state") # This is a mock
Alerts = $injector.get("Alerts")

spyOn(WalletNetwork, "resetTwoFactorToken").and.callFake((token)->
{
then: (callback) ->
if token == "token"
callback({success: true, guid: "1234"})
{
catch: (callback) ->
if token == "wrong-token"
callback()
{
}
}
}
)

spyOn($state, "go").and.callThrough()

spyOn(Alerts, "displayError").and.callFake(() ->)
spyOn(Alerts, "displayResetTwoFactor").and.callFake(() ->)

return

return

describe "with token", ->
beforeEach ->
angular.mock.inject ($controller, $rootScope) ->

scope = $rootScope.$new()

$controller "ResetTwoFactorTokenCtrl",
$scope: scope,
$stateParams: {token: "token"}

it "should show call WalletNetwork.resetTwoFactorToken()", inject((WalletNetwork) ->
expect(WalletNetwork.resetTwoFactorToken).toHaveBeenCalled()
)

it "should pass the token parameter along", inject((WalletNetwork) ->
expect(WalletNetwork.resetTwoFactorToken).toHaveBeenCalledWith("token")
)

it "should redirect to the login page", inject(($state)->
expect($state.go).toHaveBeenCalledWith("public.login-uid", { uid : '1234' })
)

it "should request a modal success message", inject((Alerts) ->
expect(Alerts.displayResetTwoFactor).toHaveBeenCalled()
)

describe "with wrong token", ->
beforeEach ->
angular.mock.inject ($controller, $rootScope) ->

scope = $rootScope.$new()

$controller "ResetTwoFactorTokenCtrl",
$scope: scope,
$stateParams: {token: "wrong-token"}

it "should display an error message", inject((Alerts)->
expect(Alerts.displayError).toHaveBeenCalled()
)

it "should redirect to the login page", inject(($state)->
expect($state.go).toHaveBeenCalledWith("public.login-no-uid")
)

0 comments on commit b2419f1

Please sign in to comment.