|
| 1 | +import { HttpStatus, INestApplication } from "@nestjs/common"; |
| 2 | +import * as request from "supertest"; |
| 3 | +import { |
| 4 | + BEARER_AUTH_TYPE, |
| 5 | + createTestingAppModule, |
| 6 | + FakeStudentUsersTypes, |
| 7 | + getStudentToken, |
| 8 | + mockUserLoginInfo, |
| 9 | +} from "../../../../testHelpers"; |
| 10 | +import { |
| 11 | + createE2EDataSources, |
| 12 | + E2EDataSources, |
| 13 | + RestrictionCode, |
| 14 | + saveFakeStudent, |
| 15 | + saveFakeStudentRestriction, |
| 16 | +} from "@sims/test-utils"; |
| 17 | +import { TestingModule } from "@nestjs/testing"; |
| 18 | +import { In } from "typeorm"; |
| 19 | +import { RestrictionNotificationType } from "@sims/sims-db"; |
| 20 | + |
| 21 | +describe("RestrictionStudentsController(e2e)-getStudentRestrictions", () => { |
| 22 | + let app: INestApplication; |
| 23 | + let db: E2EDataSources; |
| 24 | + let appModule: TestingModule; |
| 25 | + const endpoint = "/students/restriction"; |
| 26 | + |
| 27 | + beforeAll(async () => { |
| 28 | + const { nestApplication, module, dataSource } = |
| 29 | + await createTestingAppModule(); |
| 30 | + app = nestApplication; |
| 31 | + db = createE2EDataSources(dataSource); |
| 32 | + appModule = module; |
| 33 | + }); |
| 34 | + |
| 35 | + it(`Should get the active student restrictions including legacy restrictions and skipping '${RestrictionNotificationType.NoEffect}' when the student has active student restrictions.`, async () => { |
| 36 | + // Arrange |
| 37 | + const student = await saveFakeStudent(db.dataSource); |
| 38 | + // Restrictions to be associated with the student. |
| 39 | + const restrictions = await db.restriction.find({ |
| 40 | + select: { |
| 41 | + id: true, |
| 42 | + }, |
| 43 | + where: { |
| 44 | + restrictionCode: In([ |
| 45 | + // 'No effect' restrictions. |
| 46 | + RestrictionCode.LGCYAAAA, |
| 47 | + RestrictionCode.AF4, |
| 48 | + // Notification others than 'No effect' that should be returned. |
| 49 | + RestrictionCode.B6A, |
| 50 | + RestrictionCode.LGCYBBBB, |
| 51 | + RestrictionCode.LGCYCCCC, |
| 52 | + ]), |
| 53 | + }, |
| 54 | + }); |
| 55 | + // Associate all restriction with the student. |
| 56 | + const savePromises = restrictions.map((restriction) => |
| 57 | + saveFakeStudentRestriction(db.dataSource, { |
| 58 | + restriction, |
| 59 | + student, |
| 60 | + }), |
| 61 | + ); |
| 62 | + await Promise.all(savePromises); |
| 63 | + |
| 64 | + // Mock user service to return the saved student. |
| 65 | + await mockUserLoginInfo(appModule, student); |
| 66 | + |
| 67 | + // Get any student user token. |
| 68 | + const studentToken = await getStudentToken( |
| 69 | + FakeStudentUsersTypes.FakeStudentUserType1, |
| 70 | + ); |
| 71 | + |
| 72 | + // Act/Assert |
| 73 | + await request(app.getHttpServer()) |
| 74 | + .get(endpoint) |
| 75 | + .auth(studentToken, BEARER_AUTH_TYPE) |
| 76 | + .expect(HttpStatus.OK) |
| 77 | + .expect([ |
| 78 | + { code: RestrictionCode.B6A, type: RestrictionNotificationType.Error }, |
| 79 | + { code: RestrictionCode.LGCY, type: RestrictionNotificationType.Error }, |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + afterAll(async () => { |
| 84 | + await app?.close(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments