fix: Fix auth tests.

This commit is contained in:
Sam 2022-05-13 16:31:07 +02:00
parent 35f28b849d
commit c2a8fc7206
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
13 changed files with 80 additions and 40 deletions

View File

@ -28,7 +28,6 @@ async function createApiContext({
const readConfigFile = createReadConfigFile({ buildDirectory });
const config = await readConfigFile('config.json');
return {
authenticated: false,
authorize: createAuthorize({ session }),
config,
connections,

View File

@ -30,10 +30,10 @@ test('authorize public object', async () => {
test('authorize protected object, no roles', async () => {
const auth = { public: false };
let authorize = createAuthorize({ authenticated: false });
let authorize = createAuthorize({});
expect(authorize({ auth })).toBe(false);
authorize = createAuthorize({ authenticated: true });
authorize = createAuthorize({ session: { user: { sub: 'sub' } } });
expect(authorize({ auth })).toBe(true);
});
@ -43,19 +43,19 @@ test('authorize role protected object', async () => {
let authorize = createAuthorize({});
expect(authorize({ auth })).toBe(false);
authorize = createAuthorize({ authenticated: true });
authorize = createAuthorize({ session: { user: { sub: 'sub' } } });
expect(authorize({ auth })).toBe(false);
authorize = createAuthorize({ authenticated: true, roles: [] });
authorize = createAuthorize({ session: { user: { sub: 'sub', roles: [] } } });
expect(authorize({ auth })).toBe(false);
authorize = createAuthorize({ authenticated: true, roles: ['role2'] });
authorize = createAuthorize({ session: { user: { sub: 'sub', roles: ['role2'] } } });
expect(authorize({ auth })).toBe(false);
authorize = createAuthorize({ authenticated: true, roles: ['role1'] });
authorize = createAuthorize({ session: { user: { sub: 'sub', roles: ['role1'] } } });
expect(authorize({ auth })).toBe(true);
authorize = createAuthorize({ authenticated: true, roles: ['role1', 'role2'] });
authorize = createAuthorize({ session: { user: { sub: 'sub', roles: ['role1', 'role2'] } } });
expect(authorize({ auth })).toBe(true);
});

View File

@ -75,7 +75,7 @@ test('getPageConfig, protected, with user', async () => {
});
const res = await getPageConfig(
testContext({ readConfigFile: mockReadConfigFile, user: { sub: 'sub' } }),
testContext({ readConfigFile: mockReadConfigFile, session: { user: { sub: 'sub' } } }),
{ pageId: 'pageId' }
);
expect(res).toEqual({

View File

@ -98,7 +98,7 @@ const authenticatedContext = testContext({
readConfigFile: mockReadConfigFile,
operators,
secrets,
user: { sub: 'sub' },
session: { user: { sub: 'sub' } },
});
const defaultParams = {

View File

@ -23,7 +23,6 @@ async function getRootConfig(context) {
getHomeAndMenus(context),
]);
return {
authenticated: context.authenticated,
home,
lowdefyGlobal,
menus,

View File

@ -57,7 +57,6 @@ test('getRootConfig', async () => {
mockGetMenu.mockImplementation(() => menus);
const res = await getRootConfig(context);
expect(res).toEqual({
authenticated: undefined,
home: {
configured: false,
pageId: 'page',

View File

@ -21,7 +21,10 @@ import testContext from '../../../test/testContext.js';
const mockReadConfigFile = jest.fn();
const context = testContext({ readConfigFile: mockReadConfigFile });
const contextUser = testContext({ readConfigFile: mockReadConfigFile, user: { sub: 'sub' } });
const contextUser = testContext({
readConfigFile: mockReadConfigFile,
session: { user: { sub: 'sub' } },
});
beforeEach(() => {
mockReadConfigFile.mockReset();

View File

@ -31,16 +31,13 @@ function testContext({
_test: () => 'test',
},
readConfigFile,
roles,
secrets = {},
setHeader,
user,
session,
protocol = 'https',
} = {}) {
const authenticated = user && !!user.sub;
return {
authenticated,
authorize: createAuthorize({ authenticated, roles }),
authorize: createAuthorize({ session }),
config,
connections,
headers,
@ -51,7 +48,7 @@ function testContext({
readConfigFile,
secrets,
setHeader,
user,
user: session?.user,
};
}

View File

@ -30,9 +30,14 @@ test('buildAuth default', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'a', type: 'Context', auth: { public: true } },
@ -47,9 +52,14 @@ test('buildAuth no pages', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {},
},
providers: [],
session: {},
theme: {},
},
});
});
@ -71,10 +81,15 @@ test('buildAuth all protected, some public', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
public: ['a', 'b'],
roles: {},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'a', type: 'Context', auth: { public: true } },
@ -101,10 +116,15 @@ test('buildAuth all public, some protected', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
protected: ['a', 'b'],
roles: {},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'a', type: 'Context', auth: { public: false } },
@ -131,10 +151,15 @@ test('buildAuth all public', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
public: true,
roles: {},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'a', type: 'Context', auth: { public: true } },
@ -161,10 +186,15 @@ test('buildAuth all protected', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
protected: true,
roles: {},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'a', type: 'Context', auth: { public: false } },
@ -193,12 +223,17 @@ test('buildAuth roles', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {
role1: ['page1'],
role2: ['page1', 'page2'],
},
},
providers: [],
session: {},
theme: {},
},
pages: [
{ id: 'page1', type: 'Context', auth: { public: false, roles: ['role1', 'role2'] } },
@ -240,12 +275,17 @@ test('buildAuth roles and protected pages array', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {
role1: ['page1'],
},
protected: ['page1'],
},
providers: [],
session: {},
theme: {},
},
pages: [{ id: 'page1', type: 'Context', auth: { public: false, roles: ['role1'] } }],
});
@ -266,12 +306,17 @@ test('buildAuth roles and protected true', async () => {
const res = await buildAuth({ components, context });
expect(res).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {
role1: ['page1'],
},
protected: true,
},
providers: [],
session: {},
theme: {},
},
pages: [{ id: 'page1', type: 'Context', auth: { public: false, roles: ['role1'] } }],
});

View File

@ -49,16 +49,11 @@ async function validateAuthConfig({ components }) {
components.auth.theme = {};
}
const { valid } = validate({
validate({
schema: lowdefySchema.definitions.authConfig,
data: components.auth,
returnErrors: true,
});
if (!valid) {
throw new Error('lowdefy.auth does not match schema.'); // TODO: Better error message
}
if (
(components.auth.pages.protected === true && components.auth.pages.public === true) ||
(type.isArray(components.auth.pages.protected) && type.isArray(components.auth.pages.public))

View File

@ -24,9 +24,14 @@ test('validateAuthConfig no auth defined', async () => {
const result = await validateAuthConfig({ components, context });
expect(result).toEqual({
auth: {
callbacks: [],
events: [],
pages: {
roles: {},
},
providers: [],
session: {},
theme: {},
},
});
});
@ -49,7 +54,7 @@ test('validateAuthConfig invalid auth config', async () => {
},
};
await expect(validateAuthConfig({ components, context })).rejects.toThrow(
'App "config.auth.pages.protected.$" should be an array of strings.'
'App "auth.pages.protected.$" should be an array of strings.'
);
components = {
auth: {
@ -59,7 +64,7 @@ test('validateAuthConfig invalid auth config', async () => {
},
};
await expect(validateAuthConfig({ components, context })).rejects.toThrow(
'App "config.auth.pages.roles" should be an object.'
'App "auth.pages.roles" should be an object.'
);
});

View File

@ -42,12 +42,10 @@ test('empty components', async () => {
test('page auth config', async () => {
const components = {
lowdefy: '1.0.0',
config: {
auth: {
pages: {
protected: true,
public: ['page1'],
},
auth: {
pages: {
protected: true,
public: ['page1'],
},
},
};

View File

@ -69,7 +69,7 @@ export default {
type: 'object',
additionalProperties: false,
errorMessage: {
type: 'App "config.auth" should be an object.',
type: 'App "auth" should be an object.',
},
properties: {
callbacks: {
@ -144,28 +144,28 @@ export default {
protected: {
type: ['array', 'boolean'],
errorMessage: {
type: 'App "config.auth.pages.protected.$" should be an array of strings.',
type: 'App "auth.pages.protected.$" should be an array of strings.',
},
items: {
type: 'string',
description:
'Page ids for which authentication is required. When specified, all unspecified pages will be public.',
errorMessage: {
type: 'App "config.auth.pages.protected.$" should be an array of strings.',
type: 'App "auth.pages.protected.$" should be an array of strings.',
},
},
},
public: {
type: ['array', 'boolean'],
errorMessage: {
type: 'App "config.auth.pages.public.$" should be an array of strings.',
type: 'App "auth.pages.public.$" should be an array of strings.',
},
items: {
type: 'string',
description:
'Page ids for which authentication is not required. When specified, all unspecified pages will be protected.',
errorMessage: {
type: 'App "config.auth.pages.public.$" should be an array of strings.',
type: 'App "auth.pages.public.$" should be an array of strings.',
},
},
},
@ -178,12 +178,12 @@ export default {
type: 'string',
},
errorMessage: {
type: 'App "config.auth.pages.roles.[role]" should be an array of strings.',
type: 'App "auth.pages.roles.[role]" should be an array of strings.',
},
},
},
errorMessage: {
type: 'App "config.auth.pages.roles" should be an object.',
type: 'App "auth.pages.roles" should be an object.',
},
},
},