Files
directus/packages/sdk/tests/base/auth.test.ts
Nicola Krumschmidt d64ca14348 Explicitly set catch parameters to any type (#7654)
This fixes not being able to build the repo due to type issues
introduced by the Typescript 4.4 option "useUnknownInCatchVariables",
which is enabled by default in strict mode.
2021-08-27 10:33:30 -04:00

124 lines
2.3 KiB
TypeScript

/**
* @jest-environment node
*/
import { Directus } from '../../src';
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');
});
/*
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: any) {
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 {
//
}
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 {
//
}
expect(sdk.auth.token).toBeNull();
});
});