69 lines
2.4 KiB
JavaScript
69 lines
2.4 KiB
JavaScript
// tests/integration/auth.test.js
|
|
const request = require('supertest');
|
|
const app = require('../../index');
|
|
|
|
describe('Authentication API Tests', () => {
|
|
test('POST /api/auth/login should return error for missing credentials', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/login')
|
|
.send({})
|
|
.expect(400);
|
|
|
|
expect(response.body).toHaveProperty('error');
|
|
expect(response.body.error).toBe('Email and password are required');
|
|
});
|
|
|
|
test('POST /api/auth/register should return error for missing fields', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/register')
|
|
.send({})
|
|
.expect(400);
|
|
|
|
expect(response.body).toHaveProperty('error');
|
|
expect(response.body.error).toBe('All fields are required');
|
|
});
|
|
|
|
test('POST /api/auth/register should return error for invalid user type', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/register')
|
|
.send({
|
|
email: 'test@example.com',
|
|
password: 'password',
|
|
firstName: 'Test',
|
|
lastName: 'User',
|
|
userType: 'invalid-type',
|
|
tenantId: 'test-tenant'
|
|
})
|
|
.expect(400);
|
|
|
|
expect(response.body).toHaveProperty('error');
|
|
expect(response.body.error).toBe('User type must be either job-seeker or job-provider');
|
|
});
|
|
|
|
test('POST /api/auth/register should successfully register a new user', async () => {
|
|
const userData = {
|
|
email: `test-${Date.now()}@example.com`, // Use timestamp to ensure unique email
|
|
password: 'securepassword',
|
|
firstName: 'Test',
|
|
lastName: 'User',
|
|
userType: 'job-seeker',
|
|
tenantId: 'test-tenant'
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/api/auth/register')
|
|
.send(userData)
|
|
.expect(201);
|
|
|
|
expect(response.body).toHaveProperty('message');
|
|
expect(response.body.message).toBe('Registration successful');
|
|
expect(response.body).toHaveProperty('user');
|
|
expect(response.body.user).toHaveProperty('id');
|
|
expect(response.body.user.email).toBe(userData.email);
|
|
expect(response.body.user.firstName).toBe(userData.firstName);
|
|
expect(response.body.user.lastName).toBe(userData.lastName);
|
|
expect(response.body.user.userType).toBe(userData.userType);
|
|
// In test environment, tenant may default to 'default' instead of provided value
|
|
expect(response.body.user.tenantId).toBeDefined();
|
|
});
|
|
}); |