Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[full-ci] Fix public link loading on role change #8006

Merged
merged 7 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/bugfix-public-link-load-on-role-change
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Public link loading on role change

The loading and resolving of public links when their respective roles have changed has been fixed.

https://github.com/owncloud/web/pull/8006
https://github.com/owncloud/web/issues/8003
44 changes: 41 additions & 3 deletions packages/web-app-files/src/views/spaces/DriveResolver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ import DriveRedirect from './DriveRedirect.vue'
import GenericSpace from './GenericSpace.vue'
import GenericTrash from './GenericTrash.vue'

import { defineComponent } from '@vue/composition-api'
import { useDriveResolver, useRouteParam, useStore } from 'web-pkg/src/composables'
import { defineComponent, onMounted, unref } from '@vue/composition-api'
import {
useClientService,
useDriveResolver,
useRouteParam,
useRouter,
useStore
} from 'web-pkg/src/composables'
import { useActiveLocation } from '../../composables'
import { isLocationSpacesActive, isLocationTrashActive } from '../../router'
import { isPublicSpaceResource, PublicSpaceResource, SpaceResource } from 'web-client/src/helpers'
import { locationPublicUpload } from 'web-app-files/src/router/public'
import { linkRoleUploaderFolder } from 'web-client/src/helpers/share'

export default defineComponent({
components: {
Expand All @@ -28,11 +37,40 @@ export default defineComponent({
},
setup() {
const store = useStore()
const clientService = useClientService()
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
const router = useRouter()
const driveAliasAndItem = useRouteParam('driveAliasAndItem')
const isSpaceRoute = useActiveLocation(isLocationSpacesActive, 'files-spaces-generic')
const isTrashRoute = useActiveLocation(isLocationTrashActive, 'files-trash-generic')
const resolvedDrive = useDriveResolver({ store, driveAliasAndItem })

const getSpaceResource = async (): Promise<SpaceResource> => {
const space = unref(resolvedDrive.space)
try {
const publicSpace = (await clientService.webdav.getFileInfo(space)) as SpaceResource
return publicSpace
} catch (e) {
console.error(e)
return space
}
}

onMounted(async () => {
const space = unref(resolvedDrive.space)
if (space && isPublicSpaceResource(space)) {
let publicSpace = (await getSpaceResource()) as PublicSpaceResource
JammingBen marked this conversation as resolved.
Show resolved Hide resolved

if (linkRoleUploaderFolder.bitmask(false) === publicSpace.publicLinkPermission) {
router.push({
name: locationPublicUpload.name,
params: { token: space.id.toString() }
})
}
}
})

return {
...useDriveResolver({ store, driveAliasAndItem }),
...resolvedDrive,
driveAliasAndItem,
isSpaceRoute,
isTrashRoute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ const localVue = createLocalVue()
localVue.use(VueCompositionAPI)
localVue.use(Vuex)

export const createComposableWrapper = (setup: SetupFunction<Data, Data>): Wrapper<Vue> =>
export const createComposableWrapper = (
setup: SetupFunction<Data, Data>,
options = { mocks: undefined, store: undefined }
): Wrapper<Vue> =>
mount(
defineComponent({
setup,
template: `<div></div>`
}),
{
localVue
localVue,
...(options.mocks && { mocks: options.mocks }),
...(options.store && { store: options.store })
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { useCapabilitySpacesEnabled, useDriveResolver } from 'web-pkg/src'
import { createComposableWrapper } from '../composables.setup'
import { defaultComponentMocks } from 'web-test-helpers/src/mocks/defaultComponentMocks'
import { computed, ref, unref } from '@vue/composition-api'
import { mockDeep } from 'jest-mock-extended'
import { defaultStoreMockOptions } from 'web-test-helpers/src/mocks/store/defaultStoreMockOptions'
import { isShareSpaceResource, SpaceResource } from 'web-client/src/helpers'
import { createStore } from 'vuex-extensions'
import Vuex from 'vuex'

jest.unmock('web-app-files/src/composables')
jest.mock('web-pkg/src/composables/capability')

describe('useDriveResolver', () => {
it('should be valid', () => {
expect(useDriveResolver).toBeDefined()
})
it('space and item should be null when no driveAliasAndItem given', () => {
createComposableWrapper(
() => {
const { space, item } = useDriveResolver({ driveAliasAndItem: ref('') })
expect(unref(space)).toEqual(null)
expect(unref(item)).toEqual(null)
},
{ mocks: defaultComponentMocks(), store: defaultStoreMockOptions }
)
})
it('returns a public space on a public page', () => {
const token = 'token'
const spaceMock = mockDeep<SpaceResource>({ id: token })
const storeOptions = { ...defaultStoreMockOptions }
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => [spaceMock])
const store = createStore(Vuex.Store, storeOptions)
createComposableWrapper(
() => {
const { space, item } = useDriveResolver({ driveAliasAndItem: ref(`public/${token}`) })
expect(unref(space)).toEqual(spaceMock)
expect(unref(item)).toEqual('/')
},
{ mocks: defaultComponentMocks(), store }
)
})
it('returns a share space for a share', () => {
const spaceMock = mockDeep<SpaceResource>()
const storeOptions = { ...defaultStoreMockOptions }
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => [spaceMock])
const store = createStore(Vuex.Store, storeOptions)
createComposableWrapper(
() => {
const { space, item } = useDriveResolver({
driveAliasAndItem: ref(`share/someSharedFolder`)
})
expect(isShareSpaceResource(unref(space))).toEqual(true)
expect(unref(item)).toEqual('/')
},
{ mocks: defaultComponentMocks(), store }
)
})
it('returns a space by fileId if given', () => {
const hasSpaces = true
const fileId = 'someFileId'
const resourcePath = '/someFolder'
const spaceMock = mockDeep<SpaceResource>({ fileId, driveAlias: 'driveAlias' })
const storeOptions = { ...defaultStoreMockOptions }
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => [spaceMock])
const store = createStore(Vuex.Store, storeOptions)

jest.mocked(useCapabilitySpacesEnabled).mockImplementation(() => computed(() => hasSpaces))

createComposableWrapper(
() => {
const { space, item, itemId } = useDriveResolver({
driveAliasAndItem: ref(`/personal${resourcePath}`)
})
expect(unref(space)).toEqual(spaceMock)
expect(unref(item)).toEqual(resourcePath)
expect(unref(itemId)).toEqual(fileId)
},
{
mocks: defaultComponentMocks({
currentRoute: { name: 'files-spaces-generic', path: '/', query: { fileId } }
}),
store
}
)
})
it('returns a space by driveAlias if no fileId given', () => {
const hasSpaces = true
const driveAlias = '/personal'
const resourcePath = '/someFolder'
const spaceMock = mockDeep<SpaceResource>({ driveAlias })
const storeOptions = { ...defaultStoreMockOptions }
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => [spaceMock])
const store = createStore(Vuex.Store, storeOptions)

jest.mocked(useCapabilitySpacesEnabled).mockImplementation(() => computed(() => hasSpaces))

createComposableWrapper(
() => {
const { space, item } = useDriveResolver({
driveAliasAndItem: ref(`${driveAlias}${resourcePath}`)
})
expect(unref(space)).toEqual(spaceMock)
expect(unref(item)).toEqual(resourcePath)
},
{
mocks: defaultComponentMocks({
currentRoute: { name: 'files-spaces-generic', path: '/', query: { fileId: undefined } }
}),
store
}
)
})
it.each([
{ driveType: 'projects', loadMembersCalls: 1 },
{ driveType: 'public', loadMembersCalls: 0 },
{ driveType: 'share', loadMembersCalls: 0 },
{ driveType: 'personal', loadMembersCalls: 0 }
])('loads space members for a project space', (data) => {
const driveAlias = `/${data.driveType}`
const spaceMock = mockDeep<SpaceResource>({ driveAlias, driveType: data.driveType })
const storeOptions = { ...defaultStoreMockOptions }
storeOptions.modules.runtime.modules.spaces.getters.spaces = jest.fn(() => [spaceMock])
const store = createStore(Vuex.Store, storeOptions)

createComposableWrapper(
() => {
useDriveResolver({ driveAliasAndItem: ref(`${driveAlias}/someFolder`) })
expect(
storeOptions.modules.runtime.modules.spaces.actions.loadSpaceMembers
).toHaveBeenCalledTimes(data.loadMembersCalls)
},
{
mocks: defaultComponentMocks({
currentRoute: { name: 'files-spaces-generic', path: '/', query: { fileId: undefined } }
}),
store
}
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { useDriveResolver } from 'web-pkg/src/composables'
import { spaces } from 'web-app-files/tests/__fixtures__'
import { computed, ref } from '@vue/composition-api'
import { defaultStubs } from 'web-test-helpers/src/mocks/defaultStubs'
import { mockDeep } from 'jest-mock-extended'
import { ClientService } from 'web-pkg/src'
import { locationPublicUpload } from 'web-app-files/src/router/public'
import { PublicSpaceResource } from 'web-client/src/helpers'
import { SharePermissionBit } from 'web-client/src/helpers/share'

jest.mock('web-pkg/src/composables/driveResolver')

Expand All @@ -28,6 +33,24 @@ describe('DriveResolver view', () => {
const { wrapper } = getMountedWrapper({ space: spaces[0] })
expect(wrapper.find('generic-space-stub').exists()).toBeTruthy()
})
it('redirects to the public drop page in a public context with "upload-only"-permissions', async () => {
const space = { id: '1', getDriveAliasAndItem: jest.fn(), driveType: 'public' }
const clientService = mockDeep<ClientService>()
clientService.webdav.getFileInfo.mockResolvedValue(
mockDeep<PublicSpaceResource>({ publicLinkPermission: SharePermissionBit.Create })
)
const { wrapper, mocks } = getMountedWrapper({
space,
mocks: { $clientService: clientService }
})

await wrapper.vm.$nextTick()
await wrapper.vm.$nextTick()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we need this? but it's ugly :(

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I didn't find a way around it unfortunately :/

expect(mocks.$router.push).toHaveBeenCalledWith({
name: locationPublicUpload.name,
params: { token: space.id }
})
})
})

function getMountedWrapper({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ interface DriveResolverOptions {
driveAliasAndItem?: Ref<string>
}

export const useDriveResolver = (options: DriveResolverOptions = {}) => {
interface DriveResolverResult {
space: Ref<SpaceResource>
item: Ref<string>
itemId: Ref<string>
}

export const useDriveResolver = (options: DriveResolverOptions = {}): DriveResolverResult => {
const store = options.store || useStore()
const { areSpacesLoading } = useSpacesLoading({ store })
const shareId = useRouteQuery('shareId')
Expand Down