Skip to content
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
63 changes: 61 additions & 2 deletions src/lib/__tests__/apiUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { handleErrorResponse, getAuthenticatedUser } from '../apiUtils';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils';
import { RateLimitError } from '../types';
import { NextResponse } from 'next/server';
import { getServerSession } from "next-auth";

Expand Down Expand Up @@ -56,6 +57,64 @@ describe('apiUtils', () => {
});
});


describe('handleRateLimit', () => {
afterEach(() => {
vi.useRealTimers();
});

it('should throw RateLimitError using timestamp from X-RateLimit-Reset header', () => {
const resetTimestamp = Math.floor(Date.now() / 1000) + 1000;
const res = new Response(null, {
headers: { 'X-RateLimit-Reset': resetTimestamp.toString() }
});

try {
handleRateLimit(res);
expect.fail('Should have thrown RateLimitError');
} catch (error) {
expect(error).toBeInstanceOf(RateLimitError);
expect((error as RateLimitError).resetAt.getTime()).toBe(resetTimestamp * 1000);
}
});

it('should fall back to 1 hour from now if header is missing', () => {
vi.useFakeTimers();
const now = new Date('2024-01-01T12:00:00Z');
vi.setSystemTime(now);

const res = new Response(null);

try {
handleRateLimit(res);
expect.fail('Should have thrown RateLimitError');
} catch (error) {
expect(error).toBeInstanceOf(RateLimitError);
const expectedResetTimestamp = Math.floor(now.getTime() / 1000) + 3600;
expect((error as RateLimitError).resetAt.getTime()).toBe(expectedResetTimestamp * 1000);
}
});

it('should fall back to 1 hour from now if header is invalid', () => {
vi.useFakeTimers();
const now = new Date('2024-01-01T12:00:00Z');
vi.setSystemTime(now);

const res = new Response(null, {
headers: { 'X-RateLimit-Reset': 'invalid' }
});

try {
handleRateLimit(res);
expect.fail('Should have thrown RateLimitError');
} catch (error) {
expect(error).toBeInstanceOf(RateLimitError);
const expectedResetTimestamp = Math.floor(now.getTime() / 1000) + 3600;
expect((error as RateLimitError).resetAt.getTime()).toBe(expectedResetTimestamp * 1000);
}
});
});

describe('getAuthenticatedUser', () => {
it('should return user object if session is valid', async () => {
vi.mocked(getServerSession).mockResolvedValueOnce({
Expand Down
Loading