secure-link
Version:
Functions to generate and validate resource access tokens.
50 lines (38 loc) • 1.12 kB
JavaScript
const generateNginxAccessToken = require('./generateNginxAccessToken');
const errors = require('../utils/errors');
/**
* Handle options misconfiguration
*/
it('throws when no secret is provided', () => {
expect(() => {
generateNginxAccessToken({});
}).toThrow(errors.missingSecret);
});
it('throws when no resource path is provided', () => {
expect(() => {
const options = { secret: '12qw12qw' };
generateNginxAccessToken(options);
}).toThrow(errors.missingResourcePath);
});
/**
* Verify token output
*/
it('returns a token with no lifetime', () => {
const options = {
secret: '12qw12qw',
path: '/secure/example.pdf'
};
const token = generateNginxAccessToken(options);
expect(token).toEqual('nAARcLsafqiisNpnoOHmgg');
});
it('returns a token with a limited lifetime', () => {
const DATE_IN_SECONDS = 1503491433595;
const ONE_HOUR = DATE_IN_SECONDS + 3600;
const options = {
secret: '12qw12qw',
path: '/secure/example.pdf',
lifetime: ONE_HOUR
};
const token = generateNginxAccessToken(options);
expect(token).toEqual('U5I9J9r44DtaID37Azlgrg');
});