Skip to content

Commit

Permalink
Extract keychain connection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Aug 8, 2024
1 parent 7027f68 commit 0f9e04e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
51 changes: 13 additions & 38 deletions examples/starknet-react-next/tests/connect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,27 @@
import { test, expect } from "@playwright/test";
import { WebauthnEmulator } from "./webauthn";
import { Keychain } from "./keychain";

test.beforeEach(async ({ page }) => {
await page.goto("/");
const client = await page.context().newCDPSession(page);
const webauthn = new WebauthnEmulator(client);
const webauthn = new WebauthnEmulator({ client });
await webauthn.enable();
await webauthn.addVirtualAuthenticator();
});

test.describe("Sign up", () => {
test("should allow user to sign up and connect to Controller", async ({
page,
}) => {
await page.getByText("Connect").click();
const modal = page.frameLocator("#cartridge-modal");
test("Sign up", async ({ page }) => {
const keychain = new Keychain({ page });
await keychain.signup();

await expect(
modal.getByText("Play with Cartridge Controller"),
).toBeVisible();
await expect(
modal.getByText("Create your Cartridge Controller"),
).toBeVisible();

const username = `test-${Date.now()}`;
await modal.getByPlaceholder("Username").fill(username);
await modal.getByText("SIGN UP").click();

await expect(modal.getByText("Create Session").first()).toBeVisible();
await modal.getByRole("button", { name: "CREATE SESSION" }).click();

await expect(page.getByText(`Username: ${username}`)).toBeVisible();
});
await expect(page.getByText(`Username: ${keychain.username}`)).toBeVisible();
});

// test.describe("Log in", () => {
// test("should allow me to login and connect to Controller", async ({
// page,
// }) => {
// await page.getByText("Connect").click();
// const modal = page.frameLocator("#cartridge-modal");
// await modal.getByText("Log In").click();
// await expect(
// modal.getByText("Play with Cartridge Controller"),
// ).toBeVisible();
// await modal.getByPlaceholder("Username").fill("test-1");
// await modal.getByText("LOG IN").click();
test("Log in", async ({ page }) => {
const keychain = new Keychain({ page });
await keychain.signup();
await keychain.disconnect();
await keychain.login();

// await expect(page.getByText("Address: ")).toBeVisible();
// });
// });
await expect(page.getByText(`Username: ${keychain.username}`)).toBeVisible();
});
40 changes: 40 additions & 0 deletions examples/starknet-react-next/tests/keychain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FrameLocator } from "@playwright/test";
import { Page } from "@playwright/test";

export class Keychain {
private page: Page;
private modal: FrameLocator;
public username: string;

constructor({ page, username }: { page: Page; username?: string }) {
this.page = page;
this.modal = page.frameLocator("#cartridge-modal");
this.username = username ?? this.randomUsername();

Check failure

Code scanning / CodeQL

Insecure randomness High test

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.
}

async signup() {
await this.connect();
await this.modal.getByPlaceholder("Username").fill(this.username);
await this.modal.getByText("SIGN UP").click();
await this.modal.getByRole("button", { name: "CREATE SESSION" }).click();
}

async login() {
await this.connect();
await this.modal.getByText("Log In").click();
await this.modal.getByPlaceholder("Username").fill(this.username);
await this.modal.getByText("LOG IN").click();
}

disconnect() {
return this.page.getByText("Disconnect").click();
}

private connect() {
return this.page.getByText("Connect").click();
}

private randomUsername() {
return `test-${Math.random().toString().slice(2, -1)}`;
}
}
4 changes: 2 additions & 2 deletions examples/starknet-react-next/tests/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CDPSession } from "@playwright/test";

export async function addVirtualAuthenticator(client: CDPSession) {
await client.send("WebAuthn.enable");
const {} = await client.send("WebAuthn.addVirtualAuthenticator", {
await client.send("WebAuthn.addVirtualAuthenticator", {
options: {
protocol: "ctap2",
transport: "ble",
Expand All @@ -18,7 +18,7 @@ export class WebauthnEmulator {
private client: CDPSession;
public authenticatorId?: string;

constructor(client: CDPSession) {
constructor({ client }: { client: CDPSession }) {
this.client = client;
}

Expand Down

0 comments on commit 0f9e04e

Please sign in to comment.