Files
cloudron-box/src/test/janitor-test.js
Girish Ramakrishnan e8c11f6e15 Fix tests
2019-02-15 14:56:51 -08:00

108 lines
3.1 KiB
JavaScript

/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var async = require('async'),
authcodedb = require('../authcodedb.js'),
database = require('../database'),
DatabaseError = require('../databaseerror.js'),
expect = require('expect.js'),
hat = require('../hat.js'),
janitor = require('../janitor.js'),
tokendb = require('../tokendb.js');
describe('janitor', function () {
var AUTHCODE_0 = {
authCode: 'authcode-0',
clientId: 'clientid-0',
userId: 'userid-0',
expiresAt: Date.now() + 60 * 60 * 1000
};
var AUTHCODE_1 = {
authCode: 'authcode-1',
clientId: 'clientid-1',
userId: 'userid-1',
expiresAt: Date.now() - 5000
};
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'
};
var TOKEN_1 = {
id: 'tid-1',
accessToken: hat(8 * 32),
identifier: '1',
clientId: 'clientid-1',
expires: Date.now() - 1000,
scope: 'apps',
name: 'clientid1'
};
before(function (done) {
async.series([
database.initialize,
database._clear,
authcodedb.add.bind(null, AUTHCODE_0.authCode, AUTHCODE_0.clientId, AUTHCODE_0.userId, AUTHCODE_0.expiresAt),
authcodedb.add.bind(null, AUTHCODE_1.authCode, AUTHCODE_1.clientId, AUTHCODE_1.userId, AUTHCODE_1.expiresAt),
tokendb.add.bind(null, TOKEN_0),
tokendb.add.bind(null, TOKEN_1)
], done);
});
after(function (done) {
async.series([
database._clear,
database.uninitialize
], done);
});
it('can cleanupTokens', function (done) {
janitor.cleanupTokens(done);
});
it('did not remove the non-expired authcode', function (done) {
authcodedb.get(AUTHCODE_0.authCode, function (error, result) {
expect(error).to.be(null);
expect(result).to.be.eql(AUTHCODE_0);
done();
});
});
it('did remove expired authcode', function (done) {
authcodedb.get(AUTHCODE_1.authCode, function (error, result) {
expect(error).to.be.a(DatabaseError);
expect(error.reason).to.be(DatabaseError.NOT_FOUND);
expect(result).to.not.be.ok();
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(DatabaseError);
expect(error.reason).to.be(DatabaseError.NOT_FOUND);
expect(result).to.not.be.ok();
done();
});
});
});