Skip to content

Commit bafa852

Browse files
fix unit test. accept tags as object for e2e tests
1 parent 26211dc commit bafa852

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

packages/@n8n/api-types/src/dto/workflows/base-workflow.dto.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,17 @@ export const baseWorkflowShape = {
7979
parentFolderId: z.string().optional(),
8080

8181
// Tags
82-
tags: z.array(z.string()).optional(),
82+
tags: z
83+
// Accept either array of tag IDs (strings) or array of tag objects ({ id, name }) for backwards compatibility
84+
.union([z.array(z.string()), z.array(z.object({ id: z.string(), name: z.string() }))])
85+
.transform((val): string[] => {
86+
// If array of objects, extract just the ids
87+
if (val.length > 0 && typeof val[0] === 'object' && 'id' in val[0]) {
88+
return (val as Array<{ id: string; name: string }>).map((tag) => tag.id);
89+
}
90+
return val as string[];
91+
})
92+
.optional(),
8393

8494
// UI context and builder metadata
8595
uiContext: z.string().optional(),

packages/@n8n/api-types/src/dto/workflows/create-workflow.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class CreateWorkflowDto extends Z.class({
2121
// always overridden in the controller to enforce security policies:
2222
// - active is always set to false (workflows must be activated via separate endpoint)
2323
// - activeVersionId is always set to null (managed by the system)
24-
active: z.boolean().optional().default(false),
24+
active: z.boolean().optional(),
2525
activeVersionId: z.string().nullable().optional(),
2626
activeVersion: z.unknown().optional(),
2727

packages/cli/src/workflows/__tests__/workflows.controller.test.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import type { ImportWorkflowFromUrlDto } from '@n8n/api-types';
1+
import type { CreateWorkflowDto, ImportWorkflowFromUrlDto } from '@n8n/api-types';
22
import type { Logger } from '@n8n/backend-common';
3-
import type { AuthenticatedRequest, IExecutionResponse, CredentialsEntity, User } from '@n8n/db';
43
import { WorkflowEntity } from '@n8n/db';
5-
import type { WorkflowRepository } from '@n8n/db';
4+
import type {
5+
AuthenticatedRequest,
6+
IExecutionResponse,
7+
CredentialsEntity,
8+
User,
9+
WorkflowRepository,
10+
} from '@n8n/db';
611
import axios from 'axios';
712
import type { Response } from 'express';
813
import { mock } from 'jest-mock-extended';
914

15+
import { WorkflowsController } from '../workflows.controller';
16+
17+
import type { CredentialsService } from '@/credentials/credentials.service';
1018
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
1119
import { ForbiddenError } from '@/errors/response-errors/forbidden.error';
1220
import type { ExecutionService } from '@/executions/execution.service';
13-
import type { CredentialsService } from '@/credentials/credentials.service';
14-
import type { EnterpriseWorkflowService } from '@/workflows/workflow.service.ee';
1521
import type { License } from '@/license';
16-
import type { WorkflowRequest } from '../workflow.request';
1722
import type { ProjectService } from '@/services/project.service.ee';
18-
19-
import { WorkflowsController } from '../workflows.controller';
23+
import type { EnterpriseWorkflowService } from '@/workflows/workflow.service.ee';
2024

2125
jest.mock('axios');
2226

@@ -190,13 +194,13 @@ describe('WorkflowsController', () => {
190194
* Arrange
191195
*/
192196
const mockUser = mock<User>({ id: 'user-123' });
193-
const mockRequest = mock<WorkflowRequest.Create>({
197+
const mockBody: CreateWorkflowDto = {
198+
name: 'Test Workflow',
199+
nodes: [],
200+
connections: {},
201+
};
202+
const mockRequest = mock<AuthenticatedRequest>({
194203
user: mockUser,
195-
body: {
196-
name: 'Test Workflow',
197-
nodes: [],
198-
connections: {},
199-
},
200204
});
201205

202206
const mockGlobalCredential = mock<CredentialsEntity>({
@@ -243,7 +247,9 @@ describe('WorkflowsController', () => {
243247
/**
244248
* Act & Assert
245249
*/
246-
await expect(controller.create(mockRequest)).rejects.toThrow(BadRequestError);
250+
await expect(
251+
controller.create(mockRequest, res, mockBody),
252+
).rejects.toThrow(BadRequestError);
247253

248254
/**
249255
* Assert - Verify credentials were fetched with includeGlobal: true
@@ -262,13 +268,13 @@ describe('WorkflowsController', () => {
262268
* Arrange
263269
*/
264270
const mockUser = mock<User>({ id: 'user-123' });
265-
const mockRequest = mock<WorkflowRequest.Create>({
271+
const mockBody: CreateWorkflowDto = {
272+
name: 'Test Workflow',
273+
nodes: [],
274+
connections: {},
275+
};
276+
const mockRequest = mock<AuthenticatedRequest>({
266277
user: mockUser,
267-
body: {
268-
name: 'Test Workflow',
269-
nodes: [],
270-
connections: {},
271-
},
272278
});
273279

274280
const mockGlobalCredential = mock<CredentialsEntity>({
@@ -303,8 +309,12 @@ describe('WorkflowsController', () => {
303309
/**
304310
* Act & Assert
305311
*/
306-
await expect(controller.create(mockRequest)).rejects.toThrow(BadRequestError);
307-
await expect(controller.create(mockRequest)).rejects.toThrow(
312+
await expect(
313+
controller.create(mockRequest, res, mockBody),
314+
).rejects.toThrow(BadRequestError);
315+
await expect(
316+
controller.create(mockRequest, res, mockBody),
317+
).rejects.toThrow(
308318
'The workflow you are trying to save contains credentials that are not shared with you',
309319
);
310320

0 commit comments

Comments
 (0)