🐛 Fix test to suport API dynamic loading

This commit is contained in:
ricardo 2022-04-25 21:35:26 -04:00
parent 5d1838ccf5
commit 104db9c438
12 changed files with 48 additions and 47 deletions

View file

@ -30,7 +30,7 @@
"start:default": "cd bin && ./n8n",
"start:windows": "cd bin && n8n",
"test": "npm run test:sqlite",
"test:sqlite": "export N8N_LOG_LEVEL='debug'; export DB_TYPE=sqlite; jest",
"test:sqlite": "export N8N_LOG_LEVEL='silent'; export DB_TYPE=sqlite; jest",
"test:postgres": "export N8N_LOG_LEVEL='silent'; export DB_TYPE=postgresdb && jest",
"test:mysql": "export N8N_LOG_LEVEL='silent'; export DB_TYPE=mysqldb && jest",
"watch": "tsc --watch",

View file

@ -18,7 +18,7 @@ let globalOwnerRole: Role;
let globalMemberRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['auth'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['auth'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;

View file

@ -19,7 +19,7 @@ let app: express.Application;
let testDbName = '';
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['auth'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['auth'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;

View file

@ -17,7 +17,7 @@ let testDbName = '';
let globalMemberRole: Role;
beforeAll(async () => {
app = utils.initTestServer({
app = await utils.initTestServer({
applyAuth: true,
endpointGroups: ['me', 'auth', 'owner', 'users'],
});

View file

@ -19,7 +19,7 @@ let globalMemberRole: Role;
let saveCredential: SaveCredentialFunction;
beforeAll(async () => {
app = utils.initTestServer({
app = await utils.initTestServer({
endpointGroups: ['credentials'],
applyAuth: true,
});

View file

@ -18,7 +18,7 @@ let globalOwnerRole: Role;
let globalMemberRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['me'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['me'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;

View file

@ -20,7 +20,7 @@ let testDbName = '';
let globalOwnerRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['owner'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['owner'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;

View file

@ -23,7 +23,7 @@ let globalOwnerRole: Role;
let globalMemberRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['passwordReset'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['passwordReset'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;

View file

@ -27,7 +27,7 @@ let credentialOwnerRole: Role;
jest.mock('../../../src/telemetry');
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['publicApi'], applyAuth: false });
app = await utils.initTestServer({ endpointGroups: ['publicApi'], applyAuth: false });
const initResult = await testDb.init();
testDbName = initResult.testDbName;
@ -71,7 +71,7 @@ afterAll(async () => {
await testDb.terminate(testDbName);
});
test.skip('GET /users should fail due to missing API Key', async () => {
test('GET /users should fail due to missing API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: false, user: owner });
@ -83,7 +83,7 @@ test.skip('GET /users should fail due to missing API Key', async () => {
expect(response.statusCode).toBe(401);
});
test.skip('GET /users should fail due to invalid API Key', async () => {
test('GET /users should fail due to invalid API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
owner.apiKey = null;
@ -95,7 +95,7 @@ test.skip('GET /users should fail due to invalid API Key', async () => {
expect(response.statusCode).toBe(401);
});
test.skip('GET /users should fail due to member trying to access owner only endpoint', async () => {
test('GET /users should fail due to member trying to access owner only endpoint', async () => {
const member = await testDb.createUser();
@ -106,32 +106,21 @@ test.skip('GET /users should fail due to member trying to access owner only endp
expect(response.statusCode).toBe(403);
});
test.skip('GET /users should fail due no instance owner not setup', async () => {
config.set('userManagement.isInstanceOwnerSetUp', false);
test('GET /users should return all users', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
const response = await authOwnerAgent.get('/v1/users');
expect(response.statusCode).toBe(500);
});
test.skip('GET /users should return all users', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
await testDb.createUser();
const response = await authOwnerAgent.get('/v1/users');
expect(response.statusCode).toBe(200);
expect(response.body.users.length).toBe(2);
expect(response.body.data.length).toBe(2);
expect(response.body.nextCursor).toBeNull();
for (const user of response.body.users) {
for (const user of response.body.data) {
const {
id,
email,
@ -153,15 +142,26 @@ test.skip('GET /users should return all users', async () => {
expect(personalizationAnswers).toBeUndefined();
expect(password).toBeUndefined();
expect(resetPasswordToken).toBeUndefined();
//virtual method not working
//expect(isPending).toBe(false);
expect(isPending).toBe(false);
expect(globalRole).toBeUndefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
}
});
test.skip('GET /users/:identifier should fail due to missing API Key', async () => {
test('GET /users should fail due no instance owner not setup', async () => {
config.set('userManagement.isInstanceOwnerSetUp', false);
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
const response = await authOwnerAgent.get('/v1/users');
expect(response.statusCode).toBe(500);
});
test('GET /users/:identifier should fail due to missing API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: false, user: owner });
@ -173,7 +173,7 @@ test.skip('GET /users/:identifier should fail due to missing API Key', async ()
expect(response.statusCode).toBe(401);
});
test.skip('GET /users/:identifier should fail due to invalid API Key', async () => {
test('GET /users/:identifier should fail due to invalid API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
owner.apiKey = null;
@ -185,7 +185,7 @@ test.skip('GET /users/:identifier should fail due to invalid API Key', async ()
expect(response.statusCode).toBe(401);
});
test.skip('GET /users/:identifier should fail due to member trying to access owner only endpoint', async () => {
test('GET /users/:identifier should fail due to member trying to access owner only endpoint', async () => {
const member = await testDb.createUser();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: member });
@ -195,7 +195,7 @@ test.skip('GET /users/:identifier should fail due to member trying to access own
expect(response.statusCode).toBe(403);
});
test.skip('GET /users/:identifier should fail due no instance owner not setup', async () => {
test('GET /users/:identifier should fail due no instance owner not setup', async () => {
config.set('userManagement.isInstanceOwnerSetUp', false);
const owner = await Db.collections.User!.findOneOrFail();
@ -207,7 +207,7 @@ test.skip('GET /users/:identifier should fail due no instance owner not setup',
expect(response.statusCode).toBe(500);
});
test.skip('GET /users/:email with unexisting email should return 404', async () => {
test('GET /users/:email with unexisting email should return 404', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
@ -217,17 +217,17 @@ test.skip('GET /users/:email with unexisting email should return 404', async ()
expect(response.statusCode).toBe(404);
});
test.skip('GET /users/:id with unexisting id should return 404', async () => {
test('GET /users/:id with unexisting id should return 404', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
const response = await authOwnerAgent.get(`/v1/users/123`);
const response = await authOwnerAgent.get(`/v1/users/test@gmail.com`);
expect(response.statusCode).toBe(404);
});
test.skip('GET /users/:email should return a user', async () => {
test('GET /users/:email should return a user', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
@ -264,7 +264,7 @@ test.skip('GET /users/:email should return a user', async () => {
expect(updatedAt).toBeDefined();
});
test.skip('GET /users/:id should return a user', async () => {
test('GET /users/:id should return a user', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });
@ -301,7 +301,7 @@ test.skip('GET /users/:id should return a user', async () => {
expect(updatedAt).toBeDefined();
});
test.skip('POST /users should fail due to missing API Key', async () => {
test('POST /users should fail due to missing API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: false, user: owner });
@ -313,7 +313,7 @@ test.skip('POST /users should fail due to missing API Key', async () => {
expect(response.statusCode).toBe(401);
});
test.skip('POST /users should fail due to invalid API Key', async () => {
test('POST /users should fail due to invalid API Key', async () => {
const owner = await Db.collections.User!.findOneOrFail();
owner.apiKey = null;
@ -325,7 +325,7 @@ test.skip('POST /users should fail due to invalid API Key', async () => {
expect(response.statusCode).toBe(401);
});
test.skip('POST /users should fail due to member trying to access owner only endpoint', async () => {
test('POST /users should fail due to member trying to access owner only endpoint', async () => {
const member = await testDb.createUser();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: member });
@ -335,7 +335,7 @@ test.skip('POST /users should fail due to member trying to access owner only end
expect(response.statusCode).toBe(403);
});
test.skip('POST /users should fail due instance owner not setup', async () => {
test('POST /users should fail due instance owner not setup', async () => {
config.set('userManagement.isInstanceOwnerSetUp', false);
const owner = await Db.collections.User!.findOneOrFail();
@ -347,7 +347,7 @@ test.skip('POST /users should fail due instance owner not setup', async () => {
expect(response.statusCode).toBe(500);
});
test.skip('POST /users should fail due smtp email not setup', async () => {
test('POST /users should fail due smtp email not setup', async () => {
config.set('userManagement.emails.mode', '');
const owner = await Db.collections.User!.findOneOrFail();
@ -359,7 +359,7 @@ test.skip('POST /users should fail due smtp email not setup', async () => {
expect(response.statusCode).toBe(500);
});
test.skip('POST /users should fail due not valid body structure', async () => {
test('POST /users should fail due not valid body structure', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = utils.createAgent(app, { apiPath: 'public', auth: true, user: owner });

View file

@ -11,7 +11,8 @@ export function randomString(min: number, max: number) {
}
export function randomApiKey() {
return randomBytes(20).toString('hex');
const ramdonKey = randomBytes(20).toString('hex');
return `n8n_api_${ramdonKey}`;
}
const chooseRandomly = <T>(array: T[]) => array[Math.floor(Math.random() * array.length)];

View file

@ -35,7 +35,7 @@ import type { N8nApp } from '../../../src/UserManagement/Interfaces';
* @param applyAuth Whether to apply auth middleware to test server.
* @param endpointGroups Groups of endpoints to apply to test server.
*/
export function initTestServer({
export async function initTestServer({
applyAuth,
endpointGroups,
}: {
@ -66,7 +66,7 @@ export function initTestServer({
if (routerEndpoints.length) {
const map: Record<string, express.Router | express.Router[]> = {
credentials: credentialsController,
//publicApi,
publicApi: await loadPublicApiVersions(),
};
for (const group of routerEndpoints) {

View file

@ -29,7 +29,7 @@ let workflowOwnerRole: Role;
let credentialOwnerRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['users'], applyAuth: true });
app = await utils.initTestServer({ endpointGroups: ['users'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;