mirror of
https://github.com/directus/directus.git
synced 2026-02-04 07:25:01 -05:00
* testing workflow * name changed * generateHash working * changed pendingMock() to 0 because ticks > 5000 * removed empty tests * removed empty tests * Update packages/sdk/tests/utils.ts Co-authored-by: José Varela <joselcvarela@gmail.com> * updated workflow to add build * added npm run build * fixed get-filter-operators * added env to workflow * potential fix to index.test.ts * added env variables for index.test.ts * added caching and node-version matrix * added all supported node versions * only supported node versions * updated to only support 16 * removed duplicate workflow caching Co-authored-by: José Varela <joselcvarela@gmail.com>
144 lines
3.2 KiB
TypeScript
144 lines
3.2 KiB
TypeScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
|
|
import { Auth, AxiosTransport, Directus, MemoryStorage } from '../../src';
|
|
import { test, timers } from '../utils';
|
|
|
|
describe('auth (node)', function () {
|
|
test(`sets default auth mode to json`, async (url) => {
|
|
const storage = new MemoryStorage();
|
|
const transport = new AxiosTransport(url, storage);
|
|
const auth = new Auth(transport, storage);
|
|
expect(auth.options.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,
|
|
},
|
|
});
|
|
|
|
const sdk = new Directus(url);
|
|
await sdk.auth.login({
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
});
|
|
|
|
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);
|
|
|
|
const loginPromise = sdk.auth.login(
|
|
{
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
},
|
|
{
|
|
refresh: {
|
|
auto: true,
|
|
time: 2500,
|
|
},
|
|
}
|
|
);
|
|
|
|
await tick(2000);
|
|
|
|
await loginPromise;
|
|
|
|
expect(scope.pendingMocks().length).toBe(1);
|
|
expect(sdk.auth.expiring).toBe(false);
|
|
expect(sdk.storage.auth_token).toBe('some_node_access_token');
|
|
expect(sdk.storage.auth_expires).toBe(107000);
|
|
await tick(5000);
|
|
|
|
expect(scope.pendingMocks().length).toBe(0);
|
|
await flush();
|
|
expect(sdk.auth.expiring).toBe(true);
|
|
|
|
await new Promise((resolve) => {
|
|
scope.once('replied', () => {
|
|
flush().then(resolve);
|
|
});
|
|
});
|
|
|
|
expect(sdk.storage.auth_expires).toBe(112000);
|
|
expect(scope.pendingMocks().length).toBe(0);
|
|
expect(sdk.storage.auth_token).toBe('a_new_node_access_token');
|
|
expect(sdk.auth.expiring).toBe(false);
|
|
}, 100000);
|
|
});
|
|
|
|
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: {},
|
|
});
|
|
|
|
const sdk = new Directus(url);
|
|
|
|
await sdk.auth.login({
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
});
|
|
|
|
expect(sdk.auth.token).toBe('auth_token');
|
|
|
|
await sdk.auth.logout();
|
|
|
|
expect(sdk.auth.token).toBeNull();
|
|
});
|
|
});
|