From b63b1f686e2279a0bd61ac289ce5a561b1eff311 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 7 Jul 2023 04:33:41 +0200 Subject: [PATCH] feat: Add command `enableUser` to enable or disable a user This command is from server. For testing it I adjusted the `listUsers` command to allow fetching user details. Signed-off-by: Ferdinand Thiessen --- cypress/e2e/users.cy.ts | 36 +++++++++++++++++++++++++++++ lib/commands/users.ts | 51 +++++++++++++++++++++++++++++++++++++---- lib/index.ts | 19 +++++++++++---- 3 files changed, 97 insertions(+), 9 deletions(-) diff --git a/cypress/e2e/users.cy.ts b/cypress/e2e/users.cy.ts index 055d3006..5544ce9a 100644 --- a/cypress/e2e/users.cy.ts +++ b/cypress/e2e/users.cy.ts @@ -98,3 +98,39 @@ describe('Write and read user metadata', () => { }) }) }) + +describe('Enable and disable users', () => { + const hash = 'user' + randHash() + let user = new User(hash, 'password') + + beforeEach(() => cy.createUser(user)) + afterEach(() => cy.deleteUser(user)) + + it('can disable user', () => { + cy.listUsers(true).then(details => { + const usersDetails = details.filter(v => v.id === user.userId) + expect(usersDetails.length).to.eq(1) + expect(usersDetails[0].enabled).to.eq('1') + }) + + cy.enableUser(user, false).listUsers(true).then(details => { + const usersDetails = details.filter(v => v.id === user.userId) + expect(usersDetails.length).to.eq(1) + expect(usersDetails[0].enabled).to.eq('') + }) + }) + + it('can enable a user', () => { + cy.enableUser(user, false).listUsers(true).then(details => { + const usersDetails = details.filter(v => v.id === user.userId) + expect(usersDetails.length).to.eq(1) + expect(usersDetails[0].enabled).to.eq('') + }) + + cy.enableUser(user).listUsers(true).then(details => { + const usersDetails = details.filter(v => v.id === user.userId) + expect(usersDetails.length).to.eq(1) + expect(usersDetails[0].enabled).to.eq('1') + }) + }) +}) diff --git a/lib/commands/users.ts b/lib/commands/users.ts index 486f4746..969c4fb6 100644 --- a/lib/commands/users.ts +++ b/lib/commands/users.ts @@ -2,6 +2,7 @@ * @copyright 2022 John Molakvoæ * * @author John Molakvoæ + * @author Ferdinand Thiessen * * @license AGPL-3.0-or-later * @@ -87,8 +88,9 @@ export const createUser = function(user: User): Cypress.Chainable { - const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users`.replace('index.php/', '') +export function listUsers(details?: b): Cypress.Chainable[] : string[]>; +export function listUsers(details = false): Cypress.Chainable[] | string[]> { + const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users${details ? '/details' : ''}`.replace('index.php/', '') cy.clearCookies() return cy.request({ @@ -104,9 +106,19 @@ export const listUsers = function(): Cypress.Chainable { }).then((response) => { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(response.body, "text/xml"); - const users = Array.from(xmlDoc.querySelectorAll('users element')).map(v => v.textContent) - return cy.wrap(users.filter(v => typeof v === 'string') as string[]) + if (!details) { + const users = Array.from(xmlDoc.querySelectorAll('users element')).map(v => v.textContent) + return users.filter(v => typeof v === 'string') as string[] + } else { + const list = Array.from(xmlDoc.querySelectorAll('users > *')).map(v => { + // We only handle simple text properties for the moment + const properties = [...v.childNodes].filter(c => c.childNodes.length <= 1) + + return Object.fromEntries(properties.map(p => [p.nodeName, p.textContent || ''])) + }) + return list as Record[] + } }) } @@ -174,6 +186,8 @@ export const modifyUser = function(user: User, key: string, value: any): Cypress /** * Query metadata for and in behalf of a given user + * + * @param user User to change */ export const getUserData = function(user: User): Cypress.Chainable> { const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users/${user.userId}`.replace('index.php/', '') @@ -193,4 +207,31 @@ export const getUserData = function(user: User): Cypress.Chainable> { + const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users/${user.userId}/${enable ? 'enable' : 'disable'}`.replace('index.php/', '') + + return cy.request({ + method: 'PUT', + url, + form: true, + auth: { + user: 'admin', + password: 'admin', + }, + headers: { + 'OCS-ApiRequest': 'true', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }).then((response) => { + cy.log(`Enabled user ${user}`, response.status) + return cy.wrap(response) + }) +} diff --git a/lib/index.ts b/lib/index.ts index 4fd90a4f..259719be 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -21,7 +21,7 @@ */ import { getNc } from "./commands" import { login, logout } from "./commands/sessions" -import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData } from "./commands/users" +import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users" import type { Selector } from "./selectors" declare global { @@ -69,9 +69,12 @@ declare global { * Query list of users on the Nextcloud instance * * **Warning**: Using this function will reset the previous session - * @returns list of user IDs + * + * @param details Set to true to fetch users with detailed information (default false) + * @return List of user IDs or list of Users (if details was set to true) */ - listUsers(): Cypress.Chainable + listUsers(details?: b): Cypress.Chainable[] : string[]> + listUsers(details?: boolean): Cypress.Chainable[] | string[]> /** * Modify an attribute of a given user on the Nextcloud instance @@ -82,9 +85,16 @@ declare global { */ modifyUser(user: User, key: string, value: any): Cypress.Chainable> + /** + * Enable or disable a given user + * + * @param user user whom to enable or disable + * @param enable True to enable, false to disable (default is enable) + */ + enableUser(user: User, enable?: boolean): Cypress.Chainable> /** * - * Query metadata for, and in behalf, of a given user + * Query metadata for, and in behalf, of a given user * * @param user User whom metadata to query */ @@ -109,6 +119,7 @@ export const addCommands = function() { Cypress.Commands.add('deleteUser', deleteUser) Cypress.Commands.add('listUsers', listUsers) Cypress.Commands.add('modifyUser', modifyUser) + Cypress.Commands.add('enableUser', enableUser) Cypress.Commands.add('getUserData', getUserData) }