Skip to content

Commit 4c99242

Browse files
committed
tests: Added e2e testing for vaccination endpoints
1 parent 6369b63 commit 4c99242

File tree

7 files changed

+343
-17
lines changed

7 files changed

+343
-17
lines changed

api/src/modules/vaccination/vaccination.controllers.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const getAllVaccination = async (req, res, next) => {
1212
query,
1313
);
1414

15-
res.status(200).json({ vaccinations, sucess: true });
15+
res.status(200).json({ vaccinations });
1616
} catch (error) {
1717
next(error);
1818
}
@@ -38,7 +38,7 @@ const createVaccination = async (req, res, next) => {
3838
res.status(201).json({
3939
message: 'Vaccination was successfully created',
4040
success: true,
41-
newVaccination: formattedVaccinationData,
41+
vaccination: formattedVaccinationData,
4242
});
4343
} catch (error) {
4444
next(error);
@@ -56,8 +56,7 @@ const updateVaccination = async (req, res, next) => {
5656
vaccinationId,
5757
vaccinationData,
5858
);
59-
if (!updatedVaccination?.id)
60-
throw Boom.badRequest('Update animal operation returns null');
59+
6160
const formattedVaccinationData = await vaccinationService.getVaccination(
6261
userId,
6362
updatedVaccination.id,
@@ -66,7 +65,7 @@ const updateVaccination = async (req, res, next) => {
6665
res.status(200).json({
6766
message: 'Vaccination was successfully updated',
6867
success: true,
69-
updatedVaccination: formattedVaccinationData,
68+
vaccination: formattedVaccinationData,
7069
});
7170
} catch (error) {
7271
next(error);
@@ -78,17 +77,15 @@ const deleteVaccination = async (req, res, next) => {
7877
const { vaccinationId } = req.params;
7978
const userId = req.user.sub;
8079

81-
const deletedVaccination = await vaccinationService.deleteVaccination(
80+
const affectedRows = await vaccinationService.deleteVaccination(
8281
userId,
8382
vaccinationId,
8483
);
85-
if (deletedVaccination === 0)
86-
throw Boom.badRequest('Delete animal operation returns 0 rows affected');
8784

8885
res.status(200).json({
8986
message: 'Vaccination was successfully deleted',
9087
success: true,
91-
deletedVaccination,
88+
affectedRows,
9289
});
9390
} catch (error) {
9491
next(error);

api/src/modules/vaccination/vaccination.routes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { animalIdSchema } = require('../animal/animal.schemas');
88
const {
99
bodyVaccinationSchema,
1010
vaccinationIdSchema,
11+
querySchema,
12+
bodyToUpdateVaccinationSchema,
1113
} = require('./vaccination.schemas');
1214

1315
const vaccinationControllers = require('./vaccination.controllers');
@@ -60,6 +62,7 @@ const vaccinationControllers = require('./vaccination.controllers');
6062
routes.get(
6163
'/vaccination',
6264
validateSession,
65+
validatorHandler(querySchema, 'query'),
6366
vaccinationControllers.getAllVaccination,
6467
);
6568

@@ -169,7 +172,7 @@ routes.patch(
169172
'/vaccination/:vaccinationId',
170173
validateSession,
171174
validatorHandler(vaccinationIdSchema, 'params'),
172-
validatorHandler(bodyVaccinationSchema, 'body'),
175+
validatorHandler(bodyToUpdateVaccinationSchema, 'body'),
173176
vaccinationControllers.updateVaccination,
174177
);
175178

api/src/modules/vaccination/vaccination.schemas.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ const vaccinationIdSchema = Joi.object({
1212
vaccinationId: id.required(),
1313
});
1414

15+
const querySchema = Joi.object({
16+
vaccine: vaccine.optional(),
17+
animalId: id.optional(),
18+
});
19+
1520
const bodyVaccinationSchema = Joi.object({
1621
vaccine: vaccine.required(),
1722
description: description.optional(),
1823
});
1924

25+
const bodyToUpdateVaccinationSchema = Joi.object({
26+
vaccine: vaccine.optional(),
27+
description: description.optional(),
28+
});
29+
2030
module.exports = {
2131
vaccinationSchema,
2232
vaccinationIdSchema,
2333
bodyVaccinationSchema,
34+
querySchema,
35+
bodyToUpdateVaccinationSchema,
2436
};

api/src/modules/vaccination/vaccination.service.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const formatVaccination = (vaccination) => {
1010
id: vaccination.id,
1111
vaccine: vaccination.vaccine,
1212
description: vaccination.description || null,
13-
animal: vaccination.animal ? vaccination.animal.code : null,
13+
animal: vaccination.animal
14+
? { id: vaccination.animal.id, code: vaccination.animal.code }
15+
: null,
1416
registeredAt:
1517
vaccination.registeredAt.toISOString().split('T')[0] ||
1618
vaccination.registeredAt,

api/src/store/db/seeders/04-vaccination.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
}
99

1010
return queryInterface.bulkInsert(VACCINATION_TABLE, [
11+
// User 1
1112
// animal1
1213
{
1314
id: 'daa915c2-192d-43b5-930d-88596673c6e0',
@@ -43,6 +44,8 @@ module.exports = {
4344
animal_id: 'e9ea0cea-62de-47be-b17a-4f6609e945a4',
4445
registered_at: '2025-04-23T23:40:07.036Z',
4546
},
47+
48+
// User 2
4649
]);
4750
},
4851

tests/e2e/deworming.e2e.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ describe('tests for user endpoints', () => {
142142
});
143143

144144
describe('POST /animals/{animalId}/deworming', () => {
145-
const inputBody = {
146-
dewormer: 'ivermectin 5%',
147-
};
145+
let inputBody = null;
146+
147+
beforeEach(() => {
148+
inputBody = {
149+
dewormer: 'ivermectin 5%',
150+
};
151+
});
148152

149153
test('It should return a new deworming without description.', async () => {
150154
const { statusCode, body } = await api
@@ -188,7 +192,7 @@ describe('tests for user endpoints', () => {
188192
inputBody.register = new Date();
189193

190194
const { statusCode, body } = await api
191-
.post(`/api/v1/animals`)
195+
.post(`/api/v1/animals/${animalsUsr2[0].id}/deworming`)
192196
.set('Cookie', `accessToken=${accessTokenUser1}`)
193197
.send(inputBody);
194198

@@ -200,12 +204,12 @@ describe('tests for user endpoints', () => {
200204
delete inputBody.dewormer;
201205

202206
const { statusCode, body } = await api
203-
.post(`/api/v1/animals`)
207+
.post(`/api/v1/animals/${animalsUsr2[0].id}/deworming`)
204208
.set('Cookie', `accessToken=${accessTokenUser1}`)
205209
.send(inputBody);
206210

207211
expect(statusCode).toBe(400);
208-
expect(body.message).toMatch(/is required/);
212+
expect(body.message).toMatch(/was not provided|is required/);
209213
});
210214
});
211215

0 commit comments

Comments
 (0)