63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
const request = require('supertest');
|
|
const app = require('../../backend/src/index');
|
|
|
|
describe('Auth API', () => {
|
|
const testUser = {
|
|
username: 'testuser' + Date.now(),
|
|
password: 'test123',
|
|
email: 'test@example.com'
|
|
};
|
|
|
|
describe('POST /api/auth/register', () => {
|
|
it('should register a new user', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/register')
|
|
.send(testUser);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toHaveProperty('token');
|
|
expect(response.body).toHaveProperty('userId');
|
|
});
|
|
|
|
it('should not register duplicate username', async () => {
|
|
// First registration
|
|
await request(app)
|
|
.post('/api/auth/register')
|
|
.send(testUser);
|
|
|
|
// Duplicate registration
|
|
const response = await request(app)
|
|
.post('/api/auth/register')
|
|
.send(testUser);
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(response.body).toHaveProperty('error');
|
|
});
|
|
});
|
|
|
|
describe('POST /api/auth/login', () => {
|
|
it('should login with valid credentials', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/login')
|
|
.send({
|
|
username: testUser.username,
|
|
password: testUser.password
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toHaveProperty('token');
|
|
});
|
|
|
|
it('should fail with invalid credentials', async () => {
|
|
const response = await request(app)
|
|
.post('/api/auth/login')
|
|
.send({
|
|
username: testUser.username,
|
|
password: 'wrongpassword'
|
|
});
|
|
|
|
expect(response.status).toBe(401);
|
|
});
|
|
});
|
|
});
|