Skip to content

Commit d464ef8

Browse files
committed
updated create account tests
1 parent 734df5f commit d464ef8

1 file changed

Lines changed: 132 additions & 58 deletions

File tree

client/tests/e2e/1-user-account/1.1-create-add.spec.ts

Lines changed: 132 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { loginAsServerAdmin } from "../utils/authenticate";
33
import {
44
generateUniqueUsername,
55
navigateTo,
6-
selectVuetifyFirstOption,
76
selectVuetifyOptionByName,
87
} from "../utils/helpers";
98

@@ -69,14 +68,14 @@ async function expectSuccessfulCreateRedirect(page: Page) {
6968
});
7069
}
7170

71+
//TODO this test was left with kinda big redundancies to explain to the apprentices in simple terms what is going on. Small refactor later
7272
test.describe("1.1 User Accounts - CREATE Add", () => {
7373
test.beforeEach(async ({ page }) => {
7474
await loginAsServerAdmin(page);
7575
await navigateTo(page, "/user-accounts/create");
7676
});
7777

78-
//test A
79-
test("A Success - creates a new INACTIVE user account, verifies user with all details were created successfully", async ({
78+
test("A Success - creates a new INACTIVE user account", async ({
8079
page,
8180
}) => {
8281
const {
@@ -92,9 +91,7 @@ test.describe("1.1 User Accounts - CREATE Add", () => {
9291
saveButtonLocator,
9392
} = await setupCreateUserAccountPage(page);
9493

95-
const uniqueValue = generateUniqueUsername(
96-
"e2e-1.1.A-Success-User-Accounts",
97-
);
94+
const uniqueValue = generateUniqueUsername("e2e-1.1.A-Success");
9895

9996
//define values to be filled in
10097
const username = uniqueValue;
@@ -162,7 +159,7 @@ test.describe("1.1 User Accounts - CREATE Add", () => {
162159

163160
//this needs to match the fields we filled out above
164161
const expectedPayload = {
165-
institutionId: "11", //this matches SQL script for e2e testing!!!
162+
institutionId: "11", //this matches SQL script for e2e testing!!! it corresponds to SEB Server institution
166163
name: name,
167164
surname: surname,
168165
username: username,
@@ -183,51 +180,83 @@ test.describe("1.1 User Accounts - CREATE Add", () => {
183180
// 2: Clean way, set up a database query to actually verify the data is entered correct in the tables - This would be the clean way
184181
});
185182

186-
test("B Failed Validation - password too short", async ({ page }) => {
187-
const {
188-
institutionSelectLocator,
189-
usernameLocator,
190-
nameLocator,
191-
surnameLocator,
192-
emailLocator,
193-
timezoneSelectLocator,
194-
passwordLocator,
195-
confirmPasswordLocator,
196-
roleSelectLocator,
197-
saveButtonLocator,
198-
} = await setupCreateUserAccountPage(page);
183+
test("B Client Validation - checks all validations and that create button doesn't do API call", async ({
184+
page,
185+
}) => {
186+
const { passwordLocator, confirmPasswordLocator, saveButtonLocator } =
187+
await setupCreateUserAccountPage(page);
188+
189+
// mo post request allowed during this test (well none should get through)
190+
await page.route(/\/useraccount(?:\?|$)/i, async (route, request) => {
191+
if (request.method() === "POST") {
192+
await route.abort();
193+
throw new Error(
194+
"POST /useraccount was attempted during failed client validation test",
195+
);
196+
}
197+
await route.continue();
198+
});
199199

200-
const uname = generateUniqueUsername("e2e-invalid");
200+
page.on("request", (req) => {
201+
if (
202+
req.method() === "POST" &&
203+
/\/useraccount(?:\?|$)/i.test(req.url())
204+
) {
205+
throw new Error(
206+
`Validation test must not call POST /useraccount, but it did: ${req.url()}`,
207+
);
208+
}
209+
});
201210

202-
if (await institutionSelectLocator.isEnabled()) {
203-
await selectVuetifyOptionByName(
204-
page,
205-
institutionSelectLocator,
206-
"SEB Server",
207-
);
211+
async function expectFieldError(testId: string, text: string) {
212+
const field = page.getByTestId(testId);
213+
await expect(
214+
field.locator(".v-messages__message").filter({ hasText: text }),
215+
).toHaveCount(1, { timeout: 5000 });
208216
}
209217

210-
await usernameLocator.fill(uname);
211-
await nameLocator.fill("Test");
212-
await surnameLocator.fill("User");
213-
await emailLocator.fill(`${uname}@example.com`);
214-
215-
await selectVuetifyFirstOption(page, timezoneSelectLocator);
218+
const REQUIRED = "This field is required";
219+
const PASSWORD_TOO_SHORT = "Password must be at least 8 characters";
220+
const PASSWORDS_MUST_MATCH = "Passwords must match";
216221

217-
// too short (<8)
218-
await passwordLocator.fill("Abc1");
219-
await confirmPasswordLocator.fill("Abc1");
222+
// 1. Click with all empty
223+
await saveButtonLocator.click();
220224

221-
await selectVuetifyFirstOption(page, roleSelectLocator);
225+
await expectFieldError(
226+
"createUserAccount-institution-select",
227+
REQUIRED,
228+
);
229+
await expectFieldError("createUserAccount-username-input", REQUIRED);
230+
await expectFieldError("createUserAccount-name-input", REQUIRED);
231+
await expectFieldError("createUserAccount-surname-input", REQUIRED);
232+
await expectFieldError("createUserAccount-password-input", REQUIRED);
233+
await expectFieldError(
234+
"createUserAccount-confirmPassword-input",
235+
REQUIRED,
236+
);
237+
await expectFieldError("createUserAccount-role-select", REQUIRED);
222238

239+
// 2. Too short password
240+
await passwordLocator.fill("Abc1"); // < 8
223241
await saveButtonLocator.click();
224242

225-
await expect(page).toHaveURL(/\/user-accounts\/create(?:$|[?#])/i);
243+
await expectFieldError(
244+
"createUserAccount-password-input",
245+
PASSWORD_TOO_SHORT,
246+
);
226247

227-
const anyValidationMessage = page.locator(
228-
".v-messages .v-messages__message",
248+
// 3. missmatch passwords
249+
await passwordLocator.fill("Abcdefg1");
250+
await confirmPasswordLocator.fill("Abcdefg2");
251+
await saveButtonLocator.click();
252+
253+
await expectFieldError(
254+
"createUserAccount-confirmPassword-input",
255+
PASSWORDS_MUST_MATCH,
229256
);
230-
await expect(anyValidationMessage.first()).toBeVisible();
257+
258+
// expected to not change URL
259+
await expect(page).toHaveURL(/\/user-accounts\/create(?:$|[?#])/i);
231260
});
232261

233262
test("C Server Error - user account already exists (duplicate username)", async ({
@@ -246,31 +275,76 @@ test.describe("1.1 User Accounts - CREATE Add", () => {
246275
saveButtonLocator,
247276
} = await setupCreateUserAccountPage(page);
248277

249-
// Seeded by Flyway/SQL (precondition)
250-
const uname = "createtests";
278+
const uniqueValue = generateUniqueUsername("e2e-1.1.C-Server-Error");
251279

252-
if (await institutionSelectLocator.isEnabled()) {
253-
await selectVuetifyOptionByName(
254-
page,
255-
institutionSelectLocator,
256-
"SEB Server",
257-
);
258-
}
280+
const username = "createtests"; // SQL Script includes a user with this username!!!
281+
const name = uniqueValue + "Name";
282+
const surname = uniqueValue + "LastName";
283+
const email = uniqueValue + "@email.com";
284+
const password = "StrongPass123!";
259285

260-
await usernameLocator.fill(uname);
261-
await nameLocator.fill("John");
262-
await surnameLocator.fill("Doe");
263-
await emailLocator.fill(`${uname}@example.com`);
286+
const institutionToSelect = "SEB Server";
287+
const roleToSelect = "Server Administrator";
288+
const timeZoneToSelect = "Europe/Zurich";
264289

265-
await selectVuetifyFirstOption(page, timezoneSelectLocator);
290+
// fill selects
291+
await selectVuetifyOptionByName(
292+
page,
293+
institutionSelectLocator,
294+
institutionToSelect,
295+
);
296+
await selectVuetifyOptionByName(page, roleSelectLocator, roleToSelect);
297+
await selectVuetifyOptionByName(
298+
page,
299+
timezoneSelectLocator,
300+
timeZoneToSelect,
301+
);
266302

267-
await passwordLocator.fill("StrongPass123!");
268-
await confirmPasswordLocator.fill("StrongPass123!");
303+
//fill fields
304+
await usernameLocator.fill(username);
305+
await nameLocator.fill(name);
306+
await surnameLocator.fill(surname);
307+
await emailLocator.fill(email);
308+
await passwordLocator.fill(password);
309+
await confirmPasswordLocator.fill(password);
269310

270-
await selectVuetifyFirstOption(page, roleSelectLocator);
311+
const createRequestPromise = page.waitForRequest((req) => {
312+
return (
313+
req.method() === "POST" &&
314+
/\/useraccount(?:\?|$)/i.test(req.url())
315+
);
316+
});
317+
318+
const createResponsePromise = page.waitForResponse((resp) => {
319+
return (
320+
resp.request().method() === "POST" &&
321+
/\/useraccount(?:\?|$)/i.test(resp.url())
322+
);
323+
});
271324

272325
await saveButtonLocator.click();
273326

327+
const createRequest = await createRequestPromise;
328+
const createResponse = await createResponsePromise;
329+
330+
expect(createRequest.method()).toBe("POST");
331+
expect(createRequest.url()).toMatch(/\/useraccount(?:\?|$)/i);
332+
333+
expect(createResponse.status()).toBe(400);
334+
expect(createResponse.ok()).toBeFalsy();
335+
274336
await expect(page).toHaveURL(/\/user-accounts\/create(?:$|[?#])/i);
337+
338+
//check toast
339+
const toast = page.locator(".toast-item[role='alert']");
340+
341+
await expect(toast).toBeVisible();
342+
343+
await expect(toast.locator(".toast-text")).toContainText(
344+
"Field validation error",
345+
);
346+
await expect(toast.locator(".toast-text")).toContainText(
347+
"Username is already being used.",
348+
);
275349
});
276350
});

0 commit comments

Comments
 (0)