mirror of
https://github.com/directus/directus.git
synced 2026-01-30 07:58:15 -05:00
* refactor: more intuitive interfaces * refactor: simpler refresh before: on every request we were debouncing a refresh request after: call refresh only once before now() + 'expires' * refactor: prefix on base storage * fixup! refactor: simpler refresh before: on every request we were debouncing a refresh request after: call refresh only once before now() + 'expires' * refactor: simpler axios transport before: handle auth headers after: auth headers are handled on directus instance * refactor: simpler usage of Directus constructor * fixup! refactor: simpler refresh before: on every request we were debouncing a refresh request after: call refresh only once before now() + 'expires' * refactor: fix tests based on previous changes * refactor: better auth constructor before: depends on SDK instance after: depends on Transport and Storage instance * accept staticToken from auth * make transport and storage as optional on options * fix type auth refresh * simplify transport * fix test for previous changes * improve auth class * revert some IAuth props because tests * allow to force memory of localstorage on storage * add tests for previous change * document everything and simplify some things * fix override headers on request * better name typing * fix private axios * removed boolean from CLI auth.refresh() * fix missing url in some examples * soem grammar updates Co-authored-by: Jay Cammarano <jay.cammarano@gmail.com> Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
146 lines
3.1 KiB
TypeScript
146 lines
3.1 KiB
TypeScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
|
|
import { Directus } from '../../src';
|
|
import { test, timers } from '../utils';
|
|
|
|
describe('auth (node)', function () {
|
|
test(`sets default auth mode to json`, async (url) => {
|
|
const sdk = new Directus(url);
|
|
expect(sdk.auth.mode).toBe('json');
|
|
});
|
|
|
|
test(`sends default auth mode`, async (url, nock) => {
|
|
const scope = nock()
|
|
.post('/auth/login', (body) => body.mode === 'json')
|
|
.reply(200, {
|
|
data: {
|
|
access_token: 'access_token',
|
|
refresh_token: 'refresh_token',
|
|
expires: 60000,
|
|
},
|
|
});
|
|
|
|
await timers(async ({ tick }) => {
|
|
const sdk = new Directus(url);
|
|
const loginPromise = sdk.auth.login({
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
});
|
|
|
|
await tick(2500);
|
|
|
|
await loginPromise;
|
|
|
|
expect(scope.pendingMocks().length).toBe(0);
|
|
});
|
|
});
|
|
|
|
test(`authentication should auto refresh after specified period`, async (url, nock) => {
|
|
const scope = nock();
|
|
|
|
scope
|
|
.post('/auth/login', (body) => body.mode === 'json')
|
|
.reply(200, {
|
|
data: {
|
|
access_token: 'some_node_access_token',
|
|
refresh_token: 'some_node_refresh_token',
|
|
expires: 5000,
|
|
},
|
|
});
|
|
|
|
scope
|
|
.post('/auth/refresh', {
|
|
refresh_token: 'some_node_refresh_token',
|
|
})
|
|
.reply(200, {
|
|
data: {
|
|
access_token: 'a_new_node_access_token',
|
|
refresh_token: 'a_new_node_refresh_token',
|
|
expires: 5000,
|
|
},
|
|
});
|
|
|
|
expect(scope.pendingMocks().length).toBe(2);
|
|
|
|
await timers(async ({ tick, flush }) => {
|
|
const sdk = new Directus(url, { auth: { autoRefresh: true, msRefreshBeforeExpires: 2500 } });
|
|
|
|
const loginPromise = sdk.auth.login({
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
});
|
|
|
|
await tick(2500);
|
|
|
|
await loginPromise;
|
|
|
|
expect(scope.pendingMocks().length).toBe(1);
|
|
expect(sdk.storage.auth_token).toBe('some_node_access_token');
|
|
expect(sdk.storage.auth_expires).toBe(5000);
|
|
await tick(5000);
|
|
|
|
expect(scope.pendingMocks().length).toBe(0);
|
|
await flush();
|
|
|
|
await new Promise((resolve) => {
|
|
scope.once('replied', () => {
|
|
flush().then(resolve);
|
|
});
|
|
});
|
|
|
|
expect(scope.pendingMocks().length).toBe(0);
|
|
expect(sdk.storage.auth_token).toBe('a_new_node_access_token');
|
|
});
|
|
});
|
|
|
|
test(`logout sends a refresh token in body`, async (url, nock) => {
|
|
nock()
|
|
.post('/auth/login', (body) => body.mode === 'json')
|
|
.reply(
|
|
200,
|
|
{
|
|
data: {
|
|
access_token: 'auth_token',
|
|
refresh_token: 'json_refresh_token',
|
|
},
|
|
},
|
|
{
|
|
'Set-Cookie': 'directus_refresh_token=my_refresh_token; Max-Age=604800; Path=/; HttpOnly;',
|
|
}
|
|
);
|
|
|
|
nock()
|
|
.post('/auth/logout', {
|
|
refresh_token: 'json_refresh_token',
|
|
})
|
|
.reply(200, {
|
|
data: {},
|
|
});
|
|
|
|
await timers(async ({ tick }) => {
|
|
const sdk = new Directus(url);
|
|
|
|
const loginPromise = sdk.auth.login({
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
});
|
|
|
|
await tick(2500);
|
|
|
|
await loginPromise;
|
|
|
|
expect(sdk.auth.token).toBe('auth_token');
|
|
|
|
const logoutPromise = sdk.auth.logout();
|
|
|
|
await tick(2500);
|
|
|
|
await logoutPromise;
|
|
|
|
expect(sdk.auth.token).toBeNull();
|
|
});
|
|
});
|
|
});
|