Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: combine login + data creation in one step #1003

Closed
wants to merge 2 commits into from
Closed
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
97 changes: 52 additions & 45 deletions frontend/e2e/globalSetup/login-and-create-sample-data.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,56 @@ import { test as setup } from "@e2e/utils/test-with-auth"
import fs from "fs"
import path from "node:path"

setup("login", async ({ page }) => {
await page.goto("/")
await page.waitForURL(/localhost:8443/)

await page
.getByRole("textbox", { name: "Username or email" })
.fill("jane.doe")

await page.getByRole("textbox", { name: "Password" }).fill("test")

await page.getByRole("button", { name: "Sign In" }).click()

await page.context().storageState({ path: `e2e/storage/state.json` })

await page.waitForURL("/")
await page.unrouteAll({ behavior: "wait" })
})

setup("create sample data", async ({ authenticatedRequest: request }) => {
const files = [
"bgbl-1_1001_2_mods_01/aenderungsgesetz.xml",
"bgbl-1_1002_2_mods-subsitution_01/aenderungsgesetz.xml",
"bgbl-1_2017_s419/aenderungsgesetz.xml",
"bgbl-1_2023_413/aenderungsgesetz.xml",
]

for (const file of files) {
const filePath = path.join(samplesDirectory, file)
const fileContent = fs.readFileSync(filePath) // Read the file content

const formData = new FormData()
formData.append("file", new Blob([fileContent], { type: "text/xml" }), file)
formData.append("force", String(true))

const response = await request.post(
`${process.env.E2E_BASE_URL}/api/v1/announcements`,
{ multipart: formData },
)

if (!response.ok()) {
throw new Error(`Failed to set up test data: ${response.statusText()}`)
setup(
"login and create sample data",
async ({ page, authenticatedRequest: request }) => {
// Login
await page.goto("/")
await page.waitForURL(/localhost:8443/)

await page
.getByRole("textbox", { name: "Username or email" })
.fill("jane.doe")

await page.getByRole("textbox", { name: "Password" }).fill("test")

await page.getByRole("button", { name: "Sign In" }).click()

await page.context().storageState({ path: `e2e/storage/state.json` })

await page.waitForURL("/")
await page.unrouteAll({ behavior: "wait" })

// Create sample data
const files = [
"bgbl-1_1001_2_mods_01/aenderungsgesetz.xml",
"bgbl-1_1002_2_mods-subsitution_01/aenderungsgesetz.xml",
"bgbl-1_2017_s419/aenderungsgesetz.xml",
"bgbl-1_2023_413/aenderungsgesetz.xml",
]

for (const file of files) {
const filePath = path.join(samplesDirectory, file)
const fileContent = fs.readFileSync(filePath) // Read the file content

const formData = new FormData()
formData.append(
"file",
new Blob([fileContent], { type: "text/xml" }),
file,
)
formData.append("force", String(true))

const response = await request.post(
`${process.env.E2E_BASE_URL}/api/v1/announcements`,
{ multipart: formData },
)

if (!response.ok()) {
throw new Error(`Failed to set up test data: ${response.statusText()}`)
}

console.log(`Imported ${file} successfully.`)
}

console.log(`Imported ${file} successfully.`)
}
})
},
)
14 changes: 7 additions & 7 deletions frontend/e2e/utils/test-with-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
APIRequestContext,
test as base /* eslint-disable-line no-restricted-imports -- We need this here to extend it */,
} from "@playwright/test"
import { readFile, writeFile } from "node:fs/promises"
import { readFileSync, writeFileSync } from "node:fs"
import { fileURLToPath, URL } from "node:url"

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ export const test = base.extend<{
async ({ page }, use) => {
await page.route(/token$/, async (route) => {
const response = await page.request.fetch(route.request())
if (response.ok()) await saveToken(await response.json())
if (response.ok()) saveToken(await response.json())
await route.fulfill({ response })
})

Expand All @@ -51,7 +51,7 @@ export const test = base.extend<{
],

authenticatedRequest: async ({ playwright }, use) => {
const token = await restoreToken()
const token = restoreToken()

const request = await playwright.request.newContext({
extraHTTPHeaders: {
Expand All @@ -72,8 +72,8 @@ const storagePath = fileURLToPath(
*
* @param token Token to save
*/
export function saveToken(token: Token): Promise<void> {
return writeFile(storagePath, JSON.stringify(token, undefined, 2), {
function saveToken(token: Token): void {
writeFileSync(storagePath, JSON.stringify(token, undefined, 2), {
encoding: "utf-8",
})
}
Expand All @@ -84,7 +84,7 @@ export function saveToken(token: Token): Promise<void> {
*
* @returns Saved token
*/
export async function restoreToken(): Promise<Token> {
const raw = await readFile(storagePath, { encoding: "utf-8" })
function restoreToken(): Token {
const raw = readFileSync(storagePath, { encoding: "utf-8" })
return JSON.parse(raw)
}
Loading