UNPKG

dl

Version:

DreamLab Libs

388 lines (355 loc) 23.3 kB
describe("NauthManager", function () { var core = require('core'), Loader = core.http.Loader, Request = core.http.Request, Response = core.http.Response, Event = core.event.Event, ErrorEvent = core.event.ErrorEvent, NauthManager, NauthUserProfile, NauthUserServices, nauthManagerInstance, nauthManagerRequest, nauthManagerResponse, MockTests = require(__dirname + '/MockTests.js').MockTests; beforeEach(function () { if (NauthManager !== undefined) { nauthManagerRequest = new Request(); nauthManagerRequest.setHost('http://test.authorisation.grupaonet.pl'); nauthManagerRequest.setPath('http://test.authorisation.grupaonet.pl/test'); nauthManagerRequest.setQueryParam('test', 'test'); nauthManagerRequest.setHeader('referer', 'http://test.authorisation.grupaonet.pl/testreferer'); nauthManagerResponse = new Response(); nauthManagerInstance = new NauthManager(nauthManagerRequest, nauthManagerResponse); nauthManagerInstance.setPortalId(1); spyOn(nauthManagerRequest, 'setHeader').andCallThrough(); spyOn(nauthManagerResponse.headers, 'setHeader').andCallThrough(); } MockTests.addExtraMatchers(this); }); /******************************************************************************************* * Load Classes * *****************************************************************************************/ it('NauthManager can load needed classes', function(){ expect(function(){ NauthManager = require(__dirname + '/../../lib/nauth/NauthManager.js').NauthManager; NauthUserProfile = require(__dirname + '/../../lib/nauth/NauthUserProfile.js').NauthUserProfile, NauthUserServices = require(__dirname + '/../../lib/nauth/NauthUserServices.js').NauthUserServices; }).not.toThrow(); }); /******************************************************************************************* * setPortalId * *****************************************************************************************/ it("NauthManager.setPortalId", function () { expect(nauthManagerInstance.setPortalId('test')).toBe(nauthManagerInstance); expect(nauthManagerInstance._portalId).toEqual(1); expect(nauthManagerInstance._cookieName).toEqual('onet_token'); expect(nauthManagerInstance.setPortalId(21)).toBe(nauthManagerInstance); expect(nauthManagerInstance._portalId).toEqual(21); expect(nauthManagerInstance._cookieName).toEqual('onet_token21'); }); /******************************************************************************************* * setOnetApp * *****************************************************************************************/ it("NauthManager.setOnetApp", function () { expect(nauthManagerInstance.setOnetApp('test')).toBe(nauthManagerInstance); expect(nauthManagerInstance._onetApp).toEqual('test'); }); /******************************************************************************************* * isSecuredHost * *****************************************************************************************/ it("NauthManager.isSecuredHost", function () { var securedHost = NauthManager.SECURED_HOST, request, nauthManagerInstance; NauthManager.SECURED_HOST = true; nauthManagerInstance = new NauthManager(new Request('https://test.authorisation.grupaonet.pl/auth.html'), {}); expect(nauthManagerInstance.isSecuredHost()).toBeTruthy(); NauthManager.SECURED_HOST = false; expect(nauthManagerInstance.isSecuredHost()).toBeFalsy(); NauthManager.SECURED_HOST = true; nauthManagerInstance = new NauthManager(new Request('http://test.authorisation.grupaonet.pl/auth.html'), {}); expect(nauthManagerInstance.isSecuredHost()).toBeFalsy(); request = new Request('http://test.authorisation.grupaonet.pl/auth.html'); request.setHeader('x-forwarded-proto', 'https'); nauthManagerInstance = new NauthManager(request, {}); expect(nauthManagerInstance.isSecuredHost()).toBeTruthy(); NauthManager.SECURED_HOST = securedHost; }); /******************************************************************************************* * getUserProfile * *****************************************************************************************/ describe("NauthManager.getUserProfile", function () { it("NauthManager.getUserProfile empty cookie" , function() { spyOn(nauthManagerRequest, 'getCookie').andReturn(null); expect(nauthManagerInstance.getUserProfile(function(err, profile){ var profileJson = profile.toJSON(); expect(err).toBeNull(); expect(profileJson.userId).toEqual(undefined); expect(profileJson.secondaryId).toEqual(undefined); expect(profileJson.login).toEqual(undefined); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserProfile filled cookie, user not logged", function () { nauthManagerInstance._profile = new NauthUserProfile(); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); expect(nauthManagerInstance.getUserProfile(function(err, profile){ var profileJson = profile.toJSON(); expect(err).toBeNull(); expect(profileJson.userId).toEqual(undefined); expect(profileJson.secondaryId).toEqual(undefined); expect(profileJson.login).toEqual(undefined); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserProfile check request headers", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie ////Request headers testing MockTests.nauthManagerFetchUserProfileHeaderTest(this._request, 'konto.onet.pl.front', 'alamakota1234', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.httpResponse401())); }); expect(nauthManagerInstance.getUserProfile(function(err, profile){ var profileJson = profile.toJSON(); expect(err).toBeNull(); expect(profileJson.userId).toEqual(undefined); expect(profileJson.secondaryId).toEqual(undefined); expect(profileJson.login).toEqual(undefined); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserProfile filled cookie, getUserProfile - Loader.Event.ERROR", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { this.dispatchEvent(new ErrorEvent(Loader.Event.ERROR, new ErrorEvent({}, {}, 404, 'Not found'), -1, 'Http request error: 404')); }); expect(nauthManagerInstance.getUserProfile(function(err, profile){ var profileJson = profile.toJSON(); expect(err).toBeNull(); expect(profileJson.userId).toEqual(undefined); expect(profileJson.secondaryId).toEqual(undefined); expect(profileJson.login).toEqual(undefined); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserProfile filled cookie, getUserProfile all OK", function () { spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.fetchUserProfile200())); }); expect(nauthManagerInstance.getUserProfile(function(err, profile){ expect(Loader.prototype.load).toHaveBeenCalled(); var profileJson = profile.toJSON(); expect(profileJson.userId).toEqual(66801973); expect(profileJson.secondaryId).toEqual(54030768); expect(profileJson.login).toEqual('jan.kowalski@poczta.onet.pl'); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); }); /******************************************************************************************* * getUserServices * *****************************************************************************************/ describe("NauthManager.getUserServices", function () { it("NauthManager.getUserServices empty cookie" , function() { spyOn(nauthManagerRequest, 'getCookie').andReturn(null); expect(nauthManagerInstance.getUserServices(function(err, mus){ expect(err).toBeNull(); expect(mus instanceof NauthUserServices).toBeTruthy(); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserServices filled cookie, user not logged", function () { nauthManagerInstance._profile = new NauthUserProfile(); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); expect(nauthManagerInstance.getUserServices(function(err, mus){ expect(err).toBeNull(); expect(mus instanceof NauthUserServices).toBeTruthy(); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserServices check request headers", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie MockTests.nauthManagerFetchUserProfileHeaderTest(this._request, 'konto.onet.pl.front', 'alamakota1234', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.httpResponse401())); }); expect(nauthManagerInstance.getUserServices(function(err, mus){ //todo dodać obsugę jak nie ma danych expect(err).toBeNull(); expect(mus instanceof NauthUserServices).toBeTruthy(); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserServices filled cookie, getUserProfile - Loader.Event.ERROR", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { this.dispatchEvent(new ErrorEvent(Loader.Event.ERROR, new ErrorEvent({}, {}, 404, 'Not found'), -1, 'Http request error: 404')); }); expect(nauthManagerInstance.getUserServices(function(err, mus){ //todo dodać obsugę jak nie ma danych expect(err).toBeNull(); expect(mus instanceof NauthUserServices).toBeTruthy(); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); it("NauthManager.getUserServices filled cookie, getUserServices all OK", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(nauthManagerRequest, 'getCookie').andReturn('alamakota1234'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie MockTests.nauthManagerFetchUserProfileHeaderTest(this._request, 'konto.onet.pl.front', 'alamakota1234', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.fetchUserProfile200())); }); expect(nauthManagerInstance.getUserServices(function(err, mus){ expect(Loader.prototype.load).toHaveBeenCalled(); expect(err).toBeNull(); expect(mus instanceof NauthUserServices).toBeTruthy(); expect(mus.hasAccountInApplication(4,1)).toBeTruthy(); expect(mus.getIdInApplication(4,1)).toEqual([54030768]); })).toBe(nauthManagerInstance); expect(nauthManagerRequest.getCookie).toHaveBeenCalledWith('onet_token'); }); }); /******************************************************************************************** * getTokenFromCode * ********************************************************************************************/ describe("NauthManager.getTokenFromCode", function () { it("NauthManager.getTokenFromCode empty code" , function() { expect(nauthManagerInstance.getTokenFromCode(null, false, function(err, data){ expect(err).toBeNull(); expect(data).toBeNull(); MockTests.nauthManagerRemoveCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); }); it("NauthManager.getTokenFromCode - Loader.Event.ERROR", function () { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(Loader.prototype, 'load').andCallFake(function() { MockTests.nauthManagerGetTokenFromCodeHeaderTest(this._request, 'konto.onet.pl.front', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new ErrorEvent(Loader.Event.ERROR, new ErrorEvent({}, {}, 404, 'Not found'), -1, 'Http request error: 404')); }); expect(nauthManagerInstance.getTokenFromCode(MockTests.ApplicationCodeToAuth, false, function(err, data){ expect(Loader.prototype.load).toHaveBeenCalled(); expect(err.code).toEqual(-1); })).toBe(nauthManagerInstance); }); it("NauthManager.getTokenFromCode filled onetApp 401" , function() { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie MockTests.nauthManagerGetTokenFromCodeHeaderTest(this._request, 'konto.onet.pl.front', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.httpResponse401())); }); expect(nauthManagerInstance.getTokenFromCode(MockTests.ApplicationCodeToAuth, false, function(err, data){ expect(Loader.prototype.load).toHaveBeenCalled(); expect(err.code).toEqual(401); })).toBe(nauthManagerInstance); }); it("NauthManager.getTokenFromCode All OK" , function() { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie MockTests.nauthManagerGetTokenFromCodeHeaderTest(this._request, 'konto.onet.pl.front', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.getTokenFromCode200())); }); expect(nauthManagerInstance.getTokenFromCode(MockTests.ApplicationCodeToAuth, false, function(err, data){ expect(Loader.prototype.load).toHaveBeenCalled(); expect(err).toBeNull(); expect(data).toBeNull(); MockTests.nauthManagerSetCookie(nauthManagerRequest, nauthManagerResponse); })).toBe(nauthManagerInstance); }); it("NauthManager.getTokenFromCode All OK pernament" , function() { nauthManagerInstance.setOnetApp('konto.onet.pl.front'); spyOn(Loader.prototype, 'load').andCallFake(function() { //nie nadpisuje globalnie MockTests.nauthManagerGetTokenFromCodeHeaderTest(this._request, 'konto.onet.pl.front', NauthManager.OPAL.GATEWAY, NauthManager.OPAL.HOSTS.OPEN); this.dispatchEvent(new Event(Loader.Event.LOADED, MockTests.getTokenFromCode200())); }); ///pernament expect(nauthManagerInstance.getTokenFromCode(MockTests.ApplicationCodeToAuth, true, function(err, data){ expect(Loader.prototype.load).toHaveBeenCalled(); expect(err).toBeNull(); expect(data).toBeNull(); MockTests.nauthManagerSetCookie(nauthManagerRequest, nauthManagerResponse); expect(nauthManagerResponse.headers.getHeader('p3p')).toEqual('CP="ALL DSP COR IVD IVA PSD PSA TEL TAI CUS ADM CUR CON SAM OUR IND"'); expect(nauthManagerResponse.headers.getHeader('set-cookie')).toContainMatch('(^|;\\s*)expires='); })).toBe(nauthManagerInstance); }); }); /******************************************************************************************** * setCookie * ********************************************************************************************/ describe("NauthManager.setCookie", function () { it("NauthManager.setCookie session", function() { nauthManagerInstance.setCookie('test1', ' '); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test1=%20'); expect(nauthManagerResponse.headers.getHeader('p3p')).toEqual('CP="ALL DSP COR IVD IVA PSD PSA TEL TAI CUS ADM CUR CON SAM OUR IND"'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test1=%20; path=/; httponly' ], true); nauthManagerInstance.setCookie('test1', 'test'); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test1=test'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test1=%20; path=/; httponly', 'test1=test; path=/; httponly' ], true); nauthManagerInstance.setCookie('test2', 1); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test1=test; test2=1'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test1=%20; path=/; httponly', 'test1=test; path=/; httponly', 'test2=1; path=/; httponly' ], true); nauthManagerInstance.setCookie('test2', 2); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test1=test; test2=2'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test1=%20; path=/; httponly', 'test1=test; path=/; httponly', 'test2=1; path=/; httponly', 'test2=2; path=/; httponly' ], true); nauthManagerInstance.setCookie('test1', 0); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test1=0; test2=2'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test1=%20; path=/; httponly', 'test1=test; path=/; httponly', 'test2=1; path=/; httponly', 'test2=2; path=/; httponly', 'test1=0; path=/; httponly' ], true); }); it("NauthManager.setCookie expires", function() { nauthManagerInstance.setCookie('test', 1, 1000); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test=1'); expect(nauthManagerResponse.headers.getHeader('p3p')).toEqual('CP="ALL DSP COR IVD IVA PSD PSA TEL TAI CUS ADM CUR CON SAM OUR IND"'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test=1; expires=' + new Date(new Date().getTime() + 1000).toUTCString() + '; path=/; httponly' ], true); nauthManagerInstance.setCookie('test', 'invalidate', -1000); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual(null); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test=1; expires=' + new Date(new Date().getTime() + 1000).toUTCString() + '; path=/; httponly', 'test=invalidate; expires=' + new Date(new Date().getTime() - 1000).toUTCString() + '; path=/; httponly' ], true); }); it("NauthManager.setCookie secure", function() { spyOn(nauthManagerInstance, 'isSecuredHost').andCallFake(function () { return true; }); nauthManagerInstance.setCookie('test', 1); expect(nauthManagerInstance._request.getHeader('cookie')).toEqual('test=1'); expect(nauthManagerResponse.headers.getHeader('p3p')).toEqual('CP="ALL DSP COR IVD IVA PSD PSA TEL TAI CUS ADM CUR CON SAM OUR IND"'); expect(nauthManagerInstance._response.headers.setHeader).toHaveBeenCalledWith('set-cookie', [ 'test=1; path=/; secure; httponly' ], true); }); }); });