Ghost/apps/admin-x-settings/test/acceptance/general/users/roles.test.ts
Jono M fb7bf6d01e
Improved AdminX test coverage based on old admin tests (#18502)
refs https://github.com/TryGhost/Product/issues/3832

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 7eda74c</samp>

This pull request improves the validation, customization, and feedback
of various form components and modals in the admin-x-settings app. It
also adds new components for user detail modal sections and modifies the
user type to allow null values for social accounts. Additionally, it
adds `dirty` props to some integration modals and a `data-testid`
attribute to the exit settings button. It also deletes an unused file.
2023-10-06 10:06:05 +01:00

152 lines
5.8 KiB
TypeScript

import {expect, test} from '@playwright/test';
import {globalDataRequests, meWithRole, mockApi, responseFixtures} from '../../../utils/acceptance';
test.describe('User roles', async () => {
test('Shows users under their role', async ({page}) => {
await mockApi({page, requests: {
...globalDataRequests,
browseUsers: {method: 'GET', path: '/users/?limit=100&include=roles', response: responseFixtures.users}
}});
await page.goto('/');
const section = page.getByTestId('users');
await expect(section.getByTestId('owner-user')).toHaveText(/owner@test\.com/);
await expect(section.getByRole('tab')).toHaveText([
'Administrators',
'Editors',
'Authors',
'Contributors',
'Invited'
]);
const activeTab = section.locator('[role=tabpanel]:not(.hidden)');
await section.getByRole('tab', {name: 'Administrators'}).click();
await expect(activeTab.getByTestId('user-list-item')).toHaveText(/administrator@test\.com/);
await section.getByRole('tab', {name: 'Editors'}).click();
await expect(activeTab.getByTestId('user-list-item')).toHaveText(/editor@test\.com/);
await section.getByRole('tab', {name: 'Authors'}).click();
await expect(activeTab.getByTestId('user-list-item')).toHaveText(/author@test\.com/);
await section.getByRole('tab', {name: 'Contributors'}).click();
await expect(activeTab.getByTestId('user-list-item')).toHaveText(/contributor@test\.com/);
});
test('Supports changing user role', async ({page}) => {
const userToEdit = responseFixtures.users.users.find(user => user.email === 'author@test.com')!;
const {lastApiRequests} = await mockApi({page, requests: {
...globalDataRequests,
browseUsers: {method: 'GET', path: '/users/?limit=100&include=roles', response: responseFixtures.users},
browseRoles: {method: 'GET', path: '/roles/?limit=all', response: responseFixtures.roles},
browseAssignableRoles: {method: 'GET', path: '/roles/?limit=all&permissions=assign', response: responseFixtures.roles},
editUser: {method: 'PUT', path: `/users/${userToEdit.id}/?include=roles`, response: {
users: [{
...userToEdit,
roles: [responseFixtures.roles.roles.find(role => role.name === 'Editor')!]
}]
}}
}});
await page.goto('/');
const section = page.getByTestId('users');
const activeTab = section.locator('[role=tabpanel]:not(.hidden)');
await section.getByRole('tab', {name: 'Authors'}).click();
const listItem = activeTab.getByTestId('user-list-item').last();
await listItem.hover();
await listItem.getByRole('button', {name: 'Edit'}).click();
const modal = page.getByTestId('user-detail-modal');
await modal.locator('input[value=editor]').check();
await modal.getByRole('button', {name: 'Save & close'}).click();
await expect(modal.getByRole('button', {name: 'Saved'})).toBeVisible();
await expect(activeTab).toHaveText(/No authors found./);
await section.getByRole('tab', {name: 'Editors'}).click();
await expect(activeTab.getByTestId('user-list-item')).toHaveCount(2);
await expect(activeTab.getByTestId('user-list-item')).toHaveText([
/author@test\.com/,
/editor@test\.com/
]);
expect(lastApiRequests.editUser?.body).toMatchObject({
users: [{
email: 'author@test.com',
roles: [{
name: 'Editor'
}]
}]
});
});
test('Editors can only manage lower roles', async ({page}) => {
const userToEdit = responseFixtures.users.users.find(user => user.email === 'contributor@test.com')!;
await mockApi({page, requests: {
...globalDataRequests,
browseMe: {...globalDataRequests.browseMe, response: meWithRole('Editor')},
browseUsers: {method: 'GET', path: '/users/?limit=100&include=roles', response: responseFixtures.users},
editUser: {method: 'PUT', path: `/users/${userToEdit.id}/?include=roles`, response: {
users: [{
...userToEdit,
name: 'New name'
}]
}}
}});
await page.goto('/');
const section = page.getByTestId('users');
const activeTab = section.locator('[role=tabpanel]:not(.hidden)');
const modal = page.getByTestId('user-detail-modal');
// Cannot edit owner, administrators, other editors or authors
await section.getByTestId('owner-user').hover();
await expect(section.getByTestId('owner-user').getByRole('button')).toBeHidden();
await section.getByRole('tab', {name: 'Administrators'}).click();
await activeTab.getByTestId('user-list-item').last().click();
await expect(modal).toBeHidden();
await section.getByRole('tab', {name: 'Editors'}).click();
await activeTab.getByTestId('user-list-item').last().click();
await expect(modal).toBeHidden();
await section.getByRole('tab', {name: 'Authors'}).click();
await activeTab.getByTestId('user-list-item').last().click();
await expect(modal).toBeHidden();
// Can edit contributors
await section.getByRole('tab', {name: 'Contributors'}).click();
await activeTab.getByTestId('user-list-item').last().click();
await expect(modal).toBeVisible();
await expect(modal).not.toContainText('Role');
await modal.getByLabel('Full name').fill('New name');
await modal.getByRole('button', {name: 'Save'}).click();
await expect(modal).toBeHidden();
});
});