2026-03-06 19:21:45 +00:00
|
|
|
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);
|
2026-03-06 19:28:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GET /contacts should return 401 for unauthorized access', async () => {
|
|
|
|
|
const response = await app.inject({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: '/contacts'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(response.statusCode, 401);
|
2026-03-06 19:21:45 +00:00
|
|
|
});
|