2022-04-08 07:02:17 -04:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { renderHook } from '@testing-library/react-hooks'
|
|
|
|
import {
|
|
|
|
SSOProvider,
|
|
|
|
useSSOContext,
|
2022-04-22 09:49:26 -04:00
|
|
|
} from '../../../../../frontend/js/features/settings/context/sso-context'
|
2022-04-08 07:02:17 -04:00
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
const mockOauthProviders = {
|
|
|
|
google: {
|
|
|
|
descriptionKey: 'login_with_service',
|
|
|
|
descriptionOptions: { service: 'Google' },
|
|
|
|
name: 'Google',
|
|
|
|
linkPath: '/auth/google',
|
|
|
|
},
|
|
|
|
orcid: {
|
|
|
|
descriptionKey: 'oauth_orcid_description',
|
|
|
|
descriptionOptions: {
|
|
|
|
link: '/blog/434',
|
|
|
|
appName: 'Overleaf',
|
|
|
|
},
|
2022-05-26 08:58:21 -04:00
|
|
|
name: 'ORCID',
|
2022-04-22 09:49:26 -04:00
|
|
|
linkPath: '/auth/orcid',
|
|
|
|
},
|
|
|
|
twitter: {
|
|
|
|
hideWhenNotLinked: true,
|
|
|
|
name: 'Twitter',
|
|
|
|
linkPath: '/auth/twitter',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-08 07:02:17 -04:00
|
|
|
describe('SSOContext', function () {
|
|
|
|
const renderSSOContext = () =>
|
|
|
|
renderHook(() => useSSOContext(), {
|
|
|
|
wrapper: ({ children }) => <SSOProvider>{children}</SSOProvider>,
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2022-04-22 09:49:26 -04:00
|
|
|
window.metaAttributesCache = new Map()
|
|
|
|
window.metaAttributesCache.set('ol-thirdPartyIds', {
|
|
|
|
google: 'google-id',
|
|
|
|
})
|
|
|
|
window.metaAttributesCache.set('ol-oauthProviders', mockOauthProviders)
|
2022-04-08 07:02:17 -04:00
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should initialise subscriptions with their linked status', function () {
|
|
|
|
const { result } = renderSSOContext()
|
|
|
|
expect(result.current.subscriptions).to.deep.equal({
|
|
|
|
google: {
|
2022-04-22 09:49:26 -04:00
|
|
|
providerId: 'google',
|
|
|
|
provider: mockOauthProviders.google,
|
2022-04-08 07:02:17 -04:00
|
|
|
linked: true,
|
|
|
|
},
|
|
|
|
orcid: {
|
2022-04-22 09:49:26 -04:00
|
|
|
providerId: 'orcid',
|
|
|
|
provider: mockOauthProviders.orcid,
|
2022-04-08 07:02:17 -04:00
|
|
|
linked: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('unlink', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.post('express:/user/oauth-unlink', 200)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should unlink an existing subscription', async function () {
|
|
|
|
const { result, waitForNextUpdate } = renderSSOContext()
|
|
|
|
result.current.unlink('google')
|
|
|
|
await waitForNextUpdate()
|
|
|
|
expect(result.current.subscriptions.google.linked).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('when the provider is not linked, should do nothing', function () {
|
|
|
|
const { result } = renderSSOContext()
|
|
|
|
result.current.unlink('orcid')
|
|
|
|
expect(fetchMock.called()).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('supports unmounting the component while the request is inflight', async function () {
|
|
|
|
const { result, unmount } = renderSSOContext()
|
|
|
|
result.current.unlink('google')
|
|
|
|
expect(fetchMock.called()).to.be.true
|
|
|
|
unmount()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|