Only show the Rename option for a project owned by the current user (#15346)

GitOrigin-RevId: 439ba2c69aa1e3d9cd326f2b501467f6d9cca798
This commit is contained in:
Alf Eaton 2023-10-24 13:11:32 +01:00 committed by Copybot
parent 74c6085c55
commit c2c86ff794
3 changed files with 98 additions and 4 deletions

View file

@ -31,13 +31,21 @@ import { GenericErrorBoundaryFallback } from '../../../shared/components/generic
function ProjectListRoot() { function ProjectListRoot() {
const { isReady } = useWaitForI18n() const { isReady } = useWaitForI18n()
return isReady ? ( if (!isReady) {
return null
}
return <ProjectListRootInner />
}
export function ProjectListRootInner() {
return (
<ProjectListProvider> <ProjectListProvider>
<ColorPickerProvider> <ColorPickerProvider>
<ProjectListPageContent /> <ProjectListPageContent />
</ColorPickerProvider> </ColorPickerProvider>
</ProjectListProvider> </ProjectListProvider>
) : null )
} }
function ProjectListPageContent() { function ProjectListPageContent() {

View file

@ -21,7 +21,16 @@ function RenameProjectMenuItem() {
} }
}, [isMounted]) }, [isMounted])
if (selectedProjects.length !== 1) return null if (selectedProjects.length !== 1) {
return null
}
const [selectedProject] = selectedProjects
// only show Rename if the current user is the project owner
if (selectedProject.accessLevel !== 'owner') {
return null
}
return ( return (
<> <>
@ -29,7 +38,7 @@ function RenameProjectMenuItem() {
<RenameProjectModal <RenameProjectModal
handleCloseModal={handleCloseModal} handleCloseModal={handleCloseModal}
showModal={showModal} showModal={showModal}
project={selectedProjects[0]} project={selectedProject}
/> />
</> </>
) )

View file

@ -0,0 +1,77 @@
import { render, screen, within } from '@testing-library/react'
import { expect } from 'chai'
import moment from 'moment/moment'
import fetchMock from 'fetch-mock'
import { Project } from '../../../../../../../types/project/dashboard/api'
import { ProjectListRootInner } from '@/features/project-list/components/project-list-root'
const users = {
picard: {
id: '62d6d0b4c5c5030a4d696c7a',
email: 'picard@overleaf.com',
firstName: 'Jean-Luc',
lastName: 'Picard',
},
riker: {
id: '624333f147cfd8002622a1d3',
email: 'riker@overleaf.com',
firstName: 'William',
lastName: 'Riker',
},
}
const projects: Project[] = [
{
id: '62f17f594641b405ca2b3264',
name: 'Starfleet Report (owner)',
lastUpdated: moment().subtract(1, 'day').toISOString(),
lastUpdatedBy: users.riker,
accessLevel: 'owner',
source: 'owner',
archived: false,
trashed: false,
owner: users.picard,
},
{
id: '62f17f594641b405ca2b3265',
name: 'Starfleet Report (readAndWrite)',
lastUpdated: moment().subtract(1, 'day').toISOString(),
lastUpdatedBy: users.picard,
accessLevel: 'readAndWrite',
source: 'owner',
archived: false,
trashed: false,
owner: users.riker,
},
]
describe('<ProjectTools />', function () {
beforeEach(function () {
window.metaAttributesCache.set('ol-ExposedSettings', {})
window.metaAttributesCache.set('ol-prefetchedProjectsBlob', {
projects,
totalSize: 100,
})
fetchMock.get('/system/messages', [])
})
afterEach(function () {
window.metaAttributesCache.clear()
fetchMock.reset()
})
it('does not show the Rename option for a project owned by a different user', function () {
render(<ProjectListRootInner />)
screen.getByLabelText('Select Starfleet Report (readAndWrite)').click()
screen.getByRole('button', { name: 'More' }).click()
expect(screen.queryByRole('menuitem', { name: 'Rename' })).to.be.null
})
it('displays the Rename option for a project owned by the current user', function () {
render(<ProjectListRootInner />)
screen.getByLabelText('Select Starfleet Report (owner)').click()
screen.getByRole('button', { name: 'More' }).click()
screen.getByRole('menuitem', { name: 'Rename' }).click()
within(screen.getByRole('dialog')).getByText('Rename Project')
})
})