Skip to content

Commit af5c909

Browse files
committed
Add test to iiConnection
1 parent 48d9b6c commit af5c909

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/frontend/src/lib/utils/iiConnection.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ const DEFAULT_INIT: InternetIdentityInit = {
8585
feature_flag_enable_generic_open_id_fe: [],
8686
};
8787

88+
vi.mock("$lib/globals", () => ({
89+
canisterConfig: {
90+
new_flow_origins: [["https://id.ai"]],
91+
},
92+
}));
93+
8894
const mockActor = {
8995
identity_info: vi.fn().mockImplementation(async () => {
9096
// The `await` is necessary to make sure that the `getterResponse` is set before the test continues.
@@ -232,6 +238,39 @@ describe("Connection.login", () => {
232238
}
233239
});
234240

241+
it("login returns authenticated connection with expected rpID if new flow origins are enabled", async () => {
242+
const newOriginDevice = createMockDevice("https://id.ai");
243+
const mockActor = {
244+
identity_info: vi.fn().mockImplementation(async () => {
245+
// The `await` is necessary to make sure that the `getterResponse` is set before the test continues.
246+
infoResponse = await mockRawMetadata;
247+
return { Ok: { metadata: mockRawMetadata } };
248+
}),
249+
identity_metadata_replace: vi.fn().mockResolvedValue({ Ok: null }),
250+
// The order here matters, the first device is the one that would be used normally.
251+
// But we changed to push the new_flow_origins to the end.
252+
lookup: vi.fn().mockResolvedValue([newOriginDevice, mockDevice]),
253+
} as unknown as ActorSubclass<_SERVICE>;
254+
const connection = new Connection("aaaaa-aa", DEFAULT_INIT, mockActor);
255+
256+
const loginResult = await connection.login(BigInt(12345));
257+
258+
expect(loginResult.kind).toBe("loginSuccess");
259+
if (loginResult.kind === "loginSuccess") {
260+
expect(loginResult.connection).toBeInstanceOf(AuthenticatedConnection);
261+
expect(loginResult.showAddCurrentDevice).toBe(false);
262+
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledTimes(1);
263+
expect(MultiWebAuthnIdentity.fromCredentials).toHaveBeenCalledWith(
264+
[
265+
convertToValidCredentialData(newOriginDevice),
266+
convertToValidCredentialData(mockDevice),
267+
],
268+
"identity.ic0.app",
269+
true,
270+
);
271+
}
272+
});
273+
235274
it("login returns undefined RP ID if no related origins are in the config", async () => {
236275
const config: InternetIdentityInit = {
237276
...DEFAULT_INIT,

0 commit comments

Comments
 (0)