|
1 | 1 | import { NodeConfigurationDiffTransport } from "../../../src/commands/configuration-management/interfaces/node-diff.interfaces"; |
2 | 2 | import { NodeConfigurationChangeType } from "../../../src/commands/configuration-management/interfaces/diff-package.interfaces"; |
3 | | -import { mockAxiosGet, mockAxiosPost } from "../../utls/http-requests-mock"; |
| 3 | +import { |
| 4 | + mockAxiosGet, |
| 5 | + mockAxiosPost, |
| 6 | + mockedAxiosInstance, |
| 7 | + mockedPostRequestBodyByUrl, |
| 8 | +} from "../../utls/http-requests-mock"; |
4 | 9 | import { NodeDiffService } from "../../../src/commands/configuration-management/node-diff.service"; |
5 | 10 | import { testContext } from "../../utls/test-context"; |
6 | 11 | import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup"; |
7 | 12 | import { FileService } from "../../../src/core/utils/file-service"; |
8 | 13 | import { mockCreateReadStream } from "../../utls/fs-mock-utils"; |
| 14 | +import * as FormData from "form-data"; |
9 | 15 | import * as path from "path"; |
10 | 16 |
|
11 | 17 | describe("Node diff", () => { |
@@ -292,6 +298,27 @@ describe("Node diff", () => { |
292 | 298 | expect(changeDateLog.message).toContain(specificDate); |
293 | 299 | }); |
294 | 300 |
|
| 301 | + it("Should throw a FatalError when the diff API call fails", async () => { |
| 302 | + (mockedAxiosInstance.get as jest.Mock).mockRejectedValueOnce(new Error("network down")); |
| 303 | + |
| 304 | + await expect( |
| 305 | + new NodeDiffService(testContext).diff(packageKey, nodeKey, baseVersion, compareVersion, false) |
| 306 | + ).rejects.toThrow(/Problem getting the node diff/); |
| 307 | + |
| 308 | + expect(mockWriteFileSync).not.toHaveBeenCalled(); |
| 309 | + }); |
| 310 | + |
| 311 | + it("Should request the diff with both baseVersion and compareVersion query parameters", async () => { |
| 312 | + const expectedUrl = `https://myTeam.celonis.cloud/pacman/api/core/packages/${packageKey}/nodes/${nodeKey}/diff/configuration?baseVersion=${baseVersion}&compareVersion=${compareVersion}`; |
| 313 | + mockAxiosGet(expectedUrl, nodeDiff); |
| 314 | + |
| 315 | + await new NodeDiffService(testContext).diff(packageKey, nodeKey, baseVersion, compareVersion, false); |
| 316 | + |
| 317 | + expect(mockedAxiosInstance.get as jest.Mock).toHaveBeenCalledTimes(1); |
| 318 | + const calledUrl = (mockedAxiosInstance.get as jest.Mock).mock.calls[0][0]; |
| 319 | + expect(calledUrl).toBe(expectedUrl); |
| 320 | + }); |
| 321 | + |
295 | 322 | describe("With file", () => { |
296 | 323 | const file = "./node.json"; |
297 | 324 | const nodeJsonContent = Buffer.from(JSON.stringify({ key: nodeKey, configuration: { foo: "bar" } })); |
@@ -363,5 +390,37 @@ describe("Node diff", () => { |
363 | 390 | expect(loggingTestTransport.logMessages.length).toBe(11); |
364 | 391 | expect(loggingTestTransport.logMessages[0].message.trim()).toEqual(`Package Key: ${nodeDiff.packageKey}`); |
365 | 392 | }); |
| 393 | + |
| 394 | + it("Should send the node file as multipart/form-data with a 'file' field", async () => { |
| 395 | + const url = `https://myTeam.celonis.cloud/pacman/api/core/packages/${packageKey}/nodes/${nodeKey}/diff/configuration/with-file?baseVersion=STAGING`; |
| 396 | + mockAxiosPost(url, nodeDiff); |
| 397 | + |
| 398 | + await new NodeDiffService(testContext).diffWithFile(packageKey, nodeKey, "STAGING", file, false); |
| 399 | + |
| 400 | + const sentBody = mockedPostRequestBodyByUrl.get(url); |
| 401 | + expect(sentBody).toBeInstanceOf(FormData); |
| 402 | + |
| 403 | + const headers = (sentBody as FormData).getHeaders(); |
| 404 | + expect(headers["content-type"]).toMatch(/^multipart\/form-data; boundary=/); |
| 405 | + |
| 406 | + // form-data keeps the registered parts in its internal `_streams` array. Each form |
| 407 | + // field is represented by a header string followed by the value; assert that the |
| 408 | + // header chunk for the 'file' field is present. |
| 409 | + const streams: unknown[] = ((sentBody as unknown) as { _streams: unknown[] })._streams; |
| 410 | + const fileFieldHeader = streams.find( |
| 411 | + chunk => typeof chunk === "string" && chunk.includes('name="file"') |
| 412 | + ); |
| 413 | + expect(fileFieldHeader).toBeDefined(); |
| 414 | + }); |
| 415 | + |
| 416 | + it("Should throw a FatalError when the diff-with-file API call fails", async () => { |
| 417 | + (mockedAxiosInstance.post as jest.Mock).mockRejectedValueOnce(new Error("upload failed")); |
| 418 | + |
| 419 | + await expect( |
| 420 | + new NodeDiffService(testContext).diffWithFile(packageKey, nodeKey, "STAGING", file, false) |
| 421 | + ).rejects.toThrow(/Problem getting the node diff/); |
| 422 | + |
| 423 | + expect(mockWriteFileSync).not.toHaveBeenCalled(); |
| 424 | + }); |
366 | 425 | }); |
367 | 426 | }); |
0 commit comments