-
Notifications
You must be signed in to change notification settings - Fork 0
Expand test coverage for authentication routes (#565) #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
| import { Request, Response } from 'express'; | ||
| import passport from 'passport'; | ||
| import jwt from 'jsonwebtoken'; | ||
|
|
||
| import { checkTokenFitsInCookie, loginEntraID } from '../../../src/controllers/auth'; | ||
| import { config } from '../../../src/config'; | ||
|
|
||
| describe('auth controller', () => { | ||
| describe('checkTokenFitsInCookie', () => { | ||
| it('does not throw for a token within the 4096 byte limit', () => { | ||
| expect(() => checkTokenFitsInCookie('a'.repeat(4096))).not.toThrow(); | ||
| }); | ||
|
|
||
| it('throws for a token larger than 4096 bytes', () => { | ||
| expect(() => checkTokenFitsInCookie('a'.repeat(4097))).toThrow(/exceeds the maximum cookie size/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('loginEntraID', () => { | ||
| const returnURL = `${config.frontend.url}/auth/callback`; | ||
|
|
||
| let req: { login: jest.Mock }; | ||
| let res: Partial<Response> & { redirect: jest.Mock; cookie: jest.Mock }; | ||
| let next: jest.Mock; | ||
|
|
||
| // Replace passport.authenticate with a stub that immediately invokes the controller's | ||
| // callback with the supplied (err, user, info), mimicking a finished EntraID round-trip. | ||
| const stubAuthenticate = (err: Error | null, user: unknown, info?: Record<string, string>) => { | ||
| jest | ||
| .spyOn(passport, 'authenticate') | ||
| .mockImplementation( | ||
| ((_strategy: unknown, _opts: unknown, cb: (e: Error | null, u: unknown, i?: unknown) => void) => () => | ||
| cb(err, user, info)) as unknown as typeof passport.authenticate | ||
| ); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| req = { login: jest.fn((_user, _opts, cb: (e: Error | null) => void) => cb(null)) }; | ||
| res = { redirect: jest.fn(), cookie: jest.fn() }; | ||
| next = jest.fn(); | ||
| }); | ||
|
|
||
| afterEach(() => jest.restoreAllMocks()); | ||
|
|
||
| it('redirects with error=provider when passport returns an error', () => { | ||
| stubAuthenticate(new Error('boom'), undefined); | ||
| loginEntraID(req as unknown as Request, res as Response, next); | ||
| expect(res.redirect).toHaveBeenCalledWith(`${returnURL}?error=provider`); | ||
| expect(res.cookie).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('redirects with error=provider when no user is returned', () => { | ||
| stubAuthenticate(null, undefined, { message: 'no user' }); | ||
| loginEntraID(req as unknown as Request, res as Response, next); | ||
| expect(res.redirect).toHaveBeenCalledWith(`${returnURL}?error=provider`); | ||
| }); | ||
|
|
||
| it('redirects with error=login when req.login fails', () => { | ||
| stubAuthenticate(null, { id: 'user-1', email: 'a@b.com' }); | ||
| req.login = jest.fn((_user, _opts, cb: (e: Error | null) => void) => cb(new Error('login failed'))); | ||
| loginEntraID(req as unknown as Request, res as Response, next); | ||
| expect(res.redirect).toHaveBeenCalledWith(`${returnURL}?error=login`); | ||
| expect(res.cookie).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('sets a jwt cookie and redirects to the callback on success', () => { | ||
| const user = { id: 'user-1', email: 'a@b.com', name: 'A B', status: 'active', globalRoles: [], groupRoles: [] }; | ||
| stubAuthenticate(null, user); | ||
|
|
||
| loginEntraID(req as unknown as Request, res as Response, next); | ||
|
|
||
| expect(res.cookie).toHaveBeenCalledWith('jwt', expect.any(String), expect.objectContaining({ httpOnly: true })); | ||
| expect(res.redirect).toHaveBeenCalledWith(returnURL); | ||
|
|
||
| const token = res.cookie.mock.calls[0][1] as string; | ||
| const decoded = jwt.verify(token, config.auth.jwt.secret) as { user: { id: string } }; | ||
| expect(decoded.user.id).toBe('user-1'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.