-
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.
Browse files
Browse the repository at this point in the history
* add registration-pin entity * create module registrationPin * changes in user repo and user service * add registrationPindeletion ti vs in deletion module * fix imports * Update apps/server/src/shared/domain/entity/all-entities.ts Co-authored-by: Sergej Hoffmann <[email protected]> * Update apps/server/src/modules/deletion/domain/types/deletion-domain-model.enum.ts Co-authored-by: Sergej Hoffmann <[email protected]> * Update apps/server/src/modules/registration-pin/entity/registration-pin.entity.ts Co-authored-by: Sergej Hoffmann <[email protected]> * Update apps/server/src/shared/repo/user/user.repo.ts Co-authored-by: Sergej Hoffmann <[email protected]> * fix imports * small fixes * small fixes * small fixes * remove spaces * add tests --------- Co-authored-by: Sergej Hoffmann <[email protected]>
- Loading branch information
1 parent
537571d
commit fd8dd82
Showing
26 changed files
with
483 additions
and
5 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
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,6 +1,6 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { DeepMocked, createMock } from '@golevelup/ts-jest'; | ||
import { setupEntities } from '@shared/testing'; | ||
import { setupEntities, userDoFactory } from '@shared/testing'; | ||
import { AccountService } from '@modules/account/services'; | ||
import { ClassService } from '@modules/class'; | ||
import { CourseGroupService, CourseService } from '@modules/learnroom/service'; | ||
|
@@ -12,6 +12,7 @@ import { UserService } from '@modules/user'; | |
import { RocketChatService } from '@modules/rocketchat'; | ||
import { rocketChatUserFactory } from '@modules/rocketchat-user/domain/testing'; | ||
import { RocketChatUser, RocketChatUserService } from '@modules/rocketchat-user'; | ||
import { RegistrationPinService } from '@modules/registration-pin'; | ||
import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum'; | ||
import { DeletionLogService } from '../services/deletion-log.service'; | ||
import { DeletionRequestService } from '../services'; | ||
|
@@ -37,6 +38,7 @@ describe(DeletionRequestUc.name, () => { | |
let userService: DeepMocked<UserService>; | ||
let rocketChatUserService: DeepMocked<RocketChatUserService>; | ||
let rocketChatService: DeepMocked<RocketChatService>; | ||
let registrationPinService: DeepMocked<RegistrationPinService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
|
@@ -94,6 +96,10 @@ describe(DeletionRequestUc.name, () => { | |
provide: RocketChatService, | ||
useValue: createMock<RocketChatService>(), | ||
}, | ||
{ | ||
provide: RegistrationPinService, | ||
useValue: createMock<RegistrationPinService>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
|
@@ -111,6 +117,7 @@ describe(DeletionRequestUc.name, () => { | |
userService = module.get(UserService); | ||
rocketChatUserService = module.get(RocketChatUserService); | ||
rocketChatService = module.get(RocketChatService); | ||
registrationPinService = module.get(RegistrationPinService); | ||
await setupEntities(); | ||
}); | ||
|
||
|
@@ -168,10 +175,13 @@ describe(DeletionRequestUc.name, () => { | |
const setup = () => { | ||
jest.clearAllMocks(); | ||
const deletionRequestToExecute = deletionRequestFactory.build({ deleteAfter: new Date('2023-01-01') }); | ||
const user = userDoFactory.buildWithId(); | ||
const rocketChatUser: RocketChatUser = rocketChatUserFactory.build({ | ||
userId: deletionRequestToExecute.targetRefId, | ||
}); | ||
const parentEmail = '[email protected]'; | ||
|
||
registrationPinService.deleteRegistrationPinByEmail.mockResolvedValueOnce(2); | ||
classService.deleteUserDataFromClasses.mockResolvedValueOnce(1); | ||
courseGroupService.deleteUserDataFromCourseGroup.mockResolvedValueOnce(2); | ||
courseService.deleteUserDataFromCourse.mockResolvedValueOnce(2); | ||
|
@@ -186,6 +196,8 @@ describe(DeletionRequestUc.name, () => { | |
return { | ||
deletionRequestToExecute, | ||
rocketChatUser, | ||
user, | ||
parentEmail, | ||
}; | ||
}; | ||
|
||
|
@@ -215,6 +227,29 @@ describe(DeletionRequestUc.name, () => { | |
expect(accountService.deleteByUserId).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call registrationPinService.deleteRegistrationPinByEmail to delete user data in registrationPin module', async () => { | ||
const { deletionRequestToExecute } = setup(); | ||
|
||
deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]); | ||
|
||
await uc.executeDeletionRequests(); | ||
|
||
expect(registrationPinService.deleteRegistrationPinByEmail).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call userService.getParentEmailsFromUser to get parentEmails', async () => { | ||
const { deletionRequestToExecute, user, parentEmail } = setup(); | ||
|
||
deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]); | ||
userService.findById.mockResolvedValueOnce(user); | ||
userService.getParentEmailsFromUser.mockRejectedValue([parentEmail]); | ||
registrationPinService.deleteRegistrationPinByEmail.mockRejectedValueOnce(2); | ||
|
||
await uc.executeDeletionRequests(); | ||
|
||
expect(userService.getParentEmailsFromUser).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId); | ||
}); | ||
|
||
it('should call classService.deleteUserDataFromClasses to delete user data in class module', async () => { | ||
const { deletionRequestToExecute } = setup(); | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './registration-pin.entity'; |
57 changes: 57 additions & 0 deletions
57
apps/server/src/modules/registration-pin/entity/registration-pin.entity.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,57 @@ | ||
import { setupEntities } from '@shared/testing'; | ||
import { ObjectId } from '@mikro-orm/mongodb'; | ||
import { RegistrationPinEntity } from '.'; | ||
|
||
describe(RegistrationPinEntity.name, () => { | ||
beforeAll(async () => { | ||
await setupEntities(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const setup = () => { | ||
const props = { | ||
id: new ObjectId().toHexString(), | ||
email: '[email protected]', | ||
pin: 'test123', | ||
verified: false, | ||
importHash: '02a00804nnQbLbCDEMVuk56pzZ3A0SC2cYnmM9cyY25IVOnf0K3YCKqW6zxC', | ||
}; | ||
|
||
return { props }; | ||
}; | ||
|
||
describe('constructor', () => { | ||
describe('When constructor is called', () => { | ||
it('should throw an error by empty constructor', () => { | ||
// @ts-expect-error: Test case | ||
const test = () => new RegistrationPinEntity(); | ||
expect(test).toThrow(); | ||
}); | ||
|
||
it('should create a registrationPins by passing required properties', () => { | ||
const { props } = setup(); | ||
const entity: RegistrationPinEntity = new RegistrationPinEntity(props); | ||
|
||
expect(entity instanceof RegistrationPinEntity).toEqual(true); | ||
}); | ||
|
||
it(`should return a valid object with fields values set from the provided complete props object`, () => { | ||
const { props } = setup(); | ||
const entity: RegistrationPinEntity = new RegistrationPinEntity(props); | ||
|
||
const entityProps = { | ||
id: entity.id, | ||
email: entity.email, | ||
pin: entity.pin, | ||
verified: entity.verified, | ||
importHash: entity.importHash, | ||
}; | ||
|
||
expect(entityProps).toEqual(props); | ||
}); | ||
}); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
apps/server/src/modules/registration-pin/entity/registration-pin.entity.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,40 @@ | ||
import { Entity, Index, Property } from '@mikro-orm/core'; | ||
import { EntityId } from '@shared/domain'; | ||
import { BaseEntityWithTimestamps } from '@shared/domain/entity/base.entity'; | ||
|
||
export interface RegistrationPinEntityProps { | ||
id?: EntityId; | ||
email: string; | ||
pin: string; | ||
verified: boolean; | ||
importHash: string; | ||
} | ||
|
||
@Entity({ tableName: 'registrationpins' }) | ||
@Index({ properties: ['email', 'pin'] }) | ||
export class RegistrationPinEntity extends BaseEntityWithTimestamps { | ||
@Property() | ||
@Index() | ||
email: string; | ||
|
||
@Property() | ||
pin: string; | ||
|
||
@Property({ default: false }) | ||
verified: boolean; | ||
|
||
@Property() | ||
@Index() | ||
importHash: string; | ||
|
||
constructor(props: RegistrationPinEntityProps) { | ||
super(); | ||
if (props.id !== undefined) { | ||
this.id = props.id; | ||
} | ||
this.email = props.email; | ||
this.pin = props.pin; | ||
this.verified = props.verified; | ||
this.importHash = props.importHash; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
apps/server/src/modules/registration-pin/entity/testing/factory/index.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 @@ | ||
export * from './registration-pin.entity.factory'; |
18 changes: 18 additions & 0 deletions
18
...er/src/modules/registration-pin/entity/testing/factory/registration-pin.entity.factory.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 { ObjectId } from '@mikro-orm/mongodb'; | ||
import { BaseFactory } from '@shared/testing'; | ||
import { RegistrationPinEntity, RegistrationPinEntityProps } from '../../registration-pin.entity'; | ||
|
||
export const registrationPinEntityFactory = BaseFactory.define<RegistrationPinEntity, RegistrationPinEntityProps>( | ||
RegistrationPinEntity, | ||
({ sequence }) => { | ||
return { | ||
id: new ObjectId().toHexString(), | ||
email: `name-${sequence}@schul-cloud.org`, | ||
pin: `123-${sequence}`, | ||
verified: false, | ||
importHash: `importHash-${sequence}`, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
} | ||
); |
1 change: 1 addition & 0 deletions
1
apps/server/src/modules/registration-pin/entity/testing/index.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 @@ | ||
export * from './factory'; |
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,2 @@ | ||
export * from './registration-pin.module'; | ||
export { RegistrationPinService } from './service'; |
11 changes: 11 additions & 0 deletions
11
apps/server/src/modules/registration-pin/registration-pin.module.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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoggerModule } from '@src/core/logger'; | ||
import { RegistrationPinService } from './service'; | ||
import { RegistrationPinRepo } from './repo'; | ||
|
||
@Module({ | ||
imports: [LoggerModule], | ||
providers: [RegistrationPinService, RegistrationPinRepo], | ||
exports: [RegistrationPinService], | ||
}) | ||
export class RegistrationPinModule {} |
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 @@ | ||
export * from './registration-pin.repo'; |
64 changes: 64 additions & 0 deletions
64
apps/server/src/modules/registration-pin/repo/registration-pin.repo.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,64 @@ | ||
import { EntityManager } from '@mikro-orm/mongodb'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { MongoMemoryDatabaseModule } from '@infra/database'; | ||
import { cleanupCollections, userFactory } from '@shared/testing'; | ||
import { RegistrationPinRepo } from '.'; | ||
import { registrationPinEntityFactory } from '../entity/testing'; | ||
|
||
describe(RegistrationPinRepo.name, () => { | ||
let module: TestingModule; | ||
let repo: RegistrationPinRepo; | ||
let em: EntityManager; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
imports: [MongoMemoryDatabaseModule.forRoot()], | ||
providers: [RegistrationPinRepo], | ||
}).compile(); | ||
|
||
repo = module.get(RegistrationPinRepo); | ||
em = module.get(EntityManager); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await cleanupCollections(em); | ||
}); | ||
|
||
describe('deleteRegistrationPinByEmail', () => { | ||
const setup = async () => { | ||
const user = userFactory.buildWithId(); | ||
const userWithoutRegistrationPin = userFactory.buildWithId(); | ||
const registrationPinForUser = registrationPinEntityFactory.buildWithId({ email: user.email }); | ||
|
||
await em.persistAndFlush(registrationPinForUser); | ||
|
||
return { | ||
user, | ||
userWithoutRegistrationPin, | ||
}; | ||
}; | ||
|
||
describe('when registrationPin exists', () => { | ||
it('should delete registrationPins by email', async () => { | ||
const { user } = await setup(); | ||
|
||
const result: number = await repo.deleteRegistrationPinByEmail(user.email); | ||
|
||
expect(result).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('when there is no registrationPin', () => { | ||
it('should return empty array', async () => { | ||
const { userWithoutRegistrationPin } = await setup(); | ||
|
||
const result: number = await repo.deleteRegistrationPinByEmail(userWithoutRegistrationPin.email); | ||
expect(result).toEqual(0); | ||
}); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
apps/server/src/modules/registration-pin/repo/registration-pin.repo.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,14 @@ | ||
import { EntityManager } from '@mikro-orm/mongodb'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { RegistrationPinEntity } from '../entity'; | ||
|
||
@Injectable() | ||
export class RegistrationPinRepo { | ||
constructor(private readonly em: EntityManager) {} | ||
|
||
async deleteRegistrationPinByEmail(email: string): Promise<number> { | ||
const promise: Promise<number> = this.em.nativeDelete(RegistrationPinEntity, { email }); | ||
|
||
return promise; | ||
} | ||
} |
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 @@ | ||
export * from './registration-pin.service'; |
Oops, something went wrong.