diff --git a/src/services/__tests__/ioWalletService.test.ts b/src/services/__tests__/ioWalletService.test.ts index 3d90afa2b..78e0bc21d 100644 --- a/src/services/__tests__/ioWalletService.test.ts +++ b/src/services/__tests__/ioWalletService.test.ts @@ -5,6 +5,7 @@ import { Grant_typeEnum } from "../../../generated/io-wallet-api/CreateWalletAtt import { TrialSystemAPIClient } from "../../clients/trial-system.client"; import { SubscriptionStateEnum } from "../../../generated/trial-system-api/SubscriptionState"; import { IO_WALLET_TRIAL_ID } from "../../config"; +import { StatusEnum } from "../../../generated/io-wallet-api/SetWalletInstanceStatusWithFiscalCodeData"; const mockGetEntityConfiguration = jest.fn(); const mockGetNonce = jest.fn(); @@ -14,6 +15,7 @@ const mockCreateWalletAttestation = jest.fn(); const mockHealthCheck = jest.fn(); const mockGetCurrentWalletInstanceStatus = jest.fn(); const mockSetWalletInstanceStatus = jest.fn(); +const mockSetCurrentWalletInstanceStatus = jest.fn(); mockGetUserByFiscalCode.mockImplementation(() => t.success({ @@ -60,6 +62,12 @@ mockSetWalletInstanceStatus.mockImplementation(() => }) ); +mockSetCurrentWalletInstanceStatus.mockImplementation(() => + t.success({ + status: 204, + }) +); + const api = { getEntityConfiguration: mockGetEntityConfiguration, getNonce: mockGetNonce, @@ -69,6 +77,7 @@ const api = { healthCheck: mockHealthCheck, getCurrentWalletInstanceStatus: mockGetCurrentWalletInstanceStatus, setWalletInstanceStatus: mockSetWalletInstanceStatus, + setCurrentWalletInstanceStatus: mockSetCurrentWalletInstanceStatus, }; const mockCreateSubscription = jest.fn(); @@ -476,6 +485,127 @@ describe("IoWalletService#createWalletAttestation", () => { }); }); +describe("IoWalletService#setCurrentWalletInstanceStatus", () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + const status = StatusEnum["REVOKED"]; + + it("should make the correct api call", async () => { + const service = new IoWalletService(api, trialSystemApi); + + await service.setCurrentWalletInstanceStatus(status, aFiscalCode); + + expect(mockSetCurrentWalletInstanceStatus).toHaveBeenCalledWith({ + body: { + status, + fiscal_code: aFiscalCode, + }, + }); + }); + + it("should handle a success response", async () => { + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseSuccessNoContent", + }); + }); + + it("should handle a generic error when the API client returns 422", async () => { + mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => + t.success({ status: 422 }) + ); + + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseErrorGeneric", + }); + }); + + it("should handle an internal error when the API client returns 500", async () => { + const aGenericProblem = {}; + mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => + t.success({ status: 500, value: aGenericProblem }) + ); + + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseErrorInternal", + }); + }); + + it("should handle a service unavailable error when the API client returns 503", async () => { + const aGenericProblem = {}; + mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => + t.success({ status: 503, value: aGenericProblem }) + ); + + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseErrorServiceUnavailable", + }); + }); + + it("should handle an internal error when the API client returns a code not specified in spec", async () => { + const aGenericProblem = {}; + mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => + t.success({ status: 599, value: aGenericProblem }) + ); + + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseErrorInternal", + }); + }); + + it("should return an error if the api call throws an error", async () => { + mockSetCurrentWalletInstanceStatus.mockImplementationOnce(() => { + throw new Error(); + }); + const service = new IoWalletService(api, trialSystemApi); + + const res = await service.setCurrentWalletInstanceStatus( + status, + aFiscalCode + ); + + expect(res).toMatchObject({ + kind: "IResponseErrorInternal", + }); + }); +}); + describe("IoWalletService#getSubscription", () => { beforeEach(() => { jest.clearAllMocks();