Skip to content

Commit be3bfed

Browse files
committed
Add tests
1 parent 1c2fb88 commit be3bfed

1 file changed

Lines changed: 60 additions & 1 deletion

File tree

tests/commands/configuration-management/config-node-diff.spec.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { NodeConfigurationDiffTransport } from "../../../src/commands/configuration-management/interfaces/node-diff.interfaces";
22
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";
49
import { NodeDiffService } from "../../../src/commands/configuration-management/node-diff.service";
510
import { testContext } from "../../utls/test-context";
611
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
712
import { FileService } from "../../../src/core/utils/file-service";
813
import { mockCreateReadStream } from "../../utls/fs-mock-utils";
14+
import * as FormData from "form-data";
915
import * as path from "path";
1016

1117
describe("Node diff", () => {
@@ -292,6 +298,27 @@ describe("Node diff", () => {
292298
expect(changeDateLog.message).toContain(specificDate);
293299
});
294300

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+
295322
describe("With file", () => {
296323
const file = "./node.json";
297324
const nodeJsonContent = Buffer.from(JSON.stringify({ key: nodeKey, configuration: { foo: "bar" } }));
@@ -363,5 +390,37 @@ describe("Node diff", () => {
363390
expect(loggingTestTransport.logMessages.length).toBe(11);
364391
expect(loggingTestTransport.logMessages[0].message.trim()).toEqual(`Package Key: ${nodeDiff.packageKey}`);
365392
});
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+
});
366425
});
367426
});

0 commit comments

Comments
 (0)