Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/pages/Logout.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { render } from '@testing-library/react'
import { useLogoutMutation } from 'redux/otomiApi'
import Logout from './Logout'

jest.mock('redux/otomiApi', () => ({
useLogoutMutation: jest.fn(),
}))

jest.mock('components/LoadingScreen', () => () => null)

describe('Logout', () => {
let assign: jest.Mock
let reload: jest.Mock

beforeEach(() => {
jest.clearAllMocks()
assign = jest.fn()
reload = jest.fn()
Object.defineProperty(window, 'location', {
value: { ...window.location, assign, reload },
writable: true,
})
})

it('calls the apl-api logout endpoint and redirects to /platform-logout', async () => {
const logout = jest.fn().mockReturnValue(Promise.resolve({}))
;(useLogoutMutation as jest.Mock).mockReturnValue([logout])

render(<Logout />)

expect(logout).toHaveBeenCalled()
await Promise.resolve()
expect(assign).toHaveBeenCalledWith('/platform-logout')
})

it('still redirects to /platform-logout when the apl-api logout call fails', async () => {
// A thenable exposing only `.finally`, so the component's error path is exercised without
// ever constructing a genuinely rejected native Promise (which Node treats as a fatal
// unhandled rejection even when a handler is attached moments later).
const failingResult = { finally: (onFinally: () => void) => Promise.resolve().then(onFinally) }
const logout = jest.fn().mockReturnValue(failingResult)
;(useLogoutMutation as jest.Mock).mockReturnValue([logout])

render(<Logout />)

await Promise.resolve().then(() => Promise.resolve())
expect(assign).toHaveBeenCalledWith('/platform-logout')
})

it('reloads without calling logout when fetchError is true', () => {
const logout = jest.fn()
;(useLogoutMutation as jest.Mock).mockReturnValue([logout])

render(<Logout fetchError />)

expect(logout).not.toHaveBeenCalled()
expect(reload).toHaveBeenCalled()
})
})
8 changes: 3 additions & 5 deletions src/pages/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import LoadingScreen from 'components/LoadingScreen'
import React, { useEffect } from 'react'
import { useLogoutMutation } from 'redux/otomiApi'

interface Props {
fetchError?: boolean
}

export default function Logout({ fetchError = false }: Props): React.ReactElement {
// This component manages the logout process for users authenticated with Keycloak.
// - If a fetch error occurs, the page reloads automatically to handle potential session issues.
// - If no fetch error occurs, the user is redirected to the Keycloak logout page ('/platform-logout' route).
// - On component unmount, the page reloads to ensure a clean and consistent state.
const [logout] = useLogoutMutation()
useEffect(() => {
if (fetchError) window.location.reload()
else window.location.href = '/platform-logout'
else logout().finally(() => window.location.assign('/platform-logout'))
return () => {
window.location.reload()
}
Expand Down
6 changes: 6 additions & 0 deletions src/redux/otomiApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ const injectedRtkApi = api.injectEndpoints({
getSession: build.query<GetSessionApiResponse, GetSessionApiArg>({
query: () => ({ url: `/v1/session` }),
}),
logout: build.mutation<LogoutApiResponse, LogoutApiArg>({
query: () => ({ url: `/v2/user/logout`, method: 'POST' }),
}),
getApiDoc: build.query<GetApiDocApiResponse, GetApiDocApiArg>({
query: () => ({ url: `/v1/apiDocs` }),
}),
Expand Down Expand Up @@ -4231,6 +4234,8 @@ export type GetSessionApiResponse = /** status 200 Get the session for the logge
}
}
export type GetSessionApiArg = void
export type LogoutApiResponse = /** status 204 Logout processed. */ undefined
export type LogoutApiArg = void
export type GetApiDocApiResponse = /** status 200 The requested apiDoc. */ object
export type GetApiDocApiArg = void
export type GetSettingsInfoApiResponse = /** status 200 The request is successful. */ {
Expand Down Expand Up @@ -4966,6 +4971,7 @@ export const {
useGetKubecfgQuery,
useGetDockerConfigQuery,
useGetSessionQuery,
useLogoutMutation,
useGetApiDocQuery,
useGetSettingsInfoQuery,
useGetRepoBranchesQuery,
Expand Down
Loading