-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: context based login with ansattporten (#14014)
Co-authored-by: William Thorenfeldt <[email protected]> Co-authored-by: Mirko Sekulic <[email protected]>
- Loading branch information
1 parent
c811226
commit 498673f
Showing
15 changed files
with
232 additions
and
18 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
frontend/app-development/hooks/queries/useIsLoggedInWithAnsattportenQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { QueryKey } from 'app-shared/types/QueryKey'; | ||
import { useServicesContext } from 'app-shared/contexts/ServicesContext'; | ||
|
||
export const useIsLoggedInWithAnsattportenQuery = () => { | ||
const { getIsLoggedInWithAnsattporten } = useServicesContext(); | ||
return useQuery<boolean>({ | ||
queryKey: [QueryKey.IsLoggedInWithAnsattporten], | ||
queryFn: () => getIsLoggedInWithAnsattporten(), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...ader/SettingsModalButton/SettingsModal/components/Tabs/Maskinporten/Maskinporten.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from 'react'; | ||
import { screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; | ||
import { Maskinporten } from './Maskinporten'; | ||
import { renderWithProviders } from '../../../../../../../../test/mocks'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { queriesMock } from 'app-shared/mocks/queriesMock'; | ||
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
describe('Maskinporten', () => { | ||
const consoleLogMock = jest.fn(); | ||
const originalConsoleLog = console.log; | ||
beforeEach(() => { | ||
console.log = consoleLogMock; | ||
}); | ||
|
||
afterEach(() => { | ||
console.log = originalConsoleLog; | ||
}); | ||
|
||
it('should check and verify if the user is logged in', async () => { | ||
const getIsLoggedInWithAnsattportenMock = jest | ||
.fn() | ||
.mockImplementation(() => Promise.resolve(false)); | ||
|
||
renderMaskinporten({ | ||
queries: { | ||
getIsLoggedInWithAnsattporten: getIsLoggedInWithAnsattportenMock, | ||
}, | ||
}); | ||
await waitForLoggedInStatusCheckIsDone(); | ||
await waitFor(() => expect(getIsLoggedInWithAnsattportenMock).toHaveBeenCalledTimes(1)); | ||
}); | ||
|
||
it('should display information about login and login button, if user is not logged in', async () => { | ||
renderMaskinporten(); | ||
await waitForLoggedInStatusCheckIsDone(); | ||
|
||
const title = screen.getByRole('heading', { | ||
level: 2, | ||
name: textMock('settings_modal.maskinporten_tab_title'), | ||
}); | ||
expect(title).toBeInTheDocument(); | ||
|
||
const description = screen.getByText(textMock('settings_modal.maskinporten_tab_description')); | ||
expect(description).toBeInTheDocument(); | ||
|
||
const loginButton = screen.getByRole('button', { | ||
name: textMock('settings_modal.maskinporten_tab_login_with_ansattporten'), | ||
}); | ||
expect(loginButton).toBeInTheDocument(); | ||
}); | ||
|
||
it('should display content if logged in', async () => { | ||
const getIsLoggedInWithAnsattportenMock = jest | ||
.fn() | ||
.mockImplementation(() => Promise.resolve(true)); | ||
renderMaskinporten({ | ||
queries: { | ||
getIsLoggedInWithAnsattporten: getIsLoggedInWithAnsattportenMock, | ||
}, | ||
}); | ||
|
||
await waitForLoggedInStatusCheckIsDone(); | ||
|
||
const temporaryLoggedInContent = screen.getByText('View when logged in comes here'); | ||
expect(temporaryLoggedInContent).toBeInTheDocument(); | ||
}); | ||
|
||
it('should invoke "handleLoginWithAnsattPorten" when login button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
renderMaskinporten(); | ||
await waitForLoggedInStatusCheckIsDone(); | ||
|
||
const loginButton = screen.getByRole('button', { | ||
name: textMock('settings_modal.maskinporten_tab_login_with_ansattporten'), | ||
}); | ||
|
||
await user.click(loginButton); | ||
|
||
expect(consoleLogMock).toHaveBeenCalledWith( | ||
'Will be implemented in next iteration when backend is ready', | ||
); | ||
}); | ||
}); | ||
|
||
type RenderMaskinporten = { | ||
queries?: Partial<typeof queriesMock>; | ||
}; | ||
const renderMaskinporten = ({ queries = queriesMock }: RenderMaskinporten = {}) => { | ||
const queryClient = createQueryClientMock(); | ||
renderWithProviders({ ...queriesMock, ...queries }, queryClient)(<Maskinporten />); | ||
}; | ||
|
||
async function waitForLoggedInStatusCheckIsDone() { | ||
await waitForElementToBeRemoved(() => screen.queryByTitle(textMock('general.loading'))); | ||
} |
36 changes: 36 additions & 0 deletions
36
...SubHeader/SettingsModalButton/SettingsModal/components/Tabs/Maskinporten/Maskinporten.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { type ReactElement } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { TabContent } from '../../TabContent'; | ||
import { StudioButton, StudioHeading, StudioParagraph, StudioSpinner } from '@studio/components'; | ||
import { useIsLoggedInWithAnsattportenQuery } from '../../../../../../../../hooks/queries/useIsLoggedInWithAnsattportenQuery'; | ||
|
||
export const Maskinporten = (): ReactElement => { | ||
const { data: isLoggedInWithAnsattporten, isPending: isPendingAuthStatus } = | ||
useIsLoggedInWithAnsattportenQuery(); | ||
|
||
const { t } = useTranslation(); | ||
|
||
const handleLoginWithAnsattporten = (): void => { | ||
console.log('Will be implemented in next iteration when backend is ready'); | ||
}; | ||
|
||
if (isPendingAuthStatus) { | ||
return <StudioSpinner spinnerTitle={t('general.loading')} />; | ||
} | ||
|
||
if (isLoggedInWithAnsattporten) { | ||
return <div>View when logged in comes here</div>; | ||
} | ||
|
||
return ( | ||
<TabContent> | ||
<StudioHeading level={2} size='sm' spacing> | ||
{t('settings_modal.maskinporten_tab_title')} | ||
</StudioHeading> | ||
<StudioParagraph spacing>{t('settings_modal.maskinporten_tab_description')}</StudioParagraph> | ||
<StudioButton onClick={handleLoginWithAnsattporten}> | ||
{t('settings_modal.maskinporten_tab_login_with_ansattporten')} | ||
</StudioButton> | ||
</TabContent> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...eHeader/SubHeader/SettingsModalButton/SettingsModal/components/Tabs/Maskinporten/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Maskinporten } from './Maskinporten'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type SettingsModalTabId = 'about' | 'setup' | 'policy' | 'access_control'; | ||
export type SettingsModalTabId = 'about' | 'setup' | 'policy' | 'access_control' | 'maskinporten'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters