🧪 Add tests

This commit is contained in:
Iván Ovejero 2022-04-20 17:55:17 +02:00
parent 0e439c18a0
commit fdcf0a7298
4 changed files with 133 additions and 2 deletions

View file

@ -58,6 +58,7 @@ class LoadNodesAndCredentialsClass {
// In case "n8n" package is the root and the packages are
// in the "node_modules" folder underneath it.
path.join(__dirname, '..', '..', 'node_modules', 'n8n-workflow'),
path.join(__dirname, '..', 'node_modules', 'n8n-workflow'), // for test run
];
for (const checkPath of checkPaths) {
try {

View file

@ -0,0 +1,125 @@
import express from 'express';
import * as utils from './shared/utils';
import * as testDb from './shared/testDb';
import { RESPONSE_ERROR_MESSAGES } from '../../src/constants';
import type { Role } from '../../src/databases/entities/Role';
import { CredentialTypes, LoadNodesAndCredentials } from '../../src';
const SCOPES_ENDPOINT = '/oauth2-credential/scopes';
let app: express.Application;
let testDbName = '';
let globalOwnerRole: Role;
beforeAll(async () => {
app = utils.initTestServer({ endpointGroups: ['oauth2-credential'], applyAuth: true });
const initResult = await testDb.init();
testDbName = initResult.testDbName;
utils.initConfigFile();
globalOwnerRole = await testDb.getGlobalOwnerRole();
utils.initTestLogger();
const loadNodesAndCredentials = LoadNodesAndCredentials();
await loadNodesAndCredentials.init();
const credentialTypes = CredentialTypes();
await credentialTypes.init(loadNodesAndCredentials.credentialTypes);
});
afterAll(async () => {
await testDb.terminate(testDbName);
});
describe('OAuth2 scopes', () => {
beforeEach(async () => {
await testDb.truncate(['User'], testDbName);
});
test(`GET ${SCOPES_ENDPOINT} should return scopes - comma-delimited`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent
.get(SCOPES_ENDPOINT)
.query({ credentialType: 'twistOAuth2Api' });
expect(response.statusCode).toBe(200);
const scopes = response.body.data;
const TWIST_OAUTH2_API_SCOPES_TOTAL = 6;
expect(Array.isArray(scopes)).toBe(true);
expect(scopes.length).toBe(TWIST_OAUTH2_API_SCOPES_TOTAL);
});
test(`GET ${SCOPES_ENDPOINT} should return scopes - whitespace-delimited`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent
.get(SCOPES_ENDPOINT)
.query({ credentialType: 'dropboxOAuth2Api' });
expect(response.statusCode).toBe(200);
const scopes = response.body.data;
const DROPBOX_OAUTH2_API_SCOPES_TOTAL = 4;
expect(Array.isArray(scopes)).toBe(true);
expect(scopes.length).toBe(DROPBOX_OAUTH2_API_SCOPES_TOTAL);
});
test(`GET ${SCOPES_ENDPOINT} should return scope - non-delimited`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent
.get(SCOPES_ENDPOINT)
.query({ credentialType: 'harvestOAuth2Api' });
expect(response.statusCode).toBe(200);
const scopes = response.body.data;
expect(Array.isArray(scopes)).toBe(true);
expect(scopes.length).toBe(1);
});
test(`GET ${SCOPES_ENDPOINT} should fail with missing credential type`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent.get(SCOPES_ENDPOINT);
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe(RESPONSE_ERROR_MESSAGES.NO_CREDENTIAL_TYPE);
});
test(`GET ${SCOPES_ENDPOINT} should fail with non-OAuth2 credential type`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent
.get(SCOPES_ENDPOINT)
.query({ credentialType: 'disqusApi' });
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe(RESPONSE_ERROR_MESSAGES.CREDENTIAL_TYPE_NOT_OAUTH2);
});
test(`GET ${SCOPES_ENDPOINT} should fail with wrong OAuth2 credential type`, async () => {
const ownerShell = await testDb.createUserShell(globalOwnerRole);
const authOwnerShellAgent = utils.createAgent(app, { auth: true, user: ownerShell });
const response = await authOwnerShellAgent
.get(SCOPES_ENDPOINT)
.query({ credentialType: 'wrongOAuth2Api' });
expect(response.statusCode).toBe(500);
expect(response.body.message).toBe(`${RESPONSE_ERROR_MESSAGES.NO_CREDENTIAL}: wrongOAuth2Api`);
});
});

View file

@ -15,7 +15,7 @@ export type SmtpTestAccount = {
};
};
type EndpointGroup = 'me' | 'users' | 'auth' | 'owner' | 'passwordReset' | 'credentials';
type EndpointGroup = 'me' | 'users' | 'auth' | 'owner' | 'passwordReset' | 'credentials' | 'oauth2-credential';
export type CredentialPayload = {
name: string;

View file

@ -26,6 +26,7 @@ import { credentialsController } from '../../../src/api/credentials.api';
import type { User } from '../../../src/databases/entities/User';
import type { EndpointGroup, SmtpTestAccount } from './types';
import type { N8nApp } from '../../../src/UserManagement/Interfaces';
import { oauth2CredentialController } from '../../../src/api/oauth2Credential.api';
/**
* Initialize a test server.
@ -63,6 +64,7 @@ export function initTestServer({
if (routerEndpoints.length) {
const map: Record<string, express.Router> = {
credentials: credentialsController,
'oauth2-credential': oauth2CredentialController,
};
for (const group of routerEndpoints) {
@ -105,7 +107,10 @@ const classifyEndpointGroups = (endpointGroups: string[]) => {
const functionEndpoints: string[] = [];
endpointGroups.forEach((group) =>
(group === 'credentials' ? routerEndpoints : functionEndpoints).push(group),
(['credentials', 'oauth2-credential'].includes(group)
? routerEndpoints
: functionEndpoints
).push(group),
);
return [routerEndpoints, functionEndpoints];