feat: use randomly generated usernames to avoid registration clashes

This commit is contained in:
Aravinth Manivannan 2024-01-10 01:54:34 +05:30
parent 75b93856eb
commit 5dc074a883
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 47 additions and 23 deletions

View file

@ -4,6 +4,7 @@
import { test, expect } from "@playwright/test"; import { test, expect } from "@playwright/test";
import * as config from "./config"; import * as config from "./config";
import User from "./user";
test("has title", async ({ page }) => { test("has title", async ({ page }) => {
await page.goto(config.INSTANCE_URL.toString()); 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 }) => { 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.goto(config.INSTANCE_URL.toString());
await page.getByRole("link", { name: "Register" }).click(); await page.getByRole("link", { name: "Register" }).click();
await page.waitForURL("**/user/sign_up"); await page.waitForURL("**/user/sign_up");
await page.getByLabel("Username").fill(config.USER_1.username); await page.getByLabel("Username").fill(user.username);
await page.getByLabel("Email Address").fill(config.USER_1.email); await page.getByLabel("Email Address").fill(user.email);
await page await page.getByLabel("Password", { exact: true }).fill(user.password);
.getByLabel("Password", { exact: true })
.fill(config.USER_1.password);
// passwords don't match // 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(); 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("Password", { exact: true }).clear();
await page.getByLabel("Confirm Password").clear(); await page.getByLabel("Confirm Password").clear();
await page.getByLabel("Username").fill(config.USER_1.username); await page.getByLabel("Username").fill(user.username);
await page.getByLabel("Email Address").fill(config.USER_1.email); await page.getByLabel("Email Address").fill(user.email);
await page await page.getByLabel("Password", { exact: true }).fill(user.password);
.getByLabel("Password", { exact: true }) await page.getByLabel("Confirm Password").fill(user.password);
.fill(config.USER_1.password);
await page.getByLabel("Confirm Password").fill(config.USER_1.password);
await page.getByRole("button", { name: "Register Account" }).click(); await page.getByRole("button", { name: "Register Account" }).click();
await page.waitForURL("http://localhost:3001/"); 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)")); console.log(page.locator(".positive > p:nth-child(1)"));
await expect(page).toHaveTitle( 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( expect(page.locator(".positive > p:nth-child(1)")).toContainText(
"Account was successfully created. Welcome!" "Account was successfully created. Welcome!"
); );
}); });

View file

@ -3,10 +3,3 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
export const INSTANCE_URL = new URL(process.env.INSTANCE_URL) 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",
}

15
tests/user.ts Normal file
View file

@ -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";
}
}

15
tests/utils.ts Normal file
View file

@ -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('')
}