Skip to content

Commit

Permalink
feat(move): Allow moving files only shared by link into shared folder
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Dec 7, 2023
1 parent 7119eca commit a7b8831
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/drive/web/modules/move/MoveModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
buildMoveOrImportQuery,
buildOnlyFolderQuery
} from 'drive/web/modules/queries'
import { cancelMove } from 'drive/web/modules/move/helpers'
import {
cancelMove,
hasOneOfEntriesShared
} from 'drive/web/modules/move/helpers'
import { MoveOutsideSharedFolderModal } from 'drive/web/modules/move/MoveOutsideSharedFolderModal'
import { MoveSharedFolderInsideAnotherModal } from 'drive/web/modules/move/MoveSharedFolderInsideAnotherModal'

Expand Down Expand Up @@ -110,9 +113,10 @@ const MoveModal = ({ onClose, entries, classes }) => {
setMovingOutsideSharedFolder(true)
}
} else {
const hasOneOrMoreEntriesShared =
entries.filter(({ _id }) => byDocId[_id] !== undefined).length > 0
if (hasOneOrMoreEntriesShared && hasSharedParent(targetPath)) {
if (
hasOneOfEntriesShared(entries, byDocId) &&
hasSharedParent(targetPath)
) {
setMovingSharedFolderInsideAnother(true)
} else {
moveEntries()
Expand Down
41 changes: 38 additions & 3 deletions src/drive/web/modules/move/MoveModal.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ describe('MoveModal component', () => {
})

describe('move shared folder inside another', () => {
it('should move the shared folder inside another if its by link', async () => {
CozyFile.getFullpath.mockImplementation((destinationFolder, name) =>
Promise.resolve(`/${destinationFolder}/${name}`)
)

setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {
permissions: ['perm1'],
sharings: []
}
},
getSharedParentPath: () => null
})

const moveButton = await screen.findByText('Move')
fireEvent.click(moveButton)

await waitFor(() => {
expect(CozyFile.move).toHaveBeenCalled()
expect(onCloseSpy).toHaveBeenCalled()
expect(refreshSpy).toHaveBeenCalled()
})
})

it('should display an alert when move shared folder inside another', async () => {
CozyFile.getFullpath.mockImplementation((destinationFolder, name) =>
Promise.resolve(`/${destinationFolder}/${name}`)
Expand All @@ -237,7 +263,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null
})
Expand All @@ -260,7 +289,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null,
sharingContext: {
Expand Down Expand Up @@ -297,7 +329,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null,
sharingContext: {
Expand Down
21 changes: 21 additions & 0 deletions src/drive/web/modules/move/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,24 @@ export const getEntriesType = entries => {
return current.type
}, null)
}

/**
* Returns whether one of the entries that is shared not only by link
* @param {object[]} entries
* @param {object} byDocId
* @returns
*/
export const hasOneOfEntriesShared = (entries, byDocId) => {
const sharedEntries = entries.filter(({ _id }) => {
const doc = byDocId[_id]
if (doc === undefined) return false

const onlySharedByLink =
doc.permissions.length > 0 && doc.sharings.length === 0

if (onlySharedByLink) return false

return true
})
return sharedEntries.length > 0
}
31 changes: 30 additions & 1 deletion src/drive/web/modules/move/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import CozyClient from 'cozy-client'

import { cancelMove, getEntriesType } from 'drive/web/modules/move/helpers'
import {
cancelMove,
getEntriesType,
hasOneOfEntriesShared
} from 'drive/web/modules/move/helpers'
import { CozyFile } from 'models'

jest.mock('cozy-doctypes')
Expand Down Expand Up @@ -119,3 +123,28 @@ describe('getEntriesType', () => {
expect(res).toBe('element')
})
})

describe('hasOneOfEntriesShared', () => {
it('should return false if entries are not shared', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(false)
})

it('should return false if all entries are only shared by link', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {
1: { permissions: ['permission1'], sharings: [] },
2: { permissions: ['permission2'], sharings: [] }
}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(false)
})

it('should return true if at least one entry is shared', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {
2: { permissions: [], sharings: ['sharingId'] }
}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(true)
})
})

0 comments on commit a7b8831

Please sign in to comment.