Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed May 22, 2024
1 parent 92c0432 commit f4a15e3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
15 changes: 8 additions & 7 deletions test/mocha/js/components/application.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ define([
expect(apiRequest.url()).to.contain('/accounts/bootstrap');
options.done(
{
"email": "[email protected]",
"username": "[email protected]",
"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);
Expand All @@ -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(["[email protected]"]);
done();
})
Expand Down
2 changes: 1 addition & 1 deletion test/mocha/js/components/discovery_mediator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/mocha/js/components/query_mediator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
50 changes: 26 additions & 24 deletions test/mocha/js/services/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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 () {
Expand All @@ -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({
Expand All @@ -258,6 +259,7 @@ define([
api.getBeeHive.restore();
});


describe("Testing request options", function() {

var ajaxSpy;
Expand Down

0 comments on commit f4a15e3

Please sign in to comment.