|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 3 | +import { TEST_ACCOUNT_A } from "~test/test-wallets.js"; |
| 4 | +import { createWalletAdapter } from "../../../../adapters/wallet-adapter.js"; |
| 5 | +import { ethereum } from "../../../../chains/chain-definitions/ethereum.js"; |
| 6 | +import { backendAuthenticate } from "../../core/authentication/backend.js"; |
| 7 | +import { guestAuthenticate } from "../../core/authentication/guest.js"; |
| 8 | +import { siweAuthenticate } from "../../core/authentication/siwe.js"; |
| 9 | +import { loginWithOauth } from "./auth/oauth.js"; |
| 10 | +import { verifyOtp } from "./auth/otp.js"; |
| 11 | +import { InAppWebConnector } from "./web-connector.js"; |
| 12 | + |
| 13 | +vi.mock("./auth/oauth"); |
| 14 | +vi.mock("./auth/iframe-auth.ts", () => { |
| 15 | + const Auth = vi.fn(); |
| 16 | + Auth.prototype.loginWithAuthToken = vi.fn(() => { |
| 17 | + console.log("loginWithAuthToken"); |
| 18 | + return Promise.resolve({ |
| 19 | + user: { |
| 20 | + authDetails: { |
| 21 | + recoveryShareManagement: "ENCLAVE", |
| 22 | + userWalletId: "123", |
| 23 | + }, |
| 24 | + status: "Logged In, Wallet Initialized", |
| 25 | + walletAddress: "0x123", |
| 26 | + }, |
| 27 | + }); |
| 28 | + }); |
| 29 | + return { Auth }; |
| 30 | +}); |
| 31 | +vi.mock("../../core/authentication/siwe"); |
| 32 | +vi.mock("../../core/authentication/guest"); |
| 33 | +vi.mock("../../core/authentication/backend"); |
| 34 | +vi.mock("./auth/otp"); |
| 35 | +vi.mock("../../core/authentication/authEndpoint"); |
| 36 | +vi.mock("../../core/authentication/jwt"); |
| 37 | +vi.mock("../../web/utils/iFrameCommunication/InAppWalletIframeCommunicator"); |
| 38 | + |
| 39 | +describe("InAppWebConnector.connect", () => { |
| 40 | + const mockAuthToken = { |
| 41 | + storedToken: { |
| 42 | + authDetails: { |
| 43 | + userWalletId: "123", |
| 44 | + recoveryShareManagement: "ENCLAVE" as const, |
| 45 | + }, |
| 46 | + authProvider: "EmailOtp" as const, |
| 47 | + cookieString: "mock-cookie", |
| 48 | + developerClientId: TEST_CLIENT.clientId, |
| 49 | + isNewUser: false, |
| 50 | + jwtToken: "mock-jwt-token", |
| 51 | + shouldStoreCookieString: true, |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + const connector = new InAppWebConnector({ |
| 56 | + client: TEST_CLIENT, |
| 57 | + }); |
| 58 | + const mockWallet = createWalletAdapter({ |
| 59 | + adaptedAccount: TEST_ACCOUNT_A, |
| 60 | + client: TEST_CLIENT, |
| 61 | + chain: ethereum, |
| 62 | + onDisconnect: () => {}, |
| 63 | + switchChain: () => {}, |
| 64 | + }); |
| 65 | + const mockAccount = mockWallet.getAccount(); |
| 66 | + if (!mockAccount) { |
| 67 | + throw new Error("mockAccount is undefined"); |
| 68 | + } |
| 69 | + |
| 70 | + it("should handle email authentication", async () => { |
| 71 | + vi.mocked(verifyOtp).mockResolvedValueOnce(mockAuthToken); |
| 72 | + |
| 73 | + const result = await connector.connect({ |
| 74 | + strategy: "email", |
| 75 | + |
| 76 | + verificationCode: "123456", |
| 77 | + }); |
| 78 | + |
| 79 | + expect(verifyOtp).toHaveBeenCalledWith({ |
| 80 | + strategy: "email", |
| 81 | + |
| 82 | + verificationCode: "123456", |
| 83 | + client: TEST_CLIENT, |
| 84 | + ecosystem: undefined, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(result).toBeDefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should handle wallet authentication", async () => { |
| 91 | + vi.mocked(siweAuthenticate).mockResolvedValueOnce(mockAuthToken); |
| 92 | + |
| 93 | + const mockWallet = createWalletAdapter({ |
| 94 | + adaptedAccount: TEST_ACCOUNT_A, |
| 95 | + client: TEST_CLIENT, |
| 96 | + chain: ethereum, |
| 97 | + onDisconnect: () => {}, |
| 98 | + switchChain: () => {}, |
| 99 | + }); |
| 100 | + |
| 101 | + await connector.connect({ |
| 102 | + strategy: "wallet", |
| 103 | + chain: ethereum, |
| 104 | + wallet: mockWallet, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(siweAuthenticate).toHaveBeenCalledWith({ |
| 108 | + wallet: mockWallet, |
| 109 | + chain: ethereum, |
| 110 | + client: TEST_CLIENT, |
| 111 | + ecosystem: undefined, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should handle guest authentication", async () => { |
| 116 | + vi.mocked(guestAuthenticate).mockResolvedValueOnce(mockAuthToken); |
| 117 | + |
| 118 | + await connector.connect({ |
| 119 | + strategy: "guest", |
| 120 | + }); |
| 121 | + |
| 122 | + expect(guestAuthenticate).toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should handle backend authentication", async () => { |
| 126 | + vi.mocked(backendAuthenticate).mockResolvedValueOnce(mockAuthToken); |
| 127 | + |
| 128 | + await connector.connect({ |
| 129 | + strategy: "backend", |
| 130 | + walletSecret: "secret123", |
| 131 | + }); |
| 132 | + |
| 133 | + expect(backendAuthenticate).toHaveBeenCalledWith({ |
| 134 | + walletSecret: "secret123", |
| 135 | + client: TEST_CLIENT, |
| 136 | + ecosystem: undefined, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should handle oauth authentication", async () => { |
| 141 | + vi.mocked(loginWithOauth).mockResolvedValueOnce(mockAuthToken); |
| 142 | + |
| 143 | + await connector.connect({ |
| 144 | + strategy: "google", |
| 145 | + }); |
| 146 | + |
| 147 | + expect(loginWithOauth).toHaveBeenCalledWith({ |
| 148 | + authOption: "google", |
| 149 | + client: TEST_CLIENT, |
| 150 | + ecosystem: undefined, |
| 151 | + closeOpenedWindow: undefined, |
| 152 | + openedWindow: undefined, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should throw error for invalid strategy", async () => { |
| 157 | + await expect( |
| 158 | + connector.connect({ |
| 159 | + // @ts-expect-error invalid strategy |
| 160 | + strategy: "invalid", |
| 161 | + }), |
| 162 | + ).rejects.toThrow("Invalid param: invalid"); |
| 163 | + }); |
| 164 | +}); |
0 commit comments