describe('Authentication service', function() {
var authenticationService;
var $q;
var auth;
var store;
var location;
var jwtHelper;
var cookies;
beforeEach(module('authenticationModule'));
beforeEach(module(function($provide) {
auth = {
isAuthenticated: '',
authenticate: jasmine.createSpy(),
signin: jasmine.createSpy(),
signout: jasmine.createSpy(),
};
$provide.value('auth', auth);
store = {
get: jasmine.createSpy(),
set: jasmine.createSpy(),
remove: jasmine.createSpy(),
};
$provide.value('store', store);
location = {
path: jasmine.createSpy(),
url: jasmine.createSpy(),
};
$provide.value('$location', location);
jwtHelper = {
isTokenExpired: jasmine.createSpy(),
};
$provide.value('jwtHelper', jwtHelper);
cookies = {
get: jasmine.createSpy(),
set: jasmine.createSpy(),
remove: jasmine.createSpy(),
};
$provide.value('$cookies', cookies);
environmentService = {
getEnvironment: jasmine.createSpy(),
};
$provide.value('environmentService', environmentService);
}));
beforeEach(inject(function(_authenticationService_, _$q_) {
authenticationService = _authenticationService_;
$q = _$q_;
}));
it('should determine that the user is unauthenticated if no token is present', function() {
store.get.and.callFake(
function() {
return null;
});
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeFalsey;
expect(store.get).toHaveBeenCalledWith('smc_auth_token');
});
});
it('should determine that the user is unauthenticated if the token is expired', function() {
store.get.and.callFake(
function() {
return 'myToken';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return true;
});
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeFalsey;
expect(jwtHelper.isTokenExpired).toHaveBeenCalledWith('myToken');
});
});
it('should determine that the user is authenticated if request is authenticated', function() {
store.get.and.callFake(
function() {
return 'myToken';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return false;
});
auth.isAuthenticated = true;
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeTruthy;
});
});
it('should determine that the user is authenticated if token auth succeeds', function() {
store.get.and.callFake(
function() {
return 'myToken';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return false;
});
auth.isAuthenticated = false;
auth.authenticate.and.callFake(function() {return $q.when(true);});
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeTruthy;
expect(auth.authenticate).toHaveBeenCalledWith('myToken', 'myToken');
});
});
it('should return the authentication information if the user is logged in', function() {
store.get.and.callFake(
function(key) {
return key + '_value';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return false;
});
auth.isAuthenticated = true;
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeTruthy;
expect(result.profile).toEqual('profile_value');
expect(result.token).toEqual('token_value');
});
});
it('should determine that the user is unauthenticated if token authentication fails', function() {
store.get.and.callFake(
function() {
return 'myToken';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return false;
});
auth.isAuthenticated = false;
auth.authenticate.and.callFake(function() {return $q.when(false);});
authenticationService.getIsLoggedIn().then(function(result) {
expect(result).toBeFalsey;
expect(auth.authenticate).toHaveBeenCalledWith('myToken', 'myToken');
});
});
it('should sign out and clear the tokens when logging out', function() {
authenticationService.logOut();
expect(auth.signout).toHaveBeenCalled();
expect(store.remove).toHaveBeenCalledWith('smc_auth_token');
expect(store.remove).toHaveBeenCalledWith('smc_auth_profile');
});
it('should redirect to the home page when logging out', function() {
authenticationService.logOut();
expect(location.path).toHaveBeenCalledWith('/');
});
it('should call any callbacks when logging out', function() {
var foo = 0;
authenticationService.addAuthChangeCallback(function() {
foo = 1;
});
authenticationService.logOut();
expect(foo).toEqual(1);
});
it('should continue instead of re-logging in if the user is already authenticated', function() {
store.get.and.callFake(
function() {
return 'myToken';
});
jwtHelper.isTokenExpired.and.callFake(
function() {
return false;
});
auth.isAuthenticated = true;
authenticationService.authenticateAndRedirect().then(function(status) {
expect(status).toBeTruthy();
});
});
it('should stop and redirect to the login page if the user is not authenticated', function() {
store.get.and.callFake(
function() {
return null;
});
authenticationService.authenticateAndRedirect().then(function(status) {
expect(status).toBeUndefined();
expect(auth.signin).toHaveBeenCalled();
});
});
it('should call the authentication provider when logging in', function() {
authenticationService.logIn();
expect(auth.signin).toHaveBeenCalledWith({disableSignupAction: true,});
});
it('should redirect to an error page if login fails', function() {
authenticationService.processLoginFailure();
expect(location.path).toHaveBeenCalledWith('/error');
});
it('should save the auth tokens when login is successful', function() {
authenticationService.processLogin('profile1', 'token2');
expect(store.set).toHaveBeenCalledWith('smc_auth_profile', 'profile1');
expect(store.set).toHaveBeenCalledWith('smc_auth_token', 'token2');
});
it('should redirect, if specified, and then clear the cookie', function() {
cookies.get.and.callFake(function() {
return '/foo';
});
authenticationService.processLogin();
expect(location.url).toHaveBeenCalledWith('/foo');
expect(cookies.remove).toHaveBeenCalledWith('returnTo');
});
it('should use the prod authentication app in production', function() {
environmentService.getEnvironment.and.callFake(function() {
return 'PRD';
});
expect(authenticationService.getClientId()).toEqual('0lqvllBTphFILz1Awjlxo5stP8C6pMLn');
});
it('should use the non-prod authentication app outside production', function() {
environmentService.getEnvironment.and.callFake(function() {
return 'TST';
});
expect(authenticationService.getClientId()).toEqual('fuMX6JViIhikzymPiXEjsHh8dhplEirc');
});
});