From ae78a3272e58051390d272b27b9e4258572a01d9 Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Thu, 14 Mar 2024 17:41:38 +0100 Subject: [PATCH] refactor: remove sdk usage for tokeninfo --- .../composables/tokenInfo/useLoadTokenInfo.ts | 32 ++++++++++++------- .../unit/pages/resolvePublicLink.spec.ts | 30 ++++++++++------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/packages/web-runtime/src/composables/tokenInfo/useLoadTokenInfo.ts b/packages/web-runtime/src/composables/tokenInfo/useLoadTokenInfo.ts index b07c08b338a..f36e6b1e696 100644 --- a/packages/web-runtime/src/composables/tokenInfo/useLoadTokenInfo.ts +++ b/packages/web-runtime/src/composables/tokenInfo/useLoadTokenInfo.ts @@ -1,6 +1,8 @@ import { useTask } from 'vue-concurrency' +import convert from 'xml-js' import { useClientService, useAuthStore, AuthStore } from '@ownclouders/web-pkg' import { ClientService } from '@ownclouders/web-pkg' +import { urlJoin } from '@ownclouders/web-client/src/utils' export interface LoadTokenInfoOptions { clientService?: ClientService @@ -8,26 +10,34 @@ export interface LoadTokenInfoOptions { } export function useLoadTokenInfo(options: LoadTokenInfoOptions) { - const { owncloudSdk } = options.clientService || useClientService() + const clientService = options.clientService || useClientService() const authStore = options.authStore || useAuthStore() const loadTokenInfoTask = useTask(function* (signal, token: string) { - let tokenInfo try { - if (authStore.userContextReady) { - tokenInfo = yield owncloudSdk.shares.getProtectedTokenInfo(token) - } else { - tokenInfo = yield owncloudSdk.shares.getUnprotectedTokenInfo(token) + const url = authStore.userContextReady + ? 'ocs/v1.php/apps/files_sharing/api/v1/tokeninfo/protected' + : 'ocs/v1.php/apps/files_sharing/api/v1/tokeninfo/unprotected' + + // FIXME: use graph endpoint as soon as it's available: https://github.com/owncloud/ocis/issues/8617 + const { data } = authStore.userContextReady + ? yield clientService.httpAuthenticated.get(urlJoin(url, token)) + : yield clientService.httpUnAuthenticated.get(urlJoin(url, token)) + + const parsedData = convert.xml2js(data, { compact: true, nativeType: false }) as any + const tokenInfo = parsedData.ocs.data + + return { + id: tokenInfo.id._text, + link_url: tokenInfo.link_url._text, + alias_link: tokenInfo.alias_link._text === 'true', + password_protected: tokenInfo.password_protected._text === 'true', + token } } catch (e) { // backend doesn't support the token info endpoint return {} } - return { - ...tokenInfo, - alias_link: tokenInfo.alias_link === 'true', - password_protected: tokenInfo.password_protected === 'true' - } }) return { loadTokenInfoTask } diff --git a/packages/web-runtime/tests/unit/pages/resolvePublicLink.spec.ts b/packages/web-runtime/tests/unit/pages/resolvePublicLink.spec.ts index ed4caf43e3b..608bb68ff86 100644 --- a/packages/web-runtime/tests/unit/pages/resolvePublicLink.spec.ts +++ b/packages/web-runtime/tests/unit/pages/resolvePublicLink.spec.ts @@ -1,11 +1,15 @@ import ResolvePublicLink from '../../../src/pages/resolvePublicLink.vue' -import { defaultPlugins, defaultComponentMocks, shallowMount } from 'web-test-helpers' +import { defaultPlugins, defaultComponentMocks, shallowMount, nextTicks } from 'web-test-helpers' import { mockDeep } from 'vitest-mock-extended' import { CapabilityStore, ClientService } from '@ownclouders/web-pkg' import { Resource } from '@ownclouders/web-client' import { authService } from 'web-runtime/src/services/auth' +import { useLoadTokenInfo } from '../../../src/composables/tokenInfo' +import { Task } from 'vue-concurrency' vi.mock('web-runtime/src/services/auth') +vi.mock('web-runtime/src/composables/tokenInfo') + const selectors = { cardFooter: '.oc-card-footer', ocSpinnerStub: 'oc-spinner-stub', @@ -26,24 +30,21 @@ describe('resolvePublicLink', () => { describe('password required form', () => { it('should display if password is required', async () => { const { wrapper } = getWrapper({ passwordRequired: true }) - await wrapper.vm.loadTokenInfoTask.last await wrapper.vm.isPasswordRequiredTask.last - await wrapper.vm.$nextTick() + await nextTicks(4) expect(wrapper.find('form').html()).toMatchSnapshot() }) describe('submit button', () => { it('should be set as disabled if "password" is empty', async () => { const { wrapper } = getWrapper({ passwordRequired: true }) - await wrapper.vm.loadTokenInfoTask.last await wrapper.vm.isPasswordRequiredTask.last - await wrapper.vm.$nextTick() + await nextTicks(4) expect(wrapper.find(selectors.submitButton).attributes().disabled).toBe('true') }) it('should be set as enabled if "password" is not empty', async () => { const { wrapper } = getWrapper({ passwordRequired: true }) - await wrapper.vm.loadTokenInfoTask.last await wrapper.vm.isPasswordRequiredTask.last - await wrapper.vm.$nextTick() + await nextTicks(4) wrapper.vm.password = 'password' await wrapper.vm.$nextTick() expect(wrapper.find(selectors.submitButton).attributes().disabled).toBe('false') @@ -51,9 +52,8 @@ describe('resolvePublicLink', () => { it('should resolve the public link on click', async () => { const resolvePublicLinkSpy = vi.spyOn(authService, 'resolvePublicLink') const { wrapper } = getWrapper({ passwordRequired: true }) - await wrapper.vm.loadTokenInfoTask.last await wrapper.vm.isPasswordRequiredTask.last - await wrapper.vm.$nextTick() + await nextTicks(4) wrapper.vm.password = 'password' await wrapper.vm.$nextTick() await wrapper.find(selectors.submitButton).trigger('submit') @@ -65,11 +65,17 @@ describe('resolvePublicLink', () => { }) function getWrapper({ passwordRequired = false } = {}) { - const tokenInfo = { password_protected: passwordRequired ? 'true' : 'false' } + const tokenInfo = { password_protected: passwordRequired } as any + vi.mocked(useLoadTokenInfo).mockReturnValue({ + loadTokenInfoTask: mockDeep>({ + perform: () => tokenInfo, + isRunning: false, + isError: false + }) + }) + const $clientService = mockDeep() $clientService.webdav.getFileInfo.mockResolvedValue(mockDeep({ driveType: 'public' })) - $clientService.owncloudSdk.shares.getUnprotectedTokenInfo.mockResolvedValue(tokenInfo) - $clientService.owncloudSdk.shares.getProtectedTokenInfo.mockResolvedValue(tokenInfo) const mocks = { ...defaultComponentMocks(), $clientService } const capabilities = {