Initial commit
This commit is contained in:
185
backend/src/tests/auth.test.js
Normal file
185
backend/src/tests/auth.test.js
Normal file
@@ -0,0 +1,185 @@
|
||||
const request = require('supertest');
|
||||
const app = require('../server');
|
||||
const pool = require('../database/connection');
|
||||
|
||||
describe('Authentication', () => {
|
||||
beforeEach(async () => {
|
||||
// Clean up database before each test
|
||||
await pool.query('DELETE FROM users WHERE email LIKE $1', ['test%']);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.end();
|
||||
});
|
||||
|
||||
describe('POST /api/auth/register', () => {
|
||||
it('should register a new user successfully', async () => {
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
role: 'candidate'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData)
|
||||
.expect(201);
|
||||
|
||||
expect(response.body).toHaveProperty('message', 'User created successfully');
|
||||
expect(response.body).toHaveProperty('token');
|
||||
expect(response.body).toHaveProperty('user');
|
||||
expect(response.body.user.email).toBe(userData.email);
|
||||
});
|
||||
|
||||
it('should return error for duplicate email', async () => {
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
role: 'candidate'
|
||||
};
|
||||
|
||||
// Register first user
|
||||
await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData);
|
||||
|
||||
// Try to register with same email
|
||||
const response = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData)
|
||||
.expect(400);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'User already exists');
|
||||
});
|
||||
|
||||
it('should return error for invalid role', async () => {
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
role: 'invalid_role'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData)
|
||||
.expect(400);
|
||||
|
||||
expect(response.body).toHaveProperty('errors');
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/auth/login', () => {
|
||||
beforeEach(async () => {
|
||||
// Create a test user
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
role: 'candidate'
|
||||
};
|
||||
|
||||
await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData);
|
||||
});
|
||||
|
||||
it('should login with valid credentials', async () => {
|
||||
const loginData = {
|
||||
email: 'test@example.com',
|
||||
password: 'password123'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/auth/login')
|
||||
.send(loginData)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('message', 'Login successful');
|
||||
expect(response.body).toHaveProperty('token');
|
||||
expect(response.body).toHaveProperty('user');
|
||||
});
|
||||
|
||||
it('should return error for invalid credentials', async () => {
|
||||
const loginData = {
|
||||
email: 'test@example.com',
|
||||
password: 'wrongpassword'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/auth/login')
|
||||
.send(loginData)
|
||||
.expect(401);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'Invalid credentials');
|
||||
});
|
||||
|
||||
it('should return error for non-existent user', async () => {
|
||||
const loginData = {
|
||||
email: 'nonexistent@example.com',
|
||||
password: 'password123'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/auth/login')
|
||||
.send(loginData)
|
||||
.expect(401);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'Invalid credentials');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/auth/me', () => {
|
||||
let token;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create a test user and get token
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
role: 'candidate'
|
||||
};
|
||||
|
||||
const registerResponse = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData);
|
||||
|
||||
token = registerResponse.body.token;
|
||||
});
|
||||
|
||||
it('should return user data with valid token', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/auth/me')
|
||||
.set('Authorization', `Bearer ${token}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('user');
|
||||
expect(response.body.user.email).toBe('test@example.com');
|
||||
});
|
||||
|
||||
it('should return error without token', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/auth/me')
|
||||
.expect(401);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'Access token required');
|
||||
});
|
||||
|
||||
it('should return error with invalid token', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/auth/me')
|
||||
.set('Authorization', 'Bearer invalid_token')
|
||||
.expect(403);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'Invalid or expired token');
|
||||
});
|
||||
});
|
||||
});
|
||||
252
backend/src/tests/jobs.test.js
Normal file
252
backend/src/tests/jobs.test.js
Normal file
@@ -0,0 +1,252 @@
|
||||
const request = require('supertest');
|
||||
const app = require('../server');
|
||||
const pool = require('../database/connection');
|
||||
|
||||
describe('Jobs API', () => {
|
||||
let authToken;
|
||||
let employerId;
|
||||
|
||||
beforeAll(async () => {
|
||||
// Create test user and get auth token
|
||||
const userData = {
|
||||
firstName: 'Test',
|
||||
lastName: 'Employer',
|
||||
email: 'employer@test.com',
|
||||
password: 'password123',
|
||||
role: 'employer'
|
||||
};
|
||||
|
||||
const registerResponse = await request(app)
|
||||
.post('/api/auth/register')
|
||||
.send(userData);
|
||||
|
||||
authToken = registerResponse.body.token;
|
||||
|
||||
// Create employer profile
|
||||
const employerData = {
|
||||
companyName: 'Test Company',
|
||||
industry: 'Technology',
|
||||
companySize: '50-200',
|
||||
website: 'https://testcompany.com',
|
||||
description: 'A test company',
|
||||
address: '123 Test St',
|
||||
phone: '+1-555-0123'
|
||||
};
|
||||
|
||||
const employerResponse = await request(app)
|
||||
.post('/api/employers')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(employerData);
|
||||
|
||||
employerId = employerResponse.body.employer.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.end();
|
||||
});
|
||||
|
||||
describe('POST /api/jobs', () => {
|
||||
it('should create a new job posting', async () => {
|
||||
const jobData = {
|
||||
title: 'Senior Developer',
|
||||
description: 'We are looking for a senior developer',
|
||||
requirements: ['5+ years experience', 'JavaScript knowledge'],
|
||||
responsibilities: ['Develop applications', 'Code reviews'],
|
||||
location: 'San Francisco, CA',
|
||||
employmentType: 'full-time',
|
||||
salaryMin: 100000,
|
||||
salaryMax: 150000,
|
||||
remoteAllowed: true,
|
||||
experienceLevel: 'senior',
|
||||
skillsRequired: ['JavaScript', 'React', 'Node.js'],
|
||||
benefits: ['Health insurance', '401k']
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData)
|
||||
.expect(201);
|
||||
|
||||
expect(response.body).toHaveProperty('message', 'Job posting created successfully');
|
||||
expect(response.body).toHaveProperty('job');
|
||||
expect(response.body.job.title).toBe(jobData.title);
|
||||
});
|
||||
|
||||
it('should return error for missing required fields', async () => {
|
||||
const jobData = {
|
||||
title: 'Senior Developer'
|
||||
// Missing required fields
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData)
|
||||
.expect(400);
|
||||
|
||||
expect(response.body).toHaveProperty('errors');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/jobs', () => {
|
||||
beforeEach(async () => {
|
||||
// Create a test job
|
||||
const jobData = {
|
||||
title: 'Test Job',
|
||||
description: 'A test job description',
|
||||
requirements: ['Test requirement'],
|
||||
responsibilities: ['Test responsibility'],
|
||||
location: 'Test Location',
|
||||
employmentType: 'full-time'
|
||||
};
|
||||
|
||||
await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData);
|
||||
});
|
||||
|
||||
it('should return list of jobs', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('jobs');
|
||||
expect(response.body).toHaveProperty('pagination');
|
||||
expect(Array.isArray(response.body.jobs)).toBe(true);
|
||||
});
|
||||
|
||||
it('should filter jobs by search term', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/jobs?search=Test')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.jobs.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should filter jobs by location', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/jobs?location=Test')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.jobs.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/jobs/:id', () => {
|
||||
let jobId;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create a test job
|
||||
const jobData = {
|
||||
title: 'Test Job for Details',
|
||||
description: 'A test job description',
|
||||
requirements: ['Test requirement'],
|
||||
responsibilities: ['Test responsibility'],
|
||||
location: 'Test Location',
|
||||
employmentType: 'full-time'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData);
|
||||
|
||||
jobId = response.body.job.id;
|
||||
});
|
||||
|
||||
it('should return job details', async () => {
|
||||
const response = await request(app)
|
||||
.get(`/api/jobs/${jobId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('id', jobId);
|
||||
expect(response.body).toHaveProperty('title', 'Test Job for Details');
|
||||
});
|
||||
|
||||
it('should return 404 for non-existent job', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/jobs/00000000-0000-0000-0000-000000000000')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(404);
|
||||
|
||||
expect(response.body).toHaveProperty('error', 'Job not found');
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/jobs/:id', () => {
|
||||
let jobId;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create a test job
|
||||
const jobData = {
|
||||
title: 'Test Job for Update',
|
||||
description: 'A test job description',
|
||||
requirements: ['Test requirement'],
|
||||
responsibilities: ['Test responsibility'],
|
||||
location: 'Test Location',
|
||||
employmentType: 'full-time'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData);
|
||||
|
||||
jobId = response.body.job.id;
|
||||
});
|
||||
|
||||
it('should update job successfully', async () => {
|
||||
const updateData = {
|
||||
title: 'Updated Test Job',
|
||||
description: 'Updated description'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.put(`/api/jobs/${jobId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(updateData)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('message', 'Job posting updated successfully');
|
||||
expect(response.body.job.title).toBe('Updated Test Job');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/jobs/:id', () => {
|
||||
let jobId;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create a test job
|
||||
const jobData = {
|
||||
title: 'Test Job for Delete',
|
||||
description: 'A test job description',
|
||||
requirements: ['Test requirement'],
|
||||
responsibilities: ['Test responsibility'],
|
||||
location: 'Test Location',
|
||||
employmentType: 'full-time'
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/jobs')
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send(jobData);
|
||||
|
||||
jobId = response.body.job.id;
|
||||
});
|
||||
|
||||
it('should delete job successfully', async () => {
|
||||
const response = await request(app)
|
||||
.delete(`/api/jobs/${jobId}`)
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(response.body).toHaveProperty('message', 'Job posting deleted successfully');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user