Skip to content

Commit

Permalink
Add logout functionality (#76)
Browse files Browse the repository at this point in the history
- Update Authentication.spec
- Update memoryFactory.spec
- Update navbar.spec

- Notes
  - Logout is implemented on the navbar directive
  • Loading branch information
MatiasComercio committed Feb 2, 2017
1 parent 016baba commit 90d75e4
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
10 changes: 8 additions & 2 deletions app/scripts/directives/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

define(['paw', 'services/navDataService', 'services/Paths'],
function(paw) {
paw.directive('xnavbar', ['navDataService', 'Paths',
function(navDataService, Paths, NavbarService) {
paw.directive('xnavbar', ['navDataService', 'Paths', 'Authentication',
function(navDataService, Paths, Authentication) {
function controller() {
var _this = this;

Expand Down Expand Up @@ -37,6 +37,12 @@ function(paw) {
scope.sidebarOpen = function() {
navDataService.set('sidebarOpen', !navDataService.get('sidebarOpen'));
};

scope.logout = function() {
navDataService.remove('user');
Authentication.logout();
Paths.get().login().go();
};
}
};
}]);
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/services/Authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ define([], function() {
return authenticationService.getToken() !== undefined;
};

authenticationService.logout = function() {
return $cookies.remove(tokenKey);
};

return authenticationService;
};
});
5 changes: 5 additions & 0 deletions app/scripts/services/memoryFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ define(['paw'], function(paw) {

get: function(key) {
return this.memory[key];
},

remove: function(key) {
this.memory[key] = undefined;
this.notifyObservers(key);
}
};
};
Expand Down
32 changes: 31 additions & 1 deletion app/specs/directives/navbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ define([
'directives/navbar',
'navbar-template',
'api-responses',
'services/navDataService'], function() {
'services/navDataService',
'services/Authentication'], function() {
describe('NavbarDirective', function() {
beforeEach(module('paw'));
beforeEach(module('directive-templates'));
Expand Down Expand Up @@ -120,5 +121,34 @@ define([
});
});
});

// Notice that the logout button is on the top-nav, and this means
// it is only available when the user is logged in, as tested before
describe('when clicking the logout button', function() {
var navDataServiceService, AuthenticationService, $location;

// navDataService should be already required by the directive
beforeEach(inject(function(navDataService, Authentication, _$location_) {
navDataServiceService = navDataService;
AuthenticationService = Authentication;
$location = _$location_;
spyOn(AuthenticationService, 'logout');
spyOn(navDataServiceService, 'remove');
spyOn($location, 'path');
navbar.find("[ng-click='logout()']").triggerHandler('click');
}));

it('calls the Authentication service to logout', function() {
expect(AuthenticationService.logout).toHaveBeenCalled();
});

it('removes the user from the navDataService', function() {
expect(navDataServiceService.remove).toHaveBeenCalledWith('user');
});

it('redirects to the login page', inject(function(Paths) {
expect($location.path).toHaveBeenCalledWith(Paths.get().login().absolutePath());
}));
});
});
});
11 changes: 11 additions & 0 deletions app/specs/services/Authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,16 @@ function() {
expect(header).toEqual(expectedHeader);
});
});

describe('when logging out', function() {
beforeEach(function() {
AuthenticationService.setToken(expectedToken);
AuthenticationService.logout();
});

it('removes the token', function () {
expect(AuthenticationService.getToken()).toBeUndefined();
});
});
});
});
43 changes: 43 additions & 0 deletions app/specs/services/memoryFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,48 @@ define(['paw', 'angular-mocks'], function() {
expect(callbackValue).toBeUndefined();
});
});


describe('when getting a value with the same key', function() {
beforeEach(inject(function(memoryFactory) {
memory.set(validKey, expectedValue);
}));

it('returns the expected value', function() {
expect(memory.get(validKey)).toEqual(expectedValue);
});
});

describe('when getting a value with a different key', function() {
beforeEach(inject(function(memoryFactory) {
memory.set(invalidKey, expectedValue);
}));

it('returns undefined', function() {
expect(memory.get(validKey)).toBeUndefined();
});
});

describe('when removing a value with a valid key', function() {
beforeEach(inject(function(memoryFactory) {
memory.set(validKey, expectedValue);
memory.remove(validKey);
}));

it('does not remove the element', function() {
expect(memory.get(validKey)).toBeUndefined();
});
});

describe('when removing a value with a different key', function() {
beforeEach(inject(function(memoryFactory) {
memory.set(validKey, expectedValue);
memory.remove(invalidKey);
}));

it('does not remove the element', function() {
expect(memory.get(validKey)).toEqual(expectedValue);
});
});
});
});
4 changes: 4 additions & 0 deletions app/styles/modules/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ hr {
color: $black;
}
}

.sym-button {
cursor: pointer;
}
2 changes: 1 addition & 1 deletion app/views/directives/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<a ng-href='{{ controller.user.profileUrl }}'><i class='fa fa-fw fa-user'></i> {{ 'i18nMyProfile' | translate }}</a>
</li>
<li class='dropdown-option'>
<a ng-href='{{ paths.logout }}'><i class="fa fa-fw fa-power-off"></i> {{ 'i18nLogout' | translate }}</a>
<a class='sym-button no-select' ng-click='logout()'><i class="fa fa-fw fa-power-off"></i> {{ 'i18nLogout' | translate }}</a>
</li>
</ul>
</li>
Expand Down

0 comments on commit 90d75e4

Please sign in to comment.