apacuana-sdk-core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.eslintrc.cjs +22 -0
  2. package/CHANGELOG.md +1 -0
  3. package/README.md +794 -0
  4. package/babel.config.cjs +11 -0
  5. package/coverage/clover.xml +627 -0
  6. package/coverage/coverage-final.json +13 -0
  7. package/coverage/lcov-report/api/index.html +131 -0
  8. package/coverage/lcov-report/api/signatures.js.html +1093 -0
  9. package/coverage/lcov-report/api/users.js.html +292 -0
  10. package/coverage/lcov-report/base.css +224 -0
  11. package/coverage/lcov-report/block-navigation.js +87 -0
  12. package/coverage/lcov-report/certs.js.html +175 -0
  13. package/coverage/lcov-report/config/index.html +116 -0
  14. package/coverage/lcov-report/config/index.js.html +208 -0
  15. package/coverage/lcov-report/errors/index.html +116 -0
  16. package/coverage/lcov-report/errors/index.js.html +148 -0
  17. package/coverage/lcov-report/favicon.png +0 -0
  18. package/coverage/lcov-report/index.html +191 -0
  19. package/coverage/lcov-report/prettify.css +1 -0
  20. package/coverage/lcov-report/prettify.js +2 -0
  21. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  22. package/coverage/lcov-report/sorter.js +210 -0
  23. package/coverage/lcov-report/src/api/certs.js.html +853 -0
  24. package/coverage/lcov-report/src/api/faceLiveness.js.html +523 -0
  25. package/coverage/lcov-report/src/api/index.html +176 -0
  26. package/coverage/lcov-report/src/api/revocations.js.html +412 -0
  27. package/coverage/lcov-report/src/api/signatures.js.html +1420 -0
  28. package/coverage/lcov-report/src/api/users.js.html +466 -0
  29. package/coverage/lcov-report/src/config/index.html +116 -0
  30. package/coverage/lcov-report/src/config/index.js.html +256 -0
  31. package/coverage/lcov-report/src/errors/index.html +116 -0
  32. package/coverage/lcov-report/src/errors/index.js.html +148 -0
  33. package/coverage/lcov-report/src/index.html +116 -0
  34. package/coverage/lcov-report/src/index.js.html +592 -0
  35. package/coverage/lcov-report/src/success/index.html +116 -0
  36. package/coverage/lcov-report/src/success/index.js.html +106 -0
  37. package/coverage/lcov-report/src/utils/constant.js.html +256 -0
  38. package/coverage/lcov-report/src/utils/helpers.js.html +1195 -0
  39. package/coverage/lcov-report/src/utils/httpClient.js.html +622 -0
  40. package/coverage/lcov-report/src/utils/index.html +146 -0
  41. package/coverage/lcov-report/utils/constant.js.html +145 -0
  42. package/coverage/lcov-report/utils/httpClient.js.html +646 -0
  43. package/coverage/lcov-report/utils/index.html +131 -0
  44. package/coverage/lcov.info +1258 -0
  45. package/dist/api/certs.d.ts +15 -0
  46. package/dist/api/faceLiveness.d.ts +6 -0
  47. package/dist/api/revocations.d.ts +6 -0
  48. package/dist/api/signatures.d.ts +19 -0
  49. package/dist/api/users.d.ts +47 -0
  50. package/dist/config/index.d.ts +22 -0
  51. package/dist/errors/index.d.ts +10 -0
  52. package/dist/index.d.ts +25 -0
  53. package/dist/index.js +34492 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/index.mjs +34490 -0
  56. package/dist/index.mjs.map +1 -0
  57. package/dist/success/index.d.ts +7 -0
  58. package/dist/types/certs.d.ts +83 -0
  59. package/dist/types/faceLiveness.d.ts +11 -0
  60. package/dist/types/revocations.d.ts +51 -0
  61. package/dist/types/signatures.d.ts +162 -0
  62. package/dist/types/users.d.ts +260 -0
  63. package/dist/utils/constant.d.ts +49 -0
  64. package/dist/utils/helpers.d.ts +25 -0
  65. package/dist/utils/httpClient.d.ts +3 -0
  66. package/jest.config.cjs +12 -0
  67. package/package.json +51 -9
  68. package/rollup.config.js +32 -0
  69. package/src/api/certs.js +256 -0
  70. package/src/api/faceLiveness.js +146 -0
  71. package/src/api/revocations.js +109 -0
  72. package/src/api/signatures.js +445 -0
  73. package/src/api/users.js +127 -0
  74. package/src/config/index.js +57 -0
  75. package/src/errors/index.js +21 -0
  76. package/src/index.js +169 -0
  77. package/src/success/index.js +8 -0
  78. package/src/types/certs.js +67 -0
  79. package/src/types/faceLiveness.js +16 -0
  80. package/src/types/revocations.js +45 -0
  81. package/src/types/signatures.js +96 -0
  82. package/src/types/users.js +73 -0
  83. package/src/utils/constant.js +57 -0
  84. package/src/utils/helpers.js +370 -0
  85. package/src/utils/httpClient.js +179 -0
  86. package/tests/api/certs.test.js +311 -0
  87. package/tests/api/faceLiveness.test.js +170 -0
  88. package/tests/api/revocations.test.js +119 -0
  89. package/tests/api/signatures.test.js +530 -0
  90. package/tests/api/users.test.js +117 -0
  91. package/tests/index.test.js +131 -0
  92. package/tsconfig.json +16 -0
@@ -0,0 +1,311 @@
1
+ import { getConfig } from "../../src/config/index";
2
+ import { httpRequest } from "../../src/utils/httpClient";
3
+ import helpers from "../../src/utils/helpers";
4
+ import { ApacuanaAPIError } from "../../src/errors";
5
+ import ApacuanaSuccess from "../../src/success";
6
+ import {
7
+ generateCert,
8
+ getCertStatus,
9
+ getCertTypes,
10
+ getRequerimentsByTypeUser,
11
+ requestCertificate,
12
+ } from "../../src/api/certs";
13
+ import { INTEGRATION_TYPE } from "../../src/utils/constant";
14
+
15
+ jest.mock("../../src/config/index");
16
+ jest.mock("../../src/utils/httpClient");
17
+ jest.mock("../../src/utils/helpers");
18
+
19
+ describe("Certificate API - certs.js", () => {
20
+ beforeEach(() => {
21
+ jest.clearAllMocks();
22
+ });
23
+
24
+ describe("generateCert", () => {
25
+ beforeEach(() => {
26
+ jest.clearAllMocks();
27
+ });
28
+
29
+ // TEST 1: Probar el camino feliz de ONBOARDING
30
+ it("should call generateCertOnBoarding for ONBOARDING integration", async () => {
31
+ getConfig.mockReturnValue({
32
+ integrationType: INTEGRATION_TYPE.ONBOARDING,
33
+ });
34
+ const mockEncryptedCsr = { csr: "onboarding-csr" };
35
+ const mockApiResponse = {
36
+ cert: "onboarding-cert",
37
+ certifiedid: "onboarding-certified-id",
38
+ };
39
+ httpRequest.mockResolvedValue(mockApiResponse);
40
+
41
+ const result = await generateCert(mockEncryptedCsr);
42
+
43
+ expect(httpRequest).toHaveBeenCalledWith(
44
+ "services/api/register/certificate", // Endpoint de Onboarding
45
+ mockEncryptedCsr,
46
+ "POST"
47
+ );
48
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
49
+ expect(result.data).toEqual({
50
+ cert: "onboarding-cert",
51
+ certifiedid: "onboarding-certified-id",
52
+ });
53
+ });
54
+
55
+ // TEST 2: Probar el error de ONPREMISE
56
+ it("should throw an error for ONPREMISE integration", async () => {
57
+ getConfig.mockReturnValue({
58
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
59
+ });
60
+
61
+ await expect(generateCert("some-csr")).rejects.toThrow(
62
+ new ApacuanaAPIError(
63
+ "Certificate generation is not supported for integration type: ONPREMISE",
64
+ 501,
65
+ "NOT_IMPLEMENTED"
66
+ )
67
+ );
68
+ });
69
+
70
+ // TEST 3: Probar el error para un tipo inválido
71
+ it("should throw an error for an unsupported integration type", async () => {
72
+ getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
73
+
74
+ await expect(generateCert("some-csr")).rejects.toThrow(
75
+ new ApacuanaAPIError(
76
+ "Unsupported integration type: INVALID_TYPE",
77
+ 400,
78
+ "UNSUPPORTED_INTEGRATION_TYPE"
79
+ )
80
+ );
81
+ });
82
+
83
+ // Se eliminan los otros tests que asumían un comportamiento "estándar"
84
+ });
85
+
86
+ describe("getCertStatus", () => {
87
+ it("should return the certificate status from helpers", () => {
88
+ const mockUserData = { certStatus: "SOLICITUD" };
89
+ getConfig.mockReturnValue({
90
+ userData: mockUserData,
91
+ integrationType: INTEGRATION_TYPE.ONBOARDING,
92
+ });
93
+ helpers.getCertificateStatus.mockReturnValue("SOLICITUD");
94
+
95
+ const result = getCertStatus(true);
96
+
97
+ expect(helpers.getCertificateStatus).toHaveBeenCalledWith(
98
+ mockUserData,
99
+ true,
100
+ INTEGRATION_TYPE.ONBOARDING
101
+ );
102
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
103
+
104
+ // --- CORRECCIÓN AQUÍ: El objeto esperado debe estar anidado en la clave 'status' ---
105
+ expect(result.data).toEqual({
106
+ status: {
107
+ text: "Por verificar",
108
+ descriptionText:
109
+ "La información y documentos enviados están siendo revisados por la Autoridad de Registro.",
110
+ },
111
+ });
112
+ });
113
+ });
114
+
115
+ describe("getCertTypes", () => {
116
+ it("should call getCertTypesOnBoarding for ONBOARDING integration", async () => {
117
+ getConfig.mockReturnValue({
118
+ integrationType: INTEGRATION_TYPE.ONBOARDING,
119
+ });
120
+ const mockApiResponse = { types: [{ id: "1", name: "Type 1" }] };
121
+ httpRequest.mockResolvedValue(mockApiResponse);
122
+
123
+ const result = await getCertTypes();
124
+
125
+ expect(httpRequest).toHaveBeenCalledWith(
126
+ "services/api/customer/typeusers",
127
+ {},
128
+ "GET"
129
+ );
130
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
131
+ expect(result.data).toEqual({ types: mockApiResponse.types });
132
+ });
133
+
134
+ it("should throw an error for ONPREMISE integration", async () => {
135
+ getConfig.mockReturnValue({
136
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
137
+ });
138
+
139
+ await expect(getCertTypes()).rejects.toThrow(
140
+ new ApacuanaAPIError(
141
+ "Getting certificate types is not supported for integration type: ONPREMISE",
142
+ 501,
143
+ "NOT_IMPLEMENTED"
144
+ )
145
+ );
146
+ });
147
+
148
+ it("should throw an error for an unsupported integration type", async () => {
149
+ getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
150
+
151
+ await expect(getCertTypes()).rejects.toThrow(
152
+ new ApacuanaAPIError(
153
+ "Unsupported integration type: INVALID_TYPE",
154
+ 400,
155
+ "UNSUPPORTED_INTEGRATION_TYPE"
156
+ )
157
+ );
158
+ });
159
+ });
160
+
161
+ describe("getRequerimentsByTypeUser", () => {
162
+ it("should call getRequerimentsByTypeUserOnBoarding for ONBOARDING integration", async () => {
163
+ getConfig.mockReturnValue({
164
+ integrationType: INTEGRATION_TYPE.ONBOARDING,
165
+ });
166
+ const mockApiResponse = {
167
+ requirements: [{ id: "1", description: "Requirement 1" }],
168
+ };
169
+ httpRequest.mockResolvedValue(mockApiResponse);
170
+
171
+ const result = await getRequerimentsByTypeUser({ type: 1 });
172
+
173
+ expect(httpRequest).toHaveBeenCalledWith(
174
+ "services/api/customer/documentspref?typeuser=1",
175
+ {},
176
+ "GET"
177
+ );
178
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
179
+ expect(result.data).toEqual({
180
+ requirements: mockApiResponse.requirements,
181
+ });
182
+ });
183
+
184
+ it("should throw an error for ONPREMISE integration", async () => {
185
+ getConfig.mockReturnValue({
186
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
187
+ });
188
+
189
+ await expect(getRequerimentsByTypeUser({ type: 1 })).rejects.toThrow(
190
+ new ApacuanaAPIError(
191
+ "Getting requirements by user type is not supported for integration type: ONPREMISE",
192
+ 501,
193
+ "NOT_IMPLEMENTED"
194
+ )
195
+ );
196
+ });
197
+
198
+ it("should throw an error for an unsupported integration type", async () => {
199
+ getConfig.mockReturnValue({ integrationType: "INVALID_TYPE" });
200
+
201
+ await expect(getRequerimentsByTypeUser({ type: 1 })).rejects.toThrow(
202
+ new ApacuanaAPIError(
203
+ "Unsupported integration type: INVALID_TYPE",
204
+ 400,
205
+ "UNSUPPORTED_INTEGRATION_TYPE"
206
+ )
207
+ );
208
+ });
209
+
210
+ it("should throw an error if params are invalid", async () => {
211
+ await expect(getRequerimentsByTypeUser()).rejects.toThrow(
212
+ 'The "params" object with a numeric "type" property is required.'
213
+ );
214
+ await expect(getRequerimentsByTypeUser({})).rejects.toThrow(
215
+ 'The "params" object with a numeric "type" property is required.'
216
+ );
217
+ await expect(getRequerimentsByTypeUser({ type: "1" })).rejects.toThrow(
218
+ 'The "params" object with a numeric "type" property is required.'
219
+ );
220
+ });
221
+ it("should throw a generic error for other failures", async () => {
222
+ getConfig.mockReturnValue({
223
+ integrationType: INTEGRATION_TYPE.ONBOARDING,
224
+ });
225
+ const genericError = new Error("Network failure");
226
+ httpRequest.mockRejectedValue(genericError);
227
+
228
+ await expect(getRequerimentsByTypeUser({ type: 1 })).rejects.toThrow(
229
+ `Failed to get requirements: ${genericError.message}`
230
+ );
231
+ });
232
+ });
233
+ });
234
+
235
+ describe("requestCertificate", () => {
236
+ const validParams = { type: 1, documents: [] };
237
+
238
+ afterEach(() => {
239
+ jest.clearAllMocks();
240
+ });
241
+
242
+ it("should throw an error if params are invalid", async () => {
243
+ await expect(requestCertificate()).rejects.toThrow(
244
+ 'The "params" object with a numeric "type" property and a "documents" array is required.'
245
+ );
246
+ await expect(requestCertificate({ type: 1 })).rejects.toThrow(
247
+ 'The "params" object with a numeric "type" property and a "documents" array is required.'
248
+ );
249
+ await expect(requestCertificate({ documents: [] })).rejects.toThrow(
250
+ 'The "params" object with a numeric "type" property and a "documents" array is required.'
251
+ );
252
+ });
253
+
254
+ it("should request a certificate for ONBOARDING integration", async () => {
255
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONBOARDING });
256
+ const mockResponse = { message: "Certificate requested" };
257
+ httpRequest.mockResolvedValue(mockResponse);
258
+
259
+ const result = await requestCertificate(validParams);
260
+
261
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
262
+ expect(result.data).toEqual(mockResponse);
263
+ expect(httpRequest).toHaveBeenCalledWith(
264
+ "services/api/customer/request-certificate",
265
+ validParams,
266
+ "POST"
267
+ );
268
+ });
269
+
270
+ it("should throw a NOT_IMPLEMENTED error for ONPREMISE integration", async () => {
271
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONPREMISE });
272
+
273
+ await expect(requestCertificate(validParams)).rejects.toThrow(
274
+ new ApacuanaAPIError(
275
+ "Requesting a certificate is not supported for integration type: ONPREMISE",
276
+ 501,
277
+ "NOT_IMPLEMENTED"
278
+ )
279
+ );
280
+ });
281
+
282
+ it("should throw an UNSUPPORTED_INTEGRATION_TYPE error for unsupported integration types", async () => {
283
+ getConfig.mockReturnValue({ integrationType: "unsupported-type" });
284
+
285
+ await expect(requestCertificate(validParams)).rejects.toThrow(
286
+ new ApacuanaAPIError(
287
+ "Unsupported integration type: unsupported-type",
288
+ 400,
289
+ "UNSUPPORTED_INTEGRATION_TYPE"
290
+ )
291
+ );
292
+ });
293
+
294
+ it("should re-throw ApacuanaAPIError if caught", async () => {
295
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONBOARDING });
296
+ const apiError = new ApacuanaAPIError("API Error", 500, "API_ERROR");
297
+ httpRequest.mockRejectedValue(apiError);
298
+
299
+ await expect(requestCertificate(validParams)).rejects.toThrow(apiError);
300
+ });
301
+
302
+ it("should throw a generic error for other failures", async () => {
303
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONBOARDING });
304
+ const genericError = new Error("Network failure");
305
+ httpRequest.mockRejectedValue(genericError);
306
+
307
+ await expect(requestCertificate(validParams)).rejects.toThrow(
308
+ `Failed to request certificate: ${genericError.message}`
309
+ );
310
+ });
311
+ });
@@ -0,0 +1,170 @@
1
+ import {
2
+ createFaceLivenessSession,
3
+ validateFaceLiveness,
4
+ } from "../../src/api/faceLiveness";
5
+ import { httpRequest } from "../../src/utils/httpClient";
6
+ import ApacuanaSuccess from "../../src/success";
7
+ import { setConfig, getConfig } from "../../src/config";
8
+ import { ApacuanaAPIError } from "../../src/errors";
9
+ import { INTEGRATION_TYPE } from "../../src/utils/constant";
10
+
11
+ jest.mock("../../src/utils/httpClient", () => ({
12
+ httpRequest: jest.fn(),
13
+ }));
14
+
15
+ jest.mock("../../src/config", () => ({
16
+ getConfig: jest.fn(),
17
+ setConfig: jest.fn(),
18
+ }));
19
+
20
+ describe("FaceLiveness API", () => {
21
+ beforeEach(() => {
22
+ jest.clearAllMocks();
23
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONBOARDING });
24
+ });
25
+
26
+ describe("createFaceLivenessSession", () => {
27
+ it("should create a session for ONBOARDING integration", async () => {
28
+ const mockSessionId = "session-onboarding-123";
29
+ httpRequest.mockResolvedValue({ sessionid: mockSessionId });
30
+ setConfig({ onboardingId: "some-onboarding-id" });
31
+
32
+ const result = await createFaceLivenessSession();
33
+
34
+ expect(httpRequest).toHaveBeenCalledWith(
35
+ "services/api/faceliveness/create",
36
+ {},
37
+ "POST"
38
+ );
39
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
40
+ expect(result.data).toEqual({ sessionId: mockSessionId });
41
+ });
42
+
43
+ it("should throw an error if session ID is missing for ONBOARDING", async () => {
44
+ httpRequest.mockResolvedValue({}); // No sessionId
45
+
46
+ await expect(createFaceLivenessSession()).rejects.toThrow(
47
+ new ApacuanaAPIError(
48
+ "The API response does not contain the session ID.",
49
+ undefined,
50
+ "INVALID_API_RESPONSE"
51
+ )
52
+ );
53
+ });
54
+
55
+ it("should throw a NOT_IMPLEMENTED error for ONPREMISE integration", async () => {
56
+ getConfig.mockReturnValue({
57
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
58
+ });
59
+
60
+ await expect(createFaceLivenessSession()).rejects.toThrow(
61
+ new ApacuanaAPIError(
62
+ "Creating a Face Liveness session is not supported for integration type: ONPREMISE",
63
+ 501,
64
+ "NOT_IMPLEMENTED"
65
+ )
66
+ );
67
+ });
68
+
69
+ it("should throw an UNSUPPORTED_INTEGRATION_TYPE error for unsupported integration types", async () => {
70
+ getConfig.mockReturnValue({ integrationType: "unsupported-type" });
71
+
72
+ await expect(createFaceLivenessSession()).rejects.toThrow(
73
+ new ApacuanaAPIError(
74
+ "Unsupported integration type: unsupported-type",
75
+ 400,
76
+ "UNSUPPORTED_INTEGRATION_TYPE"
77
+ )
78
+ );
79
+ });
80
+
81
+ it("should re-throw ApacuanaAPIError if caught during session creation", async () => {
82
+ const apiError = new ApacuanaAPIError("API Error", 500, "API_ERROR");
83
+ httpRequest.mockRejectedValue(apiError);
84
+
85
+ await expect(createFaceLivenessSession()).rejects.toThrow(apiError);
86
+ });
87
+
88
+ it("should throw a generic error for other failures during session creation", async () => {
89
+ const genericError = new Error("Network Error");
90
+ httpRequest.mockRejectedValue(genericError);
91
+
92
+ await expect(createFaceLivenessSession()).rejects.toThrow(
93
+ `Failed to create Face Liveness session: ${genericError.message}`
94
+ );
95
+ });
96
+ });
97
+
98
+ describe("validateFaceLiveness", () => {
99
+ const sessionId = "test-session-id";
100
+
101
+ it("should throw an error if sessionId is not provided", async () => {
102
+ await expect(validateFaceLiveness({})).rejects.toThrow(
103
+ new ApacuanaAPIError(
104
+ "sessionId is a required parameter.",
105
+ 400,
106
+ "INVALID_PARAMETER"
107
+ )
108
+ );
109
+ });
110
+
111
+ it("should return 'verified' on status 200", async () => {
112
+ httpRequest.mockResolvedValue({ someData: "data" });
113
+
114
+ const result = await validateFaceLiveness({ sessionId });
115
+
116
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
117
+ expect(result.data).toEqual({ status: "verified", someData: "data" });
118
+ expect(httpRequest).toHaveBeenCalledWith(
119
+ "services/api/faceliveness/validate",
120
+ { sessionid: sessionId },
121
+ "POST"
122
+ );
123
+ });
124
+
125
+ it.each([
126
+ [402, "waitingForScan"],
127
+ [403, "rejected"],
128
+ [406, "processing"],
129
+ [408, "expired"],
130
+ ])(
131
+ "should return status '%s' for statusCode %i",
132
+ async (statusCode, status) => {
133
+ const apiError = new ApacuanaAPIError("Error", statusCode, "API_ERROR");
134
+ httpRequest.mockRejectedValue(apiError);
135
+
136
+ const result = await validateFaceLiveness({ sessionId });
137
+
138
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
139
+ expect(result.data).toEqual({ status });
140
+ }
141
+ );
142
+
143
+ it("should throw an error for unhandled status codes", async () => {
144
+ const apiError = new ApacuanaAPIError(
145
+ "Unhandled Error",
146
+ 500,
147
+ "API_ERROR"
148
+ );
149
+ httpRequest.mockRejectedValue(apiError);
150
+
151
+ await expect(validateFaceLiveness({ sessionId })).rejects.toThrow(
152
+ apiError
153
+ );
154
+ });
155
+
156
+ it("should throw a NOT_IMPLEMENTED error for ONPREMISE integration", async () => {
157
+ getConfig.mockReturnValue({
158
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
159
+ });
160
+
161
+ await expect(validateFaceLiveness({ sessionId })).rejects.toThrow(
162
+ new ApacuanaAPIError(
163
+ "Validating a Face Liveness session is not supported for integration type: ONPREMISE",
164
+ 501,
165
+ "NOT_IMPLEMENTED"
166
+ )
167
+ );
168
+ });
169
+ });
170
+ });
@@ -0,0 +1,119 @@
1
+ import {
2
+ requestRevocation,
3
+ getRevocationReasons,
4
+ } from "../../src/api/revocations";
5
+ import { httpRequest } from "../../src/utils/httpClient";
6
+ import { getConfig } from "../../src/config";
7
+ import { ApacuanaAPIError } from "../../src/errors";
8
+ import ApacuanaSuccess from "../../src/success";
9
+ import { INTEGRATION_TYPE } from "../../src/utils/constant";
10
+
11
+ jest.mock("../../src/utils/httpClient");
12
+ jest.mock("../../src/config");
13
+
14
+ describe("Revocations API", () => {
15
+ beforeEach(() => {
16
+ jest.clearAllMocks();
17
+ getConfig.mockReturnValue({ integrationType: INTEGRATION_TYPE.ONBOARDING });
18
+ });
19
+
20
+ describe("requestRevocation", () => {
21
+ it("should throw an error if reasonCode is not provided", async () => {
22
+ await expect(requestRevocation(null)).rejects.toThrow(
23
+ 'The "params" object with a numeric "reasonCode" property is required.'
24
+ );
25
+ await expect(requestRevocation({})).rejects.toThrow(
26
+ 'The "params" object with a numeric "reasonCode" property is required.'
27
+ );
28
+ });
29
+
30
+ it("should make a POST request to the correct endpoint with the reasonCode", async () => {
31
+ const params = { reasonCode: 1 };
32
+ const mockResponse = { success: true };
33
+ httpRequest.mockResolvedValue(mockResponse);
34
+
35
+ const result = await requestRevocation(params);
36
+
37
+ expect(httpRequest).toHaveBeenCalledWith(
38
+ "services/api/onboardingclient/requestcert",
39
+ { reason: params.reasonCode },
40
+ "POST"
41
+ );
42
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
43
+ expect(result.data).toEqual(mockResponse);
44
+ });
45
+
46
+ it("should throw a generic error if the httpRequest fails", async () => {
47
+ const params = { reasonCode: 1 };
48
+ const apiError = new Error("Something went wrong");
49
+ httpRequest.mockRejectedValue(apiError);
50
+
51
+ await expect(requestRevocation(params)).rejects.toThrow(
52
+ `Failed to request revocation: ${apiError.message}`
53
+ );
54
+ });
55
+
56
+ it("should re-throw ApacuanaAPIError if caught", async () => {
57
+ const params = { reasonCode: 1 };
58
+ const apiError = new ApacuanaAPIError("API Error");
59
+ httpRequest.mockRejectedValue(apiError);
60
+
61
+ await expect(requestRevocation(params)).rejects.toThrow(apiError);
62
+ });
63
+ });
64
+
65
+ describe("getRevocationReasons", () => {
66
+ it("should make a GET request and return the reasons for ONBOARDING", async () => {
67
+ const mockReasons = [{ code: "01", description: "Test Reason" }];
68
+ httpRequest.mockResolvedValue({ records: mockReasons });
69
+
70
+ const result = await getRevocationReasons();
71
+
72
+ expect(httpRequest).toHaveBeenCalledWith(
73
+ "config/api/revocation/reasonsonboarding",
74
+ {},
75
+ "GET"
76
+ );
77
+ expect(result).toBeInstanceOf(ApacuanaSuccess);
78
+ expect(result.data).toEqual({ reasons: mockReasons });
79
+ });
80
+
81
+ it("should throw an error if the API response does not contain 'records'", async () => {
82
+ httpRequest.mockResolvedValue({ data: [] }); // Invalid response
83
+
84
+ await expect(getRevocationReasons()).rejects.toThrow(
85
+ new ApacuanaAPIError("Failed to fetch revocation reasons.")
86
+ );
87
+ });
88
+
89
+ it("should re-throw ApacuanaAPIError if caught", async () => {
90
+ const apiError = new ApacuanaAPIError("API Error");
91
+ httpRequest.mockRejectedValue(apiError);
92
+
93
+ await expect(getRevocationReasons()).rejects.toThrow(apiError);
94
+ });
95
+
96
+ it("should throw a generic error for other types of errors", async () => {
97
+ const genericError = new Error("Something went wrong");
98
+ httpRequest.mockRejectedValue(genericError);
99
+
100
+ await expect(getRevocationReasons()).rejects.toThrow(
101
+ `Failed to get revocation reasons: ${genericError.message}`
102
+ );
103
+ });
104
+
105
+ it("should throw not implemented for ONPREMISE integration", async () => {
106
+ getConfig.mockReturnValue({
107
+ integrationType: INTEGRATION_TYPE.ONPREMISE,
108
+ });
109
+
110
+ await expect(getRevocationReasons()).rejects.toThrow(
111
+ new ApacuanaAPIError(
112
+ "Getting revocation reasons is not supported for integration type: ONPREMISE",
113
+ 501,
114
+ "NOT_IMPLEMENTED"
115
+ )
116
+ );
117
+ });
118
+ });
119
+ });