From 9bc3e26db61f7c2161b1549e84954513301bb4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Fran=C3=A7ois?= Date: Fri, 27 Dec 2024 14:37:55 +0100 Subject: [PATCH] :truck: chore(api): move student certification to `src` --- .../certification-center-controller.js | 20 --- .../certification-centers/index.js | 33 ---- .../certification-center-controller.js | 24 +++ .../application/session-students-route.js | 46 ++++++ .../usecases/find-students-for-enrolment.js | 4 +- .../student-certification-serializer.js | 0 .../certification-center-controller_test.js | 98 ----------- .../certification-center-controller_test.js | 140 ++++++++++++++++ .../session-students-route_test.js | 155 ++++++++++++++++++ .../find-students-for-enrolment_test.js | 8 +- .../student-certification-serializer_test.js | 6 +- .../certification-center-controller_test.js | 61 +------ .../certification-centers/index_test.js | 149 ----------------- 13 files changed, 375 insertions(+), 369 deletions(-) create mode 100644 api/src/certification/enrolment/application/certification-center-controller.js create mode 100644 api/src/certification/enrolment/application/session-students-route.js rename api/{lib => src/certification/enrolment}/domain/usecases/find-students-for-enrolment.js (89%) rename api/{lib/infrastructure/serializers/jsonapi => src/certification/enrolment/infrastructure/serializers}/student-certification-serializer.js (100%) create mode 100644 api/tests/certification/enrolment/acceptance/application/certification-center-controller_test.js create mode 100644 api/tests/certification/enrolment/unit/application/session-students-route_test.js rename api/tests/{ => certification/enrolment}/unit/domain/usecases/find-students-for-enrolment_test.js (92%) rename api/tests/{unit/infrastructure/serializers/jsonapi => certification/enrolment/unit/infrastructure/serializers}/student-certification-serializer_test.js (77%) diff --git a/api/lib/application/certification-centers/certification-center-controller.js b/api/lib/application/certification-centers/certification-center-controller.js index 61c95d131f0..5df50c18908 100644 --- a/api/lib/application/certification-centers/certification-center-controller.js +++ b/api/lib/application/certification-centers/certification-center-controller.js @@ -2,25 +2,6 @@ import * as divisionSerializer from '../../../src/prescription/campaign/infrastr import * as certificationCenterMembershipSerializer from '../../../src/shared/infrastructure/serializers/jsonapi/certification-center-membership.serializer.js'; import { usecases as teamUsecases } from '../../../src/team/domain/usecases/index.js'; import { usecases } from '../../domain/usecases/index.js'; -import * as studentCertificationSerializer from '../../infrastructure/serializers/jsonapi/student-certification-serializer.js'; - -const getStudents = async function (request) { - const certificationCenterId = request.params.certificationCenterId; - const sessionId = request.params.sessionId; - - const { filter, page } = request.query; - if (filter.divisions && !Array.isArray(filter.divisions)) { - filter.divisions = [filter.divisions]; - } - - const { data, pagination } = await usecases.findStudentsForEnrolment({ - certificationCenterId, - sessionId, - page, - filter, - }); - return studentCertificationSerializer.serialize(data, pagination); -}; const getDivisions = async function (request) { const certificationCenterId = request.params.certificationCenterId; @@ -77,7 +58,6 @@ const certificationCenterController = { createCertificationCenterMembershipByEmail, findCertificationCenterMembershipsByCertificationCenter, getDivisions, - getStudents, updateReferer, }; diff --git a/api/lib/application/certification-centers/index.js b/api/lib/application/certification-centers/index.js index 4a8f3546825..a7c61a86d24 100644 --- a/api/lib/application/certification-centers/index.js +++ b/api/lib/application/certification-centers/index.js @@ -93,39 +93,6 @@ const register = async function (server) { server.route([ ...adminRoutes, ...certifRoutes, - { - method: 'GET', - path: '/api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', - config: { - validate: { - params: Joi.object({ - certificationCenterId: identifiersType.certificationCenterId, - sessionId: identifiersType.sessionId, - }), - query: Joi.object({ - filter: Joi.object({ - divisions: Joi.array().items(Joi.string()), - }).default({}), - page: { - number: Joi.number().integer(), - size: Joi.number().integer(), - }, - }), - }, - pre: [ - { - method: securityPreHandlers.checkUserIsMemberOfCertificationCenter, - assign: 'isMemberOfCertificationCenter', - }, - ], - handler: certificationCenterController.getStudents, - notes: [ - '- **Cette route est restreinte aux utilisateurs authentifiés**\n' + - "- Récupération d'une liste d'élèves SCO à partir d'un identifiant de centre de certification", - ], - tags: ['api', 'certification-center', 'students', 'session'], - }, - }, { method: 'GET', path: '/api/certification-centers/{certificationCenterId}/divisions', diff --git a/api/src/certification/enrolment/application/certification-center-controller.js b/api/src/certification/enrolment/application/certification-center-controller.js new file mode 100644 index 00000000000..a5078b2ab2f --- /dev/null +++ b/api/src/certification/enrolment/application/certification-center-controller.js @@ -0,0 +1,24 @@ +import { usecases } from '../domain/usecases/index.js'; +import * as studentCertificationSerializer from '../infrastructure/serializers/student-certification-serializer.js'; + +const getStudents = async function (request) { + const certificationCenterId = request.params.certificationCenterId; + const sessionId = request.params.sessionId; + + const { filter, page } = request.query; + if (filter.divisions && !Array.isArray(filter.divisions)) { + filter.divisions = [filter.divisions]; + } + + const { data, pagination } = await usecases.findStudentsForEnrolment({ + certificationCenterId, + sessionId, + page, + filter, + }); + return studentCertificationSerializer.serialize(data, pagination); +}; + +const certificationCenterController = { getStudents }; + +export { certificationCenterController }; diff --git a/api/src/certification/enrolment/application/session-students-route.js b/api/src/certification/enrolment/application/session-students-route.js new file mode 100644 index 00000000000..10b57fd12ef --- /dev/null +++ b/api/src/certification/enrolment/application/session-students-route.js @@ -0,0 +1,46 @@ +import Joi from 'joi'; + +import { securityPreHandlers } from '../../../shared/application/security-pre-handlers.js'; +import { identifiersType } from '../../../shared/domain/types/identifiers-type.js'; +import { certificationCenterController } from './certification-center-controller.js'; + +const register = async function (server) { + server.route([ + { + method: 'GET', + path: '/api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', + config: { + validate: { + params: Joi.object({ + certificationCenterId: identifiersType.certificationCenterId, + sessionId: identifiersType.sessionId, + }), + query: Joi.object({ + filter: Joi.object({ + divisions: Joi.array().items(Joi.string()), + }).default({}), + page: { + number: Joi.number().integer(), + size: Joi.number().integer(), + }, + }), + }, + pre: [ + { + method: securityPreHandlers.checkUserIsMemberOfCertificationCenter, + assign: 'isMemberOfCertificationCenter', + }, + ], + handler: certificationCenterController.getStudents, + notes: [ + '- **Cette route est restreinte aux utilisateurs authentifiés**\n' + + "- Récupération d'une liste d'élèves SCO à partir d'un identifiant de centre de certification", + ], + tags: ['api', 'certification-center', 'students', 'session'], + }, + }, + ]); +}; + +const name = 'certification-centers-api'; +export { name, register }; diff --git a/api/lib/domain/usecases/find-students-for-enrolment.js b/api/src/certification/enrolment/domain/usecases/find-students-for-enrolment.js similarity index 89% rename from api/lib/domain/usecases/find-students-for-enrolment.js rename to api/src/certification/enrolment/domain/usecases/find-students-for-enrolment.js index 99424715f85..5cfd2cefca7 100644 --- a/api/lib/domain/usecases/find-students-for-enrolment.js +++ b/api/src/certification/enrolment/domain/usecases/find-students-for-enrolment.js @@ -1,5 +1,5 @@ -import { NotFoundError } from '../../../src/shared/domain/errors.js'; -import { StudentForEnrolment } from '../../../src/shared/domain/read-models/StudentForEnrolment.js'; +import { NotFoundError } from '../../../../shared/domain/errors.js'; +import { StudentForEnrolment } from '../../../../shared/domain/read-models/StudentForEnrolment.js'; const findStudentsForEnrolment = async function ({ certificationCenterId, diff --git a/api/lib/infrastructure/serializers/jsonapi/student-certification-serializer.js b/api/src/certification/enrolment/infrastructure/serializers/student-certification-serializer.js similarity index 100% rename from api/lib/infrastructure/serializers/jsonapi/student-certification-serializer.js rename to api/src/certification/enrolment/infrastructure/serializers/student-certification-serializer.js diff --git a/api/tests/acceptance/application/certification-centers/certification-center-controller_test.js b/api/tests/acceptance/application/certification-centers/certification-center-controller_test.js index 5fd4993f9db..37638673e9c 100644 --- a/api/tests/acceptance/application/certification-centers/certification-center-controller_test.js +++ b/api/tests/acceptance/application/certification-centers/certification-center-controller_test.js @@ -49,104 +49,6 @@ describe('Acceptance | API | Certification Center', function () { }); }); - describe('GET /api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', function () { - let request; - const externalId = 'XXXX'; - - function _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session) { - return { - method: 'GET', - url: - '/api/certification-centers/' + - certificationCenter.id + - '/sessions/' + - session.id + - '/students?page[size]=10&page[number]=1', - headers: { authorization: generateValidRequestAuthorizationHeader(user.id) }, - }; - } - function _buildOrganizationLearnersNotConnectedUserRequest(certificationCenter, session) { - return { - method: 'GET', - url: - '/api/certification-centers/' + - certificationCenter.id + - '/sessions/' + - session.id + - '/students?page[size]=10&page[number]=1', - }; - } - - context('when user is connected', function () { - it('should return 200 HTTP status', async function () { - // given - const { certificationCenter, user } = _buildUserWithCertificationCenterMemberShip(externalId); - const organization = databaseBuilder.factory.buildOrganization({ externalId, type: 'SCO' }); - const session = databaseBuilder.factory.buildSession({ certificationCenterId: certificationCenter.id }); - _buildOrganizationLearners(organization, { firstName: 'Laura', lastName: 'certifForEver', division: '2ndB' }); - await databaseBuilder.commit(); - - const request = _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session); - - // when - const response = await server.inject(request); - - // then - expect(response.statusCode).to.equal(200); - }); - - it('should return the organization learners asked', async function () { - // given - const { certificationCenter, user } = _buildUserWithCertificationCenterMemberShip(externalId); - const organization = databaseBuilder.factory.buildOrganization({ type: 'SCO', externalId }); - const session = databaseBuilder.factory.buildSession({ certificationCenterId: certificationCenter.id }); - _buildOrganizationLearners( - organization, - { id: 1, division: '2ndB', firstName: 'Laura', lastName: 'Certif4Ever' }, - { id: 2, division: '2ndA', firstName: 'Laura', lastName: 'Booooo' }, - { id: 3, division: '2ndA', firstName: 'Laura', lastName: 'aaaaa' }, - { id: 4, division: '2ndA', firstName: 'Bart', lastName: 'Coucou' }, - { id: 5, division: '2ndA', firstName: 'Arthur', lastName: 'Coucou' }, - ); - await databaseBuilder.commit(); - - const request = _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session); - - // when - const response = await server.inject(request); - - // then - expect(_.map(response.result.data, 'id')).to.deep.equal(['3', '2', '5', '4', '1']); - }); - }); - - context('when user is not connected', function () { - it('should return 401 HTTP status code if user is not authenticated', async function () { - // given - _buildUserWithCertificationCenterMemberShip(externalId); - databaseBuilder.factory.buildOrganization({ externalId }); - const certificationCenterWhereUserDoesNotHaveAccess = databaseBuilder.factory.buildCertificationCenter({ - externalId, - }); - const session = databaseBuilder.factory.buildSession({ - certificationCenterId: certificationCenterWhereUserDoesNotHaveAccess.id, - }); - await databaseBuilder.commit(); - - request = _buildOrganizationLearnersNotConnectedUserRequest( - certificationCenterWhereUserDoesNotHaveAccess, - session, - ); - - // when - const response = await server.inject(request); - - // then - expect(response.statusCode).to.equal(401); - }); - }); - }); - describe('GET /api/admin/certification-centers/{id}/certification-center-memberships', function () { context('when certification center membership is linked to the certification center', function () { it('should return 200 HTTP status', async function () { diff --git a/api/tests/certification/enrolment/acceptance/application/certification-center-controller_test.js b/api/tests/certification/enrolment/acceptance/application/certification-center-controller_test.js new file mode 100644 index 00000000000..cd0fbd3d8c7 --- /dev/null +++ b/api/tests/certification/enrolment/acceptance/application/certification-center-controller_test.js @@ -0,0 +1,140 @@ +import _ from 'lodash'; + +import { + createServer, + databaseBuilder, + expect, + generateValidRequestAuthorizationHeader, + insertUserWithRoleSuperAdmin, +} from '../../../../test-helper.js'; + +describe('Acceptance | API | Certification Center', function () { + let server; + + beforeEach(async function () { + server = await createServer(); + await insertUserWithRoleSuperAdmin(); + }); + + describe('GET /api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', function () { + let request; + const externalId = 'XXXX'; + + function _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session) { + return { + method: 'GET', + url: + '/api/certification-centers/' + + certificationCenter.id + + '/sessions/' + + session.id + + '/students?page[size]=10&page[number]=1', + headers: { authorization: generateValidRequestAuthorizationHeader(user.id) }, + }; + } + function _buildOrganizationLearnersNotConnectedUserRequest(certificationCenter, session) { + return { + method: 'GET', + url: + '/api/certification-centers/' + + certificationCenter.id + + '/sessions/' + + session.id + + '/students?page[size]=10&page[number]=1', + }; + } + + context('when user is connected', function () { + it('should return 200 HTTP status', async function () { + // given + const { certificationCenter, user } = _buildUserWithCertificationCenterMemberShip(externalId); + const organization = databaseBuilder.factory.buildOrganization({ externalId, type: 'SCO' }); + const session = databaseBuilder.factory.buildSession({ certificationCenterId: certificationCenter.id }); + _buildOrganizationLearners(organization, { firstName: 'Laura', lastName: 'certifForEver', division: '2ndB' }); + await databaseBuilder.commit(); + + const request = _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session); + + // when + const response = await server.inject(request); + + // then + expect(response.statusCode).to.equal(200); + }); + + it('should return the organization learners asked', async function () { + // given + const { certificationCenter, user } = _buildUserWithCertificationCenterMemberShip(externalId); + const organization = databaseBuilder.factory.buildOrganization({ type: 'SCO', externalId }); + const session = databaseBuilder.factory.buildSession({ certificationCenterId: certificationCenter.id }); + _buildOrganizationLearners( + organization, + { id: 1, division: '2ndB', firstName: 'Laura', lastName: 'Certif4Ever' }, + { id: 2, division: '2ndA', firstName: 'Laura', lastName: 'Booooo' }, + { id: 3, division: '2ndA', firstName: 'Laura', lastName: 'aaaaa' }, + { id: 4, division: '2ndA', firstName: 'Bart', lastName: 'Coucou' }, + { id: 5, division: '2ndA', firstName: 'Arthur', lastName: 'Coucou' }, + ); + await databaseBuilder.commit(); + + const request = _buildOrganizationLearnersWithConnectedUserRequest(user, certificationCenter, session); + + // when + const response = await server.inject(request); + + // then + expect(_.map(response.result.data, 'id')).to.deep.equal(['3', '2', '5', '4', '1']); + }); + }); + + context('when user is not connected', function () { + it('should return 401 HTTP status code if user is not authenticated', async function () { + // given + _buildUserWithCertificationCenterMemberShip(externalId); + databaseBuilder.factory.buildOrganization({ externalId }); + const certificationCenterWhereUserDoesNotHaveAccess = databaseBuilder.factory.buildCertificationCenter({ + externalId, + }); + const session = databaseBuilder.factory.buildSession({ + certificationCenterId: certificationCenterWhereUserDoesNotHaveAccess.id, + }); + await databaseBuilder.commit(); + + request = _buildOrganizationLearnersNotConnectedUserRequest( + certificationCenterWhereUserDoesNotHaveAccess, + session, + ); + + // when + const response = await server.inject(request); + + // then + expect(response.statusCode).to.equal(401); + }); + }); + }); + + function _buildOrganizationLearners(organization, ...students) { + const AFTER_BEGINNING_OF_THE_2020_SCHOOL_YEAR = '2020-10-15'; + return students.map((student) => + databaseBuilder.factory.buildOrganizationLearner({ + organizationId: organization.id, + ...student, + updatedAt: AFTER_BEGINNING_OF_THE_2020_SCHOOL_YEAR, + }), + ); + } + + function _buildUserWithCertificationCenterMemberShip(certificationCenterExternalId) { + const user = databaseBuilder.factory.buildUser({}); + const certificationCenter = databaseBuilder.factory.buildCertificationCenter({ + externalId: certificationCenterExternalId, + type: 'SCO', + }); + databaseBuilder.factory.buildCertificationCenterMembership({ + certificationCenterId: certificationCenter.id, + userId: user.id, + }); + return { user, certificationCenter }; + } +}); diff --git a/api/tests/certification/enrolment/unit/application/session-students-route_test.js b/api/tests/certification/enrolment/unit/application/session-students-route_test.js new file mode 100644 index 00000000000..f132b4a1152 --- /dev/null +++ b/api/tests/certification/enrolment/unit/application/session-students-route_test.js @@ -0,0 +1,155 @@ +import { certificationCenterController } from '../../../../../src/certification/enrolment/application/certification-center-controller.js'; +import * as moduleUnderTest from '../../../../../src/certification/enrolment/application/session-students-route.js'; +import { securityPreHandlers } from '../../../../../src/shared/application/security-pre-handlers.js'; +import { expect, HttpTestServer, sinon } from '../../../../test-helper.js'; + +describe('Certification | Enrolment | Unit | Application | Session Students Route', function () { + describe('GET /api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', function () { + it('should reject unexpected filters ', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?filter[unexpected][]=5', + ); + + // then + expect(result.statusCode).to.equal(400); + }); + + it('should accept a string array of one element as division filter ', async function () { + // given + sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); + sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?filter[divisions][]="3EMEB"', + ); + + // then + expect(result.statusCode).to.equal(200); + }); + + it('should accept a string array of several elements as division filter ', async function () { + // given + sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); + sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?filter[divisions][]="3EMEB"&filter[divisions][]="3EMEA"', + ); + + // then + expect(result.statusCode).to.equal(200); + }); + + it('should reject a division filter if it is not an array', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?filter[divisions]="3EMEA"', + ); + + // then + expect(result.statusCode).to.equal(400); + }); + + it('should accept a pagination', async function () { + // given + sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); + sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?page[number]=1&page[size]=25', + ); + + // then + expect(result.statusCode).to.equal(200); + }); + + it('should reject a page number which is not a number', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?page[number]=a', + ); + + // then + expect(result.statusCode).to.equal(400); + }); + + it('should reject a page size which is not a number', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + // when + const result = await httpTestServer.request( + 'GET', + '/api/certification-centers/1/sessions/2/students?page[size]=a', + ); + + // then + expect(result.statusCode).to.equal(400); + }); + + it('should accept an empty query string', async function () { + // given + sinon.stub(certificationCenterController, 'getStudents').returns('ok'); + sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request('GET', '/api/certification-centers/1/sessions/2/students'); + + // then + expect(result.statusCode).to.equal(200); + }); + + it('should reject an invalid certification-centers id', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request('GET', '/api/certification-centers/invalid/sessions/2/students'); + + // then + expect(result.statusCode).to.equal(400); + }); + + it('should reject an invalid session id', async function () { + // given + const httpTestServer = new HttpTestServer(); + await httpTestServer.register(moduleUnderTest); + + // when + const result = await httpTestServer.request('GET', '/api/certification-centers/1/sessions/invalid/students'); + + // then + expect(result.statusCode).to.equal(400); + }); + }); +}); diff --git a/api/tests/unit/domain/usecases/find-students-for-enrolment_test.js b/api/tests/certification/enrolment/unit/domain/usecases/find-students-for-enrolment_test.js similarity index 92% rename from api/tests/unit/domain/usecases/find-students-for-enrolment_test.js rename to api/tests/certification/enrolment/unit/domain/usecases/find-students-for-enrolment_test.js index 8b8806a13f8..23b2b5c42f2 100644 --- a/api/tests/unit/domain/usecases/find-students-for-enrolment_test.js +++ b/api/tests/certification/enrolment/unit/domain/usecases/find-students-for-enrolment_test.js @@ -1,9 +1,9 @@ import _ from 'lodash'; -import { findStudentsForEnrolment } from '../../../../lib/domain/usecases/find-students-for-enrolment.js'; -import { NotFoundError } from '../../../../src/shared/domain/errors.js'; -import { StudentForEnrolment } from '../../../../src/shared/domain/read-models/StudentForEnrolment.js'; -import { domainBuilder, expect, sinon } from '../../../test-helper.js'; +import { findStudentsForEnrolment } from '../../../../../../src/certification/enrolment/domain/usecases/find-students-for-enrolment.js'; +import { NotFoundError } from '../../../../../../src/shared/domain/errors.js'; +import { StudentForEnrolment } from '../../../../../../src/shared/domain/read-models/StudentForEnrolment.js'; +import { domainBuilder, expect, sinon } from '../../../../../test-helper.js'; describe('Unit | UseCase | find-students-for-enrolment', function () { const certificationCenterId = 1; diff --git a/api/tests/unit/infrastructure/serializers/jsonapi/student-certification-serializer_test.js b/api/tests/certification/enrolment/unit/infrastructure/serializers/student-certification-serializer_test.js similarity index 77% rename from api/tests/unit/infrastructure/serializers/jsonapi/student-certification-serializer_test.js rename to api/tests/certification/enrolment/unit/infrastructure/serializers/student-certification-serializer_test.js index 3de8e3f0a24..2320148c284 100644 --- a/api/tests/unit/infrastructure/serializers/jsonapi/student-certification-serializer_test.js +++ b/api/tests/certification/enrolment/unit/infrastructure/serializers/student-certification-serializer_test.js @@ -1,6 +1,6 @@ -import * as serializer from '../../../../../lib/infrastructure/serializers/jsonapi/student-certification-serializer.js'; -import { StudentForEnrolment } from '../../../../../src/shared/domain/read-models/StudentForEnrolment.js'; -import { domainBuilder, expect } from '../../../../test-helper.js'; +import * as serializer from '../../../../../../src/certification/enrolment/infrastructure/serializers/student-certification-serializer.js'; +import { StudentForEnrolment } from '../../../../../../src/shared/domain/read-models/StudentForEnrolment.js'; +import { domainBuilder, expect } from '../../../../../test-helper.js'; describe('Unit | Serializer | JSONAPI | student-certification-serializer', function () { describe('#serialize', function () { diff --git a/api/tests/unit/application/certification-centers/certification-center-controller_test.js b/api/tests/unit/application/certification-centers/certification-center-controller_test.js index 14ed71d70f2..2d571d233d5 100644 --- a/api/tests/unit/application/certification-centers/certification-center-controller_test.js +++ b/api/tests/unit/application/certification-centers/certification-center-controller_test.js @@ -1,68 +1,9 @@ import { certificationCenterController } from '../../../../lib/application/certification-centers/certification-center-controller.js'; import { usecases } from '../../../../lib/domain/usecases/index.js'; import { usecases as teamUsecases } from '../../../../src/team/domain/usecases/index.js'; -import { domainBuilder, expect, hFake, sinon } from '../../../test-helper.js'; +import { expect, hFake, sinon } from '../../../test-helper.js'; describe('Unit | Controller | certifications-center-controller', function () { - describe('#getStudents', function () { - it('should return a paginated serialized list of students', async function () { - // given - const student = domainBuilder.buildOrganizationLearner({ division: '3A' }); - - const request = { - auth: { - credentials: { userId: '111' }, - }, - params: { - certificationCenterId: 99, - sessionId: 88, - }, - query: { - page: { - size: 10, - number: 1, - }, - filter: { - divisions: '3A', - }, - }, - }; - - sinon - .stub(usecases, 'findStudentsForEnrolment') - .withArgs({ - certificationCenterId: 99, - sessionId: 88, - page: { size: 10, number: 1 }, - filter: { divisions: ['3A'] }, - }) - .resolves({ - data: [student], - pagination: { page: 1, pageSize: 10, rowCount: 1, pageCount: 1 }, - }); - - // when - const response = await certificationCenterController.getStudents(request, hFake); - - // then - expect(response).to.deep.equal({ - data: [ - { - attributes: { - birthdate: student.birthdate, - division: student.division, - 'first-name': student.firstName, - 'last-name': student.lastName, - }, - id: `${student.id}`, - type: 'students', - }, - ], - meta: { page: 1, pageSize: 10, rowCount: 1, pageCount: 1 }, - }); - }); - }); - describe('#getDivisions', function () { it('Should return a serialized list of divisions', async function () { // given diff --git a/api/tests/unit/application/certification-centers/index_test.js b/api/tests/unit/application/certification-centers/index_test.js index 80e1c78afc1..b2e4a72a9df 100644 --- a/api/tests/unit/application/certification-centers/index_test.js +++ b/api/tests/unit/application/certification-centers/index_test.js @@ -39,155 +39,6 @@ describe('Unit | Router | certification-center-router', function () { }); }); - describe('GET /api/certification-centers/{certificationCenterId}/sessions/{sessionId}/students', function () { - it('should reject unexpected filters ', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?filter[unexpected][]=5', - ); - - // then - expect(result.statusCode).to.equal(400); - }); - - it('should accept a string array of one element as division filter ', async function () { - // given - sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); - sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?filter[divisions][]="3EMEB"', - ); - - // then - expect(result.statusCode).to.equal(200); - }); - - it('should accept a string array of several elements as division filter ', async function () { - // given - sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); - sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?filter[divisions][]="3EMEB"&filter[divisions][]="3EMEA"', - ); - - // then - expect(result.statusCode).to.equal(200); - }); - - it('should reject a division filter if it is not an array', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?filter[divisions]="3EMEA"', - ); - - // then - expect(result.statusCode).to.equal(400); - }); - - it('should accept a pagination', async function () { - // given - sinon.stub(certificationCenterController, 'getStudents').callsFake((request, h) => h.response().code(200)); - sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?page[number]=1&page[size]=25', - ); - - // then - expect(result.statusCode).to.equal(200); - }); - - it('should reject a page number which is not a number', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?page[number]=a', - ); - - // then - expect(result.statusCode).to.equal(400); - }); - - it('should reject a page size which is not a number', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - // when - const result = await httpTestServer.request( - 'GET', - '/api/certification-centers/1/sessions/2/students?page[size]=a', - ); - - // then - expect(result.statusCode).to.equal(400); - }); - - it('should accept an empty query string', async function () { - // given - sinon.stub(certificationCenterController, 'getStudents').returns('ok'); - sinon.stub(securityPreHandlers, 'checkUserIsMemberOfCertificationCenter').returns(() => true); - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request('GET', '/api/certification-centers/1/sessions/2/students'); - - // then - expect(result.statusCode).to.equal(200); - }); - - it('should reject an invalid certification-centers id', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request('GET', '/api/certification-centers/invalid/sessions/2/students'); - - // then - expect(result.statusCode).to.equal(400); - }); - - it('should reject an invalid session id', async function () { - // given - const httpTestServer = new HttpTestServer(); - await httpTestServer.register(moduleUnderTest); - - // when - const result = await httpTestServer.request('GET', '/api/certification-centers/1/sessions/invalid/students'); - - // then - expect(result.statusCode).to.equal(400); - }); - }); - describe('GET /api/admin/certification-centers/{certificationCenterId}/certification-center-memberships', function () { const method = 'GET'; const url = '/api/admin/certification-centers/1/certification-center-memberships';