Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/e2e/helpers/actorGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import path from "node:path";
// (covers pool.query, execute and transaction connections alike).
export const VERSIONED = [
"op_dates",
"op_form_responses",
"op_forms",
"op_position_trainings",
"op_position_type",
"op_qr_codes",
Expand Down
224 changes: 224 additions & 0 deletions client/e2e/tests/39-forms.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import { test, expect } from "@playwright/test";

import {
assignRole,
cleanupAllTestData,
closePool,
getPool,
insertVolunteer,
} from "../helpers/db";
import { buildSessionCookieHeader } from "../helpers/session";
import {
makeTestVolunteer,
signInAs,
ROLE_BEHAVIORAL_STANDARDS_ID,
} from "../fixtures/test-data";

// #844: yearly forms authored in the app. Draft → publish; volunteers answer
// while it's open; FormEditor edits and sees the answers (no Admin needed).
const ROLE_FORM_EDITOR_ID = 2000044;
const SLUG = "e2e-vis";
const editor = makeTestVolunteer({
shiftboardId: 9_000_520,
playaName: "E2E Form Editor",
worldName: "Form Editor",
email: "e2e-form-editor@test.local",
passcode: "5200",
});
const volunteer = makeTestVolunteer({
shiftboardId: 9_000_521,
playaName: "E2E Form Filler",
worldName: "Form Filler",
email: "e2e-form-filler@test.local",
passcode: "5210",
});
const as = (id: number) => ({ Cookie: buildSessionCookieHeader(id) });
const QUESTIONS = [
{
id: "plans",
label: "Plans",
options: ["Yes", "No"],
other: true,
required: true,
type: "choice",
},
{
id: "roles",
label: "Roles",
options: ["Gate", "Lab", ""],
type: "checkboxes",
},
{ id: "rank", label: "Rank", options: ["A", "B", "C"], type: "ranking" },
{ id: "note", label: "Join the list", type: "info" },
{ id: "else", label: "Anything else", type: "textarea" },
];
const cleanup = async () => {
const db = getPool();
await db.execute(
"DELETE r FROM op_form_responses r JOIN op_forms f USING (form_id) WHERE f.slug = ?",
[SLUG]
);
await db.execute("DELETE FROM op_forms WHERE slug = ?", [SLUG]);
};

test.describe("Yearly forms (#844)", () => {
test.beforeAll(async () => {
await cleanupAllTestData();
await cleanup();
for (const v of [editor, volunteer]) {
await insertVolunteer(v);
await assignRole(v.shiftboardId, ROLE_BEHAVIORAL_STANDARDS_ID);
}
await assignRole(editor.shiftboardId, ROLE_FORM_EDITOR_ID);
});

test.afterAll(async () => {
await cleanup();
await cleanupAllTestData();
await closePool();
});

test("draft → publish → answer → admin sees it", async ({ request }) => {
const create = (who: number, year: number) =>
request.post("/api/forms", {
data: JSON.stringify({ slug: SLUG, title: "E2E VIS", year }),
headers: as(who),
});
expect((await create(volunteer.shiftboardId, 2098)).status()).toBe(403);
const made = await create(editor.shiftboardId, 2098);
expect(made.status()).toBe(201);
const { formId } = await made.json();
expect((await create(editor.shiftboardId, 2098)).status()).toBe(409);

const patch = (body: object, who = editor.shiftboardId) =>
request.patch(`/api/forms/${formId}`, {
data: JSON.stringify(body),
headers: as(who),
});
expect(
(
await patch({
questions: [
{ id: "x", label: "One", options: ["a"], type: "choice" },
],
})
).status()
).toBe(400);
expect(
(await patch({ questions: QUESTIONS }, volunteer.shiftboardId)).status()
).toBe(403);
expect((await patch({ questions: QUESTIONS })).status()).toBe(200);

// a draft is hidden from volunteers, visible to the editor (the review)
const get = (who: number) =>
request.get(`/api/forms/${formId}`, { headers: as(who) });
expect((await get(volunteer.shiftboardId)).status()).toBe(404);
const draft = await (await get(editor.shiftboardId)).json();
expect(draft.form.questions[1].options).toEqual(["Gate", "Lab"]); // blank choice dropped
const open = async () =>
(
await (
await request.get("/api/forms", {
headers: as(volunteer.shiftboardId),
})
).json()
).find((f: { formId: number }) => f.formId === formId);
expect(await open()).toBeUndefined();

expect((await patch({ status: "published" })).status()).toBe(200);
expect((await open())?.submittedAt).toBeNull();

const put = (answers: object) =>
request.put(`/api/forms/${formId}`, {
data: JSON.stringify({ answers }),
headers: as(volunteer.shiftboardId),
});
expect((await put({ roles: ["Gate"] })).status()).toBe(400); // required
expect((await put({ plans: "Maybe" })).status()).toBe(400); // not a choice
const saved = await put({
else: " hi ",
plans: "Other: if I get a ticket",
rank: ["C", "A", "Nope", "B"],
roles: ["Gate", "Bogus"],
});
expect(saved.status()).toBe(200);
expect((await saved.json()).answers).toEqual({
else: "hi",
plans: "Other: if I get a ticket",
rank: ["C", "A", "B"],
roles: ["Gate"],
});
expect((await open())?.submittedAt).toBeTruthy();

const answers = (who: number) =>
request.get(`/api/forms/${formId}/responses`, { headers: as(who) });
expect((await answers(volunteer.shiftboardId)).status()).toBe(403);
const rows = await (await answers(editor.shiftboardId)).json();
expect(rows).toHaveLength(1);
expect(rows[0]).toMatchObject({
playaName: volunteer.playaName,
shiftboardId: volunteer.shiftboardId,
});

// closed: no more changes
expect((await patch({ closesAt: "2000-01-01 00:00" })).status()).toBe(200);
expect((await put({ plans: "Yes" })).status()).toBe(409);
expect(await open()).toBeUndefined();

// next year's draft starts from this year's questions
const next = await create(editor.shiftboardId, 2099);
const copy = await (
await request.get(`/api/forms/${(await next.json()).formId}`, {
headers: as(editor.shiftboardId),
})
).json();
expect(copy.form.status).toBe("draft");
expect(copy.form.questions.map((q: { id: string }) => q.id)).toEqual(
QUESTIONS.map((q) => q.id)
);
});

test("volunteer fills it in from the checklist", async ({
page,
request,
}) => {
const made = await request.post("/api/forms", {
data: JSON.stringify({
slug: SLUG,
title: "E2E Checklist Form",
year: 2097,
}),
headers: as(editor.shiftboardId),
});
const { formId } = await made.json();
await request.patch(`/api/forms/${formId}`, {
data: JSON.stringify({ questions: QUESTIONS, status: "published" }),
headers: as(editor.shiftboardId),
});

await signInAs(page, volunteer);
await page
.getByRole("link", { name: "Fill in E2E Checklist Form" })
.click();
await page.waitForURL(`**/forms/${formId}`);
await page.getByRole("radio", { name: "Other:" }).check();
await page.getByRole("textbox", { name: "Plans: other" }).fill("depends");
await page.getByRole("checkbox", { name: "Lab" }).check();
await page.getByRole("button", { name: "Move B up" }).click();
await page.getByRole("button", { name: "Submit" }).click();
await expect(
page.getByText("Thanks, your answers are saved.")
).toBeVisible();

const [rows] = await getPool().query(
"SELECT answers FROM op_form_responses WHERE form_id = ? AND shiftboard_id = ?",
[formId, volunteer.shiftboardId]
);
const saved = (rows as { answers: string }[])[0].answers;
expect(JSON.parse(saved)).toEqual({
plans: "Other: depends",
rank: ["B", "A", "C"],
roles: ["Lab"],
});
});
});
Loading
Loading