refactor(core): Update tag endpoints to use DTOs and injectable config (#12380)

This commit is contained in:
Ricardo Espinoza
2025-01-09 14:17:11 -05:00
committed by GitHub
parent 95f055d23a
commit b1a40a231b
26 changed files with 282 additions and 103 deletions

View File

@@ -1,8 +1,8 @@
import { GlobalConfig } from '@n8n/config';
import { Container } from '@n8n/di';
import type { INode } from 'n8n-workflow';
import { ActiveWorkflowManager } from '@/active-workflow-manager';
import config from '@/config';
import { STARTING_NODES } from '@/constants';
import type { Project } from '@/databases/entities/project';
import type { TagEntity } from '@/databases/entities/tag-entity';
@@ -36,6 +36,8 @@ let activeWorkflowManager: ActiveWorkflowManager;
const testServer = utils.setupTestServer({ endpointGroups: ['publicApi'] });
const license = testServer.license;
const globalConfig = Container.get(GlobalConfig);
mockInstance(ExecutionService);
beforeAll(async () => {
@@ -69,6 +71,8 @@ beforeEach(async () => {
authOwnerAgent = testServer.publicApiAgentFor(owner);
authMemberAgent = testServer.publicApiAgentFor(member);
globalConfig.tags.disabled = false;
});
afterEach(async () => {
@@ -1287,8 +1291,8 @@ describe('GET /workflows/:id/tags', () => {
test('should fail due to invalid API Key', testWithAPIKey('get', '/workflows/2/tags', 'abcXYZ'));
test('should fail if workflowTagsDisabled', async () => {
config.set('workflowTagsDisabled', true);
test('should fail if N8N_WORKFLOW_TAGS_DISABLED', async () => {
globalConfig.tags.disabled = true;
const response = await authOwnerAgent.get('/workflows/2/tags');
@@ -1297,16 +1301,12 @@ describe('GET /workflows/:id/tags', () => {
});
test('should fail due to non-existing workflow', async () => {
config.set('workflowTagsDisabled', false);
const response = await authOwnerAgent.get('/workflows/2/tags');
expect(response.statusCode).toBe(404);
});
test('should return all tags of owned workflow', async () => {
config.set('workflowTagsDisabled', false);
const tags = await Promise.all([await createTag({}), await createTag({})]);
const workflow = await createWorkflow({ tags }, member);
@@ -1331,8 +1331,6 @@ describe('GET /workflows/:id/tags', () => {
});
test('should return empty array if workflow does not have tags', async () => {
config.set('workflowTagsDisabled', false);
const workflow = await createWorkflow({}, member);
const response = await authMemberAgent.get(`/workflows/${workflow.id}/tags`);
@@ -1347,8 +1345,8 @@ describe('PUT /workflows/:id/tags', () => {
test('should fail due to invalid API Key', testWithAPIKey('put', '/workflows/2/tags', 'abcXYZ'));
test('should fail if workflowTagsDisabled', async () => {
config.set('workflowTagsDisabled', true);
test('should fail if N8N_WORKFLOW_TAGS_DISABLED', async () => {
globalConfig.tags.disabled = true;
const response = await authOwnerAgent.put('/workflows/2/tags').send([]);
@@ -1357,16 +1355,12 @@ describe('PUT /workflows/:id/tags', () => {
});
test('should fail due to non-existing workflow', async () => {
config.set('workflowTagsDisabled', false);
const response = await authOwnerAgent.put('/workflows/2/tags').send([]);
expect(response.statusCode).toBe(404);
});
test('should add the tags, workflow have not got tags previously', async () => {
config.set('workflowTagsDisabled', false);
const workflow = await createWorkflow({}, member);
const tags = await Promise.all([await createTag({}), await createTag({})]);
@@ -1425,8 +1419,6 @@ describe('PUT /workflows/:id/tags', () => {
});
test('should add the tags, workflow have some tags previously', async () => {
config.set('workflowTagsDisabled', false);
const tags = await Promise.all([await createTag({}), await createTag({}), await createTag({})]);
const oldTags = [tags[0], tags[1]];
const newTags = [tags[0], tags[2]];
@@ -1513,8 +1505,6 @@ describe('PUT /workflows/:id/tags', () => {
});
test('should fail to add the tags as one does not exist, workflow should maintain previous tags', async () => {
config.set('workflowTagsDisabled', false);
const tags = await Promise.all([await createTag({}), await createTag({})]);
const oldTags = [tags[0], tags[1]];
const workflow = await createWorkflow({ tags: oldTags }, member);

View File

@@ -240,6 +240,7 @@ test('should not report outdated instance when up to date', async () => {
test('should report security settings', async () => {
Container.get(GlobalConfig).diagnostics.enabled = true;
const testAudit = await securityAuditService.run(['instance']);
const section = getRiskSection(

View File

@@ -8,6 +8,7 @@ import type { SuperAgentTest } from './shared/types';
import * as utils from './shared/utils/';
let authOwnerAgent: SuperAgentTest;
const testServer = utils.setupTestServer({ endpointGroups: ['tags'] });
beforeAll(async () => {
@@ -22,8 +23,8 @@ beforeEach(async () => {
describe('POST /tags', () => {
test('should create tag', async () => {
const resp = await authOwnerAgent.post('/tags').send({ name: 'test' });
expect(resp.statusCode).toBe(200);
expect(resp.statusCode).toBe(200);
const dbTag = await Container.get(TagRepository).findBy({ name: 'test' });
expect(dbTag.length === 1);
});
@@ -38,4 +39,59 @@ describe('POST /tags', () => {
const dbTag = await Container.get(TagRepository).findBy({ name: 'test' });
expect(dbTag.length).toBe(1);
});
test('should delete tag', async () => {
const newTag = Container.get(TagRepository).create({ name: 'test' });
await Container.get(TagRepository).save(newTag);
const resp = await authOwnerAgent.delete(`/tags/${newTag.id}`);
expect(resp.status).toBe(200);
const dbTag = await Container.get(TagRepository).findBy({ name: 'test' });
expect(dbTag.length).toBe(0);
});
test('should update tag name', async () => {
const newTag = Container.get(TagRepository).create({ name: 'test' });
await Container.get(TagRepository).save(newTag);
const resp = await authOwnerAgent.patch(`/tags/${newTag.id}`).send({ name: 'updated' });
expect(resp.status).toBe(200);
const dbTag = await Container.get(TagRepository).findBy({ name: 'updated' });
expect(dbTag.length).toBe(1);
});
test('should retrieve all tags', async () => {
const newTag = Container.get(TagRepository).create({ name: 'test' });
const savedTag = await Container.get(TagRepository).save(newTag);
const resp = await authOwnerAgent.get('/tags');
expect(resp.status).toBe(200);
expect(resp.body.data.length).toBe(1);
expect(resp.body.data[0]).toMatchObject({
id: savedTag.id,
name: savedTag.name,
createdAt: savedTag.createdAt.toISOString(),
updatedAt: savedTag.updatedAt.toISOString(),
});
});
test('should retrieve all tags with with usage count', async () => {
const newTag = Container.get(TagRepository).create({ name: 'test' });
const savedTag = await Container.get(TagRepository).save(newTag);
const resp = await authOwnerAgent.get('/tags').query({ withUsageCount: 'true' });
expect(resp.status).toBe(200);
expect(resp.body.data.length).toBe(1);
expect(resp.body.data[0]).toMatchObject({
id: savedTag.id,
name: savedTag.name,
createdAt: savedTag.createdAt.toISOString(),
updatedAt: savedTag.updatedAt.toISOString(),
usageCount: 0,
});
});
});

View File

@@ -40,6 +40,7 @@ beforeAll(async () => {
mock(),
mock(),
mock(),
mock(),
);
});

View File

@@ -20,3 +20,8 @@ writeFileSync(
mode: 0o600,
},
);
// This is needed to ensure that `process.env` overrides in tests
// are set before any of the config classes are instantiated.
// TODO: delete this after we are done migrating everything to config classes
import '@/config';