-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'BC-7695-update-typescript' of github.com:hpi-schul-clou…
…d/schulcloud-server into BC-7695-update-typescript
- Loading branch information
Showing
24 changed files
with
758 additions
and
16 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
apps/server/src/migrations/mikro-orm/Migration20240724090901.ts
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Migration } from '@mikro-orm/migrations-mongodb'; | ||
|
||
export class Migration20240724090901 extends Migration { | ||
async up(): Promise<void> { | ||
const superheroRoleUpdate = await this.getCollection('roles').updateOne( | ||
{ name: 'superhero' }, | ||
{ | ||
$addToSet: { | ||
permissions: { | ||
$each: ['USER_LOGIN_MIGRATION_FORCE'], | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
if (superheroRoleUpdate.modifiedCount > 0) { | ||
console.info('Permission USER_LOGIN_MIGRATION_FORCE was added to role superhero.'); | ||
} | ||
} | ||
|
||
async down(): Promise<void> { | ||
const superheroRoleUpdate = await this.getCollection('roles').updateOne( | ||
{ name: 'superhero' }, | ||
{ | ||
$pull: { | ||
permissions: { | ||
$in: ['USER_LOGIN_MIGRATION_FORCE'], | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
if (superheroRoleUpdate.modifiedCount > 0) { | ||
console.info('Rollback: Removed permission USER_LOGIN_MIGRATION_FORCE from role superhero.'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -27,8 +27,7 @@ import axios from 'axios'; | |
import MockAdapter from 'axios-mock-adapter'; | ||
import { UUID } from 'bson'; | ||
import { Response } from 'supertest'; | ||
import { UserLoginMigrationResponse } from '../dto'; | ||
import { Oauth2MigrationParams } from '../dto/oauth2-migration.params'; | ||
import { ForceMigrationParams, Oauth2MigrationParams, UserLoginMigrationResponse } from '../dto'; | ||
|
||
jest.mock('jwks-rsa', () => () => { | ||
return { | ||
|
@@ -1405,4 +1404,99 @@ describe('UserLoginMigrationController (API)', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('[GET] /user-login-migrations/force-migration', () => { | ||
describe('when forcing a school to migrate', () => { | ||
const setup = async () => { | ||
const targetSystem: SystemEntity = systemEntityFactory | ||
.withOauthConfig() | ||
.buildWithId({ alias: 'SANIS', provisioningStrategy: SystemProvisioningStrategy.SANIS }); | ||
|
||
const sourceSystem: SystemEntity = systemEntityFactory.buildWithId(); | ||
|
||
const school: SchoolEntity = schoolEntityFactory.buildWithId({ | ||
systems: [sourceSystem], | ||
}); | ||
|
||
const email = '[email protected]'; | ||
const { adminAccount, adminUser } = UserAndAccountTestFactory.buildAdmin({ | ||
email, | ||
school, | ||
}); | ||
const { superheroAccount, superheroUser } = UserAndAccountTestFactory.buildSuperhero(); | ||
|
||
await em.persistAndFlush([ | ||
sourceSystem, | ||
targetSystem, | ||
school, | ||
superheroAccount, | ||
superheroUser, | ||
adminAccount, | ||
adminUser, | ||
]); | ||
em.clear(); | ||
|
||
const loggedInClient = await testApiClient.login(superheroAccount); | ||
|
||
const requestBody: ForceMigrationParams = new ForceMigrationParams(); | ||
requestBody.email = email; | ||
requestBody.externalUserId = 'externalUserId'; | ||
requestBody.externalSchoolId = 'externalSchoolId'; | ||
|
||
return { | ||
requestBody, | ||
loggedInClient, | ||
sourceSystem, | ||
targetSystem, | ||
school, | ||
adminUser, | ||
}; | ||
}; | ||
|
||
it('should start the migration for the school and migrate the user and school', async () => { | ||
const { requestBody, loggedInClient, school, sourceSystem, targetSystem, adminUser } = await setup(); | ||
|
||
const response: Response = await loggedInClient.post(`/force-migration`, requestBody); | ||
|
||
expect(response.status).toEqual(HttpStatus.CREATED); | ||
|
||
const userLoginMigration = await em.findOneOrFail(UserLoginMigrationEntity, { school: school.id }); | ||
expect(userLoginMigration.sourceSystem?.id).toEqual(sourceSystem.id); | ||
expect(userLoginMigration.targetSystem.id).toEqual(targetSystem.id); | ||
|
||
expect(await em.findOne(User, adminUser.id)).toEqual( | ||
expect.objectContaining({ | ||
externalId: requestBody.externalUserId, | ||
}) | ||
); | ||
|
||
expect(await em.findOne(SchoolEntity, school.id)).toEqual( | ||
expect.objectContaining({ | ||
externalId: requestBody.externalSchoolId, | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when authentication of user failed', () => { | ||
const setup = () => { | ||
const requestBody: ForceMigrationParams = new ForceMigrationParams(); | ||
requestBody.email = '[email protected]'; | ||
requestBody.externalUserId = 'externalUserId'; | ||
requestBody.externalSchoolId = 'externalSchoolId'; | ||
|
||
return { | ||
requestBody, | ||
}; | ||
}; | ||
|
||
it('should throw an UnauthorizedException', async () => { | ||
const { requestBody } = setup(); | ||
|
||
const response: Response = await testApiClient.post(`/force-migration`, requestBody); | ||
|
||
expect(response.status).toEqual(HttpStatus.UNAUTHORIZED); | ||
}); | ||
}); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
.../server/src/modules/user-login-migration/controller/dto/request/force-migration.params.ts
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export class ForceMigrationParams { | ||
@IsEmail() | ||
@ApiProperty({ description: 'Email of the administrator' }) | ||
email!: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
@ApiProperty({ description: 'Target externalId to link it with an external account' }) | ||
externalUserId!: string; | ||
|
||
@IsString() | ||
@IsNotEmpty() | ||
@ApiProperty({ description: 'Target externalId to link it with an external school' }) | ||
externalSchoolId!: string; | ||
} |
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
File renamed without changes.
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
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
31 changes: 31 additions & 0 deletions
31
...er-login-migration/loggable/user-login-migration-invalid-admin.loggable-exception.spec.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { UserLoginMigrationInvalidAdminLoggableException } from './user-login-migration-invalid-admin.loggable-exception'; | ||
|
||
describe(UserLoginMigrationInvalidAdminLoggableException.name, () => { | ||
describe('getLogMessage', () => { | ||
const setup = () => { | ||
const userId = new ObjectId().toHexString(); | ||
const exception = new UserLoginMigrationInvalidAdminLoggableException(userId); | ||
|
||
return { | ||
exception, | ||
userId, | ||
}; | ||
}; | ||
|
||
it('should return the correct log message', () => { | ||
const { exception, userId } = setup(); | ||
|
||
const message = exception.getLogMessage(); | ||
|
||
expect(message).toEqual({ | ||
type: 'USER_LOGIN_MIGRATION_INVALID_ADMIN', | ||
message: 'The user is not an administrator', | ||
stack: exception.stack, | ||
data: { | ||
userId, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
...es/user-login-migration/loggable/user-login-migration-invalid-admin.loggable-exception.ts
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { UnprocessableEntityException } from '@nestjs/common'; | ||
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger'; | ||
|
||
export class UserLoginMigrationInvalidAdminLoggableException extends UnprocessableEntityException implements Loggable { | ||
constructor(private readonly userId: string) { | ||
super(); | ||
} | ||
|
||
getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage { | ||
return { | ||
type: 'USER_LOGIN_MIGRATION_INVALID_ADMIN', | ||
message: 'The user is not an administrator', | ||
stack: this.stack, | ||
data: { | ||
userId: this.userId, | ||
}, | ||
}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...n-migration/loggable/user-login-migration-multiple-email-users.loggable-exception.spec.ts
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { UserLoginMigrationMultipleEmailUsersLoggableException } from './user-login-migration-multiple-email-users.loggable-exception'; | ||
|
||
describe(UserLoginMigrationMultipleEmailUsersLoggableException.name, () => { | ||
describe('getLogMessage', () => { | ||
const setup = () => { | ||
const email = '[email protected]'; | ||
const exception = new UserLoginMigrationMultipleEmailUsersLoggableException(email); | ||
|
||
return { | ||
exception, | ||
email, | ||
}; | ||
}; | ||
|
||
it('should return the correct log message', () => { | ||
const { exception, email } = setup(); | ||
|
||
const message = exception.getLogMessage(); | ||
|
||
expect(message).toEqual({ | ||
type: 'USER_LOGIN_MIGRATION_MULTIPLE_EMAIL_USERS', | ||
message: 'There is multiple users with this email', | ||
stack: exception.stack, | ||
data: { | ||
email, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...-login-migration/loggable/user-login-migration-multiple-email-users.loggable-exception.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { UnprocessableEntityException } from '@nestjs/common'; | ||
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger'; | ||
|
||
export class UserLoginMigrationMultipleEmailUsersLoggableException | ||
extends UnprocessableEntityException | ||
implements Loggable | ||
{ | ||
constructor(private readonly email: string) { | ||
super(); | ||
} | ||
|
||
getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage { | ||
return { | ||
type: 'USER_LOGIN_MIGRATION_MULTIPLE_EMAIL_USERS', | ||
message: 'There is multiple users with this email', | ||
stack: this.stack, | ||
data: { | ||
email: this.email, | ||
}, | ||
}; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...igration/loggable/user-login-migration-school-already-migrated.loggable-exception.spec.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { UserLoginMigrationSchoolAlreadyMigratedLoggableException } from './user-login-migration-school-already-migrated.loggable-exception'; | ||
|
||
describe(UserLoginMigrationSchoolAlreadyMigratedLoggableException.name, () => { | ||
describe('getLogMessage', () => { | ||
const setup = () => { | ||
const schoolId = new ObjectId().toHexString(); | ||
const exception = new UserLoginMigrationSchoolAlreadyMigratedLoggableException(schoolId); | ||
|
||
return { | ||
exception, | ||
schoolId, | ||
}; | ||
}; | ||
|
||
it('should return the correct log message', () => { | ||
const { exception, schoolId } = setup(); | ||
|
||
const message = exception.getLogMessage(); | ||
|
||
expect(message).toEqual({ | ||
type: 'USER_LOGIN_MIGRATION_SCHOOL_HAS_ALREADY_MIGRATED', | ||
message: 'School has already migrated', | ||
stack: exception.stack, | ||
data: { | ||
schoolId, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.