Skip to content

Commit

Permalink
COM-3718 - fix reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Manon Palin committed Jun 6, 2024
1 parent 14233bb commit 53d54f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/routes/preHandlers/questionnaireHistories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const User = require('../../models/User');
const Course = require('../../models/Course');
const QuestionnaireHistory = require('../../models/QuestionnaireHistory');
const { END_COURSE } = require('../../helpers/constants');
const { areObjectIdsEquals } = require('../../helpers/utils');
const UtilsHelper = require('../../helpers/utils');
const { checkQuestionnaireAnswersList } = require('./utils');

exports.authorizeAddQuestionnaireHistory = async (req) => {
Expand Down Expand Up @@ -36,7 +36,8 @@ exports.authorizeQuestionnaireHistoryUpdate = async (req) => {
.lean();
if (!questionnaireHistory) throw Boom.notFound();

const loggedUserIsCourseTrainer = areObjectIdsEquals(questionnaireHistory.course.trainer, credentials._id);
const courseTrainer = questionnaireHistory.course.trainer;
const loggedUserIsCourseTrainer = UtilsHelper.areObjectIdsEquals(courseTrainer, credentials._id);
if (!loggedUserIsCourseTrainer) throw Boom.forbidden();

const cardIds = trainerAnswers.map(answer => answer.card);
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/questionnaireHistories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ describe('QUESTIONNAIRE HISTORIES ROUTES - POST /questionnairehistories', () =>
});
});

describe('QUESTIONNAIRE HISTORIES ROUTES - PUT /questionnairehistorie/{_id}', () => {
describe('QUESTIONNAIRE HISTORIES ROUTES - PUT /questionnairehistories/{_id}', () => {
let authToken;
beforeEach(populateDB);
const endSelfPositionningQuestionnaireHistoryId = questionnaireHistoriesList[1]._id;
Expand Down Expand Up @@ -479,6 +479,20 @@ describe('QUESTIONNAIRE HISTORIES ROUTES - PUT /questionnairehistorie/{_id}', ()
expect(response.statusCode).toBe(404);
});

it('should return 404 if questionnaireHistory has START_COURSE timeline', async () => {
const startSelfPositionningQuestionnaireHistoryId = questionnaireHistoriesList[2]._id;

const payload = { trainerAnswers: [{ card: cardsList[1]._id }] };
const response = await app.inject({
method: 'PUT',
url: `/questionnairehistories/${startSelfPositionningQuestionnaireHistoryId}`,
payload,
headers: { Cookie: `alenvi_token=${authToken}` },
});

expect(response.statusCode).toBe(404);
});

it('should return 404 if card is not in questionnaire', async () => {
const payload = { trainerAnswers: [{ card: cardsList[2]._id }] };
const response = await app.inject({
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/seed/questionnaireHistoriesSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
SELF_POSITIONNING,
ON_SITE,
END_COURSE,
START_COURSE,
} = require('../../../src/helpers/constants');
const { authCompany, companyWithoutSubscription } = require('../../seed/authCompaniesSeed');

Expand Down Expand Up @@ -128,6 +129,15 @@ const questionnaireHistoriesList = [
timeline: END_COURSE,
questionnaireAnswersList: [{ card: cardsList[1]._id, answerList: ['2'] }],
},
{
_id: new ObjectId(),
course: coursesList[1]._id,
user: questionnaireHistoriesUsersList[1],
questionnaire: questionnairesList[2]._id,
company: authCompany._id,
timeline: START_COURSE,
questionnaireAnswersList: [{ card: cardsList[1]._id, answerList: ['1'] }],
},
];

const courseHistoriesList = [
Expand Down

0 comments on commit 53d54f5

Please sign in to comment.