diff --git a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts index b4303779ee..5e1ccb52a2 100644 --- a/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts +++ b/apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts @@ -12,10 +12,8 @@ import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { UserDO } from '@shared/domain/domainobject'; import { SystemProvisioningStrategy } from '@shared/domain/interface/system-provisioning.strategy'; -import { userDoFactory } from '@shared/testing'; import { Logger } from '@src/core/logger'; import { Account } from '@src/modules/account'; -import { accountDoFactory } from '@src/modules/account/testing'; import { ExternalUserDto, OauthDataDto, ProvisioningService, ProvisioningSystemDto } from '@src/modules/provisioning'; import { School } from '@src/modules/school'; import { schoolFactory } from '@src/modules/school/testing'; @@ -28,6 +26,7 @@ import { TspOauthDataMapper } from './tsp-oauth-data.mapper'; import { TspSyncConfig } from './tsp-sync.config'; import { TspSyncService } from './tsp-sync.service'; import { TspSyncStrategy } from './tsp-sync.strategy'; +import { TspSyncMigrationService } from './tsp-sync-migration.service'; describe(TspSyncStrategy.name, () => { let module: TestingModule; @@ -37,6 +36,7 @@ describe(TspSyncStrategy.name, () => { let provisioningService: DeepMocked; let tspOauthDataMapper: DeepMocked; let tspLegacyMigrationService: DeepMocked; + let tspSyncMigrationService: DeepMocked; beforeAll(async () => { module = await Test.createTestingModule({ @@ -56,26 +56,7 @@ describe(TspSyncStrategy.name, () => { }, { provide: ConfigService, - useValue: createMock>({ - getOrThrow: (key: string) => { - switch (key) { - case 'TSP_SYNC_SCHOOL_LIMIT': - return 10; - case 'TSP_SYNC_SCHOOL_DAYS_TO_FETCH': - return 1; - case 'TSP_SYNC_DATA_LIMIT': - return 10; - case 'TSP_SYNC_DATA_DAYS_TO_FETCH': - return 1; - case 'TSP_SYNC_MIGRATION_LIMIT': - return 10; - case 'FEATURE_TSP_MIGRATION_ENABLED': - return true; - default: - throw new Error(`Unknown key: ${key}`); - } - }, - }), + useValue: createMock>(), }, { provide: ProvisioningService, @@ -89,6 +70,10 @@ describe(TspSyncStrategy.name, () => { provide: TspLegacyMigrationService, useValue: createMock(), }, + { + provide: TspSyncMigrationService, + useValue: createMock(), + }, ], }).compile(); @@ -98,11 +83,13 @@ describe(TspSyncStrategy.name, () => { provisioningService = module.get(ProvisioningService); tspOauthDataMapper = module.get(TspOauthDataMapper); tspLegacyMigrationService = module.get(TspLegacyMigrationService); + tspSyncMigrationService = module.get(TspSyncMigrationService); }); afterEach(() => { jest.clearAllMocks(); jest.resetAllMocks(); + jest.restoreAllMocks(); }); afterAll(async () => { @@ -148,14 +135,6 @@ describe(TspSyncStrategy.name, () => { tspSyncService.findSchool.mockResolvedValue(params.foundSchool ?? undefined); tspSyncService.findSchoolsForSystem.mockResolvedValueOnce(params.foundSystemSchools ?? []); - tspSyncService.findUserByTspUid.mockResolvedValueOnce( - params.foundTspUidUser !== undefined ? params.foundTspUidUser : userDoFactory.build() - ); - tspSyncService.updateUser.mockResolvedValueOnce(params.updatedUser ?? userDoFactory.build()); - tspSyncService.findAccountByExternalId.mockResolvedValueOnce( - params.foundTspUidAccount !== undefined ? params.foundTspUidAccount : accountDoFactory.build() - ); - tspSyncService.updateAccount.mockResolvedValueOnce(params.updatedAccount ?? accountDoFactory.build()); tspSyncService.findTspSystemOrFail.mockResolvedValueOnce(params.foundSystem ?? systemFactory.build()); tspOauthDataMapper.mapTspDataToOauthData.mockReturnValueOnce(params.mappedOauthDto ?? []); @@ -266,36 +245,12 @@ describe(TspSyncStrategy.name, () => { expect(tspFetchService.fetchTspStudentMigrations).toHaveBeenCalled(); }); - it('find user by tsp Uid', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.findUserByTspUid).toHaveBeenCalled(); - }); - - it('should update user', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.updateUser).toHaveBeenCalled(); - }); - - it('should find account by tsp Uid', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.findAccountByExternalId).toHaveBeenCalled(); - }); - - it('should update account', async () => { + it('should call tspSyncMigrationService', async () => { setup(); await sut.sync(); - expect(tspSyncService.updateAccount).toHaveBeenCalled(); + expect(tspSyncMigrationService.migrateTspUsers).toHaveBeenCalled(); }); }); }); @@ -369,77 +324,5 @@ describe(TspSyncStrategy.name, () => { expect(tspSyncService.createSchool).not.toHaveBeenCalled(); }); }); - - describe('when UidAlt or UidNeu is missing during migration', () => { - const setup = () => { - const tspTeacher: RobjExportLehrerMigration = { - lehrerUidAlt: undefined, - lehrerUidNeu: faker.string.alpha(), - }; - const tspStudent: RobjExportSchuelerMigration = { - schuelerUidAlt: faker.string.alpha(), - schuelerUidNeu: undefined, - }; - - setupMockServices({ - fetchedStudentMigrations: [tspStudent], - fetchedTeacherMigrations: [tspTeacher], - }); - }; - - it('should return false and not call findUserByTspUid', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.findUserByTspUid).not.toHaveBeenCalled(); - }); - }); - - describe('when no user is found during migration', () => { - const setup = () => { - const tspTeacher: RobjExportLehrerMigration = { - lehrerUidAlt: faker.string.alpha(), - lehrerUidNeu: faker.string.alpha(), - }; - - setupMockServices({ - fetchedTeacherMigrations: [tspTeacher], - foundTspUidUser: null, - }); - - return { tspTeacher }; - }; - - it('should throw and not call updateUser', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.updateUser).not.toHaveBeenCalled(); - }); - }); - - describe('when no account is found during migration', () => { - const setup = () => { - const tspTeacher: RobjExportLehrerMigration = { - lehrerUidAlt: faker.string.alpha(), - lehrerUidNeu: faker.string.alpha(), - }; - - setupMockServices({ - fetchedTeacherMigrations: [tspTeacher], - foundTspUidAccount: null, - }); - }; - - it('should throw and not call updateAccount', async () => { - setup(); - - await sut.sync(); - - expect(tspSyncService.updateAccount).not.toHaveBeenCalled(); - }); - }); }); });