sso-context.test.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { expect } from 'chai'
  2. import { renderHook } from '@testing-library/react-hooks'
  3. import {
  4. SSOProvider,
  5. useSSOContext,
  6. } from '../../../../../frontend/js/features/settings/context/sso-context'
  7. import fetchMock from 'fetch-mock'
  8. const mockOauthProviders = {
  9. google: {
  10. descriptionKey: 'login_with_service',
  11. descriptionOptions: { service: 'Google' },
  12. name: 'Google',
  13. linkPath: '/auth/google',
  14. },
  15. orcid: {
  16. descriptionKey: 'oauth_orcid_description',
  17. descriptionOptions: {
  18. link: '/blog/434',
  19. appName: 'Overleaf',
  20. },
  21. name: 'ORCID',
  22. linkPath: '/auth/orcid',
  23. },
  24. }
  25. describe('SSOContext', function () {
  26. const renderSSOContext = () =>
  27. renderHook(() => useSSOContext(), {
  28. wrapper: ({ children }) => <SSOProvider>{children}</SSOProvider>,
  29. })
  30. beforeEach(function () {
  31. window.metaAttributesCache.set('ol-thirdPartyIds', {
  32. google: 'google-id',
  33. })
  34. window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
  35. fetchMock.reset()
  36. })
  37. it('should initialise subscriptions with their linked status', function () {
  38. const { result } = renderSSOContext()
  39. expect(result.current.subscriptions).to.deep.equal({
  40. google: {
  41. providerId: 'google',
  42. provider: mockOauthProviders.google,
  43. linked: true,
  44. },
  45. orcid: {
  46. providerId: 'orcid',
  47. provider: mockOauthProviders.orcid,
  48. linked: false,
  49. },
  50. })
  51. })
  52. describe('unlink', function () {
  53. beforeEach(function () {
  54. fetchMock.post('express:/user/oauth-unlink', 200)
  55. })
  56. it('should unlink an existing subscription', async function () {
  57. const { result, waitForNextUpdate } = renderSSOContext()
  58. result.current.unlink('google')
  59. await waitForNextUpdate()
  60. expect(result.current.subscriptions.google.linked).to.be.false
  61. })
  62. it('when the provider is not linked, should do nothing', function () {
  63. const { result } = renderSSOContext()
  64. result.current.unlink('orcid')
  65. expect(fetchMock.called()).to.be.false
  66. })
  67. it('supports unmounting the component while the request is inflight', async function () {
  68. const { result, unmount } = renderSSOContext()
  69. result.current.unlink('google')
  70. expect(fetchMock.called()).to.be.true
  71. unmount()
  72. })
  73. })
  74. })