auto(agent): Add tests for contacts route
This commit is contained in:
parent
6d9bf4c461
commit
54d5b5d876
10 changed files with 115 additions and 119 deletions
47
backend/src/__tests__/auth.test.js
Normal file
47
backend/src/__tests__/auth.test.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
const test = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const { authenticate } = require('../routes/auth');
|
||||
|
||||
test('authenticate should reject invalid credentials', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
username: 'invalid',
|
||||
password: 'wrong'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
status: (code) => {
|
||||
res.statusCode = code;
|
||||
return res;
|
||||
},
|
||||
json: (data) => {
|
||||
res.data = data;
|
||||
}
|
||||
};
|
||||
|
||||
await authenticate(req, res);
|
||||
assert.strictEqual(res.statusCode, 401);
|
||||
assert.deepStrictEqual(res.data, { error: 'Invalid credentials' });
|
||||
});
|
||||
|
||||
test('authenticate should accept valid credentials', async () => {
|
||||
const req = {
|
||||
body: {
|
||||
username: 'admin',
|
||||
password: 'password'
|
||||
}
|
||||
};
|
||||
const res = {
|
||||
status: (code) => {
|
||||
res.statusCode = code;
|
||||
return res;
|
||||
},
|
||||
json: (data) => {
|
||||
res.data = data;
|
||||
}
|
||||
};
|
||||
|
||||
await authenticate(req, res);
|
||||
assert.strictEqual(res.statusCode, 200);
|
||||
assert.ok(res.data.token);
|
||||
});
|
||||
46
backend/src/__tests__/contacts.test.js
Normal file
46
backend/src/__tests__/contacts.test.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { test } from 'node:test';
|
||||
import * as assert from 'node:assert';
|
||||
import { app } from '../app.js';
|
||||
|
||||
test('GET /contacts should return contacts', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/contacts'
|
||||
});
|
||||
|
||||
assert.strictEqual(response.statusCode, 200);
|
||||
assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8');
|
||||
});
|
||||
|
||||
test('POST /contacts should create a new contact', async () => {
|
||||
const newContact = {
|
||||
name: 'John Doe',
|
||||
email: 'john.doe@example.com',
|
||||
phone: '123-456-7890'
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/contacts',
|
||||
payload: newContact
|
||||
});
|
||||
|
||||
assert.strictEqual(response.statusCode, 201);
|
||||
assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8');
|
||||
});
|
||||
|
||||
test('POST /contacts should validate contact data', async () => {
|
||||
const invalidContact = {
|
||||
name: '',
|
||||
email: 'invalid-email',
|
||||
phone: ''
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/contacts',
|
||||
payload: invalidContact
|
||||
});
|
||||
|
||||
assert.strictEqual(response.statusCode, 400);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue