|
| 1 | +import type { Page, TestInfo } from "@playwright/test"; |
| 2 | + |
| 3 | +export const implementedFaults = [ |
| 4 | + "api-team-500", |
| 5 | + "script-404", |
| 6 | + "unexpected-dashboard-redirect", |
| 7 | +] as const; |
| 8 | + |
| 9 | +export type FaultName = (typeof implementedFaults)[number]; |
| 10 | + |
| 11 | +const faultTargets: Record<FaultName, string[]> = { |
| 12 | + "api-team-500": [ |
| 13 | + "should change user name in general settings and verify it appears in team members list", |
| 14 | + ], |
| 15 | + "script-404": [ |
| 16 | + "should successfully change password and sign in with new credentials", |
| 17 | + ], |
| 18 | + "unexpected-dashboard-redirect": [ |
| 19 | + "should navigate to dashboard activity section and verify user activities", |
| 20 | + ], |
| 21 | +}; |
| 22 | + |
| 23 | +export async function installFaultsForTest(page: Page, testInfo: TestInfo) { |
| 24 | + const faults = process.env.FAULTS?.trim(); |
| 25 | + |
| 26 | + for (const faultName of implementedFaults) { |
| 27 | + if (faults !== "all" && faults !== faultName) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + |
| 31 | + if (!faultTargets[faultName].includes(testInfo.title)) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + switch (faultName) { |
| 36 | + case "api-team-500": |
| 37 | + await installApiTeam500(page); |
| 38 | + break; |
| 39 | + case "script-404": |
| 40 | + await installScript404(page); |
| 41 | + break; |
| 42 | + case "unexpected-dashboard-redirect": |
| 43 | + await installUnexpectedDashboardRedirect(page); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +async function installApiTeam500(page: Page) { |
| 50 | + const faultName = "api-team-500"; |
| 51 | + |
| 52 | + await page.route("**/api/team", async (route) => { |
| 53 | + await route.fulfill({ |
| 54 | + status: 500, |
| 55 | + contentType: "application/json", |
| 56 | + headers: { |
| 57 | + "x-fault-injected": faultName, |
| 58 | + }, |
| 59 | + body: JSON.stringify({ error: `Fault injected: ${faultName}` }), |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +async function installScript404(page: Page) { |
| 65 | + const faultName = "script-404"; |
| 66 | + |
| 67 | + await page.route("**/*", async (route) => { |
| 68 | + if (route.request().resourceType() === "script") { |
| 69 | + await route.fulfill({ |
| 70 | + status: 404, |
| 71 | + contentType: "application/javascript", |
| 72 | + headers: { |
| 73 | + "x-fault-injected": faultName, |
| 74 | + }, |
| 75 | + body: `throw new Error("Fault injected: ${faultName}");`, |
| 76 | + }); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + await route.continue(); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +async function installUnexpectedDashboardRedirect(page: Page) { |
| 85 | + const faultName = "unexpected-dashboard-redirect"; |
| 86 | + |
| 87 | + await page.route("**/dashboard", async (route) => { |
| 88 | + if (route.request().resourceType() === "document") { |
| 89 | + await route.fulfill({ |
| 90 | + status: 302, |
| 91 | + headers: { |
| 92 | + location: "/sign-in", |
| 93 | + "x-fault-injected": faultName, |
| 94 | + }, |
| 95 | + body: "", |
| 96 | + }); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + await route.continue(); |
| 101 | + }); |
| 102 | +} |
0 commit comments