Files
ocis/web/packages/web-pkg/tests/unit/composables/embedMode/useEmbedMode.spec.ts
Matteo 6069640d49 [full-ci] fix(web): break web-pkg/design-system/web-test-helpers dependency cycle (#13038)
fix(web): break web-pkg/design-system/web-test-helpers dependency cycle

web-pkg, design-system and web-test-helpers formed a cycle: the mount
helpers have to install the design system (its components are registered
globally) and web-pkg's mocked pinia stores, so web-test-helpers was
imported by both packages that imported it back.

Resolved by layering the helpers onto the packages that already need
them, instead of adding another one:

  design-system/testing  mount/shallowMount, prop types, design system +
                         gettext + router-link stub plugins
  web-pkg/src/testing    the above, extended with casl abilities, mocked
                         pinia stores and component mocks
  web-test-helpers       re-export of web-pkg/src/testing

design-system sits at the bottom of the workspace graph, so it cannot
depend on a package that installs it - hence the generic half lives
there. web-pkg composes those plugins rather than duplicating them, so
every helper is still defined exactly once, and no package depends on
one above it.

The public API of `@ownclouders/web-test-helpers` is unchanged, so
external extensions are unaffected.

Also:
- pin `typescript` via `pnpm.overrides` - without it, a second peer-
  resolved typescript version forks `vue`, which forks `vue3-gettext`
  into two module instances, breaking `inject(GetTextSymbol)` for any
  component mounted through these helpers.
- keep `@ownclouders/design-system/testing` external in web-pkg's lib
  build. rollup matches `external` strings exactly, so the subpath was
  inlined, shipping a second copy of the design system and of
  @vue/test-utils into the published bundle - components and wrappers
  were then not identity-equal to the consumer's own, and gettext
  injection broke.
- name the web-test-helpers umd output `.cjs`. The package is ESM, so the
  `.js` file that publishConfig.exports pointed `require` at could not be
  required.

Verified: `pnpm install --frozen-lockfile` clean, zero cyclic-workspace
warnings; `pnpm test:unit` 380 files / 3129 tests pass; `pnpm check:types`
0 errors; `pnpm lint`/`check:format` 0 errors; `pnpm build` succeeds;
published artifacts checked by mounting through the built `dist/` bundles
the way an external extension resolves them via publishConfig.exports.
2026-10-02 08:13:31 +02:00

260 lines
7.7 KiB
TypeScript

import { useEmbedMode } from '../../../../src/composables/embedMode'
import { defaultComponentMocks, getComposableWrapper } from '../../../../src/testing'
import { unref } from 'vue'
describe('useEmbedMode', () => {
describe('isEnabled', () => {
it('when embed mode is disabled should return false', () => {
getComposableWrapper(
() => {
const { isEnabled } = useEmbedMode()
expect(unref(isEnabled)).toStrictEqual(false)
},
getWrapperOptions({ enabled: false })
)
})
it('when embed mode is enabled should return true', () => {
getComposableWrapper(
() => {
const { isEnabled } = useEmbedMode()
expect(unref(isEnabled)).toStrictEqual(true)
},
getWrapperOptions({ enabled: true })
)
})
})
describe('isLocationPicker', () => {
it('when target is not location should return false', () => {
getComposableWrapper(
() => {
const { isLocationPicker } = useEmbedMode()
expect(unref(isLocationPicker)).toStrictEqual(false)
},
getWrapperOptions({ target: 'resources' })
)
})
it('when target is location should return true', () => {
getComposableWrapper(
() => {
const { isLocationPicker } = useEmbedMode()
expect(unref(isLocationPicker)).toStrictEqual(true)
},
getWrapperOptions({ target: 'location' })
)
})
})
describe('isFilePicker', () => {
it('when target is not file should return false', () => {
getComposableWrapper(
() => {
const { isFilePicker } = useEmbedMode()
expect(unref(isFilePicker)).toStrictEqual(false)
},
getWrapperOptions({ target: 'resources' })
)
})
it('when target is file should return true', () => {
getComposableWrapper(
() => {
const { isFilePicker } = useEmbedMode()
expect(unref(isFilePicker)).toStrictEqual(true)
},
getWrapperOptions({ target: 'file' })
)
})
})
describe('messagesTargetOrigin', () => {
it('when messagesOrigin is set should return it', () => {
getComposableWrapper(
() => {
const { messagesTargetOrigin } = useEmbedMode()
expect(unref(messagesTargetOrigin)).toStrictEqual('message-origin')
},
getWrapperOptions({ messagesOrigin: 'message-origin' })
)
})
})
describe('isDelegatingAuthentication', () => {
it('when delegation is enabled but embed mode is not enabled should return false', () => {
getComposableWrapper(
() => {
const { isDelegatingAuthentication } = useEmbedMode()
expect(unref(isDelegatingAuthentication)).toStrictEqual(false)
},
getWrapperOptions({ enabled: false, delegateAuthentication: true })
)
})
it('when delegation is enabled and embed mode is enabled should return true', () => {
getComposableWrapper(
() => {
const { isDelegatingAuthentication } = useEmbedMode()
expect(unref(isDelegatingAuthentication)).toStrictEqual(true)
},
getWrapperOptions({ enabled: true, delegateAuthentication: true })
)
})
it('when delegation is disabled and embed mode is enabled should return false', () => {
getComposableWrapper(
() => {
const { isDelegatingAuthentication } = useEmbedMode()
expect(unref(isDelegatingAuthentication)).toStrictEqual(false)
},
getWrapperOptions({ enabled: false, delegateAuthentication: false })
)
})
})
describe('postMessage', () => {
it('when targetOrigin is not set should call postMessage without any origin', () => {
window.parent.postMessage = vi.fn() as (...args: unknown[]) => unknown
getComposableWrapper(
() => {
const { postMessage } = useEmbedMode()
postMessage('owncloud-embed:request-token', { hello: 'world' })
expect(window.parent.postMessage).toHaveBeenCalledWith(
{
name: 'owncloud-embed:request-token',
data: { hello: 'world' }
},
{}
)
},
getWrapperOptions({ messagesOrigin: undefined })
)
})
it('when targetOrigin is set should call postMessage with its value as origin', () => {
window.parent.postMessage = vi.fn() as (...args: unknown[]) => unknown
getComposableWrapper(
() => {
const { postMessage } = useEmbedMode()
postMessage('owncloud-embed:request-token', { hello: 'world' })
expect(window.parent.postMessage).toHaveBeenCalledWith(
{
name: 'owncloud-embed:request-token',
data: { hello: 'world' }
},
{ targetOrigin: 'messages-origin' }
)
},
getWrapperOptions({ messagesOrigin: 'messages-origin' })
)
})
})
describe('verifyDelegatedAuthenticationOrigin', () => {
it('when delegateAuthenticationOrigin is not set should return false', () => {
getComposableWrapper(
() => {
const { verifyDelegatedAuthenticationOrigin } = useEmbedMode()
expect(verifyDelegatedAuthenticationOrigin('event-origin')).toStrictEqual(false)
},
{ mocks: defaultComponentMocks() }
)
})
it('when delegateAuthenticationOrigin is set and origins match should return true', () => {
getComposableWrapper(
() => {
const { verifyDelegatedAuthenticationOrigin } = useEmbedMode()
expect(verifyDelegatedAuthenticationOrigin('event-origin')).toStrictEqual(true)
},
getWrapperOptions({ delegateAuthenticationOrigin: 'event-origin' })
)
})
it('when delegateAuthenticationOrigin is set but origins do not match should return false', () => {
getComposableWrapper(
() => {
const { verifyDelegatedAuthenticationOrigin } = useEmbedMode()
expect(verifyDelegatedAuthenticationOrigin('event-origin')).toStrictEqual(false)
},
getWrapperOptions({ delegateAuthenticationOrigin: 'authentication-origin' })
)
})
})
describe('verifyMessageOrigin', () => {
it('returns true for the application origin', () => {
getComposableWrapper(
() => {
const { verifyMessageOrigin } = useEmbedMode()
expect(verifyMessageOrigin(window.location.origin)).toStrictEqual(true)
},
{ mocks: defaultComponentMocks() }
)
})
it('returns false for a foreign origin when messagesOrigin is not set', () => {
getComposableWrapper(
() => {
const { verifyMessageOrigin } = useEmbedMode()
expect(verifyMessageOrigin('https://attacker.example.com')).toStrictEqual(false)
},
{ mocks: defaultComponentMocks() }
)
})
it('returns true for the configured messagesOrigin', () => {
getComposableWrapper(
() => {
const { verifyMessageOrigin } = useEmbedMode()
expect(verifyMessageOrigin('https://trusted.example.com')).toStrictEqual(true)
},
getWrapperOptions({ messagesOrigin: 'https://trusted.example.com' })
)
})
it('returns false for a foreign origin even when messagesOrigin is set', () => {
getComposableWrapper(
() => {
const { verifyMessageOrigin } = useEmbedMode()
expect(verifyMessageOrigin('https://attacker.example.com')).toStrictEqual(false)
},
getWrapperOptions({ messagesOrigin: 'https://trusted.example.com' })
)
})
})
})
const getWrapperOptions = (embed = {}) => ({
mocks: defaultComponentMocks(),
pluginOptions: {
piniaOptions: {
configState: { options: { embed } }
}
}
})