Skip to content

Commit 07c4ef8

Browse files
committed
Add tests
1 parent a3ce4d9 commit 07c4ef8

File tree

2 files changed

+219
-248
lines changed

2 files changed

+219
-248
lines changed

src/__tests__/terminalCloudAPI.spec.ts

Lines changed: 135 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,115 +11,177 @@ let terminalCloudAPI: TerminalCloudAPI;
1111
let scope: nock.Scope;
1212

1313
beforeEach((): void => {
14-
if (!nock.isActive()) {
15-
nock.activate();
16-
}
17-
client = createClient(process.env.ADYEN_TERMINAL_APIKEY);
14+
if (!nock.isActive()) {
15+
nock.activate();
16+
}
17+
client = createClient(process.env.ADYEN_TERMINAL_APIKEY);
1818

19-
terminalCloudAPI = new TerminalCloudAPI(client);
20-
scope = nock(`${client.config.terminalApiCloudEndpoint}`);
19+
terminalCloudAPI = new TerminalCloudAPI(client);
20+
scope = nock(`${client.config.terminalApiCloudEndpoint}`);
2121
});
2222

2323
afterEach((): void => {
24-
nock.cleanAll();
24+
nock.cleanAll();
2525
});
2626

2727
describe("Terminal Cloud API", (): void => {
28-
test("should make an async payment request", async (): Promise<void> => {
29-
scope.post("/async").reply(200, asyncRes);
28+
test("should make an async payment request", async (): Promise<void> => {
29+
scope.post("/async").reply(200, asyncRes);
3030

31-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
31+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
3232

33-
const requestResponse: string = await terminalCloudAPI.async(terminalAPIPaymentRequest);
33+
const requestResponse: string = await terminalCloudAPI.async(terminalAPIPaymentRequest);
3434

35-
expect(requestResponse).toEqual("ok");
36-
});
35+
expect(requestResponse).toEqual("ok");
36+
});
3737

38-
test("should make a sync payment request", async (): Promise<void> => {
39-
scope.post("/sync").reply(200, syncRes);
38+
test("should make a sync payment request", async (): Promise<void> => {
39+
scope.post("/sync").reply(200, syncRes);
4040

41-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
42-
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
41+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
42+
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
4343

44-
expect(terminalAPIResponse.SaleToPOIResponse?.PaymentResponse).toBeDefined();
45-
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader).toBeDefined();
46-
});
44+
expect(terminalAPIResponse.SaleToPOIResponse?.PaymentResponse).toBeDefined();
45+
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader).toBeDefined();
46+
});
4747

48-
test("should make a sync payment request with additional attributes", async (): Promise<void> => {
49-
scope.post("/sync").reply(200, syncTerminalPaymentResponse);
48+
test("should make a sync payment request with additional attributes", async (): Promise<void> => {
49+
scope.post("/sync").reply(200, syncTerminalPaymentResponse);
5050

51-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
51+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
5252

53-
await expect(async () => {
54-
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
55-
expect(terminalAPIResponse.SaleToPOIResponse?.PaymentResponse).toBeDefined();
56-
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader).toBeDefined();
57-
}).not.toThrow();
53+
await expect(async () => {
54+
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
55+
expect(terminalAPIResponse.SaleToPOIResponse?.PaymentResponse).toBeDefined();
56+
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader).toBeDefined();
57+
}).not.toThrow();
5858

59-
});
59+
});
6060

61-
test("should return event notification Reject", async (): Promise<void> => {
61+
test("should return event notification Reject", async (): Promise<void> => {
6262

63-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
64-
scope.post("/sync").reply(200, syncResEventNotification);
63+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
64+
scope.post("/sync").reply(200, syncResEventNotification);
6565

66-
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
66+
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
6767

68-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
69-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("Reject");
68+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
69+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("Reject");
7070

71-
});
71+
});
7272

73-
test("should return event notification Shutdown with additional attributes", async (): Promise<void> => {
73+
test("should return event notification Shutdown with additional attributes", async (): Promise<void> => {
7474

75-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
76-
scope.post("/sync").reply(200, syncResEventNotificationWithAdditionalAttributes);
75+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
76+
scope.post("/sync").reply(200, syncResEventNotificationWithAdditionalAttributes);
7777

78-
await expect(async () => {
79-
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
80-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
81-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("Shutdown");
82-
expect(terminalAPIResponse.SaleToPOIRequest?.MessageHeader).toBeDefined();
83-
}).not.toThrow();
84-
});
78+
await expect(async () => {
79+
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
80+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
81+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("Shutdown");
82+
expect(terminalAPIResponse.SaleToPOIRequest?.MessageHeader).toBeDefined();
83+
}).not.toThrow();
84+
});
8585

86-
test("should return event notification with unknown enum", async (): Promise<void> => {
86+
test("should return event notification with unknown enum", async (): Promise<void> => {
8787

88-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
89-
scope.post("/sync").reply(200, syncResEventNotificationWithUnknownEnum);
88+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
89+
scope.post("/sync").reply(200, syncResEventNotificationWithUnknownEnum);
9090

91-
await expect(async () => {
92-
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
93-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
94-
// EventToNotify is unknown, so it holds whatever value is found in the payload
95-
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("this is unknown");
91+
await expect(async () => {
92+
const terminalAPIResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
93+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification).toBeDefined();
94+
// EventToNotify is unknown, so it holds whatever value is found in the payload
95+
expect(terminalAPIResponse.SaleToPOIRequest?.EventNotification?.EventToNotify).toBe("this is unknown");
9696

97-
}).not.toThrow();
98-
});
97+
}).not.toThrow();
98+
});
9999

100-
test("should make an async refund request", async (): Promise<void> => {
101-
scope.post("/sync").reply(200, syncRes);
100+
test("should make an async refund request", async (): Promise<void> => {
101+
scope.post("/sync").reply(200, syncRes);
102102

103-
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
104-
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
103+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
104+
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
105105

106-
const pOITransactionId = terminalAPIResponse.SaleToPOIResponse!.PaymentResponse!.POIData!.POITransactionID;
107-
expect(pOITransactionId).toBeTruthy();
106+
const pOITransactionId = terminalAPIResponse.SaleToPOIResponse!.PaymentResponse!.POIData!.POITransactionID;
107+
expect(pOITransactionId).toBeTruthy();
108108

109-
scope.post("/sync").reply(200, syncRefund);
109+
scope.post("/sync").reply(200, syncRefund);
110110

111-
const terminalAPIRefundRequest = createTerminalAPIRefundRequest(pOITransactionId);
112-
const id = Math.floor(Math.random() * Math.floor(10000000)).toString();
113-
terminalAPIRefundRequest.SaleToPOIRequest.MessageHeader.ServiceID = id;
114-
const saleToAcquirerData: terminal.SaleToAcquirerData = new terminal.SaleToAcquirerData();
115-
saleToAcquirerData.currency = "EUR";
116-
terminalAPIRefundRequest.SaleToPOIRequest.ReversalRequest!.SaleData!.SaleToAcquirerData = saleToAcquirerData;
117-
const terminalAPIRefundResponse = await terminalCloudAPI.sync(terminalAPIRefundRequest);
111+
const terminalAPIRefundRequest = createTerminalAPIRefundRequest(pOITransactionId);
112+
const id = Math.floor(Math.random() * Math.floor(10000000)).toString();
113+
terminalAPIRefundRequest.SaleToPOIRequest.MessageHeader.ServiceID = id;
114+
const saleToAcquirerData: terminal.SaleToAcquirerData = new terminal.SaleToAcquirerData();
115+
saleToAcquirerData.currency = "EUR";
116+
terminalAPIRefundRequest.SaleToPOIRequest.ReversalRequest!.SaleData!.SaleToAcquirerData = saleToAcquirerData;
117+
const terminalAPIRefundResponse = await terminalCloudAPI.sync(terminalAPIRefundRequest);
118118

119-
expect(terminalAPIRefundResponse.SaleToPOIResponse?.ReversalResponse?.Response.Result).toBe("Success");
120-
}, 20000);
121-
});
119+
expect(terminalAPIRefundResponse.SaleToPOIResponse?.ReversalResponse?.Response.Result).toBe("Success");
120+
}, 20000);
121+
122+
test("async should handle 308", async (): Promise<void> => {
123+
124+
const terminalApiHost = "https://terminal-api-test.adyen.com";
125+
126+
const client = new Client({ apiKey: "YOUR_API_KEY", environment: "TEST" });
127+
const terminalCloudAPI = new TerminalCloudAPI(client);
128+
129+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
130+
// custom value to trigger mock 308 response
131+
terminalAPIPaymentRequest.SaleToPOIRequest.MessageHeader.POIID = "response-with-redirect";
132+
133+
// Mock first request: returns a 308 redirect with Location header
134+
nock(terminalApiHost)
135+
.post("/async", (body) => {
136+
return body?.SaleToPOIRequest?.MessageHeader?.POIID === "response-with-redirect";
137+
})
138+
.reply(308, "", { Location: `${terminalApiHost}/async?redirect=false` });
139+
140+
// Mock follow-up request: returns successful response 'ok'
141+
nock(terminalApiHost)
142+
.post("/async?redirect=false")
143+
.reply(200, "ok");
144+
145+
const terminalAPIResponse = await terminalCloudAPI.async(terminalAPIPaymentRequest);
122146

147+
expect(terminalAPIResponse).toEqual("ok");
148+
});
149+
150+
test("sync should handle 308", async (): Promise<void> => {
151+
const terminalApiHost = "https://terminal-api-test.adyen.com";
152+
153+
const client = new Client({ apiKey: "YOUR_API_KEY", environment: "TEST" });
154+
const terminalCloudAPI = new TerminalCloudAPI(client);
155+
156+
const terminalAPIPaymentRequest = createTerminalAPIPaymentRequest();
157+
// custom value to trigger mock 308 response
158+
terminalAPIPaymentRequest.SaleToPOIRequest.MessageHeader.POIID = "response-with-redirect";
159+
160+
// Mock first request: returns a 308 redirect with Location header
161+
nock(terminalApiHost)
162+
.post("/sync", (body) => {
163+
return body?.SaleToPOIRequest?.MessageHeader?.POIID === "response-with-redirect";
164+
})
165+
.reply(308, "", { Location: `${terminalApiHost}/sync?redirect=false` });
166+
167+
// Mock follow-up request: returns successful response
168+
nock(terminalApiHost)
169+
.post("/sync?redirect=false")
170+
.reply(200, {
171+
SaleToPOIResponse: {
172+
PaymentResponse: { Response: "Authorised" },
173+
MessageHeader: { SaleID: "001-308" },
174+
},
175+
});
176+
177+
const terminalAPIResponse: terminal.TerminalApiResponse = await terminalCloudAPI.sync(terminalAPIPaymentRequest);
178+
179+
expect(terminalAPIResponse.SaleToPOIResponse?.PaymentResponse).toBeDefined();
180+
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader).toBeDefined();
181+
expect(terminalAPIResponse.SaleToPOIResponse?.MessageHeader?.SaleID).toBe("001-308");
182+
});
183+
184+
});
123185

124186
export const syncTerminalPaymentResponse = {
125187
"SaleToPOIResponse": {

0 commit comments

Comments
 (0)