Skip to content

Commit

Permalink
Wait for principals to not be anonymous after sign-in (#2016)
Browse files Browse the repository at this point in the history
* Wait for principals to not be anonymous after sign-in

The switch to non-dockerized selenium tests exposed some flakyness
in the way principals are collected from the test app after sign-in.

This PR introduces a check to wait for the principal to update.

* Add waitForAuthenticated method to the DemoAppView
  • Loading branch information
Frederik Rothenberger authored Nov 7, 2023
1 parent 1e7da7d commit 072544c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
originToRelyingPartyId,
runInBrowser,
switchToPopup,
waitToClose,
} from "../util";
import { AuthenticateView, DemoAppView, ErrorView } from "../views";

Expand Down Expand Up @@ -147,8 +146,6 @@ test("Should fetch /.well-known/ii-alternative-origins using the non-raw url", a
const authenticateView = new AuthenticateView(browser);
await authenticateView.waitForDisplay();
await authenticateView.pickAnchor(userNumber);
await waitToClose(browser);

expect(await niceDemoAppView.getPrincipal()).not.toBe("2vxsx-fae");
await niceDemoAppView.waitForAuthenticated();
});
}, 300_000);
8 changes: 3 additions & 5 deletions src/frontend/src/test-e2e/delegationTtl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ test("Delegation maxTimeToLive: 1 min", async () => {
await demoAppView.signin();
await switchToPopup(browser);
await FLOWS.registerNewIdentityAuthenticateView(DEVICE_NAME1, browser);
await waitToClose(browser);
await demoAppView.waitForDisplay();
expect(await demoAppView.getPrincipal()).not.toBe("2vxsx-fae");
await demoAppView.waitForAuthenticated();

const exp = await browser.$("#expiration").getText();
// compare only up to one decimal place for the 1min test
expect(Number(exp) / 60_000_000_000).toBeCloseTo(1, 0);
Expand Down Expand Up @@ -58,8 +57,7 @@ test("Delegation maxTimeToLive: 2 months", async () => {
await demoAppView.signin();
await switchToPopup(browser);
await FLOWS.registerNewIdentityAuthenticateView(DEVICE_NAME1, browser);
await waitToClose(browser);
expect(await demoAppView.getPrincipal()).not.toBe("2vxsx-fae");
await demoAppView.waitForAuthenticated();
const exp = await browser.$("#expiration").getText();
// NB: Max out at 30 days
expect(Number(exp) / 2_592_000_000_000_000).toBeCloseTo(1);
Expand Down
18 changes: 3 additions & 15 deletions src/frontend/src/test-e2e/pinAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import {
TEST_APP_NICE_URL,
} from "./constants";
import { FLOWS } from "./flows";
import {
addVirtualAuthenticator,
runInBrowser,
switchToPopup,
waitToClose,
} from "./util";
import { addVirtualAuthenticator, runInBrowser, switchToPopup } from "./util";
import {
AuthenticateView,
DemoAppView,
Expand Down Expand Up @@ -142,11 +137,8 @@ test("Log into client application using PIN registration flow", async () => {
await demoAppView.signin();
await switchToPopup(browser);
await FLOWS.registerPinNewIdentityAuthenticateView(pin, browser);
await waitToClose(browser);
await demoAppView.waitForDisplay();
const principal = await demoAppView.getPrincipal();
expect(principal).not.toBe(Principal.anonymous().toText());

const principal = await demoAppView.waitForAuthenticated();
expect(await demoAppView.whoami(REPLICA_URL, TEST_APP_CANISTER_ID)).toBe(
principal
);
Expand Down Expand Up @@ -175,10 +167,6 @@ test("Register with PIN then log into client application", async () => {
await switchToPopup(browser);

await FLOWS.loginPinAuthenticateView(userNumber, pin, browser);
await waitToClose(browser);

await demoAppView.waitForDisplay();
const principal = await demoAppView.getPrincipal();
expect(principal).not.toBe(Principal.anonymous().toText());
await demoAppView.waitForAuthenticated();
}, APPLE_USER_AGENT);
}, 300_000);
10 changes: 3 additions & 7 deletions src/frontend/src/test-e2e/principals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
originToRelyingPartyId,
runInBrowser,
switchToPopup,
waitToClose,
} from "./util";
import { AuthenticateView, DemoAppView } from "./views";

Expand Down Expand Up @@ -49,9 +48,8 @@ test("Should issue the same principal to nice url and canonical url", async () =
let authenticateView = new AuthenticateView(browser);
await authenticateView.waitForDisplay();
await authenticateView.pickAnchor(userNumber);
await waitToClose(browser);

const principal1 = await canonicalDemoAppView.getPrincipal();
const principal1 = await canonicalDemoAppView.waitForAuthenticated();

const niceDemoAppView = new DemoAppView(browser);
await niceDemoAppView.open(TEST_APP_NICE_URL, II_URL);
Expand All @@ -76,9 +74,8 @@ test("Should issue the same principal to nice url and canonical url", async () =
authenticateView = new AuthenticateView(browser);
await authenticateView.waitForDisplay();
await authenticateView.pickAnchor(userNumber);
await waitToClose(browser);

const principal2 = await niceDemoAppView.getPrincipal();
const principal2 = await niceDemoAppView.waitForAuthenticated();
expect(principal1).toEqual(principal2);
});
}, 300_000);
Expand Down Expand Up @@ -116,9 +113,8 @@ test("Should issue the same principal to dapps on legacy & official domains", as
let authenticateView = new AuthenticateView(browser);
await authenticateView.waitForDisplay();
await authenticateView.pickAnchor(userNumber);
await waitToClose(browser);

return await canonicalDemoAppView.getPrincipal();
return canonicalDemoAppView.waitForAuthenticated();
};

// Compare that principals issues for ic0.app & icp0.io are the same
Expand Down
13 changes: 2 additions & 11 deletions src/frontend/src/test-e2e/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
originToRelyingPartyId,
runInBrowser,
switchToPopup,
waitToClose,
} from "./util";
import { AuthenticateView, DemoAppView, MainView } from "./views";

Expand Down Expand Up @@ -67,11 +66,7 @@ test("Log into client application, after registration", async () => {
await demoAppView.signin();
await switchToPopup(browser);
await FLOWS.registerNewIdentityAuthenticateView(DEVICE_NAME1, browser);
await waitToClose(browser);
await demoAppView.waitForDisplay();
const principal = await demoAppView.getPrincipal();
expect(principal).not.toBe("2vxsx-fae");

const principal = await demoAppView.waitForAuthenticated();
expect(await demoAppView.whoami(REPLICA_URL, TEST_APP_CANISTER_ID)).toBe(
principal
);
Expand Down Expand Up @@ -113,11 +108,7 @@ test("Register first then log into client application", async () => {
await authenticateView.waitForDisplay();
await authenticateView.pickAnchor(userNumber);
await FLOWS.skipRecoveryNag(browser);
await waitToClose(browser);
await demoAppView.waitForDisplay();
const principal = await demoAppView.getPrincipal();
expect(principal).not.toBe("2vxsx-fae");

const principal = await demoAppView.waitForAuthenticated();
expect(await demoAppView.whoami(REPLICA_URL, TEST_APP_CANISTER_ID)).toBe(
principal
);
Expand Down
15 changes: 15 additions & 0 deletions src/frontend/src/test-e2e/views.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { zip } from "$src/utils/utils";
import { Principal } from "@dfinity/principal";
import { nonNullish } from "@dfinity/utils";
import { waitToClose } from "./util";

class View {
constructor(protected browser: WebdriverIO.Browser) {}
Expand Down Expand Up @@ -606,6 +608,19 @@ export class DemoAppView extends View {
await this.browser.$("#signinBtn").click();
}

/** Waits for the authentication to finish, the window to close and the principal to update.
* Returns the principal after the user has been authenticated.
*/
async waitForAuthenticated(): Promise<string> {
// wait for the demo app to close the II window
await waitToClose(this.browser);
// wait for the demo app to update the principal
await this.browser.waitUntil(
async () => (await this.getPrincipal()) !== Principal.anonymous().toText()
);
return this.getPrincipal();
}

async signout(): Promise<void> {
await this.browser.$("#signoutBtn").click();
}
Expand Down

0 comments on commit 072544c

Please sign in to comment.