Skip to content

Commit f64f24c

Browse files
committed
Fix unit test
1 parent b18885c commit f64f24c

1 file changed

Lines changed: 43 additions & 60 deletions

File tree

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { IdentityClient } from '@frontegg/client';
2-
import { IEntityWithRoles } from '@frontegg/client/dist/src/clients/identity/types';
31
import { FastifyRequest } from 'fastify';
4-
import {
5-
getCloudToken,
6-
getCloudUser,
7-
} from '../../../../src/app/user-info/cloud-auth';
2+
import jwt from 'jsonwebtoken';
3+
import { getVerifiedUser } from '../../../../src/app/user-info/cloud-auth';
84

95
type MockFastifyRequest = FastifyRequest & {
106
cookies: Record<string, string>;
@@ -41,75 +37,62 @@ function createMockRequest(options: {
4137
} as unknown as MockFastifyRequest;
4238
}
4339

44-
describe('cloud-auth', () => {
45-
describe('getCloudToken', () => {
46-
it('should extract token from Authorization header', () => {
47-
const mockRequest = createMockRequest({
48-
headers: { authorization: 'Bearer test-token' },
49-
cookies: {},
50-
});
40+
describe('getVerifiedUser', () => {
41+
const publicKey = 'test-public-key';
5142

52-
expect(getCloudToken(mockRequest)).toBe('test-token');
53-
});
43+
beforeEach(() => {
44+
jest.spyOn(jwt, 'verify').mockReset();
45+
});
5446

55-
it('should get token from cookie when Authorization header is missing', () => {
56-
const mockRequest = createMockRequest({
57-
headers: {},
58-
cookies: { 'cloud-token': 'cookie-token' },
59-
});
47+
it('should return undefined when no token is present (no header, no cookie)', () => {
48+
const mockRequest = createMockRequest({ headers: {}, cookies: {} });
6049

61-
expect(getCloudToken(mockRequest)).toBe('cookie-token');
62-
});
50+
const result = getVerifiedUser(mockRequest, publicKey);
6351

64-
it('should return undefined when no token is present', () => {
65-
const mockRequest = createMockRequest({
66-
headers: {},
67-
cookies: {},
68-
});
52+
expect(result).toBeUndefined();
53+
expect(jwt.verify).not.toHaveBeenCalled();
54+
});
6955

70-
expect(getCloudToken(mockRequest)).toBeUndefined();
56+
it('should verify token from Authorization header', () => {
57+
const payload = { sub: '123' } as any;
58+
(jwt.verify as jest.Mock).mockReturnValue(payload);
59+
const mockRequest = createMockRequest({
60+
headers: { authorization: 'Bearer header-token' },
61+
cookies: {},
7162
});
72-
});
7363

74-
describe('getCloudUser', () => {
75-
let mockIdentityClient: jest.Mocked<IdentityClient>;
64+
const result = getVerifiedUser(mockRequest, publicKey);
7665

77-
beforeEach(() => {
78-
mockIdentityClient = {
79-
validateIdentityOnToken: jest.fn(),
80-
} as never;
81-
});
66+
expect(jwt.verify).toHaveBeenCalledWith('header-token', publicKey);
67+
expect(result).toEqual(payload);
68+
});
8269

83-
it('should return null when no token is provided', async () => {
84-
const result = await getCloudUser(mockIdentityClient);
85-
expect(result).toBeNull();
70+
it('should verify token from cookie when Authorization header is missing', () => {
71+
const payload = { sub: 'abc' } as any;
72+
(jwt.verify as jest.Mock).mockReturnValue(payload);
73+
const mockRequest = createMockRequest({
74+
headers: {},
75+
cookies: { 'cloud-token': 'cookie-token' },
8676
});
8777

88-
it('should return user when validation succeeds', async () => {
89-
const mockUser = { id: '123', roles: [] };
90-
mockIdentityClient.validateIdentityOnToken.mockResolvedValue(
91-
mockUser as unknown as IEntityWithRoles,
92-
);
78+
const result = getVerifiedUser(mockRequest, publicKey);
9379

94-
const result = await getCloudUser(mockIdentityClient, 'valid-token');
80+
expect(jwt.verify).toHaveBeenCalledWith('cookie-token', publicKey);
81+
expect(result).toEqual(payload);
82+
});
9583

96-
expect(result).toEqual(mockUser);
97-
expect(mockIdentityClient.validateIdentityOnToken).toHaveBeenCalledWith(
98-
'valid-token',
99-
);
84+
it('should return undefined when verification fails (throws)', () => {
85+
(jwt.verify as jest.Mock).mockImplementation(() => {
86+
throw new Error('invalid');
87+
});
88+
const mockRequest = createMockRequest({
89+
headers: { authorization: 'Bearer bad-token' },
90+
cookies: {},
10091
});
10192

102-
it('should return null when validation fails', async () => {
103-
mockIdentityClient.validateIdentityOnToken.mockRejectedValue(
104-
new Error('Invalid token'),
105-
);
106-
107-
const result = await getCloudUser(mockIdentityClient, 'invalid-token');
93+
const result = getVerifiedUser(mockRequest, publicKey);
10894

109-
expect(result).toBeNull();
110-
expect(mockIdentityClient.validateIdentityOnToken).toHaveBeenCalledWith(
111-
'invalid-token',
112-
);
113-
});
95+
expect(result).toBeUndefined();
96+
expect(jwt.verify).toHaveBeenCalledWith('bad-token', publicKey);
11497
});
11598
});

0 commit comments

Comments
 (0)