153 lines
No EOL
4.6 KiB
JavaScript
153 lines
No EOL
4.6 KiB
JavaScript
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);
|
|
});
|
|
|
|
test('GET /contacts should return 401 for unauthorized access', async () => {
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/contacts'
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 401);
|
|
});
|
|
|
|
// Test for contacts request endpoint
|
|
test('POST /contacts/request should validate dealId and targetUserId', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/request',
|
|
payload: { dealId: -1, targetUserId: 2 }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 400);
|
|
});
|
|
|
|
test('POST /contacts/respond should validate requestId and accept', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/respond',
|
|
payload: { requestId: -1, accept: 'not-a-boolean' }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 400);
|
|
});
|
|
|
|
test('GET /contacts/deal/:dealId should validate dealId', async () => {
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/contacts/deal/invalid'
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 400);
|
|
});
|
|
|
|
// Additional test for contacts request endpoint with valid data
|
|
test('POST /contacts/request should handle valid request data', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/request',
|
|
payload: { dealId: 1, targetUserId: 2 }
|
|
});
|
|
|
|
// This test will fail if the route is not properly implemented or if there's no valid deal
|
|
assert.strictEqual(response.statusCode, 404); // Expected since we don't have a real deal in test context
|
|
});
|
|
|
|
// Additional test for contacts respond endpoint with valid data
|
|
test('POST /contacts/respond should handle valid response data', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/respond',
|
|
payload: { requestId: 1, accept: true }
|
|
});
|
|
|
|
// This test will fail if the route is not properly implemented or if there's no valid request in test context
|
|
assert.strictEqual(response.statusCode, 404); // Expected since we don't have a real request in test context
|
|
});
|
|
|
|
// Test for contacts request endpoint with proper validation
|
|
test('POST /contacts/request should validate dealId and targetUserId (zod)', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/request',
|
|
payload: { dealId: 'not-a-number', targetUserId: 2 }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 400);
|
|
});
|
|
|
|
// Test for contacts respond endpoint with proper validation
|
|
test('POST /contacts/respond should validate requestId and accept (zod)', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/respond',
|
|
payload: { requestId: 'not-a-number', accept: true }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 400);
|
|
});
|
|
|
|
// Test for contacts request endpoint with valid data and proper error handling
|
|
test('POST /contacts/request should handle forbidden access', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/request',
|
|
payload: { dealId: 1, targetUserId: 2 }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 403); // Forbidden due to no valid user context
|
|
});
|
|
|
|
// Test for contacts respond endpoint with valid data and proper error handling
|
|
test('POST /contacts/respond should handle forbidden access', async () => {
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/contacts/respond',
|
|
payload: { requestId: 1, accept: true }
|
|
});
|
|
|
|
assert.strictEqual(response.statusCode, 403); // Forbidden due to no valid user context
|
|
}); |