This commit is contained in:
75
test/spec/AuthHandler.ts
Normal file
75
test/spec/AuthHandler.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import http from 'http';
|
||||
import auth from '../../lib/Authority';
|
||||
import { get } from '../Helper';
|
||||
|
||||
const expires_seconds = 600;
|
||||
const refresh_expires_seconds = 3600;
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
xdescribe ('auth handler', () => {
|
||||
let server: http.Server|null = null;
|
||||
beforeAll (() => {
|
||||
server = http.createServer ((req, res) => {
|
||||
res.writeHead (404);
|
||||
res.end ();
|
||||
});
|
||||
server.listen (3000);
|
||||
|
||||
jasmine.clock ()
|
||||
.install ();
|
||||
jasmine.clock ()
|
||||
.mockDate (new Date);
|
||||
});
|
||||
|
||||
it ('should return a valid access and refresh token', async () => {
|
||||
const resp = await get ({ authorization: 'Basic foo:bar' });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (200);
|
||||
const data = JSON.parse (resp.body as string);
|
||||
const at = data.access_token;
|
||||
const rt = data.refresh_token;
|
||||
expect (resp.headers['set-cookie'])
|
||||
.toContain (`cookie_jar=${at}`);
|
||||
expect (resp.headers['cache-control'])
|
||||
.toEqual ('no-store');
|
||||
expect (resp.headers.pragma)
|
||||
.toEqual ('no-cache');
|
||||
expect (data.token_type)
|
||||
.toEqual ('bearer');
|
||||
expect (data.expires_in)
|
||||
.toEqual (expires_seconds);
|
||||
expect (data.refresh_expires_in)
|
||||
.toEqual (refresh_expires_seconds);
|
||||
|
||||
expect (at as string)
|
||||
.toMatch (/^[0-9a-z-._~+/]+$/ui);
|
||||
expect (rt as string)
|
||||
.toMatch (/^[0-9a-z-._~+/]+$/ui);
|
||||
|
||||
const atv = auth.verify (at as string);
|
||||
expect (atv.valid)
|
||||
.toEqual (true);
|
||||
expect (atv.authorized)
|
||||
.toEqual (true);
|
||||
expect (atv.type)
|
||||
.toEqual ('access_token');
|
||||
|
||||
const rtv = auth.verify (rt as string);
|
||||
expect (rtv.valid)
|
||||
.toEqual (true);
|
||||
expect (rtv.authorized)
|
||||
.toEqual (false);
|
||||
expect (rtv.type)
|
||||
.toEqual ('refresh_token');
|
||||
});
|
||||
|
||||
afterAll (() => {
|
||||
if (server === null)
|
||||
throw new Error ('server is null');
|
||||
server.close ();
|
||||
jasmine.clock ()
|
||||
.tick (24 * 60 * 60 * 1000);
|
||||
jasmine.clock ()
|
||||
.uninstall ();
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user