Skip to content

Commit

Permalink
refactor: remove sdk usage for tokeninfo
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Mar 15, 2024
1 parent 526a216 commit ae78a32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
32 changes: 21 additions & 11 deletions packages/web-runtime/src/composables/tokenInfo/useLoadTokenInfo.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
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
authStore?: AuthStore
}

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 }
Expand Down
30 changes: 18 additions & 12 deletions packages/web-runtime/tests/unit/pages/resolvePublicLink.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -26,34 +30,30 @@ 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')
})
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')
Expand All @@ -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<Task<any, any>>({
perform: () => tokenInfo,
isRunning: false,
isError: false
})
})

const $clientService = mockDeep<ClientService>()
$clientService.webdav.getFileInfo.mockResolvedValue(mockDeep<Resource>({ driveType: 'public' }))
$clientService.owncloudSdk.shares.getUnprotectedTokenInfo.mockResolvedValue(tokenInfo)
$clientService.owncloudSdk.shares.getProtectedTokenInfo.mockResolvedValue(tokenInfo)
const mocks = { ...defaultComponentMocks(), $clientService }

const capabilities = {
Expand Down

0 comments on commit ae78a32

Please sign in to comment.