Skip to content

Commit

Permalink
update tests and clean imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hodgson committed Oct 25, 2024
1 parent 36f4f9f commit aa0d6cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
FullContextProvider,
SynapseClient,
} from '../../../index'
import {
MOCK_USER_ID,
mockUserProfileData,
} from '../../../mocks/user/mock_user_profile'
import { MOCK_USER_ID } from '../../../mocks/user/mock_user_profile'
import { SynapseClientError } from '@sage-bionetworks/synapse-client/util/SynapseClientError'
import { MemoryRouter } from 'react-router-dom'
import * as UseDetectSSOCodeModule from '../../hooks/useDetectSSOCode'
Expand Down Expand Up @@ -101,7 +98,6 @@ describe('ApplicationSessionManager tests', () => {
'getAccessTokenFromCookie',
)
const signOutSpy = jest.spyOn(SynapseClient, 'signOut')
const mockGetUserProfile = jest.spyOn(SynapseClient, 'getUserProfile')
const mockAuthenticatedOn = jest.spyOn(SynapseClient, 'getAuthenticatedOn')
const mockTermsOfServiceStatus = jest.spyOn(
SynapseClient,
Expand All @@ -122,15 +118,14 @@ describe('ApplicationSessionManager tests', () => {

it('Bootstraps when signed in', async () => {
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)
mockTermsOfServiceStatus.mockResolvedValue(
TERMS_OF_SERVICE_STATUS_UP_TO_DATE,
)
const context = render()

await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockGetUserProfile).toHaveBeenCalled()
expect(mockTermsOfServiceStatus).toHaveBeenCalled()
expect(signOutSpy).not.toHaveBeenCalled()
expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE)
})
Expand All @@ -141,19 +136,12 @@ describe('ApplicationSessionManager tests', () => {
TERMS_OF_SERVICE_STATUS_MUST_AGREE_NOW,
)
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockRejectedValue(
new SynapseClientError(
403,
'Terms of use have not been signed.',
expect.getState().currentTestName!,
),
)

const context = render()

await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockGetUserProfile).toHaveBeenCalled()
expect(mockTermsOfServiceStatus).toHaveBeenCalled()
expect(signOutSpy).not.toHaveBeenCalled()
expect(context.result.current).toMatchObject(
EXPECTED_AUTH_STATE_TERMS_NOT_ACCEPTED,
Expand All @@ -179,7 +167,9 @@ describe('ApplicationSessionManager tests', () => {

// At this point, useDetectSSOCode would use the URL query parameters to finish sign in
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)
mockTermsOfServiceStatus.mockResolvedValue(
TERMS_OF_SERVICE_STATUS_UP_TO_DATE,
)
mockUseDetectSSOCode.mockReturnValue({ isLoading: false })

// useDetectSSOCode will then invoke onSignInComplete to refresh the session
Expand Down Expand Up @@ -297,14 +287,13 @@ describe('ApplicationSessionManager tests', () => {
)
// Using the app's Login component, the user logs in. the component invokes refreshSession once the token is stored
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)

// Call under test
await context.result.current.refreshSession()

await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockGetUserProfile).toHaveBeenCalled()
expect(mockTermsOfServiceStatus).toHaveBeenCalled()
expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE)
})
})
Expand All @@ -315,13 +304,12 @@ describe('ApplicationSessionManager tests', () => {
TERMS_OF_SERVICE_STATUS_UP_TO_DATE,
)
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)

const context = render()

await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockGetUserProfile).toHaveBeenCalled()
expect(mockTermsOfServiceStatus).toHaveBeenCalled()
expect(signOutSpy).not.toHaveBeenCalled()
expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE)
})
Expand All @@ -344,7 +332,6 @@ describe('ApplicationSessionManager tests', () => {
TERMS_OF_SERVICE_STATUS_UP_TO_DATE,
)
mockGetAccessToken.mockResolvedValue(MOCK_ACCESS_TOKEN)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)
mockAuthenticatedOn.mockResolvedValue({
authenticatedOn,
})
Expand All @@ -355,7 +342,7 @@ describe('ApplicationSessionManager tests', () => {

await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockGetUserProfile).toHaveBeenCalled()
expect(mockTermsOfServiceStatus).toHaveBeenCalled()
expect(mockAuthenticatedOn).toHaveBeenCalled()
expect(signOutSpy).not.toHaveBeenCalled()
expect(context.result.current).toMatchObject(EXPECTED_AUTH_STATE)
Expand All @@ -368,7 +355,9 @@ describe('ApplicationSessionManager tests', () => {
const maxAge = 60 * 60
const token = 'asdf'
mockGetAccessToken.mockResolvedValue(token)
mockGetUserProfile.mockResolvedValue(mockUserProfileData)
mockTermsOfServiceStatus.mockResolvedValue(
TERMS_OF_SERVICE_STATUS_UP_TO_DATE,
)

console.log(dayjs.utc().subtract(24, 'hours').format())
mockAuthenticatedOn.mockResolvedValue({
Expand All @@ -382,7 +371,7 @@ describe('ApplicationSessionManager tests', () => {
await waitFor(() => {
expect(mockGetAccessToken).toHaveBeenCalled()
expect(mockAuthenticatedOn).toHaveBeenCalled()
expect(mockGetUserProfile).not.toHaveBeenCalled()
expect(mockTermsOfServiceStatus).not.toHaveBeenCalled()
expect(signOutSpy).not.toHaveBeenCalled()
expect(context.result.current).toMatchObject(EXPECTED_ANONYMOUS_STATE)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { ApplicationSessionContextProvider } from './ApplicationSessionContext'
import { SynapseContextProvider, SynapseContextType } from '../../context'
import dayjs from 'dayjs'
import { useTermsOfServiceStatus } from '../../../synapse-queries/termsOfService/useTermsOfService'
import { PresentToAll } from '@mui/icons-material'
import { TermsOfServiceState } from '@sage-bionetworks/synapse-types'

export type ApplicationSessionManagerProps = React.PropsWithChildren<{
downloadCartPageUrl?: string
Expand Down

0 comments on commit aa0d6cb

Please sign in to comment.