-
Notifications
You must be signed in to change notification settings - Fork 0
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
Developer #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
import * as aiService from '../../service/ai/ai.service.js'; | ||
|
||
vi.mock('../../service/ai/ai.service.js'); // Mockando o aiService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.