Skip to content

Commit

Permalink
Tests/338 340 unit integration (#159)
Browse files Browse the repository at this point in the history
* approval_date set while tsr is accepted by teacher

* implemented integration tests for
'GET /api/teacher/thesis-start-requests'  and 'POST /api/teacher/thesis-start-requests/:id/review'

* fix sonarlint suggestions

* implemented unit tests for 'supervisorReviewThesisStartRequest'
  • Loading branch information
Sylvie-Molinatto committed Jan 17, 2024
1 parent 277c736 commit 5a7179a
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 6 deletions.
8 changes: 4 additions & 4 deletions backend/src/dao/thesis_start_request_dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ exports.updateThesisStartRequestStatus = async (request_id, new_status) => {
*/
exports.supervisorReviewThesisStartRequest = async (supervisorId, tsrId, review) => {
/** @type {string} */
const query = `UPDATE thesisStartRequest SET status = ?, changes_requested = ? WHERE id = ? AND supervisor_id = ? AND STATUS IN (?, ?);`;
const query = `UPDATE thesisStartRequest SET status = ?, changes_requested = ?, approval_date = ? WHERE id = ? AND supervisor_id = ? AND STATUS IN (?, ?);`;
/** @type {(string | null)[]} */
const params = [];
if (review.action === "request changes") {
params.push(THESIS_START_REQUEST_STATUS.CHANGES_REQUESTED, review.changes);
params.push(THESIS_START_REQUEST_STATUS.CHANGES_REQUESTED, review.changes, null);
} else {
const newStatus = review.action === "accept" ? THESIS_START_REQUEST_STATUS.ACCEPTED_BY_TEACHER : THESIS_START_REQUEST_STATUS.REJECTED_BY_TEACHER;

params.push(newStatus, null);
const approval_date = review.action === "accept" ? new AdvancedDate().toISOString() : null;
params.push(newStatus, null, approval_date);
}
params.push(tsrId);
params.push(supervisorId);
Expand Down
182 changes: 181 additions & 1 deletion backend/test/integration/thesis.teacher.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {app} = require("../../src/app");
const utils = require("../utils");
const thesisProposalDao = require('../../src/dao/thesis_proposal_dao');
const thesisApplicationDao = require('../../src/dao/thesis_application_dao');
const thesisStartRequestDao = require('../../src/dao/thesis_start_request_dao');
const utilsDao = require('../../src/dao/utils_dao');
const usersDao = require('../../src/dao/users_dao');
const db = require('../../src/services/db');
Expand All @@ -15,6 +16,7 @@ const fs = require('fs');
const fse = require('fs-extra');
const CronTasksService = require("../../src/services/CronTasksService");
const AdvancedDate = require('../../src/models/AdvancedDate');
const { THESIS_START_REQUEST_STATUS } = require('../../src/enums');

let marcoRossiAgent;
beforeAll(async () => {
Expand Down Expand Up @@ -1493,7 +1495,7 @@ describe('DELETE /api/thesis-proposals/:id/archive', () => {
expect(response.body).toEqual({message: 'You can\'t un-archive a thesis that has already been assigned'});
});

test('should return 400 error if the proposal is expired and it\s not specified a new expiration date', async () => {
test('should return 400 error if the proposal is expired and it\'s not specified a new expiration date', async () => {

const proposal = db.prepare('INSERT INTO thesisProposal (title, supervisor_id, type, description, required_knowledge, notes, creation_date, expiration, level, is_archived) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
.run('Title', 'd279620', 'research project', 'Description', 'Required knowledge', 'Notes', '2022-10-10T10:45:50.121Z', '2023-11-10T23:59:59.999Z', 'LM', 1);
Expand Down Expand Up @@ -1654,3 +1656,181 @@ describe('DELETE /api/thesis-proposals/:id/archive', () => {
expect(response.body).toEqual('Internal Server Error');
});
});

describe('GET /api/teacher/thesis-start-requests', () => {
test('should return an empty list of thesis start requests for the teacher', async () => {

// Make a request to the API endpoint
const response = await marcoRossiAgent
.get('/api/teacher/thesis-start-requests')
.set('credentials', 'include');

// Assertions
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('$metadata');
expect(response.body).toHaveProperty('items');
expect(response.body.items).toEqual([]);
});

test('should return a list of thesis start requests for the teacher', async () => {

const tsr = db.prepare('INSERT INTO thesisStartRequest (student_id, title, description, supervisor_id, creation_date, status) VALUES (?, ?, ?, ?, ?, ?)')
.run('s318952', 'Title', 'Description', 'd279620','2021-10-10T10:45:50.121Z', THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY);
db.prepare('INSERT INTO thesisStartCoSupervisor (start_request_id, cosupervisor_id) VALUES (?, ?)')
.run(tsr.lastInsertRowid, 'd370335');

// Make a request to the API endpoint
const response = await marcoRossiAgent
.get('/api/teacher/thesis-start-requests')
.set('credentials', 'include');

// Assertions
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('$metadata');
expect(response.body).toHaveProperty('items');
expect(response.body.items).toEqual([{
id: tsr.lastInsertRowid,
student: {
id: 's318952',
name: 'Sylvie',
surname: 'Molinatto',
email: 's318952@studenti.polito.it',
},
application_id: null,
proposal_id: null,
title: 'Title',
description: 'Description',
supervisor: {
id: 'd279620',
name: 'Marco',
surname: 'Rossi',
email: 'd279620@polito.it',
cod_group: 'Group1',
cod_department: 'Dep1',
},
co_supervisors: [
{
id: 'd370335',
name: 'Luca',
surname: 'Bianchi',
email: 'd370335@polito.it',
cod_group: 'Group2',
cod_department: 'Dep2',
}
],
creation_date: '2021-10-10T10:45:50.121Z',
approval_date: null,
status: THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY,
changes_requested: null,
}]);
});

test('should handle errors and call the next middleware', async () => {
// Mock the function in your thesisStartRequestDao module to throw an error
jest.spyOn(thesisStartRequestDao, 'listThesisStartRequests').mockRejectedValue(new Error('Test error'));

// Make a request to the API endpoint
const response = await marcoRossiAgent
.get('/api/teacher/thesis-start-requests')
.set('credentials', 'include');

// Assertions
expect(response.status).toBe(500);
expect(response.body).toEqual('Internal Server Error');
});

})

describe('POST /api/teacher/thesis-start-requests/:id/review', () => {
test('should successfully accept a thesis start request', async () => {

const tsr = db.prepare('INSERT INTO thesisStartRequest (student_id, title, description, supervisor_id, creation_date, status) VALUES (?, ?, ?, ?, ?, ?)')
.run('s318952', 'Title', 'Description', 'd279620','2021-10-10T10:45:50.121Z', THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY);
db.prepare('INSERT INTO thesisStartCoSupervisor (start_request_id, cosupervisor_id) VALUES (?, ?)')
.run(tsr.lastInsertRowid, 'd370335');
const review = {
action: 'accept',
}
// Make a request to the API endpoint
await marcoRossiAgent
.post(`/api/teacher/thesis-start-requests/${tsr.lastInsertRowid}/review`)
.set('credentials', 'include')
.send(review)
.expect(204);

});

test('should successfully request changes for thesis start request', async () => {

const tsr = db.prepare('INSERT INTO thesisStartRequest (student_id, title, description, supervisor_id, creation_date, status) VALUES (?, ?, ?, ?, ?, ?)')
.run('s318952', 'Title', 'Description', 'd279620','2021-10-10T10:45:50.121Z', THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY);
db.prepare('INSERT INTO thesisStartCoSupervisor (start_request_id, cosupervisor_id) VALUES (?, ?)')
.run(tsr.lastInsertRowid, 'd370335');
const review = {
action: 'request changes',
changes: 'Changes',
}
// Make a request to the API endpoint
await marcoRossiAgent
.post(`/api/teacher/thesis-start-requests/${tsr.lastInsertRowid}/review`)
.set('credentials', 'include')
.send(review)
.expect(204);

});

test('should successfully reject a thesis start request', async () => {

const tsr = db.prepare('INSERT INTO thesisStartRequest (student_id, title, description, supervisor_id, creation_date, status) VALUES (?, ?, ?, ?, ?, ?)')
.run('s318952', 'Title', 'Description', 'd279620','2021-10-10T10:45:50.121Z', THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY);
db.prepare('INSERT INTO thesisStartCoSupervisor (start_request_id, cosupervisor_id) VALUES (?, ?)')
.run(tsr.lastInsertRowid, 'd370335');
const review = {
action: 'reject',
}
// Make a request to the API endpoint
await marcoRossiAgent
.post(`/api/teacher/thesis-start-requests/${tsr.lastInsertRowid}/review`)
.set('credentials', 'include')
.send(review)
.expect(204);
});

test('should throw an error if the thesis start request doesn\'t exist', async () => {

const review = {
action: 'accept',
}
// Make a request to the API endpoint
const response = await marcoRossiAgent
.post(`/api/teacher/thesis-start-requests/1000/review`)
.set('credentials', 'include')
.send(review)
.expect(404);

expect(response.body).toEqual({message: 'No thesis start request with id 1000 found or you are not authorized to access it.'});

});

test('should handle errors and call the next middleware', async () => {

const tsr = db.prepare('INSERT INTO thesisStartRequest (student_id, title, description, supervisor_id, creation_date, status) VALUES (?, ?, ?, ?, ?, ?)')
.run('s318952', 'Title', 'Description', 'd279620','2021-10-10T10:45:50.121Z', THESIS_START_REQUEST_STATUS.ACCEPTED_BY_SECRETARY);
db.prepare('INSERT INTO thesisStartCoSupervisor (start_request_id, cosupervisor_id) VALUES (?, ?)')
.run(tsr.lastInsertRowid, 'd370335');

// Mock the function in your thesisStartRequestDao module to throw an error
jest.spyOn(thesisStartRequestDao, 'supervisorReviewThesisStartRequest').mockRejectedValue(new Error('Test error'));
const review = {
action: 'accept',
}
// Make a request to the API endpoint
const response = await marcoRossiAgent
.post(`/api/teacher/thesis-start-requests/${tsr.lastInsertRowid}/review`)
.set('credentials', 'include')
.send(review)
.expect(500);

expect(response.body).toEqual('Internal Server Error');
});
});
Loading

0 comments on commit 5a7179a

Please sign in to comment.