auto(agent): Improve auth route tests with node:test and assert
This commit is contained in:
parent
9d5f08a885
commit
6c82af0d99
2 changed files with 78 additions and 14 deletions
|
|
@ -1,12 +1,30 @@
|
|||
const test = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const { authenticate } = require('../routes/auth');
|
||||
const { pool } = require('../db/connection.js');
|
||||
|
||||
test('authenticate should reject invalid credentials', async () => {
|
||||
// Mock the database pool
|
||||
const mockPool = {
|
||||
query: (sql, params) => {
|
||||
if (sql.includes('INSERT INTO users')) {
|
||||
return [{ insertId: 1 }];
|
||||
}
|
||||
if (sql.includes('SELECT id, email, password_hash FROM users')) {
|
||||
return [[{ id: 1, email: 'test@example.com', password_hash: '$2a$12$...' }]];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// Replace the actual pool with mock
|
||||
const originalPool = pool;
|
||||
pool.query = mockPool.query;
|
||||
|
||||
test('register should handle valid input', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
username: 'invalid',
|
||||
password: 'wrong'
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
displayName: 'Test User'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
|
|
@ -19,16 +37,16 @@ test('authenticate should reject invalid credentials', async () => {
|
|||
}
|
||||
};
|
||||
|
||||
await authenticate(req, res);
|
||||
assert.strictEqual(res.statusCode, 401);
|
||||
assert.deepStrictEqual(res.data, { error: 'Invalid credentials' });
|
||||
await require('../routes/auth').default.post('/register', req, res);
|
||||
assert.strictEqual(res.statusCode, 201);
|
||||
assert.ok(res.data.token);
|
||||
});
|
||||
|
||||
test('authenticate should accept valid credentials', async () => {
|
||||
test('login should handle valid credentials', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
username: 'admin',
|
||||
password: 'password'
|
||||
email: 'test@example.com',
|
||||
password: 'password123'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
|
|
@ -41,7 +59,53 @@ test('authenticate should accept valid credentials', async () => {
|
|||
}
|
||||
};
|
||||
|
||||
await authenticate(req, res);
|
||||
await require('../routes/auth').default.post('/login', req, res);
|
||||
assert.strictEqual(res.statusCode, 200);
|
||||
assert.ok(res.data.token);
|
||||
});
|
||||
});
|
||||
|
||||
test('register should reject invalid email', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
email: 'invalid-email',
|
||||
password: 'password123',
|
||||
displayName: 'Test User'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
status: (code) => {
|
||||
res.statusCode = code;
|
||||
return res;
|
||||
},
|
||||
json: (data) => {
|
||||
res.data = data;
|
||||
}
|
||||
};
|
||||
|
||||
await require('../routes/auth').default.post('/register', req, res);
|
||||
assert.strictEqual(res.statusCode, 400);
|
||||
});
|
||||
|
||||
test('login should reject invalid credentials', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
email: 'nonexistent@example.com',
|
||||
password: 'wrongpassword'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
status: (code) => {
|
||||
res.statusCode = code;
|
||||
return res;
|
||||
},
|
||||
json: (data) => {
|
||||
res.data = data;
|
||||
}
|
||||
};
|
||||
|
||||
await require('../routes/auth').default.post('/login', req, res);
|
||||
assert.strictEqual(res.statusCode, 401);
|
||||
});
|
||||
|
||||
// Restore the original pool
|
||||
pool.query = originalPool;
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
LAST_ROUTE=addresses.js
|
||||
UPDATED_AT=2026-03-06T19:52:37Z
|
||||
LAST_ROUTE=auth.js
|
||||
UPDATED_AT=2026-03-06T19:53:13Z
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue