Skip to content

Commit

Permalink
feat: Add command enableUser to enable or disable a user
Browse files Browse the repository at this point in the history
This command is from server. For testing it I adjusted the `listUsers` command
to allow fetching user details.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jul 7, 2023
1 parent 99151f1 commit b63b1f6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
36 changes: 36 additions & 0 deletions cypress/e2e/users.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
51 changes: 46 additions & 5 deletions lib/commands/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @copyright 2022 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @license AGPL-3.0-or-later
*
Expand Down Expand Up @@ -87,8 +88,9 @@ export const createUser = function(user: User): Cypress.Chainable<Cypress.Respon
* **Warning**: Using this function will reset the previous session
* @returns list of user IDs
*/
export const listUsers = function(): Cypress.Chainable<string[]> {
const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users`.replace('index.php/', '')
export function listUsers<b extends boolean>(details?: b): Cypress.Chainable<b extends true ? Record<string, string>[] : string[]>;
export function listUsers(details = false): Cypress.Chainable<Record<string, string>[] | string[]> {
const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users${details ? '/details' : ''}`.replace('index.php/', '')

cy.clearCookies()
return cy.request({
Expand All @@ -104,9 +106,19 @@ export const listUsers = function(): Cypress.Chainable<string[]> {
}).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[]

Check warning on line 112 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L111-L112

Added lines #L111 - L112 were not covered by tests
} else {
const list = Array.from(xmlDoc.querySelectorAll('users > *')).map(v => {

Check warning on line 114 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L114

Added line #L114 was not covered by tests
// We only handle simple text properties for the moment
const properties = [...v.childNodes].filter(c => c.childNodes.length <= 1)

Check warning on line 116 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L116

Added line #L116 was not covered by tests

return Object.fromEntries(properties.map(p => [p.nodeName, p.textContent || '']))
})
return list as Record<string, string>[]

Check warning on line 120 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L120

Added line #L120 was not covered by tests
}
})
}

Expand Down Expand Up @@ -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<Cypress.Response<any>> {
const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users/${user.userId}`.replace('index.php/', '')
Expand All @@ -193,4 +207,31 @@ export const getUserData = function(user: User): Cypress.Chainable<Cypress.Respo

return cy.wrap(response)
})
}
}

/**
* Enable or disable a user
*
* @param {User} user the user to dis- / enable
* @param {boolean} enable True if the user should be enable, false to disable
*/
export const enableUser = function(user: User, enable = true): Cypress.Chainable<Cypress.Response<any>> {
const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users/${user.userId}/${enable ? 'enable' : 'disable'}`.replace('index.php/', '')

return cy.request({

Check warning on line 221 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L221

Added line #L221 was not covered by tests
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)

Check warning on line 235 in lib/commands/users.ts

View check run for this annotation

Codecov / codecov/patch

lib/commands/users.ts#L234-L235

Added lines #L234 - L235 were not covered by tests
})
}
19 changes: 15 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<string[]>
listUsers<b extends boolean>(details?: b): Cypress.Chainable<b extends true ? Record<string, string>[] : string[]>
listUsers(details?: boolean): Cypress.Chainable<Record<string,string>[] | string[]>

/**
* Modify an attribute of a given user on the Nextcloud instance
Expand All @@ -82,9 +85,16 @@ declare global {
*/
modifyUser(user: User, key: string, value: any): Cypress.Chainable<Cypress.Response<any>>

/**
* 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<Cypress.Response<any>>
/**
*
* 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
*/
Expand All @@ -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)
}

Expand Down

0 comments on commit b63b1f6

Please sign in to comment.