mirror of
https://github.com/directus/directus.git
synced 2026-02-16 08:25:10 -05:00
* add new sdk * update version * fixes and sdk documentation * typing updates, documentation * added missing endpoints * targeting minified version for unpkg * removed unused types file * fixed non minified versions * fix sdk exports * fix the fix * Remove old sdk * Remove old sdk docs * Install types for Jest, add npm test * Rely on npm exclusively * Remove examples folder * Move typescript down * Update sdk.md * added auto refresh and requested changes added more http test calls fixed typing issue in customized types * remove unused endpoint * updated docs * added singletons, fixed typing issues, added password handlers * rename graphql function and fixed system endpoint * Remove unused imports, fix build Co-authored-by: rijkvanzanten <rijkvanzanten@me.com> Co-authored-by: Ben Haynes <ben@rngr.org>
134 lines
2.6 KiB
TypeScript
134 lines
2.6 KiB
TypeScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
|
|
import { Directus } from '../../src';
|
|
import { InvalidRefreshTime, NotAuthenticated } from '../../src/errors';
|
|
import { test } from '../utils';
|
|
|
|
describe('auth', function () {
|
|
test(`static auth should validate token`, async (url, nock) => {
|
|
nock()
|
|
.get('/users/me')
|
|
.query({
|
|
access_token: 'token',
|
|
})
|
|
.reply(203);
|
|
|
|
const sdk = new Directus(url);
|
|
await sdk.auth.static('token');
|
|
});
|
|
|
|
it(`throws when refreshing without authenticating first`, async () => {
|
|
const sdk = new Directus('http://localhost');
|
|
|
|
try {
|
|
await sdk.auth.refresh();
|
|
fail('Should have failed');
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(NotAuthenticated);
|
|
}
|
|
});
|
|
|
|
test(`throws when refresh time resolves to an invalid value`, async (url, nock) => {
|
|
nock()
|
|
.post('/auth/login')
|
|
.reply(200, {
|
|
data: {
|
|
access_token: 'access_token',
|
|
refresh_token: 'refresh_token',
|
|
expires: 10000, // expires in 10 seconds
|
|
},
|
|
});
|
|
|
|
const sdk = new Directus(url);
|
|
try {
|
|
await sdk.auth.login(
|
|
{
|
|
email: 'wolfulus@gmail.com',
|
|
password: 'password',
|
|
},
|
|
{
|
|
refresh: {
|
|
auto: true,
|
|
time: 15000, // but we ask to refresh 15 seconds before the expiration
|
|
},
|
|
}
|
|
);
|
|
fail('Should have failed');
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(InvalidRefreshTime);
|
|
}
|
|
});
|
|
|
|
test(`successful auth should set the token`, async (url, nock) => {
|
|
nock()
|
|
.get('/users/me')
|
|
.query({
|
|
access_token: 'token',
|
|
})
|
|
.reply(203);
|
|
|
|
const sdk = new Directus(url);
|
|
await sdk.auth.static('token');
|
|
|
|
expect(sdk.auth.token);
|
|
});
|
|
|
|
test(`invalid credentials token should not set the token`, async (url, nock) => {
|
|
nock()
|
|
.get('/users/me')
|
|
.reply(401, {
|
|
errors: [
|
|
{
|
|
message: 'Invalid token',
|
|
extensions: {
|
|
code: 'EUNAUTHORIZED',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
const sdk = new Directus(url);
|
|
try {
|
|
await sdk.auth.login({
|
|
email: 'invalid@email.com',
|
|
password: 'invalid_password',
|
|
});
|
|
fail('Should have thrown due to error response');
|
|
} catch (err) {
|
|
//
|
|
}
|
|
|
|
expect(sdk.auth.token).toBeNull();
|
|
});
|
|
|
|
test(`invalid static token should not set the token`, async (url, nock) => {
|
|
nock()
|
|
.get('/users/me')
|
|
.query({
|
|
access_token: 'token',
|
|
})
|
|
.reply(401, {
|
|
errors: [
|
|
{
|
|
message: 'Invalid token',
|
|
extensions: {
|
|
code: 'EUNAUTHORIZED',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
const sdk = new Directus(url);
|
|
try {
|
|
await sdk.auth.static('token');
|
|
fail('Should have thrown due to error response');
|
|
} catch (err) {
|
|
//
|
|
}
|
|
|
|
expect(sdk.auth.token).toBeNull();
|
|
});
|
|
});
|