feat(API): Add tag support to public API (#8588)

Co-authored-by: Jesús Burgers <jesus.burgers@chakray.co.uk>
Co-authored-by: Jesús Burgers <43568066+jburgers-chakray@users.noreply.github.com>
This commit is contained in:
Omar Ajoue
2024-02-09 15:10:03 +00:00
committed by GitHub
parent 64b10d7f5c
commit a743a40376
22 changed files with 1131 additions and 6 deletions

View File

@@ -0,0 +1,337 @@
import type { SuperAgentTest } from 'supertest';
import Container from 'typedi';
import type { User } from '@db/entities/User';
import { TagRepository } from '@db/repositories/tag.repository';
import { randomApiKey } from '../shared/random';
import * as utils from '../shared/utils/';
import * as testDb from '../shared/testDb';
import { createUser } from '../shared/db/users';
import { createTag } from '../shared/db/tags';
let owner: User;
let member: User;
let authOwnerAgent: SuperAgentTest;
let authMemberAgent: SuperAgentTest;
const testServer = utils.setupTestServer({ endpointGroups: ['publicApi'] });
beforeAll(async () => {
owner = await createUser({
role: 'global:owner',
apiKey: randomApiKey(),
});
member = await createUser({
role: 'global:member',
apiKey: randomApiKey(),
});
});
beforeEach(async () => {
await testDb.truncate(['Tag']);
authOwnerAgent = testServer.publicApiAgentFor(owner);
authMemberAgent = testServer.publicApiAgentFor(member);
});
const testWithAPIKey =
(method: 'get' | 'post' | 'put' | 'delete', url: string, apiKey: string | null) => async () => {
void authOwnerAgent.set({ 'X-N8N-API-KEY': apiKey });
const response = await authOwnerAgent[method](url);
expect(response.statusCode).toBe(401);
};
describe('GET /tags', () => {
test('should fail due to missing API Key', testWithAPIKey('get', '/tags', null));
test('should fail due to invalid API Key', testWithAPIKey('get', '/tags', 'abcXYZ'));
test('should return all tags', async () => {
await Promise.all([createTag({}), createTag({}), createTag({})]);
const response = await authMemberAgent.get('/tags');
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(3);
expect(response.body.nextCursor).toBeNull();
for (const tag of response.body.data) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
}
});
test('should return all tags with pagination', async () => {
await Promise.all([createTag({}), createTag({}), createTag({})]);
const response = await authMemberAgent.get('/tags?limit=1');
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(1);
expect(response.body.nextCursor).not.toBeNull();
const response2 = await authMemberAgent.get(`/tags?limit=1&cursor=${response.body.nextCursor}`);
expect(response2.statusCode).toBe(200);
expect(response2.body.data.length).toBe(1);
expect(response2.body.nextCursor).not.toBeNull();
expect(response2.body.nextCursor).not.toBe(response.body.nextCursor);
const responses = [...response.body.data, ...response2.body.data];
for (const tag of responses) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
}
// check that we really received a different result
expect(response.body.data[0].id).not.toBe(response2.body.data[0].id);
});
});
describe('GET /tags/:id', () => {
test('should fail due to missing API Key', testWithAPIKey('get', '/tags/gZqmqiGAuo1dHT7q', null));
test(
'should fail due to invalid API Key',
testWithAPIKey('get', '/tags/gZqmqiGAuo1dHT7q', 'abcXYZ'),
);
test('should fail due to non-existing tag', async () => {
const response = await authOwnerAgent.get('/tags/gZqmqiGAuo1dHT7q');
expect(response.statusCode).toBe(404);
});
test('should retrieve tag', async () => {
// create tag
const tag = await createTag({});
const response = await authMemberAgent.get(`/tags/${tag.id}`);
expect(response.statusCode).toBe(200);
const { id, name, createdAt, updatedAt } = response.body;
expect(id).toEqual(tag.id);
expect(name).toEqual(tag.name);
expect(createdAt).toEqual(tag.createdAt.toISOString());
expect(updatedAt).toEqual(tag.updatedAt.toISOString());
});
});
describe('DELETE /tags/:id', () => {
test(
'should fail due to missing API Key',
testWithAPIKey('delete', '/tags/gZqmqiGAuo1dHT7q', null),
);
test(
'should fail due to invalid API Key',
testWithAPIKey('delete', '/tags/gZqmqiGAuo1dHT7q', 'abcXYZ'),
);
test('should fail due to non-existing tag', async () => {
const response = await authOwnerAgent.delete('/tags/gZqmqiGAuo1dHT7q');
expect(response.statusCode).toBe(404);
});
test('owner should delete the tag', async () => {
// create tag
const tag = await createTag({});
const response = await authOwnerAgent.delete(`/tags/${tag.id}`);
expect(response.statusCode).toBe(200);
const { id, name, createdAt, updatedAt } = response.body;
expect(id).toEqual(tag.id);
expect(name).toEqual(tag.name);
expect(createdAt).toEqual(tag.createdAt.toISOString());
expect(updatedAt).toEqual(tag.updatedAt.toISOString());
// make sure the tag actually deleted from the db
const deletedTag = await Container.get(TagRepository).findOneBy({
id: tag.id,
});
expect(deletedTag).toBeNull();
});
test('non-owner should not delete tag', async () => {
// create tag
const tag = await createTag({});
const response = await authMemberAgent.delete(`/tags/${tag.id}`);
expect(response.statusCode).toBe(403);
const { message } = response.body;
expect(message).toEqual('Forbidden');
// make sure the tag was not deleted from the db
const notDeletedTag = await Container.get(TagRepository).findOneBy({
id: tag.id,
});
expect(notDeletedTag).not.toBeNull();
});
});
describe('POST /tags', () => {
test('should fail due to missing API Key', testWithAPIKey('post', '/tags', null));
test('should fail due to invalid API Key', testWithAPIKey('post', '/tags', 'abcXYZ'));
test('should fail due to invalid body', async () => {
const response = await authOwnerAgent.post('/tags').send({});
expect(response.statusCode).toBe(400);
});
test('should create tag', async () => {
const payload = {
name: 'Tag 1',
};
const response = await authMemberAgent.post('/tags').send(payload);
expect(response.statusCode).toBe(201);
const { id, name, createdAt, updatedAt } = response.body;
expect(id).toBeDefined();
expect(name).toBe(payload.name);
expect(createdAt).toBeDefined();
expect(updatedAt).toEqual(createdAt);
// check if created tag in DB
const tag = await Container.get(TagRepository).findOne({
where: {
id,
},
});
expect(tag?.name).toBe(name);
expect(tag?.createdAt.toISOString()).toEqual(createdAt);
expect(tag?.updatedAt.toISOString()).toEqual(updatedAt);
});
test('should not create tag if tag with same name exists', async () => {
const tag = {
name: 'Tag 1',
};
// create tag
await createTag(tag);
const response = await authMemberAgent.post('/tags').send(tag);
expect(response.statusCode).toBe(409);
const { message } = response.body;
expect(message).toBe('Tag already exists');
});
});
describe('PUT /tags/:id', () => {
test('should fail due to missing API Key', testWithAPIKey('put', '/tags/gZqmqiGAuo1dHT7q', null));
test(
'should fail due to invalid API Key',
testWithAPIKey('put', '/tags/gZqmqiGAuo1dHT7q', 'abcXYZ'),
);
test('should fail due to non-existing tag', async () => {
const response = await authOwnerAgent.put('/tags/gZqmqiGAuo1dHT7q').send({
name: 'testing',
});
expect(response.statusCode).toBe(404);
});
test('should fail due to invalid body', async () => {
const response = await authOwnerAgent.put('/tags/gZqmqiGAuo1dHT7q').send({});
expect(response.statusCode).toBe(400);
});
test('should update tag', async () => {
const tag = await createTag({});
const payload = {
name: 'New name',
};
const response = await authOwnerAgent.put(`/tags/${tag.id}`).send(payload);
const { id, name, updatedAt } = response.body;
expect(response.statusCode).toBe(200);
expect(id).toBe(tag.id);
expect(name).toBe(payload.name);
expect(updatedAt).not.toBe(tag.updatedAt.toISOString());
// check updated tag in DB
const dbTag = await Container.get(TagRepository).findOne({
where: {
id,
},
});
expect(dbTag?.name).toBe(payload.name);
expect(dbTag?.updatedAt.getTime()).toBeGreaterThan(tag.updatedAt.getTime());
});
test('should fail if there is already a tag with a the new name', async () => {
const toUpdateTag = await createTag({});
const otherTag = await createTag({ name: 'Some name' });
const payload = {
name: otherTag.name,
};
const response = await authOwnerAgent.put(`/tags/${toUpdateTag.id}`).send(payload);
expect(response.statusCode).toBe(409);
const { message } = response.body;
expect(message).toBe('Tag already exists');
// check tags haven't be updated in DB
const toUpdateTagFromDb = await Container.get(TagRepository).findOne({
where: {
id: toUpdateTag.id,
},
});
expect(toUpdateTagFromDb?.name).toEqual(toUpdateTag.name);
expect(toUpdateTagFromDb?.createdAt.toISOString()).toEqual(toUpdateTag.createdAt.toISOString());
expect(toUpdateTagFromDb?.updatedAt.toISOString()).toEqual(toUpdateTag.updatedAt.toISOString());
const otherTagFromDb = await Container.get(TagRepository).findOne({
where: {
id: otherTag.id,
},
});
expect(otherTagFromDb?.name).toEqual(otherTag.name);
expect(otherTagFromDb?.createdAt.toISOString()).toEqual(otherTag.createdAt.toISOString());
expect(otherTagFromDb?.updatedAt.toISOString()).toEqual(otherTag.updatedAt.toISOString());
});
});

View File

@@ -1,4 +1,5 @@
import type { SuperAgentTest } from 'supertest';
import config from '@/config';
import Container from 'typedi';
import type { INode } from 'n8n-workflow';
import { STARTING_NODES } from '@/constants';
@@ -250,6 +251,43 @@ describe('GET /workflows', () => {
}
});
test('should return all owned workflows filtered by name', async () => {
const workflowName = 'Workflow 1';
const [workflow] = await Promise.all([
createWorkflow({ name: workflowName }, member),
createWorkflow({}, member),
]);
const response = await authMemberAgent.get(`/workflows?name=${workflowName}`);
expect(response.statusCode).toBe(200);
expect(response.body.data.length).toBe(1);
const {
id,
connections,
active,
staticData,
nodes,
settings,
name,
createdAt,
updatedAt,
tags: wfTags,
} = response.body.data[0];
expect(id).toBeDefined();
expect(name).toBe(workflowName);
expect(connections).toBeDefined();
expect(active).toBe(false);
expect(staticData).toBeDefined();
expect(nodes).toBeDefined();
expect(settings).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
});
test('should return all workflows for owner', async () => {
await Promise.all([
createWorkflow({}, owner),
@@ -1111,3 +1149,308 @@ describe('PUT /workflows/:id', () => {
expect(sharedWorkflow?.role).toEqual('workflow:owner');
});
});
describe('GET /workflows/:id/tags', () => {
test('should fail due to missing API Key', testWithAPIKey('get', '/workflows/2/tags', null));
test('should fail due to invalid API Key', testWithAPIKey('get', '/workflows/2/tags', 'abcXYZ'));
test('should fail if workflowTagsDisabled', async () => {
config.set('workflowTagsDisabled', true);
const response = await authOwnerAgent.get('/workflows/2/tags');
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe('Workflow Tags Disabled');
});
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);
const response = await authMemberAgent.get(`/workflows/${workflow.id}/tags`);
expect(response.statusCode).toBe(200);
expect(response.body.length).toBe(2);
for (const tag of response.body) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
tags.forEach((tag: TagEntity) => {
expect(tags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
});
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`);
expect(response.statusCode).toBe(200);
expect(response.body.length).toBe(0);
});
});
describe('PUT /workflows/:id/tags', () => {
test('should fail due to missing API Key', testWithAPIKey('put', '/workflows/2/tags', null));
test('should fail due to invalid API Key', testWithAPIKey('put', '/workflows/2/tags', 'abcXYZ'));
test('should fail if workflowTagsDisabled', async () => {
config.set('workflowTagsDisabled', true);
const response = await authOwnerAgent.put('/workflows/2/tags').send([]);
expect(response.statusCode).toBe(400);
expect(response.body.message).toBe('Workflow Tags Disabled');
});
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({})]);
const payload = [
{
id: tags[0].id,
},
{
id: tags[1].id,
},
];
const response = await authMemberAgent.put(`/workflows/${workflow.id}/tags`).send(payload);
expect(response.statusCode).toBe(200);
expect(response.body.length).toBe(2);
for (const tag of response.body) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
tags.forEach((tag: TagEntity) => {
expect(tags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
// Check the association in DB
const sharedWorkflow = await Container.get(SharedWorkflowRepository).findOne({
where: {
userId: member.id,
workflowId: workflow.id,
},
relations: ['workflow.tags'],
});
expect(sharedWorkflow?.workflow.tags).toBeDefined();
expect(sharedWorkflow?.workflow.tags?.length).toBe(2);
if (sharedWorkflow?.workflow.tags !== undefined) {
for (const tag of sharedWorkflow?.workflow.tags) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
tags.forEach((tag: TagEntity) => {
expect(tags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
}
});
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]];
const workflow = await createWorkflow({ tags: oldTags }, member);
// Check the association in DB
const oldSharedWorkflow = await Container.get(SharedWorkflowRepository).findOne({
where: {
userId: member.id,
workflowId: workflow.id,
},
relations: ['workflow.tags'],
});
expect(oldSharedWorkflow?.workflow.tags).toBeDefined();
expect(oldSharedWorkflow?.workflow.tags?.length).toBe(2);
if (oldSharedWorkflow?.workflow.tags !== undefined) {
for (const tag of oldSharedWorkflow?.workflow.tags) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
oldTags.forEach((tag: TagEntity) => {
expect(oldTags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
}
const payload = [
{
id: newTags[0].id,
},
{
id: newTags[1].id,
},
];
const response = await authMemberAgent.put(`/workflows/${workflow.id}/tags`).send(payload);
expect(response.statusCode).toBe(200);
expect(response.body.length).toBe(2);
for (const tag of response.body) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
newTags.forEach((tag: TagEntity) => {
expect(newTags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
// Check the association in DB
const sharedWorkflow = await Container.get(SharedWorkflowRepository).findOne({
where: {
userId: member.id,
workflowId: workflow.id,
},
relations: ['workflow.tags'],
});
expect(sharedWorkflow?.workflow.tags).toBeDefined();
expect(sharedWorkflow?.workflow.tags?.length).toBe(2);
if (sharedWorkflow?.workflow.tags !== undefined) {
for (const tag of sharedWorkflow?.workflow.tags) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
newTags.forEach((tag: TagEntity) => {
expect(newTags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
}
});
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);
// Check the association in DB
const oldSharedWorkflow = await Container.get(SharedWorkflowRepository).findOne({
where: {
userId: member.id,
workflowId: workflow.id,
},
relations: ['workflow.tags'],
});
expect(oldSharedWorkflow?.workflow.tags).toBeDefined();
expect(oldSharedWorkflow?.workflow.tags?.length).toBe(2);
if (oldSharedWorkflow?.workflow.tags !== undefined) {
for (const tag of oldSharedWorkflow?.workflow.tags) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
oldTags.forEach((tag: TagEntity) => {
expect(oldTags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
}
const payload = [
{
id: oldTags[0].id,
},
{
id: 'TagDoesNotExist',
},
];
const response = await authMemberAgent.put(`/workflows/${workflow.id}/tags`).send(payload);
expect(response.statusCode).toBe(404);
expect(response.body.message).toBe('Some tags not found');
// Check the association in DB
const sharedWorkflow = await Container.get(SharedWorkflowRepository).findOne({
where: {
userId: member.id,
workflowId: workflow.id,
},
relations: ['workflow.tags'],
});
expect(sharedWorkflow?.workflow.tags).toBeDefined();
expect(sharedWorkflow?.workflow.tags?.length).toBe(2);
if (sharedWorkflow?.workflow.tags !== undefined) {
for (const tag of sharedWorkflow?.workflow.tags) {
const { id, name, createdAt, updatedAt } = tag;
expect(id).toBeDefined();
expect(name).toBeDefined();
expect(createdAt).toBeDefined();
expect(updatedAt).toBeDefined();
oldTags.forEach((tag: TagEntity) => {
expect(oldTags.some((savedTag) => savedTag.id === tag.id)).toBe(true);
});
}
}
});
});