-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): prevent session from being cancelled when already finalized
- Loading branch information
1 parent
1405167
commit 16052c5
Showing
3 changed files
with
79 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
api/src/certification/session-management/domain/usecases/cancel-certification-course.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 65 additions & 19 deletions
84
...certification/session-management/unit/domain/usecases/cancel-certification-course_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,74 @@ | ||
import CertificationCancelled from '../../../../../../lib/domain/events/CertificationCancelled.js'; | ||
import { SessionAlreadyFinalizedError } from '../../../../../../src/certification/session-management/domain/errors.js'; | ||
import { cancelCertificationCourse } from '../../../../../../src/certification/session-management/domain/usecases/cancel-certification-course.js'; | ||
import { domainBuilder, expect, sinon } from '../../../../../test-helper.js'; | ||
import { catchErr, domainBuilder, expect, sinon } from '../../../../../test-helper.js'; | ||
|
||
describe('Certification | Session-management | Unit | Domain | UseCases | cancel-certification-course', function () { | ||
it('should cancel the certification course', async function () { | ||
// given | ||
const certificationCourse = domainBuilder.buildCertificationCourse({ id: 123 }); | ||
sinon.spy(certificationCourse, 'cancel'); | ||
const certificationCourseRepository = { | ||
update: sinon.stub(), | ||
get: sinon.stub(), | ||
}; | ||
certificationCourseRepository.get.withArgs({ id: 123 }).resolves(certificationCourse); | ||
certificationCourseRepository.update.resolves(); | ||
describe('when session is finalized', function () { | ||
it('should cancel the certification course', async function () { | ||
// given | ||
const juryId = 123; | ||
const session = domainBuilder.certification.sessionManagement.buildSession({ | ||
finalizedAt: new Date('2020-01-01'), | ||
}); | ||
const certificationCourse = domainBuilder.buildCertificationCourse({ id: 123, sessionId: session.id }); | ||
sinon.spy(certificationCourse, 'cancel'); | ||
const certificationCourseRepository = { | ||
update: sinon.stub(), | ||
get: sinon.stub(), | ||
}; | ||
const sessionRepository = { | ||
get: sinon.stub(), | ||
}; | ||
certificationCourseRepository.get.withArgs({ id: 123 }).resolves(certificationCourse); | ||
certificationCourseRepository.update.resolves(); | ||
sessionRepository.get.withArgs({ id: certificationCourse.sessionId }).resolves(session); | ||
|
||
// when | ||
const cancelledEvent = await cancelCertificationCourse({ | ||
certificationCourseId: 123, | ||
certificationCourseRepository, | ||
// when | ||
const cancelledEvent = await cancelCertificationCourse({ | ||
certificationCourseId: 123, | ||
certificationCourseRepository, | ||
sessionRepository, | ||
juryId, | ||
}); | ||
|
||
// then | ||
expect(certificationCourse.cancel).to.have.been.calledOnce; | ||
expect(certificationCourseRepository.update).to.have.been.calledWithExactly({ certificationCourse }); | ||
expect(cancelledEvent).to.deepEqualInstance(new CertificationCancelled({ certificationCourseId: 123, juryId })); | ||
}); | ||
}); | ||
|
||
describe('when session is not finalized', function () { | ||
it('should not cancel the certification course', async function () { | ||
// given | ||
const juryId = 123; | ||
const session = domainBuilder.certification.sessionManagement.buildSession({ finalizedAt: null }); | ||
const certificationCourse = domainBuilder.buildCertificationCourse({ id: 123, sessionId: session.id }); | ||
sinon.spy(certificationCourse, 'cancel'); | ||
const certificationCourseRepository = { | ||
update: sinon.stub(), | ||
get: sinon.stub(), | ||
}; | ||
const sessionRepository = { | ||
get: sinon.stub(), | ||
}; | ||
certificationCourseRepository.get.withArgs({ id: 123 }).resolves(certificationCourse); | ||
certificationCourseRepository.update.resolves(); | ||
sessionRepository.get.withArgs({ id: certificationCourse.sessionId }).resolves(session); | ||
|
||
// then | ||
expect(certificationCourse.cancel).to.have.been.calledOnce; | ||
expect(certificationCourseRepository.update).to.have.been.calledWithExactly({ certificationCourse }); | ||
expect(cancelledEvent).to.deepEqualInstance(new CertificationCancelled({ certificationCourseId: 123 })); | ||
// when | ||
const error = await catchErr(cancelCertificationCourse)({ | ||
certificationCourseId: 123, | ||
certificationCourseRepository, | ||
sessionRepository, | ||
juryId, | ||
}); | ||
|
||
// then | ||
expect(certificationCourse.cancel).to.not.have.been.called; | ||
expect(certificationCourseRepository.update).to.not.have.been.called; | ||
expect(error).to.be.instanceOf(SessionAlreadyFinalizedError); | ||
}); | ||
}); | ||
}); |