Skip to content

Commit

Permalink
feat: User.createRandom(), use User in docker addUser
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 f60d530 commit 02780b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lib/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ export class User {
this.password = password
this.language = language
}

static createRandom() {
const uid = (Math.random() + 1).toString(36).substring(7)
return new User(uid)
}
}

9 changes: 6 additions & 3 deletions lib/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { basename, join, resolve, sep } from 'path'
import { existsSync, readFileSync } from 'fs'
import { XMLParser } from 'fast-xml-parser'

import { User } from './User'

const SERVER_IMAGE = 'ghcr.io/nextcloud/continuous-integration-shallow-server'
const VENDOR_APPS = {
text: 'https://github.com/nextcloud/text.git',
Expand Down Expand Up @@ -286,6 +288,7 @@ export const configureNextcloud = async function(apps = ['viewer'], vendoredBran
export const setupUsers = async function(container?: Container) {
console.log('\nCreating test users… 👤')
const users = ['test1', 'test2', 'test3', 'test4', 'test5']
.map(uid => new User(uid))
for (const user of users) {
await addUser(user, { container, verbose: true })
}
Expand Down Expand Up @@ -462,12 +465,12 @@ export const getSystemConfig = function(
* Add a user to the Nextcloud in the container.
*/
export const addUser = function(
user: string,
user: User,
{ container, env=[], verbose=false }: Partial<Omit<RunExecOptions, 'user'>> = {},
) {
return runOcc(
['user:add', user, '--password-from-env'],
{ container, verbose, env: ['OC_PASS=' + user, ...env] }
['user:add', user.userId, '--password-from-env'],
{ container, verbose, env: ['OC_PASS=' + user.password, ...env] }
)
}

Expand Down
4 changes: 2 additions & 2 deletions playwright/support/fixtures/random-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const test = base.extend({
baseURL,
})

const uid = await createRandomUser()
await login(page.request, uid, uid)
const user = await createRandomUser()
await login(page.request, user)

await use(page)
await page.close()
Expand Down
21 changes: 10 additions & 11 deletions playwright/support/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

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

/**
Expand All @@ -21,17 +21,16 @@ export function restoreDatabase() {
*/
export async function login(
request: APIRequestContext,
user: string,
password: string,
user: User,
) {
const tokenResponse = await request.get('./csrftoken')
expect(tokenResponse.status()).toBe(200)
const requesttoken = (await tokenResponse.json()).token

const loginResponse = await request.post('./login', {
form: {
user,
password,
user: user.userId,
password: user.password,
requesttoken,
},
headers: {
Expand All @@ -45,11 +44,11 @@ export async function login(
}

/**
* Create a new random user (password is set to the UID)
* @return The UID of the new user
* Create a new random user
* @return The new user
*/
export async function createRandomUser(): Promise<string> {
const uid = (Math.random() + 1).toString(36).substring(7)
await addUser(uid)
return uid
export async function createRandomUser(): Promise<User> {
const user = User.createRandom()
await addUser(user)
return user
}

0 comments on commit 02780b6

Please sign in to comment.