-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GAP-1984) Adds Navbar for authenticated users (#71)
* initial * import * amend import * add a couple of mocks * rm * add logout tests * assert on axios * add back button, rm breadcrumbs * run prettier * rm * setup auth context, mock next-config in all tests * hide sign out when OL disabled * useauth * fix predicate * get ol from runtimeConfig * refactor env vars - use useContext * update env example * update env example * update env example
- Loading branch information
Showing
32 changed files
with
471 additions
and
334 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import '@testing-library/jest-dom'; | ||
import { merge } from 'lodash'; | ||
import Logout from '../../../pages/api/logout'; | ||
import { getSessionIdFromCookies } from '../../../src/utils/session'; | ||
import axios from 'axios'; | ||
|
||
jest.mock('../../../src/utils/session'); | ||
jest.mock('axios'); | ||
|
||
const mockedRedirect = jest.fn(); | ||
const mockedSetHeader = jest.fn(); | ||
const mockedSend = jest.fn(); | ||
|
||
const req = (overrides = {}) => | ||
merge( | ||
{ | ||
headers: { | ||
referer: `/referer`, | ||
}, | ||
cookies: { sessionCookieName: 'testSessionId' }, | ||
}, | ||
overrides, | ||
); | ||
|
||
const res = (overrides = {}) => | ||
merge( | ||
{ | ||
redirect: mockedRedirect, | ||
setHeader: mockedSetHeader, | ||
send: mockedSend, | ||
}, | ||
overrides, | ||
); | ||
|
||
describe('Logout page', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
process.env.ONE_LOGIN_ENABLED = 'false'; | ||
process.env.LOGOUT = 'http://localhost:8082/logout'; | ||
}); | ||
|
||
it('Should clear back-end authentication session if there is session_id cookie available', async () => { | ||
(getSessionIdFromCookies as jest.Mock).mockReturnValue('testSessionId'); | ||
await Logout(req(), res()); | ||
|
||
expect(axios.delete).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Should NOT try to clear back-end authentication session if session_id cookie not available', async () => { | ||
(getSessionIdFromCookies as jest.Mock).mockReturnValue(''); | ||
await Logout(req(), res()); | ||
|
||
expect(axios.delete).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('Should clear session_id cookie', async () => { | ||
await Logout(req(), res()); | ||
|
||
expect(mockedSetHeader).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Should redirect to login page', async () => { | ||
process.env.V2_LOGOUT_URL = 'http://localhost:8082/logout'; | ||
process.env.LOGOUT_URL = 'http://localhost:8082/logout'; | ||
|
||
await Logout(req(), res()); | ||
|
||
expect(mockedRedirect).toHaveBeenNthCalledWith( | ||
1, | ||
302, | ||
'http://localhost:8082/logout', | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -15,12 +15,6 @@ jest.mock('../../../src/utils/jwt', () => ({ | |
})), | ||
})); | ||
|
||
jest.mock('next/config', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { serverRuntimeConfig: {} }; | ||
}); | ||
}); | ||
|
||
const req = { | ||
body: { | ||
email: '[email protected]', | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ module.exports = { | |
sassOptions: { | ||
includePaths: [path.join(__dirname, 'styles')], | ||
}, | ||
|
||
output: 'standalone', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getSessionIdFromCookies } from '../../../src/utils/session'; | ||
import axios from 'axios'; | ||
|
||
const Logout = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const sessionCookie = getSessionIdFromCookies(req); | ||
if (sessionCookie) await logoutAdmin(sessionCookie); | ||
|
||
res.setHeader( | ||
'Set-Cookie', | ||
`session_id=deleted; Path=/; secure; HttpOnly; SameSite=Strict; expires=Thu, 01 Jan 2003 00:00:00 GMT`, | ||
); | ||
res.redirect(302, process.env.V2_LOGOUT_URL); | ||
}; | ||
|
||
const axiosSessionConfig = (sessionId: string) => ({ | ||
withCredentials: true, | ||
headers: { | ||
Cookie: `SESSION=${sessionId};`, | ||
}, | ||
}); | ||
|
||
const logoutAdmin = async (sessionCookie: string) => | ||
axios.delete( | ||
`${process.env.ADMIN_BACKEND_HOST}/logout`, | ||
axiosSessionConfig(sessionCookie), | ||
); | ||
|
||
export default Logout; |
Oops, something went wrong.