Summary Table
| Categories |
Total Count |
| PII |
0 |
| URL |
0 |
| DNS |
0 |
| EKL |
0 |
| IP |
0 |
| PORT |
0 |
| VsID |
0 |
| CF |
0 |
| AI |
0 |
| VPD |
0 |
| PL |
0 |
| Other |
0 |
File Content
define(['authentication_service'], function () {
'use strict';
describe('Authentication Service --', function () {
var $scope,
$http,
$httpBackend,
$q,
$window,
$location,
$injector,
service,
localResourceDirectory,
localResourceDirectoryServiceMock,
localDeferred,
pageServiceMock,
userSessionMock,
windowOpenSpy;
beforeEach(function() {
module('angularTemplateApp');
localResourceDirectoryServiceMock = jasmine.createSpyObj('localResourceDirectoryService', ['fetch']);
pageServiceMock = { appUri:'http://test.com/va-tool-set/' };
userSessionMock = jasmine.createSpyObj('userSession', ['fetch']);
localResourceDirectory = {
'login': '/authorize',
'logout': '/logout',
'rest-v1': '/rest/v1',
'oauth-info': '/oauth/token',
'oauth-token': '/oauth/info'
};
module(function($provide) {
$provide.value('localResourceDirectoryService', localResourceDirectoryServiceMock);
$provide.value('pageService', pageServiceMock);
$provide.value('userSession', userSessionMock);
});
inject(function($rootScope, _$http_, _$httpBackend_, _$q_, _$window_, _$location_, _$injector_, authenticationService) {
$scope = $rootScope;
$http = _$http_;
$httpBackend = _$httpBackend_;
$q = _$q_;
$window = _$window_;
$location = _$location_;
$injector = _$injector_;
service = authenticationService;
var localResourceDeferred = $q.defer();
localResourceDeferred.resolve(localResourceDirectory);
localResourceDirectoryServiceMock.fetch.andReturn(localResourceDeferred.promise);
$httpBackend.whenGET('resources.json').respond(localResourceDirectory);
$httpBackend.whenGET(localResourceDirectory['oauth-info']).respond('200', {});
});
});
/* GENERAL FLOW OF AUTH SERVICE USAGE */
// From splash page, beginLogon() --> service.authenticate() --> service.checkAuthStatus()
// checkAuthStatus: calls validateToken()
// - validateToken: returns 200 response if valid, calls various other authService functions related to tokens and fetches resourceDirectory
// -- sets http.defaults header with currentToken
// -- then does a GET on link mapped to resourceDirectory item 'mhpuser' -- the actual authentication, if its promise resolves we're good to go
describe('the service', function () {
beforeEach(function () {
windowOpenSpy = spyOn(window, 'open');
sessionStorage.setItem('token', null);
localDeferred = $q.defer();
localDeferred.resolve('success'); // general resolved/success promise
});
it('should have some commonly used methods defined', function () {
expect(service.readLocalResourceDirectory).toBeDefined();
// used in other parts of the app
expect(service.authenticate).toBeDefined();
expect(service.checkForAuthCode).toBeDefined();
expect(service.getNewToken).toBeDefined();
expect(service.checkAuthStatus).toBeDefined();
expect(service.isAuthenticated).toBeDefined();
expect(service.wipeSessionData).toBeDefined();
expect(service.logoutRedirectToLaunchpad).toBeDefined();
});
it('should store session token', function () {
expect(sessionStorage.getItem('token')).toEqual(JSON.stringify(null));
service.storeSessionToken('fakeToken12345');
expect(sessionStorage.getItem('token')).toEqual(JSON.stringify('fakeToken12345'));
});
it('should be able to delete cookie', function () {
var testCookie = 'fakeCookie=12345';
document.cookie = testCookie;
expect(document.cookie).toEqual(testCookie);
service.deleteCookie('fakeCookie');
expect(document.cookie).toEqual('');
});
it('should wipe session data', function () {
spyOn(service, 'deleteCookie').andCallThrough();
service.wipeSessionData();
$scope.$digest();
expect(service.deleteCookie).toHaveBeenCalled();
});
it('should login with redirect', function () {
service.authenticate();
$scope.$digest();
expect(window.open).toHaveBeenCalled();
});
// call to checkAuthStatus is in router.js
it('should logout with redirect', function () {
spyOn(service, 'wipeSessionData').andReturn(localDeferred.promise);
service.logoutRedirectToLaunchpad();
$scope.$digest();
expect(window.open).toHaveBeenCalled();
expect(service.wipeSessionData).toHaveBeenCalled();
});
});
});
});