|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +import { getServerSession } from 'next-auth'; |
| 5 | +import { resetWithChallengePrismaMock } from '@/__mocks__/prisma'; |
| 6 | +import { POST } from '@/app/api/challenge/claim/route'; |
| 7 | +import { claimChallengeReward } from '@/services/challenge/challenge-claim'; |
| 8 | +import { canClaimChallenge } from '@/services/challenge/challenge-status'; |
| 9 | + |
| 10 | +// Mock dependencies |
| 11 | +jest.mock('next-auth'); |
| 12 | +jest.mock('@/services/challenge/challenge-status'); |
| 13 | +jest.mock('@/services/challenge/challenge-claim'); |
| 14 | +jest.mock('@/lib/prisma', () => { |
| 15 | + const { getPrismaMock } = require('@/__mocks__/prisma'); |
| 16 | + return { |
| 17 | + get prisma() { |
| 18 | + return getPrismaMock(); |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +const mockGetServerSession = getServerSession as jest.MockedFunction< |
| 24 | + typeof getServerSession |
| 25 | +>; |
| 26 | +const mockCanClaimChallenge = canClaimChallenge as jest.MockedFunction< |
| 27 | + typeof canClaimChallenge |
| 28 | +>; |
| 29 | +const mockClaimChallengeReward = claimChallengeReward as jest.MockedFunction< |
| 30 | + typeof claimChallengeReward |
| 31 | +>; |
| 32 | + |
| 33 | +describe('/api/challenge/claim', () => { |
| 34 | + let mockPrisma: any; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + jest.clearAllMocks(); |
| 38 | + mockPrisma = resetWithChallengePrismaMock(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('POST', () => { |
| 42 | + it('should return 401 when not authenticated', async () => { |
| 43 | + mockGetServerSession.mockResolvedValue(null); |
| 44 | + |
| 45 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 46 | + method: 'POST', |
| 47 | + body: JSON.stringify({ challengeId: '1' }), |
| 48 | + }); |
| 49 | + |
| 50 | + const response = await POST(request); |
| 51 | + const data = await response.json(); |
| 52 | + |
| 53 | + expect(response.status).toBe(401); |
| 54 | + expect(data.message).toBe('Unauthorized'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return 400 when challengeId is missing', async () => { |
| 58 | + mockGetServerSession.mockResolvedValue({ |
| 59 | + user: { id: '1' }, |
| 60 | + }); |
| 61 | + |
| 62 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 63 | + method: 'POST', |
| 64 | + body: JSON.stringify({}), |
| 65 | + }); |
| 66 | + |
| 67 | + const response = await POST(request); |
| 68 | + const data = await response.json(); |
| 69 | + |
| 70 | + expect(response.status).toBe(400); |
| 71 | + expect(data.message).toBe('Challenge ID is required'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should successfully claim challenge reward', async () => { |
| 75 | + mockGetServerSession.mockResolvedValue({ |
| 76 | + user: { id: '1' }, |
| 77 | + }); |
| 78 | + |
| 79 | + mockPrisma.$transaction.mockImplementation(async (callback: any) => { |
| 80 | + const mockTx = mockPrisma; |
| 81 | + return await callback(mockTx); |
| 82 | + }); |
| 83 | + |
| 84 | + mockCanClaimChallenge.mockResolvedValue({ canClaim: true }); |
| 85 | + mockClaimChallengeReward.mockResolvedValue({ |
| 86 | + success: true, |
| 87 | + message: 'Reward claimed successfully', |
| 88 | + transactionId: BigInt(123), |
| 89 | + }); |
| 90 | + |
| 91 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 92 | + method: 'POST', |
| 93 | + body: JSON.stringify({ challengeId: '1' }), |
| 94 | + }); |
| 95 | + |
| 96 | + const response = await POST(request); |
| 97 | + const data = await response.json(); |
| 98 | + |
| 99 | + expect(response.status).toBe(200); |
| 100 | + expect(data.message).toBe('Reward claimed successfully'); |
| 101 | + expect(data.transactionId).toBe('123'); |
| 102 | + |
| 103 | + expect(mockCanClaimChallenge).toHaveBeenCalledWith( |
| 104 | + BigInt(1), |
| 105 | + BigInt(1), |
| 106 | + mockPrisma |
| 107 | + ); |
| 108 | + expect(mockClaimChallengeReward).toHaveBeenCalledWith( |
| 109 | + { challengeId: BigInt(1), userId: BigInt(1) }, |
| 110 | + mockPrisma |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should return 500 when cannot claim challenge', async () => { |
| 115 | + mockGetServerSession.mockResolvedValue({ |
| 116 | + user: { id: '1' }, |
| 117 | + }); |
| 118 | + |
| 119 | + mockPrisma.$transaction.mockImplementation(async (callback: any) => { |
| 120 | + const mockTx = mockPrisma; |
| 121 | + return await callback(mockTx); |
| 122 | + }); |
| 123 | + |
| 124 | + mockCanClaimChallenge.mockResolvedValue({ |
| 125 | + canClaim: false, |
| 126 | + reason: 'Already claimed', |
| 127 | + }); |
| 128 | + |
| 129 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 130 | + method: 'POST', |
| 131 | + body: JSON.stringify({ challengeId: '1' }), |
| 132 | + }); |
| 133 | + |
| 134 | + const response = await POST(request); |
| 135 | + const data = await response.json(); |
| 136 | + |
| 137 | + expect(response.status).toBe(500); |
| 138 | + expect(data.message).toBe('Already claimed'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return 400 when claim service returns failure', async () => { |
| 142 | + mockGetServerSession.mockResolvedValue({ |
| 143 | + user: { id: '1' }, |
| 144 | + }); |
| 145 | + |
| 146 | + mockPrisma.$transaction.mockImplementation(async (callback: any) => { |
| 147 | + const mockTx = mockPrisma; |
| 148 | + return await callback(mockTx); |
| 149 | + }); |
| 150 | + |
| 151 | + mockCanClaimChallenge.mockResolvedValue({ canClaim: true }); |
| 152 | + mockClaimChallengeReward.mockResolvedValue({ |
| 153 | + success: false, |
| 154 | + message: 'ISA account not found', |
| 155 | + }); |
| 156 | + |
| 157 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 158 | + method: 'POST', |
| 159 | + body: JSON.stringify({ challengeId: '1' }), |
| 160 | + }); |
| 161 | + |
| 162 | + const response = await POST(request); |
| 163 | + const data = await response.json(); |
| 164 | + |
| 165 | + expect(response.status).toBe(400); |
| 166 | + expect(data.message).toBe('ISA account not found'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should handle unexpected errors', async () => { |
| 170 | + mockGetServerSession.mockResolvedValue({ |
| 171 | + user: { id: '1' }, |
| 172 | + }); |
| 173 | + |
| 174 | + mockPrisma.$transaction.mockRejectedValue(new Error('Database error')); |
| 175 | + |
| 176 | + const request = new Request('http://localhost/api/challenge/claim', { |
| 177 | + method: 'POST', |
| 178 | + body: JSON.stringify({ challengeId: '1' }), |
| 179 | + }); |
| 180 | + |
| 181 | + const response = await POST(request); |
| 182 | + const data = await response.json(); |
| 183 | + |
| 184 | + expect(response.status).toBe(500); |
| 185 | + expect(data.message).toBe('Database error'); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments