tokens: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-06-04 09:28:40 -07:00
parent 593038907c
commit 7bee7b9ef8
16 changed files with 517 additions and 727 deletions

View File

@@ -6,71 +6,51 @@
'use strict';
var async = require('async'),
BoxError = require('../boxerror.js'),
database = require('../database'),
const common = require('./common.js'),
expect = require('expect.js'),
hat = require('../hat.js'),
janitor = require('../janitor.js'),
tokendb = require('../tokendb.js');
tokens = require('../tokens.js');
describe('janitor', function () {
var TOKEN_0 = {
id: 'tid-0',
accessToken: hat(8 * 32),
identifier: '0',
clientId: 'clientid-0',
expires: Date.now() + 60 * 60 * 1000,
scope: 'settings',
name: 'clientid0',
lastUsedTime: null
};
var TOKEN_1 = {
id: 'tid-1',
accessToken: hat(8 * 32),
before(common.setup);
after(common.cleanup);
const token1 = {
name: 'token1',
identifier: '1',
clientId: 'clientid-1',
expires: Date.now() - 1000,
scope: 'apps',
name: 'clientid1',
expires: Number.MAX_SAFE_INTEGER,
lastUsedTime: null,
scope: 'unused'
};
const token2 = {
name: 'token2',
identifier: '2',
clientId: 'clientid-2',
expires: Date.now(),
lastUsedTime: null
};
before(function (done) {
async.series([
database.initialize,
database._clear,
tokendb.add.bind(null, TOKEN_0),
tokendb.add.bind(null, TOKEN_1)
], done);
it('can cleanupTokens', async function () {
let result = await tokens.add(token1);
token1.id = result.id;
token1.accessToken = result.accessToken;
result = await tokens.add(token2);
token2.id = result.id;
token2.accessToken = result.accessToken;
await janitor.cleanupTokens();
});
after(function (done) {
async.series([
database._clear,
database.uninitialize
], done);
it('did not remove the non-expired token', async function () {
const result = await tokens.getByAccessToken(token1.accessToken);
expect(result).to.be.eql(token1);
});
it('can cleanupTokens', function (done) {
janitor.cleanupTokens(done);
});
it('did not remove the non-expired token', function (done) {
tokendb.getByAccessToken(TOKEN_0.accessToken, function (error, result) {
expect(error).to.be(null);
expect(result).to.be.eql(TOKEN_0);
done();
});
});
it('did remove the non-expired token', function (done) {
tokendb.getByAccessToken(TOKEN_1.accessToken, function (error, result) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.be(BoxError.NOT_FOUND);
expect(result).to.not.be.ok();
done();
});
it('did remove the non-expired token', async function () {
const result = await tokens.getByAccessToken(token2.accessToken);
expect(result).to.be(null);
});
});