From 5dc074a883197d45e0c4631b9491bb2b6c254298 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 10 Jan 2024 01:54:34 +0530 Subject: [PATCH] feat: use randomly generated usernames to avoid registration clashes --- tests/auth.spec.ts | 33 +++++++++++++++++---------------- tests/config.ts | 7 ------- tests/user.ts | 15 +++++++++++++++ tests/utils.ts | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 tests/user.ts create mode 100644 tests/utils.ts diff --git a/tests/auth.spec.ts b/tests/auth.spec.ts index 4348fc3..2309537 100644 --- a/tests/auth.spec.ts +++ b/tests/auth.spec.ts @@ -4,6 +4,7 @@ import { test, expect } from "@playwright/test"; import * as config from "./config"; +import User from "./user"; test("has title", async ({ page }) => { await page.goto(config.INSTANCE_URL.toString()); @@ -19,17 +20,19 @@ test("Go to explore page", async ({ page }) => { }); test("Go to register page", async ({ page }) => { + const user = new User("gotoregisterpage"); + + console.log(`running with user ${user.username}`); + await page.goto(config.INSTANCE_URL.toString()); await page.getByRole("link", { name: "Register" }).click(); await page.waitForURL("**/user/sign_up"); - await page.getByLabel("Username").fill(config.USER_1.username); - await page.getByLabel("Email Address").fill(config.USER_1.email); - await page - .getByLabel("Password", { exact: true }) - .fill(config.USER_1.password); + await page.getByLabel("Username").fill(user.username); + await page.getByLabel("Email Address").fill(user.email); + await page.getByLabel("Password", { exact: true }).fill(user.password); // passwords don't match - await page.getByLabel("Confirm Password").fill(config.USER_1.username); + await page.getByLabel("Confirm Password").fill(user.username); await page.getByRole("button", { name: "Register Account" }).click(); @@ -48,12 +51,10 @@ test("Go to register page", async ({ page }) => { await page.getByLabel("Password", { exact: true }).clear(); await page.getByLabel("Confirm Password").clear(); - await page.getByLabel("Username").fill(config.USER_1.username); - await page.getByLabel("Email Address").fill(config.USER_1.email); - await page - .getByLabel("Password", { exact: true }) - .fill(config.USER_1.password); - await page.getByLabel("Confirm Password").fill(config.USER_1.password); + await page.getByLabel("Username").fill(user.username); + await page.getByLabel("Email Address").fill(user.email); + await page.getByLabel("Password", { exact: true }).fill(user.password); + await page.getByLabel("Confirm Password").fill(user.password); await page.getByRole("button", { name: "Register Account" }).click(); await page.waitForURL("http://localhost:3001/"); @@ -61,10 +62,10 @@ test("Go to register page", async ({ page }) => { console.log(page.locator(".positive > p:nth-child(1)")); await expect(page).toHaveTitle( - `${config.USER_1.username} - Dashboard - Forgejo: Beyond coding. We Forge.` + `${user.username} - Dashboard - Forgejo: Beyond coding. We Forge.` ); - expect(page.locator(".positive > p:nth-child(1)")).toContainText( - "Account was successfully created. Welcome!" - ); + expect(page.locator(".positive > p:nth-child(1)")).toContainText( + "Account was successfully created. Welcome!" + ); }); diff --git a/tests/config.ts b/tests/config.ts index 2ae3eac..8b919f7 100644 --- a/tests/config.ts +++ b/tests/config.ts @@ -3,10 +3,3 @@ // SPDX-License-Identifier: AGPL-3.0-or-later export const INSTANCE_URL = new URL(process.env.INSTANCE_URL) - -export const USER_1 = { - username: "playwrightuser1", - email: "playwrightuser1@example.com", - password: "628acdbd8b7b3f", - name: "Playwright User1", -} diff --git a/tests/user.ts b/tests/user.ts new file mode 100644 index 0000000..b07195b --- /dev/null +++ b/tests/user.ts @@ -0,0 +1,15 @@ +import { getRandomString } from "./utils"; + +export default class User { + username: string; + name: string; + email: string; + password: string; + + constructor(name: string) { + this.name = name; + this.username = `playwright_${getRandomString(6)}_${name}`; + this.email = `${this.username}@playwright.local`; + this.password = "628acdbd8b7b3f"; + } +} diff --git a/tests/utils.ts b/tests/utils.ts new file mode 100644 index 0000000..b4c97a5 --- /dev/null +++ b/tests/utils.ts @@ -0,0 +1,15 @@ +// generateId :: Integer -> String +export function getRandomString (len: number) { + let crypto = require("crypto"); + let id = crypto.randomBytes(len).toString('hex'); + return id; + +// function dec2hex (dec) { +// return dec.toString(16).padStart(2, "0") +// } +// +// +// var arr = new Uint8Array((len || 40) / 2) +// window.crypto.getRandomValues(arr) +// return Array.from(arr, dec2hex).join('') +}