Skip to content

Commit

Permalink
feat(playwright): createRandomUser() and login()
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Feb 11, 2025
1 parent ecb169e commit 19e6621
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
39 changes: 17 additions & 22 deletions playwright/support/utils/session.ts → lib/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { runExec, addUser, User } from '../../../dist'
import { expect, type APIRequestContext } from '@playwright/test'
import { User } from './User'
import { addUser } from './docker'
import type { APIRequestContext } from 'playwright'

/**
* Restore database and data folder for tests
* Create a new random user
* @return The new user
*/
export function restoreDatabase() {
runExec('rm -rf data && tar -xf backup.tar')
export async function createRandomUser(): Promise<User> {
const user = User.createRandom()
await addUser(user)
return user
}

/**
* Helper to login on the Nextcloud instance
* @param request API request object
* @param user The username to login
* @param password The password to login
* @param user The user to login
*/
export async function login(
request: APIRequestContext,
user: User,
) {
const tokenResponse = await request.get('./csrftoken')
expect(tokenResponse.status()).toBe(200)
const tokenResponse = await request.get('./csrftoken', {
failOnStatusCode: true,
})
const requesttoken = (await tokenResponse.json()).token

const loginResponse = await request.post('./login', {
Expand All @@ -36,19 +40,10 @@ export async function login(
headers: {
Origin: tokenResponse.url().replace(/index.php.*/, ''),
},
failOnStatusCode: true,
})
expect(loginResponse.status()).toBe(200)

const response = await request.get('apps/files')
expect(response.status()).toBe(200)
}

/**
* Create a new random user
* @return The new user
*/
export async function createRandomUser(): Promise<User> {
const user = User.createRandom()
await addUser(user)
return user
const response = await request.get('apps/files', {
failOnStatusCode: true,
})
}
2 changes: 1 addition & 1 deletion playwright/support/fixtures/random-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { test as base } from '@playwright/test'
import { createRandomUser, login } from '../utils/session'
import { createRandomUser, login } from '../../../dist/playwright'

interface RandomUserFixture {
user: User
Expand Down
10 changes: 10 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ export default [
file: 'dist/cypress.js',
format: 'cjs',
}),

// Playwright commands and utils
config('./lib/playwright.ts', {
file: 'dist/playwright.mjs',
format: 'esm',
}),
config('./lib/playwright.ts', {
file: 'dist/playwright.js',
format: 'cjs',
}),
]

0 comments on commit 19e6621

Please sign in to comment.