From 9b991775536ca838d1b278f81126404373389a17 Mon Sep 17 00:00:00 2001 From: Josh Wulf Date: Fri, 14 Jun 2024 20:41:15 +1200 Subject: [PATCH 1/2] fix(zeebe): add headers to all REST method calls method calls other that Topology were not passing headers with the request. They now pass headers. --- ...Topology.spec.ts => Topology-rest.spec.ts} | 0 src/zeebe/zb/ZeebeRESTClient.ts | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) rename src/__tests__/zeebe/integration-rest/{Topology.spec.ts => Topology-rest.spec.ts} (100%) diff --git a/src/__tests__/zeebe/integration-rest/Topology.spec.ts b/src/__tests__/zeebe/integration-rest/Topology-rest.spec.ts similarity index 100% rename from src/__tests__/zeebe/integration-rest/Topology.spec.ts rename to src/__tests__/zeebe/integration-rest/Topology-rest.spec.ts diff --git a/src/zeebe/zb/ZeebeRESTClient.ts b/src/zeebe/zb/ZeebeRESTClient.ts index 2141aef2..cf3c8311 100644 --- a/src/zeebe/zb/ZeebeRESTClient.ts +++ b/src/zeebe/zb/ZeebeRESTClient.ts @@ -26,13 +26,13 @@ const ZEEBE_REST_API_VERSION = 'v1' */ interface TaskChangeSet { /* The due date of the task. Reset by providing an empty String. */ - dueDate: Date | string + dueDate?: Date | string /* The follow-up date of the task. Reset by providing an empty String. */ - followUpDate: Date | string + followUpDate?: Date | string /* The list of candidate users of the task. Reset by providing an empty list. */ - candidateUsers: string[] + candidateUsers?: string[] /* The list of candidate groups of the task. Reset by providing an empty list. */ - candidateGroups: string[] + candidateGroups?: string[] } export class ZeebeRestClient { @@ -111,27 +111,29 @@ export class ZeebeRestClient { } /* Completes a user task with the given key. */ - public completeUserTask({ + public async completeUserTask({ userTaskKey, - variables, + variables = {}, action = 'complete', }: { userTaskKey: string - variables: Record - action: string + variables?: Record + action?: string }) { + const headers = await this.getHeaders() return this.rest.then((rest) => rest.post(`user-tasks/${userTaskKey}/completion`, { body: JSON.stringify({ variables, action, }), + headers, }) ) } /* Assigns a user task with the given key to the given assignee. */ - public assignTask({ + public async assignTask({ userTaskKey, assignee, allowOverride = true, @@ -142,6 +144,8 @@ export class ZeebeRestClient { allowOverride?: boolean action: string }) { + const headers = await this.getHeaders() + return this.rest.then((rest) => rest.post(`user-tasks/${userTaskKey}/assignment`, { body: JSON.stringify({ @@ -149,28 +153,34 @@ export class ZeebeRestClient { action, assignee, }), + headers, }) ) } /** Update a user task with the given key. */ - public updateTask({ + public async updateTask({ userTaskKey, changeset, }: { userTaskKey: string changeset: TaskChangeSet }) { + const headers = await this.getHeaders() + return this.rest.then((rest) => rest.post(`user-tasks/${userTaskKey}/update`, { body: JSON.stringify(changeset), + headers, }) ) } /* Removes the assignee of a task with the given key. */ - public removeAssignee({ userTaskKey }: { userTaskKey: string }) { + public async removeAssignee({ userTaskKey }: { userTaskKey: string }) { + const headers = await this.getHeaders() + return this.rest.then((rest) => - rest.delete(`user-tasks/${userTaskKey}/assignee`) + rest.delete(`user-tasks/${userTaskKey}/assignee`, { headers }) ) } } From 03868450522d4df3dabdcd0861cfeff2102c9aa5 Mon Sep 17 00:00:00 2001 From: Josh Wulf Date: Tue, 18 Jun 2024 13:59:47 +1200 Subject: [PATCH 2/2] test(zeebe): add test for completeUserTask --- .../GetCustomCertificateBuffer.unit.spec.ts | 10 ++-- .../tasklist/tasklist.integration.spec.ts | 6 +-- src/__tests__/testdata/zeebe-user-task.bpmn | 48 +++++++++++++++++++ .../integration-rest/UserTask-rest.spec.ts | 33 +++++++++++++ .../OnConnectionError.spec.ts | 6 +-- .../createProcessInstance-mt.spec.ts | 2 +- src/zeebe/zb/ZeebeRESTClient.ts | 3 +- 7 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 src/__tests__/testdata/zeebe-user-task.bpmn create mode 100644 src/__tests__/zeebe/integration-rest/UserTask-rest.spec.ts diff --git a/src/__tests__/lib/GetCustomCertificateBuffer.unit.spec.ts b/src/__tests__/lib/GetCustomCertificateBuffer.unit.spec.ts index 84eaf0ee..481d5b67 100644 --- a/src/__tests__/lib/GetCustomCertificateBuffer.unit.spec.ts +++ b/src/__tests__/lib/GetCustomCertificateBuffer.unit.spec.ts @@ -53,11 +53,11 @@ test('Can use a custom root certificate to connect to a REST API', async () => { CAMUNDA_OPERATE_BASE_URL: 'https://localhost:3012', }, }) - console.log('Trying to get process instance with certificate') + // console.log('Trying to get process instance with certificate') const res = await c.getProcessInstance('1') - console.log( - `Got response from self-signed secured server: ${res.bpmnProcessId}` - ) + // console.log( + // `Got response from self-signed secured server: ${res.bpmnProcessId}` + // ) expect(res.bpmnProcessId).toBe('test') const c1 = new OperateApiClient({ config: { @@ -68,7 +68,7 @@ test('Can use a custom root certificate to connect to a REST API', async () => { let threw = false try { - console.log('Trying to get process instance without certificate') + // console.log('Trying to get process instance without certificate') await c1.getProcessInstance('1') } catch (e) { threw = true diff --git a/src/__tests__/tasklist/tasklist.integration.spec.ts b/src/__tests__/tasklist/tasklist.integration.spec.ts index 82205487..d7d53a08 100644 --- a/src/__tests__/tasklist/tasklist.integration.spec.ts +++ b/src/__tests__/tasklist/tasklist.integration.spec.ts @@ -107,11 +107,7 @@ describe('TasklistApiClient', () => { const tasklist = new TasklistApiClient() expect(p).toBeTruthy() const operate = new OperateApiClient() - const res = await operate - .getProcessInstance(p!.processInstanceKey) - .catch((e) => { - console.log('Error getting process instance', e) - }) + const res = await operate.getProcessInstance(p!.processInstanceKey) expect(res).toBeTruthy() const tasks = await tasklist.searchTasks({ state: 'CREATED' }) const taskid = tasks[0].id diff --git a/src/__tests__/testdata/zeebe-user-task.bpmn b/src/__tests__/testdata/zeebe-user-task.bpmn new file mode 100644 index 00000000..bfdb8338 --- /dev/null +++ b/src/__tests__/testdata/zeebe-user-task.bpmn @@ -0,0 +1,48 @@ + + + + + Flow_0k05u9v + + + + Flow_1sm7z6x + + + + + + + Flow_0k05u9v + Flow_1sm7z6x + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/__tests__/zeebe/integration-rest/UserTask-rest.spec.ts b/src/__tests__/zeebe/integration-rest/UserTask-rest.spec.ts new file mode 100644 index 00000000..e3f7827e --- /dev/null +++ b/src/__tests__/zeebe/integration-rest/UserTask-rest.spec.ts @@ -0,0 +1,33 @@ +import { TasklistApiClient } from '../../../tasklist' +import { ZeebeGrpcClient, ZeebeRestClient } from '../../../zeebe' + +jest.setTimeout(30000) +test('can update a task', async () => { + const grpc = new ZeebeGrpcClient() + + await grpc.deployResource({ + processFilename: './src/__tests__/testdata/zeebe-user-task.bpmn', + }) + await grpc.createProcessInstance({ + bpmnProcessId: 'zeebe-user-task-test', + variables: {}, + }) + const tasklist = new TasklistApiClient() + await new Promise((resolve) => setTimeout(resolve, 10000)) + const tasks = await tasklist.searchTasks({ + state: 'CREATED', + }) + const zbc = new ZeebeRestClient() + const res = await zbc.completeUserTask({ + userTaskKey: tasks[0].id, + }) + expect(res.statusCode).toBe(204) + const res2 = await zbc + .completeUserTask({ + userTaskKey: '2251799814261421', + }) + .catch((e) => { + return e + }) + expect(res2.statusCode).toBe(404) +}) diff --git a/src/__tests__/zeebe/local-integration/OnConnectionError.spec.ts b/src/__tests__/zeebe/local-integration/OnConnectionError.spec.ts index 990b7d99..0f50094c 100644 --- a/src/__tests__/zeebe/local-integration/OnConnectionError.spec.ts +++ b/src/__tests__/zeebe/local-integration/OnConnectionError.spec.ts @@ -133,10 +133,10 @@ xtest('Does not call the onConnectionError handler if there is a business error' const zbc2 = new ZeebeGrpcClient() zbc2.on('connectionError', () => { // tslint:disable-next-line: no-console - console.log('OnConnectionError!!!! Incrementing calledF') // @DEBUG - const e = new Error() + // console.log('OnConnectionError!!!! Incrementing calledF') // @DEBUG + // const e = new Error() // tslint:disable-next-line: no-console - console.log(e.stack) // @DEBUG + // console.log(e.stack) // @DEBUG calledF++ }) diff --git a/src/__tests__/zeebe/multitenancy/createProcessInstance-mt.spec.ts b/src/__tests__/zeebe/multitenancy/createProcessInstance-mt.spec.ts index f3802eeb..5cc3cdfb 100644 --- a/src/__tests__/zeebe/multitenancy/createProcessInstance-mt.spec.ts +++ b/src/__tests__/zeebe/multitenancy/createProcessInstance-mt.spec.ts @@ -37,7 +37,7 @@ test('Will not throw an error if tenantId is provided when starting a process in await client.cancelProcessInstance(p.processInstanceKey) } catch (e) { threwError = true - console.log(e) + // console.log(e) } expect(threwError).toBe(false) diff --git a/src/zeebe/zb/ZeebeRESTClient.ts b/src/zeebe/zb/ZeebeRESTClient.ts index cf3c8311..a0e41e84 100644 --- a/src/zeebe/zb/ZeebeRESTClient.ts +++ b/src/zeebe/zb/ZeebeRESTClient.ts @@ -110,7 +110,8 @@ export class ZeebeRestClient { ) as Promise } - /* Completes a user task with the given key. */ + /* Completes a user task with the given key. The method either completes the task or throws 400, 404, or 409. + Documentation: https://docs.camunda.io/docs/apis-tools/zeebe-api-rest/specifications/complete-a-user-task/ */ public async completeUserTask({ userTaskKey, variables = {},