Skip to content

Commit

Permalink
adjust tests (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Dec 17, 2024
1 parent 29534a2 commit 8d19ebf
Showing 1 changed file with 11 additions and 128 deletions.
139 changes: 11 additions & 128 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -37,6 +36,7 @@ describe(TspSyncStrategy.name, () => {
let provisioningService: DeepMocked<ProvisioningService>;
let tspOauthDataMapper: DeepMocked<TspOauthDataMapper>;
let tspLegacyMigrationService: DeepMocked<TspLegacyMigrationService>;
let tspSyncMigrationService: DeepMocked<TspSyncMigrationService>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand All @@ -56,26 +56,7 @@ describe(TspSyncStrategy.name, () => {
},
{
provide: ConfigService,
useValue: createMock<ConfigService<TspSyncConfig, true>>({
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<ConfigService<TspSyncConfig, true>>(),
},
{
provide: ProvisioningService,
Expand All @@ -89,6 +70,10 @@ describe(TspSyncStrategy.name, () => {
provide: TspLegacyMigrationService,
useValue: createMock<TspLegacyMigrationService>(),
},
{
provide: TspSyncMigrationService,
useValue: createMock<TspSyncMigrationService>(),
},
],
}).compile();

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 ?? []);
Expand Down Expand Up @@ -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();
});
});
});
Expand Down Expand Up @@ -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();
});
});
});
});

0 comments on commit 8d19ebf

Please sign in to comment.