Add websitebuilder app
This commit is contained in:
167
src/app/api/auth/register/__tests__/route.test.ts
Normal file
167
src/app/api/auth/register/__tests__/route.test.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import { registerUser } from '@/services/auth.service';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { POST } from '../route';
|
||||
|
||||
// Mock the auth service
|
||||
vi.mock('@/services/auth.service', () => ({
|
||||
registerUser: vi.fn(),
|
||||
}));
|
||||
|
||||
describe('POST /api/auth/register', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should register a new user successfully', async () => {
|
||||
const mockUser = {
|
||||
id: 'user-123',
|
||||
email: 'test@example.com',
|
||||
fullName: 'Test User',
|
||||
role: 'user',
|
||||
emailVerified: false,
|
||||
};
|
||||
|
||||
vi.mocked(registerUser).mockResolvedValue({
|
||||
success: true,
|
||||
user: mockUser,
|
||||
});
|
||||
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'test@example.com',
|
||||
password: 'TestPassword123!',
|
||||
fullName: 'Test User',
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(data.success).toBe(true);
|
||||
expect(data.user).toEqual(mockUser);
|
||||
expect(data.message).toContain('Registration successful');
|
||||
expect(registerUser).toHaveBeenCalledWith({
|
||||
email: 'test@example.com',
|
||||
password: 'TestPassword123!',
|
||||
fullName: 'Test User',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return validation error for invalid email', async () => {
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'invalid-email',
|
||||
password: 'TestPassword123!',
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(data.error).toBe('Validation failed');
|
||||
expect(data.details).toBeDefined();
|
||||
expect(registerUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return validation error for short password', async () => {
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'test@example.com',
|
||||
password: 'short',
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(data.error).toBe('Validation failed');
|
||||
expect(data.details).toBeDefined();
|
||||
expect(registerUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when registration fails', async () => {
|
||||
vi.mocked(registerUser).mockResolvedValue({
|
||||
success: false,
|
||||
error: 'Email already registered',
|
||||
});
|
||||
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'test@example.com',
|
||||
password: 'TestPassword123!',
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(data.error).toBe('Email already registered');
|
||||
});
|
||||
|
||||
it('should handle missing required fields', async () => {
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'test@example.com',
|
||||
// password missing
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(data.error).toBe('Validation failed');
|
||||
expect(registerUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle invalid JSON', async () => {
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: 'invalid json',
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
expect(data.error).toBe('Internal server error');
|
||||
});
|
||||
|
||||
it('should allow registration without fullName', async () => {
|
||||
const mockUser = {
|
||||
id: 'user-123',
|
||||
email: 'test@example.com',
|
||||
role: 'user',
|
||||
emailVerified: false,
|
||||
};
|
||||
|
||||
vi.mocked(registerUser).mockResolvedValue({
|
||||
success: true,
|
||||
user: mockUser,
|
||||
});
|
||||
|
||||
const request = new Request('http://localhost:3000/api/auth/register', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
email: 'test@example.com',
|
||||
password: 'TestPassword123!',
|
||||
}),
|
||||
});
|
||||
|
||||
const response = await POST(request as any);
|
||||
const data = await response.json();
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(data.success).toBe(true);
|
||||
expect(data.user).toEqual(mockUser);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user