|
| 1 | +import { GET } from '@api/activity-logs/[id]/route' |
| 2 | +import { ActivityLogService } from '@api/activity-logs/services/activity-log.service' |
| 3 | +import APIError from '@api/core/exceptions/api' |
| 4 | +import authenticate from '@api/core/utils/authenticate' |
| 5 | +import User from '@api/core/models/User.model' |
| 6 | +import { buildNextRequest } from '@api/tests/__utils__/testUtils' |
| 7 | +import httpStatus from 'http-status' |
| 8 | +import { z } from 'zod' |
| 9 | + |
| 10 | +jest.mock('@api/activity-logs/services/activity-log.service', () => ({ |
| 11 | + ActivityLogService: jest.fn().mockImplementation(() => ({ |
| 12 | + get: (taskId: string) => { |
| 13 | + z.string().uuid().parse(taskId) |
| 14 | + return Promise.resolve([]) |
| 15 | + }, |
| 16 | + })), |
| 17 | +})) |
| 18 | + |
| 19 | +jest.mock('@api/core/utils/authenticate', () => jest.fn()) |
| 20 | + |
| 21 | +describe('activity log route', () => { |
| 22 | + const mockUser = new User('iu-token', { |
| 23 | + workspaceId: 'workspace-id', |
| 24 | + internalUserId: 'internal-user-id', |
| 25 | + }) |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks() |
| 29 | + jest.spyOn(console, 'error').mockImplementation() |
| 30 | + jest.mocked(authenticate).mockResolvedValue(mockUser) |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.restoreAllMocks() |
| 35 | + }) |
| 36 | + |
| 37 | + it('returns a quiet 401 response when token is missing', async () => { |
| 38 | + jest.mocked(authenticate).mockRejectedValue(new APIError(httpStatus.UNAUTHORIZED, 'Please provide a valid token')) |
| 39 | + |
| 40 | + const req = buildNextRequest('/api/activity-logs/activity-id') |
| 41 | + const response = await GET(req, { params: Promise.resolve({ id: 'activity-id' }) }) |
| 42 | + const body = await response.json() |
| 43 | + |
| 44 | + expect(response.status).toBe(httpStatus.UNAUTHORIZED) |
| 45 | + expect(body.error).toBe('Please provide a valid token') |
| 46 | + expect(console.error).not.toHaveBeenCalled() |
| 47 | + }) |
| 48 | + |
| 49 | + it('returns a quiet 422 response when task id is not a valid UUID', async () => { |
| 50 | + const req = buildNextRequest('/api/activity-logs/not-a-uuid?token=iu-token') |
| 51 | + const response = await GET(req, { params: Promise.resolve({ id: 'not-a-uuid' }) }) |
| 52 | + const body = await response.json() |
| 53 | + |
| 54 | + expect(response.status).toBe(httpStatus.UNPROCESSABLE_ENTITY) |
| 55 | + expect(body.error).toContain('Invalid uuid') |
| 56 | + expect(console.error).not.toHaveBeenCalled() |
| 57 | + expect(ActivityLogService).toHaveBeenCalledWith(mockUser) |
| 58 | + }) |
| 59 | +}) |
0 commit comments