I provide three essential services. The RequestAuthorizationService reaches out to the OAuthAuthorizationDataService for token-based authorization credentials, which are then securely stored in session storage by the sessionStorageManagerService.
The focus of the test lies on verifying the invocation of the sessionStorageManagerService.set() method. I am having trouble ensuring that this method is indeed being called.
Despite reviewing numerous resources, I am unable to pinpoint the issue when trying to confirm the method call within the 'then' block. Every time I run the test, it claims that the method was not executed. Any insights or guidance on this matter would be greatly appreciated!
Below you can find the corresponding code snippets:
RequestAuthorizationService
(function () {
'use strict';
var serviceId = 'requestAuthorizationService';
angular
.module('dilib')
.service(serviceId, requestAuthorizationService);
requestAuthorizationService.$inject = ['$q', 'Restangular', 'cryptoService', 'OAuthAuthenticationDataService', 'sessionStorageManagerService'];
function requestAuthorizationService($q, Restangular, cryptoService, OAuthAuthenticationDataService, sessionStorageManagerService) {
//API
var service = {
requestAuthorization: requestAuthorization,
}
return service;
/////////////////////////////////////////////////////////////////////
function requestAuthorization(user) {
var defer = $q.defer();
var userLocal = undefined;
if (typeof user === "undefined") {
userLocal = {
username: 'visitor',
password: cryptoService.getMD5('qwe123')
};
} else {
userLocal = user;
}
OAuthAuthenticationDataService.authenticate(userLocal).then(function (result) {
//To be tested
sessionStorageManagerService.set('authorizationData',
{
token: result.access_token,
username: user.username
});
defer.resolve(userdata.username);
}, function (msg) {
defer.reject(msg);
});
return defer.promise;
}
}
})();
OAuthAuthenticationDataService
(function () {
'use strict';
var serviceId = 'OAuthAuthenticationDataService';
angular
.module('dilib')
.service(serviceId, OAuthAuthenticationDataService);
OAuthAuthenticationDataService.$inject = ['Restangular'];
function OAuthAuthenticationDataService(Restangular) {
var OAuthHttpHeader = {
"Content-Type": 'application/x-www-form-urlencoded'
};
var oAuthEndpointResource = Restangular.all('/token');
var service = {
authenticate : authenticate
}
return service;
function authenticate(user) {
return oAuthEndpointResource.post('grant_type=password&username=' + user.username + '&password=' + user.password, {}, OAuthHttpHeader);
}
}
})();
sessionStorageManagerService
(function () {
'use strict';
var serviceId = 'sessionStorageManagerService';
angular
.module('dilib')
.service(serviceId, sessionStorageManagerService);
sessionStorageManagerService.$inject = ['localStorageService'];
function sessionStorageManagerService(localStorageService) {
var service = {
get: get,
set: set
};
return service;
function set(key, val) {
localStorageService.set(key, val);
}
function get(key) {
return localStorageService.get(key);
}
}
})();
Test
describe('requestAuthorizationService', function () {
var RestangularProvider,
localStorageServiceProvider,
cryptoServiceMockSvc,
OAuthAuthenticationDataServiceMockSvc,
sessionStorageManagerServiceMockSvc,
requestAuthorizationService,
$q,
$rootScope;
beforeEach(function () {
angular.module('ngAnimate', []);
angular.module('ngRoute', []);
angular.module('dilib.layout', []);
//angular.module('LocalStorageModule', []);
angular.module('http-auth-interceptor', []);
//angular.module('restangular', []);
});
beforeEach(function () {
module('dilib', function (_RestangularProvider_, _localStorageServiceProvider_) {
RestangularProvider = _RestangularProvider_;
localStorageServiceProvider = _localStorageServiceProvider_;
});
});
//beforeEach(inject());
beforeEach(function () {
module(function ($provide) {
$provide.service('cryptoService', function () {
this.getMD5 = jasmine.createSpy('getMD5').and.callFake(function (param) {
var returnVal;
if (param == 'qwe123') {
returnVal = '200820e3227815ed1756a6b531e7e0d2';
}
if (param == 'qwe321') {
returnVal = 'blabla';
}
return returnVal;
});
});
$provide.service('OAuthAuthenticationDataService', function () {
this.authenticate = jasmine.createSpy('authenticate').and.callFake(function (userObject) {
var defer = $q.defer();
defer.resolve({ access_token: '1234' });
return defer.promise;
});
});
$provide.service('sessionStorageManagerService', function () {
this.get = jasmine.createSpy('get').and.callFake(function(param) {
return param;
});
this.set = jasmine.createSpy('set').and.callFake(function(param) {
return param;
});
});
});
});
beforeEach(inject(function (cryptoService,
OAuthAuthenticationDataService,
sessionStorageManagerService,
_requestAuthorizationService_,
_$q_,
_$rootScope_) {
cryptoServiceMockSvc = cryptoService;
OAuthAuthenticationDataServiceMockSvc = OAuthAuthenticationDataService;
sessionStorageManagerServiceMockSvc = sessionStorageManagerService;
requestAuthorizationService = _requestAuthorizationService_;
$q = _$q_;
$rootScope = _$rootScope_;
}));
describe('requestAuthorization method', function () {
describe('OAuth authentication result will be passed through sessionStorageManager', function () {
it('default value result will be passed through', function () {
//try 1
// OAuthAuthenticationDataServiceMockSvc.authenticate().then(function(result) {
// console.log('result', result);
// expect(sessionStorageManagerServiceMockSvc.set).toHaveBeenCalled();
// });
// $rootScope.$digest();
//try 2
// OAuthAuthenticationDataServiceMockSvc.authenticate();
// expect(sessionStorageManagerServiceMockSvc.set).toHaveBeenCalled();
//-----------------
// $rootScope.$digest();
});
});
});
});