Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #8

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/middleware/ai/validateRequest.middleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import express from 'express';
import request from 'supertest';
import validateRequest from './validateRequest.middleware.js'; // Ajuste o caminho conforme necessário
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.

import * as aiService from '../../service/ai/ai.service.js';

vi.mock('../../service/ai/ai.service.js'); // Mockando o aiService
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.


describe('validateRequest Middleware', () => {
let app;

beforeAll(() => {
app = express();
app.use(express.json()); // Middleware para parsear JSON
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.

app.post('/test', validateRequest, (req, res) => res.send('OK')); // Define a rota que usa o middleware
});

afterEach(() => {
vi.clearAllMocks(); // Limpa mocks após cada teste
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.

});

it('should respond with 200 and a success message for valid requests', async () => {
const mockResponse = {
content: JSON.stringify({
message: 'OK',
statusCode: 200,
status: 'OK',
}),
};

aiService.default.mockResolvedValue(mockResponse); // Mockando a resposta do aiService
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.


const response = await request(app)
.post('/test')
.send({ input: 'Valid input' })
.set('Authorization', 'Bearer token');

expect(response.status).toBe(200);
expect(response.text).toBe('OK');

expect(aiService.default).toHaveBeenCalledWith(expect.objectContaining({
body: { input: 'Valid input' },
header: expect.objectContaining({
authorization: 'Bearer token',
}),
query: {},
method: 'POST',
url: '/test',
}));
});

it('should respond with an error message for invalid requests', async () => {
const mockErrorResponse = {
content: JSON.stringify({
message: 'Invalid input',
statusCode: 400,
status: 'error',
}),
};

aiService.default.mockResolvedValue(mockErrorResponse); // Mockando uma resposta de erro
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.


const response = await request(app)
.post('/test')
.send({ input: '' }) // Simulando um input inválido
.set('Authorization', 'Bearer token');

expect(response.status).toBe(400);
expect(response.body).toEqual({
status: 'error',
message: 'Invalid input',
});
});

it('should respond with an error message for missing fields', async () => {
const mockErrorResponse = {
content: JSON.stringify({
message: 'Missing fields',
statusCode: 422,
status: 'error',
}),
};

aiService.default.mockResolvedValue(mockErrorResponse); // Mockando uma resposta de erro
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in this line is in Portuguese. It's better to keep comments in English for consistency, especially if the rest of the code is in English.


const response = await request(app)
.post('/test')
.send({}) // Simulando uma requisição sem corpo
.set('Authorization', 'Bearer token');

expect(response.status).toBe(422);
expect(response.body).toEqual({
status: 'error',
message: 'Missing fields',
});
});
});
Loading