From f4a15e3f77a555680b924b8115fef616c1288c94 Mon Sep 17 00:00:00 2001 From: Tim Hostetler <6970899+thostetler@users.noreply.github.com> Date: Wed, 22 May 2024 14:51:06 -0400 Subject: [PATCH] Update tests --- test/mocha/js/components/application.spec.js | 15 +++--- .../js/components/discovery_mediator.spec.js | 2 +- .../js/components/query_mediator.spec.js | 2 +- test/mocha/js/services/api.spec.js | 50 ++++++++++--------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/test/mocha/js/components/application.spec.js b/test/mocha/js/components/application.spec.js index 4d05b9239..13e1156dd 100644 --- a/test/mocha/js/components/application.spec.js +++ b/test/mocha/js/components/application.spec.js @@ -180,14 +180,15 @@ define([ expect(apiRequest.url()).to.contain('/accounts/bootstrap'); options.done( { - "email": "user@gmail.com", + "username": "user@gmail.com", "scopes": ["user"], "access_token": "ap0MkGjroS1zzijLlk9fV2UKXdRDo5nzUueTNaog", "token_type": "Bearer", - "csrf": "1428969367##8460e442cb2984810486bf959048a05d7e7d9e78", - "expire_in": "2500-01-01T00:00:00", - "refresh_token": "KKGJp56UlpKgfHUuNNNJvj3XgepWlkTfKKtqmpkM", - }); + "csrf": "142896367##8460e442cb2984810486bf959048a05d7e7d9e78", + "expire_at": 16725225600, + "refresh_token": "KKGJp56UlpKgfHUuNNNvJvJ3XqepWLkTfKKtqmpKM", + } + ); }; app.getBeeHive().addService('Api', api); @@ -197,8 +198,8 @@ define([ app.getApiAccess({reconnect: true}) .done(function () { expect(spy.called).to.eql(true); - expect(api.access_token).to.eql('Bearer ap0MkGjroS1zzijLlk9fV2UKXdRDo5nzUueTNaog'); - //every time onbootstrap is called, update the user object with email/undefined to show that user is anonymous + expect(api.access_token).to.eql('Bearer test-token'); + //every time onbootstrap is called, update the user object with username/undefined to show that user is anonymous expect(fakeUser.setUser.args[0]).to.eql(["user@gmail.com"]); done(); }) diff --git a/test/mocha/js/components/discovery_mediator.spec.js b/test/mocha/js/components/discovery_mediator.spec.js index 1ce47fc84..4ebc8d21e 100644 --- a/test/mocha/js/components/discovery_mediator.spec.js +++ b/test/mocha/js/components/discovery_mediator.spec.js @@ -36,7 +36,7 @@ define([ var minsub; beforeEach(function() { var api = new Api(); - api.expire_in = Date.now() + 100000000; + api.expire_at = Date.now() + 100000000; minsub = new MinimalPubSub({verbose: false, Api: api}); this.server = sinon.fakeServer.create(); diff --git a/test/mocha/js/components/query_mediator.spec.js b/test/mocha/js/components/query_mediator.spec.js index 8d7888819..a6e686dc0 100644 --- a/test/mocha/js/components/query_mediator.spec.js +++ b/test/mocha/js/components/query_mediator.spec.js @@ -99,7 +99,7 @@ define([ this.beehive = beehive; beehive.addObject("AppStorage", {clearSelectedPapers : sinon.spy()}); var api = new Api(); - api.expire_in = Date.now() + 100000000; + api.expire_at = Date.now() + 100000000; sinon.spy(api, 'request'); beehive.addService('Api', api); var ps = new PubSub(); diff --git a/test/mocha/js/services/api.spec.js b/test/mocha/js/services/api.spec.js index 03d924b8c..d3b8fff1f 100644 --- a/test/mocha/js/services/api.spec.js +++ b/test/mocha/js/services/api.spec.js @@ -22,7 +22,7 @@ define([ var __getApi = function (options) { var api = new Api(options); - api.expire_in = Date.now() + 10000000; + api.expire_at = Math.floor(new Date('2500-01-01').getTime() / 1000); return api; }; @@ -189,49 +189,49 @@ define([ expect(spy.thisValues[0]).to.have.ownProperty('api', 'request'); }); - it("should automatically request a new token if there is less than 2 minutes before token expiration", function(){ + it("should automatically request a new token if there is less than 2 minutes before token expiration", function() { + var api = __getApi({ url: '/api/1' }); // url is there, but i want to be explicit - var api = __getApi({url: '/api/1'}); // url is there, but i want to be explicit + const getSecondsInFuture = (seconds) => { + return Math.floor( + new Date(new Date().getTime() + seconds * 1000).getTime() / 1000 + ); + }; api.access_token = 'foo'; - api.expire_in = "2016-08-16T18:12:00" - - api.getCurrentUTCMoment = function(){ - //mock a EDT time - return Moment.parseZone("2016-08-16T14:11:00-04:00").utc(); - } + // set to less than the expiration time + api.expire_at = getSecondsInFuture(90); api._request = sinon.spy(); - api.getApiAccess = sinon.spy(function(){ + api.getApiAccess = sinon.spy(function() { var d = $.Deferred(); d.resolve(); - api.access_token = 'boo' + api.access_token = 'boo'; return d.promise(); }); - var q = new ApiQuery({q: 'foo'}); + var q = new ApiQuery({ q: 'foo' }); - api.request(new ApiRequest({target: '/test', query: q, sender: 'woo'})); + api.request(new ApiRequest({ target: '/test', query: q, sender: 'woo' })); + // we should have called getApiAccess once and _request once because we are less than 2 minutes away from expiration expect(api.getApiAccess.callCount).to.eql(1); expect(api.access_token).to.eql('boo'); expect(api._request.callCount).to.eql(1); - //this request should not lead to an access_token refresh bc - //expiration is 3 minutes in the future - - api.expire_in = "2016-08-16T18:13:05.982Z"; + // set to more than the expiration time + api.expire_at = getSecondsInFuture(180); - api.getCurrentUTCMoment = function(){ - return Moment("2016-08-16T18:11:05.982Z").utc(); - } + // reset the call counts + api.getApiAccess.reset(); + api._request.reset(); api.request(new ApiRequest({target: '/test', query: q, sender: 'woo'})); - expect(api.getApiAccess.callCount).to.eql(1); - expect(api._request.callCount).to.eql(2); - + // because we are more than 2 minutes away from expiration, we should not call getApiAccess + expect(api.getApiAccess.callCount).to.eql(0); + expect(api._request.callCount).to.eql(1); }); it("should correctly reset the token if it has expired", function () { @@ -243,8 +243,9 @@ define([ } }; }); + api.access_token = 'foo'; - api.expire_in = Date.now(); + api.expire_at = Math.floor(Date.now() / 1000); // Set expire_at to current time in seconds since epoch var sendRequest = function () { api.request(new ApiRequest({ @@ -258,6 +259,7 @@ define([ api.getBeeHive.restore(); }); + describe("Testing request options", function() { var ajaxSpy;