Skip to content
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

[TTAHUB-3569] Coverage >= 90% src/middleware/authMiddleware.js #2512

Merged
merged 2 commits into from
Dec 4, 2024
Merged
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
35 changes: 35 additions & 0 deletions src/middleware/authMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ describe('authMiddleware', () => {
expect(mockResponse.redirect).not.toHaveBeenCalledWith(process.env.TTA_SMART_HUB_URI);
});

it('login should set referrerPath to empty string if referrer is undefined', () => {
const mockSession = jest.fn();
mockSession.userId = undefined;
const mockRequest = {
path: '/api/login',
session: mockSession,
headers: {},
};
const mockResponse = {
redirect: jest.fn(),
sendStatus: jest.fn(),
};
login(mockRequest, mockResponse);
expect(mockRequest.session.referrerPath).toBe('');
});

it('bypass authorization if variables are set for UAT or accessibility testing', async () => {
// auth is bypassed if non-prod NODE_ENV and BYPASS_AUTH = 'true', needed for cucumber and axe
const user = {
Expand Down Expand Up @@ -201,4 +217,23 @@ describe('authMiddleware', () => {

await destroyUser(mockUser);
});

it('should return immediately if headers are already sent', async () => {
const mockNext = jest.fn();
const mockSession = jest.fn();
mockSession.userId = undefined;
const mockRequest = {
path: '/api/endpoint',
session: mockSession,
};
const mockResponse = {
headersSent: true,
redirect: jest.fn(),
sendStatus: jest.fn(),
};
await authMiddleware(mockRequest, mockResponse, mockNext);
expect(mockResponse.redirect).not.toHaveBeenCalled();
expect(mockResponse.sendStatus).not.toHaveBeenCalled();
expect(mockNext).not.toHaveBeenCalled();
});
});