From 503d15dcd70a1829fa0f4d4b6eed46af04c4189f Mon Sep 17 00:00:00 2001 From: RiccardoMolinari95 Date: Mon, 23 Dec 2024 17:08:21 +0100 Subject: [PATCH] test: add test eid reissuing --- .../machine/eid/__tests__/machine.test.ts | 137 +++++++++++++++++- 1 file changed, 136 insertions(+), 1 deletion(-) diff --git a/ts/features/itwallet/machine/eid/__tests__/machine.test.ts b/ts/features/itwallet/machine/eid/__tests__/machine.test.ts index c7d51a9c7ab..dddce76a1ad 100644 --- a/ts/features/itwallet/machine/eid/__tests__/machine.test.ts +++ b/ts/features/itwallet/machine/eid/__tests__/machine.test.ts @@ -92,7 +92,10 @@ describe("itwEidIssuanceMachine", () => { resetWalletInstance, trackWalletInstanceCreation, trackWalletInstanceRevocation, - onInit: assign(onInit) + onInit: assign(onInit), + setIsReissuing : assign({ + isReissuing: true + }) }, actors: { createWalletInstance: fromPromise(createWalletInstance), @@ -982,4 +985,136 @@ describe("itwEidIssuanceMachine", () => { expect(actor.getSnapshot().value).toStrictEqual("Idle"); }); + + it("Should obtain an eID (SPID), reissuing mode", async () => { + + //The wallet instance and attestation already exist + const initialContext = { + ...InitialContext, + integrityKeyTag: T_INTEGRITY_KEY, + walletInstanceAttestation: T_WIA, + }; + + const actor = createActor(mockedMachine); + actor.start(); + + actor.getSnapshot().context = initialContext; + + await waitFor(() => expect(onInit).toHaveBeenCalledTimes(1)); + + expect(actor.getSnapshot().value).toStrictEqual("Idle"); + expect(actor.getSnapshot().tags).toStrictEqual(new Set()); + + actor.send({ type: "start-reissuing" }); + + expect(actor.getSnapshot().value).toStrictEqual({ + UserIdentification: "ModeSelection" + }); + + expect(actor.getSnapshot().context).toStrictEqual({ + ...initialContext, + isReissuing: true + }); + + /** + * Choose SPID as identification mode + */ + + actor.send({ type: "select-identification-mode", mode: "spid" }); + + expect(actor.getSnapshot().value).toStrictEqual({ + UserIdentification: { + Spid: "IdpSelection" + } + }); + expect(actor.getSnapshot().tags).toStrictEqual(new Set()); + expect(navigateToIdpSelectionScreen).toHaveBeenCalledTimes(1); + + /** + * Choose first IDP in list for SPID identification + */ + + startAuthFlow.mockImplementation(() => Promise.resolve({})); + + requestEid.mockImplementation(() => + Promise.resolve(ItwStoredCredentialsMocks.eid) + ); + + issuedEidMatchesAuthenticatedUser.mockImplementation(() => true); + + actor.send({ type: "select-spid-idp", idp: idps[0] }); + + expect(actor.getSnapshot().value).toStrictEqual({ + UserIdentification: { + Spid: "StartingSpidAuthFlow" + } + }); + + expect(actor.getSnapshot().context).toStrictEqual({ + ...initialContext, + integrityKeyTag: T_INTEGRITY_KEY, + walletInstanceAttestation: T_WIA, + isReissuing: true, + identification: { + mode: "spid", + idpId: idps[0].id + } + }); + + expect(actor.getSnapshot().tags).toStrictEqual(new Set([ItwTags.Loading])); + + await waitFor(() => expect(startAuthFlow).toHaveBeenCalledTimes(1)); + + expect(actor.getSnapshot().value).toStrictEqual({ + UserIdentification: { + Spid: "CompletingSpidAuthFlow" + } + }); + + actor.send({ + type: "user-identification-completed", + authRedirectUrl: "http://test.it" + }); + + expect(actor.getSnapshot().value).toStrictEqual({ + Issuance: "RequestingEid" + }); + + expect(actor.getSnapshot().tags).toStrictEqual(new Set([ItwTags.Loading])); + expect(actor.getSnapshot().context).toMatchObject({ + authenticationContext: { + callbackUrl: "http://test.it" + } + }); + expect(navigateToEidPreviewScreen).toHaveBeenCalledTimes(1); + + // EID obtained + + await waitFor(() => + expect(actor.getSnapshot().value).toStrictEqual({ + Issuance: "DisplayingPreview" + }) + ); + + actor.send({ type: "add-to-wallet" }); + + expect(storeEidCredential).toHaveBeenCalledTimes(1); + expect(setWalletInstanceToValid).toHaveBeenCalledTimes(1); + expect(navigateToWallet).toHaveBeenCalledTimes(1); + + expect(actor.getSnapshot().context).toStrictEqual({ + ...initialContext, + integrityKeyTag: T_INTEGRITY_KEY, + walletInstanceAttestation: T_WIA, + isReissuing: true, + identification: { + mode: "spid", + idpId: idps[0].id + }, + authenticationContext: expect.objectContaining({ + callbackUrl: "http://test.it" + }), + eid: ItwStoredCredentialsMocks.eid + }); + }); });