Files
directus/packages/sdk/tests/base/auth.node.test.ts
Jake Cattrall d24dacd075 Use date (epoch) compare workflow instead of timer to refresh token in SDK (#12399)
* use date compare workflow instead of timer

* convert milliseconds to seconds

* add missing references to auth_expired_at key

* correct calculation and presume getTime returns milliseconds

* resolve issue with static tokens

* prevents token refresh before everything else is initialized

* remove timer based authentication tests

Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
2022-04-01 09:31:11 -04:00

78 lines
1.6 KiB
TypeScript

/**
* @jest-environment node
*/
import { Directus } from '../../src';
import { test } 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,
},
});
const sdk = new Directus(url);
const loginPromise = sdk.auth.login({
email: 'wolfulus@gmail.com',
password: 'password',
});
await loginPromise;
expect(scope.pendingMocks().length).toBe(0);
});
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);
const loginPromise = sdk.auth.login({
email: 'wolfulus@gmail.com',
password: 'password',
});
await loginPromise;
expect(sdk.auth.token).toBe('auth_token');
const logoutPromise = sdk.auth.logout();
await logoutPromise;
expect(sdk.auth.token).toBeNull();
});
});