Skip to content

Commit 7819071

Browse files
committed
Add and improve tests
1 parent c40a083 commit 7819071

File tree

4 files changed

+220
-2
lines changed

4 files changed

+220
-2
lines changed

src/__mocks__/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ import {
3737

3838
export const createClient = (apiKey = process.env.ADYEN_API_KEY): Client => {
3939
const config: Config = new Config();
40+
config.environment = "TEST";
4041
config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_TEST;
4142
config.terminalApiLocalEndpoint = "https://mocked_local_endpoint.com";
4243
config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_TEST;
4344
config.apiKey = apiKey == null ? "apiKey" : apiKey;
44-
return new Client({ config });
45+
return new Client(config);
4546
};
4647

4748
export const createBasicAuthClient = (): Client => {

src/__tests__/checkout.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ describe("Checkout", (): void => {
391391
fail();
392392
} catch (e) {
393393
if(e instanceof Error) {
394-
expect(e.message).toEqual("Please provide your unique live url prefix on the setEnvironment() call on the Client.");
394+
expect(e.message).toEqual("Live endpoint URL prefix must be provided for LIVE environment.");
395395

396396
} else {
397397
fail();

src/__tests__/client.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Client from "../client";
2+
import Config, { RegionEnum } from "../config";
23

34
describe("API Client", function (): void {
45
test("should be able to make a request using basic auth", async function (): Promise<void> {
@@ -42,6 +43,16 @@ describe("API Client", function (): void {
4243
expect(client.config.applicationName).toBe("my_application_name");
4344
});
4445

46+
test("should define timeout in Config", () => {
47+
const client = new Client({
48+
apiKey: "ADYEN_API_KEY",
49+
environment: "TEST",
50+
connectionTimeoutMillis: 30000
51+
});
52+
53+
expect(client.config.connectionTimeoutMillis).toBe(30000);
54+
});
55+
4556
test("should set timeout", () => {
4657
const client = new Client({
4758
apiKey: "ADYEN_API_KEY",
@@ -51,4 +62,75 @@ describe("API Client", function (): void {
5162
client.setTimeouts(30000);
5263
expect(client.config.connectionTimeoutMillis).toBe(30000);
5364
});
65+
66+
test("should throw error if environment is not defined", () => {
67+
expect(() => {
68+
new Client({ apiKey: "ADYEN_API_KEY" } as any);
69+
}).toThrow("Environment must be defined");
70+
});
71+
72+
test("should throw error if environment is LIVE and region is invalid", () => {
73+
const config = new Config({
74+
apiKey: "ADYEN_API_KEY",
75+
environment: "LIVE",
76+
region: "INVALID" as RegionEnum,
77+
liveEndpointUrlPrefix: "prefix"
78+
});
79+
expect(() => {
80+
new Client(config);
81+
}).toThrow("Invalid region provided: INVALID");
82+
});
83+
84+
test("should set terminalApiCloudEndpoint for TEST region", () => {
85+
const config = new Config({
86+
apiKey: "ADYEN_API_KEY",
87+
environment: "TEST"
88+
});
89+
const client = new Client(config);
90+
expect(client.config.terminalApiCloudEndpoint).toBeDefined();
91+
expect(client.config.terminalApiCloudEndpoint).toBe("https://terminal-api-test.adyen.com");
92+
});
93+
94+
test("should set terminalApiCloudEndpoint for LIVE region", () => {
95+
const config = new Config({
96+
apiKey: "ADYEN_API_KEY",
97+
environment: "LIVE",
98+
region: RegionEnum.US,
99+
liveEndpointUrlPrefix: "prefix"
100+
});
101+
const client = new Client(config);
102+
expect(client.config.terminalApiCloudEndpoint).toBeDefined();
103+
expect(client.config.terminalApiCloudEndpoint).toBe("https://terminal-api-us.adyen.com");
104+
});
105+
106+
test("should throw error if liveEndpointUrlPrefix is missing in LIVE environment", () => {
107+
const config = new Config({
108+
apiKey: "ADYEN_API_KEY",
109+
environment: "LIVE"
110+
});
111+
expect(() => {
112+
new Client(config);
113+
}).toThrow("Live endpoint URL prefix must be provided for LIVE environment.");
114+
});
115+
116+
test("should set and get custom http client", () => {
117+
const config = new Config({
118+
apiKey: "ADYEN_API_KEY",
119+
environment: "TEST"
120+
});
121+
const client = new Client(config);
122+
const mockHttpClient = { request: jest.fn() };
123+
client.httpClient = mockHttpClient as any;
124+
expect(client.httpClient).toBe(mockHttpClient);
125+
});
126+
127+
test("should set application name via setApplicationName", () => {
128+
const config = new Config({
129+
apiKey: "ADYEN_API_KEY",
130+
environment: "TEST"
131+
});
132+
const client = new Client(config);
133+
client.setApplicationName("test_app");
134+
expect(client.config.applicationName).toBe("test_app");
135+
});
54136
});

src/__tests__/service.spec.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import Service from "../service";
2+
import Client from "../client";
3+
import Config from "../config";
4+
5+
class TestService extends Service {
6+
public constructor(client: Client) {
7+
super(client);
8+
}
9+
public testCreateBaseUrl(url: string): string {
10+
return this.createBaseUrl(url);
11+
}
12+
}
13+
14+
describe("Service", () => {
15+
let client: Client;
16+
17+
beforeEach(() => {
18+
// Default config for each test
19+
client = new Client(new Config({
20+
apiKey: "test_key",
21+
environment: "TEST"
22+
}));
23+
});
24+
25+
it("should replace -live with -test for non-LIVE environments", () => {
26+
const service = new TestService(client);
27+
const url = "https://pal-live.adyen.com/pal/servlet/";
28+
expect(service.testCreateBaseUrl(url)).toBe("https://pal-test.adyen.com/pal/servlet/");
29+
});
30+
31+
it("should throw error if liveEndpointUrlPrefix is undefined in LIVE environment", () => {
32+
// create Client for TEST environment without liveEndpointUrlPrefix
33+
const config = new Config({
34+
apiKey: "test_key",
35+
environment: "TEST"
36+
});
37+
client = new Client(config);
38+
// change to LIVE
39+
client.config.environment = "LIVE";
40+
41+
const service = new TestService(client);
42+
expect(() => service.testCreateBaseUrl("https://pal-live.adyen.com/pal/servlet/"))
43+
.toThrow("Live endpoint URL prefix must be provided for LIVE environment.");
44+
});
45+
46+
it("should throw error if liveEndpointUrlPrefix is empty for pal- URLs", () => {
47+
// create Client for TEST environment without liveEndpointUrlPrefix
48+
const config = new Config({
49+
apiKey: "test_key",
50+
environment: "TEST",
51+
liveEndpointUrlPrefix: ""
52+
});
53+
client = new Client(config);
54+
// change to LIVE
55+
client.config.environment = "LIVE";
56+
57+
const service = new TestService(client);
58+
expect(() => service.testCreateBaseUrl("https://pal-live.adyen.com/pal/servlet/"))
59+
.toThrow("Live endpoint URL prefix must be provided for LIVE environment.");
60+
});
61+
62+
it("should replace pal-test with pal-live using liveEndpointUrlPrefix", () => {
63+
const config = new Config({
64+
apiKey: "test_key",
65+
environment: "LIVE",
66+
liveEndpointUrlPrefix: "mycompany"
67+
});
68+
client = new Client(config);
69+
70+
const service = new TestService(client);
71+
const url = "https://pal-test.adyen.com/pal/servlet/";
72+
expect(service.testCreateBaseUrl(url)).toBe(
73+
"https://mycompany-pal-live.adyenpayments.com/pal/servlet/"
74+
);
75+
});
76+
77+
it("should throw error if liveEndpointUrlPrefix is empty for checkout- URLs", () => {
78+
// create Client for TEST environment without liveEndpointUrlPrefix
79+
const config = new Config({
80+
apiKey: "test_key",
81+
environment: "TEST",
82+
liveEndpointUrlPrefix: ""
83+
});
84+
client = new Client(config);
85+
// change to LIVE
86+
client.config.environment = "LIVE";
87+
88+
const service = new TestService(client);
89+
expect(() => service.testCreateBaseUrl("https://checkout-test.adyen.com/"))
90+
.toThrow("Live endpoint URL prefix must be provided for LIVE environment.");
91+
});
92+
93+
it("should replace checkout-test with checkout-live using liveEndpointUrlPrefix", () => {
94+
const config = new Config({
95+
apiKey: "test_key",
96+
environment: "LIVE",
97+
liveEndpointUrlPrefix: "mycompany"
98+
});
99+
client = new Client(config);
100+
101+
const service = new TestService(client);
102+
const url = "https://checkout-test.adyen.com/";
103+
expect(service.testCreateBaseUrl(url)).toBe(
104+
"https://mycompany-checkout-live.adyenpayments.com/checkout/"
105+
);
106+
});
107+
108+
it("should replace checkout-test with checkout-live for possdk/v68 using liveEndpointUrlPrefix", () => {
109+
const config = new Config({
110+
apiKey: "test_key",
111+
environment: "LIVE",
112+
liveEndpointUrlPrefix: "mycompany"
113+
});
114+
client = new Client(config);
115+
116+
const service = new TestService(client);
117+
const url = "https://checkout-test.adyen.com/possdk/v68";
118+
expect(service.testCreateBaseUrl(url)).toBe(
119+
"https://mycompany-checkout-live.adyenpayments.com/possdk/v68"
120+
);
121+
});
122+
123+
it("should replace -test with -live for other URLs", () => {
124+
const config = new Config({
125+
apiKey: "test_key",
126+
environment: "LIVE",
127+
liveEndpointUrlPrefix: "mycompany"
128+
});
129+
client = new Client(config);
130+
131+
const service = new TestService(client);
132+
const url = "https://some-test.adyen.com/api/";
133+
expect(service.testCreateBaseUrl(url)).toBe("https://some-live.adyen.com/api/");
134+
});
135+
});

0 commit comments

Comments
 (0)