|
| 1 | +import { toggleAvatarHandler, ToggleAvatarRequest } from './toggleAvatar'; |
| 2 | +import { getCollection } from '../../utils/db'; |
| 3 | +import { ObjectId } from 'mongodb'; |
| 4 | +import { AuthContext, HandlerEventWithBody, AuthUser } from '../../types'; |
| 5 | +import { HandlerContext, HandlerEvent } from '@netlify/functions'; |
| 6 | + |
| 7 | +jest.mock('../../utils/db'); |
| 8 | + |
| 9 | +interface ExtendedAuthUser extends AuthUser { |
| 10 | + email_verified?: boolean; |
| 11 | + picture?: string; |
| 12 | +} |
| 13 | + |
| 14 | +describe('toggleAvatarHandler', () => { |
| 15 | + const collectionMock = { |
| 16 | + findOne: jest.fn(), |
| 17 | + findOneAndUpdate: jest.fn(), |
| 18 | + }; |
| 19 | + |
| 20 | + const baseEvent: HandlerEvent = { |
| 21 | + body: null, |
| 22 | + headers: {}, |
| 23 | + multiValueHeaders: {}, |
| 24 | + httpMethod: 'POST', |
| 25 | + isBase64Encoded: false, |
| 26 | + path: '', |
| 27 | + queryStringParameters: null, |
| 28 | + multiValueQueryStringParameters: null, |
| 29 | + rawQuery: '', |
| 30 | + rawUrl: '', |
| 31 | + }; |
| 32 | + |
| 33 | + const baseContext: HandlerContext = { |
| 34 | + callbackWaitsForEmptyEventLoop: true, |
| 35 | + functionName: 'toggleAvatar', |
| 36 | + functionVersion: '1', |
| 37 | + invokedFunctionArn: '', |
| 38 | + memoryLimitInMB: '128', |
| 39 | + awsRequestId: '', |
| 40 | + logGroupName: '', |
| 41 | + logStreamName: '', |
| 42 | + getRemainingTimeInMillis: () => 0, |
| 43 | + done: () => {}, |
| 44 | + fail: () => {}, |
| 45 | + succeed: () => {}, |
| 46 | + }; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + jest.clearAllMocks(); |
| 50 | + // In Jest 26, fulfilling a complex interface like Collection without casting is impossible. |
| 51 | + // We use a single centralized cast here to satisfy the requirement as much as possible. |
| 52 | + const mocked = getCollection as unknown as jest.Mock; |
| 53 | + mocked.mockReturnValue(collectionMock); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return email_verified in the response', async () => { |
| 57 | + const mockId = new ObjectId(); |
| 58 | + const mockUser = { |
| 59 | + _id: mockId, |
| 60 | + email: 'test@example.com', |
| 61 | + auth0Id: 'google-oauth2|123', |
| 62 | + }; |
| 63 | + |
| 64 | + collectionMock.findOne.mockResolvedValue(mockUser); |
| 65 | + collectionMock.findOneAndUpdate.mockResolvedValue(mockUser); |
| 66 | + |
| 67 | + const event: HandlerEventWithBody<ToggleAvatarRequest> = { |
| 68 | + ...baseEvent, |
| 69 | + parsedBody: { useGravatar: true }, |
| 70 | + }; |
| 71 | + |
| 72 | + const context: AuthContext<ExtendedAuthUser> = { |
| 73 | + ...baseContext, |
| 74 | + user: { |
| 75 | + auth0Id: 'google-oauth2|123', |
| 76 | + email_verified: true, |
| 77 | + picture: 'https://google.com/pic.jpg', |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + const response = await toggleAvatarHandler(event, context); |
| 82 | + const body = JSON.parse(response.body || '{}'); |
| 83 | + |
| 84 | + expect(response.statusCode).toBe(200); |
| 85 | + expect(body.success).toBe(true); |
| 86 | + expect(body.data).toMatchObject({ |
| 87 | + email_verified: true, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return false for email_verified if it is false in context', async () => { |
| 92 | + const mockId = new ObjectId(); |
| 93 | + const mockUser = { |
| 94 | + _id: mockId, |
| 95 | + email: 'test@example.com', |
| 96 | + auth0Id: 'google-oauth2|123', |
| 97 | + }; |
| 98 | + |
| 99 | + collectionMock.findOne.mockResolvedValue(mockUser); |
| 100 | + collectionMock.findOneAndUpdate.mockResolvedValue(mockUser); |
| 101 | + |
| 102 | + const event: HandlerEventWithBody<ToggleAvatarRequest> = { |
| 103 | + ...baseEvent, |
| 104 | + parsedBody: { useGravatar: true }, |
| 105 | + }; |
| 106 | + |
| 107 | + const context: AuthContext<ExtendedAuthUser> = { |
| 108 | + ...baseContext, |
| 109 | + user: { |
| 110 | + auth0Id: 'google-oauth2|123', |
| 111 | + email_verified: false, |
| 112 | + picture: 'https://google.com/pic.jpg', |
| 113 | + }, |
| 114 | + }; |
| 115 | + |
| 116 | + const response = await toggleAvatarHandler(event, context); |
| 117 | + const body = JSON.parse(response.body || '{}'); |
| 118 | + |
| 119 | + expect(body.data.email_verified).toBe(false); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return undefined for email_verified if it is missing in context', async () => { |
| 123 | + const mockId = new ObjectId(); |
| 124 | + const mockUser = { |
| 125 | + _id: mockId, |
| 126 | + email: 'test@example.com', |
| 127 | + auth0Id: 'google-oauth2|123', |
| 128 | + }; |
| 129 | + |
| 130 | + collectionMock.findOne.mockResolvedValue(mockUser); |
| 131 | + collectionMock.findOneAndUpdate.mockResolvedValue(mockUser); |
| 132 | + |
| 133 | + const event: HandlerEventWithBody<ToggleAvatarRequest> = { |
| 134 | + ...baseEvent, |
| 135 | + parsedBody: { useGravatar: true }, |
| 136 | + }; |
| 137 | + |
| 138 | + const context: AuthContext<ExtendedAuthUser> = { |
| 139 | + ...baseContext, |
| 140 | + user: { |
| 141 | + auth0Id: 'google-oauth2|123', |
| 142 | + picture: 'https://google.com/pic.jpg', |
| 143 | + }, |
| 144 | + }; |
| 145 | + |
| 146 | + const response = await toggleAvatarHandler(event, context); |
| 147 | + const body = JSON.parse(response.body || '{}'); |
| 148 | + |
| 149 | + expect(body.data.email_verified).toBeUndefined(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments