Skip to content

Commit

Permalink
Move shareTokenDoFactory to sharing module
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Jan 16, 2025
1 parent e88e7ef commit d21b84e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { CopyElementType, CopyStatus, CopyStatusEnum } from '@modules/copy-helpe
import { Test, TestingModule } from '@nestjs/testing';
import { courseFactory } from '@testing/factory/course.factory';
import { currentUserFactory } from '@testing/factory/currentuser.factory';
import { shareTokenFactory } from '@testing/factory/share-token.do.factory';
import { setupEntities } from '@testing/setup-entities';
import { ShareTokenParentType } from '../domainobject/share-token.do';
import { ShareTokenUC } from '../uc';
import { ShareTokenInfoDto } from '../uc/dto';
import { ShareTokenController } from './share-token.controller';
import { shareTokenDOFactory } from '../testing/share-token.do.factory';

describe('ShareTokenController', () => {
let module: TestingModule;
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('ShareTokenController', () => {
describe('creating a token', () => {
const setup = () => {
const currentUser = currentUserFactory.build();
const shareToken = shareTokenFactory.build({ token: 'ctuW1FG0RsTo' });
const shareToken = shareTokenDOFactory.build({ token: 'ctuW1FG0RsTo' });
uc.createShareToken.mockResolvedValue(shareToken);
const body = {
parentType: shareToken.payload.parentType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Test, TestingModule } from '@nestjs/testing';
import { LegacyLogger } from '@src/core/logger';
import { cleanupCollections } from '@testing/cleanup-collections';
import { schoolEntityFactory } from '@testing/factory/school-entity.factory';
import { shareTokenFactory } from '@testing/factory/share-token.do.factory';
import { ShareTokenContextType } from '../domainobject/share-token.do';
import { shareTokenDOFactory } from '../testing/share-token.do.factory';
import { ShareTokenRepo } from './share-token.repo';

describe('ShareTokenRepo', () => {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('ShareTokenRepo', () => {

describe('findOneByToken', () => {
it('should find a shareToken by its token', async () => {
const shareToken = shareTokenFactory.build();
const shareToken = shareTokenDOFactory.build();
await repo.save(shareToken);

const result = await repo.findOneByToken(shareToken.token);
Expand All @@ -51,7 +51,7 @@ describe('ShareTokenRepo', () => {
it('should include context id', async () => {
const school = schoolEntityFactory.build();
await em.persistAndFlush([school]);
const shareToken = shareTokenFactory.build({
const shareToken = shareTokenDOFactory.build({
context: { contextType: ShareTokenContextType.School, contextId: school.id },
});
await repo.save(shareToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { courseFactory } from '@testing/factory/course.factory';
import { lessonFactory } from '@testing/factory/lesson.factory';
import { shareTokenFactory } from '@testing/factory/share-token.do.factory';
import { taskFactory } from '@testing/factory/task.factory';
import { setupEntities } from '@testing/setup-entities';
import { ShareTokenContextType, ShareTokenParentType } from '../domainobject/share-token.do';
import { ShareTokenRepo } from '../repo/share-token.repo';
import { shareTokenDOFactory } from '../testing/share-token.do.factory';
import { ShareTokenService } from './share-token.service';
import { TokenGenerator } from './token-generator.service';

Expand Down Expand Up @@ -127,7 +127,7 @@ describe('ShareTokenService', () => {

describe('lookup', () => {
it('should lookup a shareToken using a token', async () => {
const shareToken = shareTokenFactory.build();
const shareToken = shareTokenDOFactory.build();
repo.findOneByToken.mockResolvedValue(shareToken);

const result = await service.lookupToken(shareToken.token);
Expand All @@ -144,7 +144,7 @@ describe('ShareTokenService', () => {
});

it('should throw an error when shareToken is expired', async () => {
const shareToken = shareTokenFactory.build({ expiresAt: new Date(Date.now() - 10000) });
const shareToken = shareTokenDOFactory.build({ expiresAt: new Date(Date.now() - 10000) });
repo.findOneByToken.mockResolvedValue(shareToken);

const lookupToken = async () => service.lookupToken(shareToken.token);
Expand All @@ -159,7 +159,7 @@ describe('ShareTokenService', () => {
courseService.findById.mockResolvedValue(course);

const payload = { parentId: course.id, parentType: ShareTokenParentType.Course };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
repo.findOneByToken.mockResolvedValue(shareToken);

const result = await service.lookupTokenWithParentName(shareToken.token);
Expand All @@ -172,7 +172,7 @@ describe('ShareTokenService', () => {
lessonService.findById.mockResolvedValue(lesson);

const payload = { parentId: lesson.id, parentType: ShareTokenParentType.Lesson };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
repo.findOneByToken.mockResolvedValue(shareToken);

const result = await service.lookupTokenWithParentName(shareToken.token);
Expand All @@ -185,7 +185,7 @@ describe('ShareTokenService', () => {
taskService.findById.mockResolvedValue(task);

const payload = { parentId: task.id, parentType: ShareTokenParentType.Task };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
repo.findOneByToken.mockResolvedValue(shareToken);

const result = await service.lookupTokenWithParentName(shareToken.token);
Expand All @@ -199,7 +199,7 @@ describe('ShareTokenService', () => {
columnBoardService.findById.mockResolvedValue(columnBoard);

const payload = { parentId: columnBoard.id, parentType: ShareTokenParentType.ColumnBoard };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
repo.findOneByToken.mockResolvedValue(shareToken);

return { columnBoard, shareToken };
Expand All @@ -215,7 +215,7 @@ describe('ShareTokenService', () => {
it('should throw if parent type is not supported', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const shareToken = shareTokenFactory.build({ payload: { parentType: 'invalid' } });
const shareToken = shareTokenDOFactory.build({ payload: { parentType: 'invalid' } });
repo.findOneByToken.mockResolvedValue(shareToken);

const lookupToken = async () => service.lookupTokenWithParentName(shareToken.token);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* istanbul ignore file */
import { ObjectId } from '@mikro-orm/mongodb';
import { ShareTokenDO, ShareTokenParentType } from '@modules/sharing/domainobject/share-token.do';
import { EntityId } from '@shared/domain/types';
import { ObjectId } from '@mikro-orm/mongodb';
import { Factory } from 'fishery';

class ShareTokenFactory extends Factory<ShareTokenDO> {
class ShareTokenDOFactory extends Factory<ShareTokenDO> {
/* istanbul ignore next */
withId(id?: EntityId) {
return this.params({ id: new ObjectId(id).toHexString() });
}
}

export const shareTokenFactory = ShareTokenFactory.define(({ sequence }) => {
export const shareTokenDOFactory = ShareTokenDOFactory.define(({ sequence }) => {
return {
token: `share-token-${sequence}`,
payload: {
Expand Down
36 changes: 18 additions & 18 deletions apps/server/src/modules/sharing/uc/share-token.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { StorageLocation } from '@modules/files-storage/interface';
import { CourseCopyService, CourseService } from '@modules/learnroom';
import { LessonCopyService, LessonService } from '@modules/lesson';
import { RoomService } from '@modules/room';
import { RoomMembershipService } from '@modules/room-membership';
import { SchoolService } from '@modules/school';
import { schoolFactory } from '@modules/school/testing';
import { TaskCopyService, TaskService } from '@modules/task';
Expand All @@ -18,16 +19,15 @@ import { Test, TestingModule } from '@nestjs/testing';
import { FeatureDisabledLoggableException } from '@shared/common/loggable-exception';
import { Permission } from '@shared/domain/interface';
import { LegacyLogger } from '@src/core/logger';
import { RoomMembershipService } from '@src/modules/room-membership';
import { courseFactory } from '@testing/factory/course.factory';
import { lessonFactory } from '@testing/factory/lesson.factory';
import { schoolEntityFactory } from '@testing/factory/school-entity.factory';
import { shareTokenFactory } from '@testing/factory/share-token.do.factory';
import { taskFactory } from '@testing/factory/task.factory';
import { userFactory } from '@testing/factory/user.factory';
import { setupEntities } from '@testing/setup-entities';
import { ShareTokenContextType, ShareTokenParentType, ShareTokenPayload } from '../domainobject/share-token.do';
import { ShareTokenService } from '../service';
import { shareTokenDOFactory } from '../testing/share-token.do.factory';
import { ShareTokenUC } from './share-token.uc';

describe('ShareTokenUC', () => {
Expand Down Expand Up @@ -492,7 +492,7 @@ describe('ShareTokenUC', () => {
it('should return service result', async () => {
const user = userFactory.buildWithId();
const course = courseFactory.buildWithId();
const shareToken = shareTokenFactory.build();
const shareToken = shareTokenDOFactory.build();

authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);
courseService.findById.mockResolvedValueOnce(course);
Expand Down Expand Up @@ -521,7 +521,7 @@ describe('ShareTokenUC', () => {
parentType: ShareTokenParentType.Course,
parentId: course.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName: course.name });

return { user, school, shareToken, course };
Expand Down Expand Up @@ -575,7 +575,7 @@ describe('ShareTokenUC', () => {
parentType: ShareTokenParentType.Lesson,
parentId: lesson.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName: lesson.name });

return { user, school, shareToken, lesson, course };
Expand Down Expand Up @@ -629,7 +629,7 @@ describe('ShareTokenUC', () => {
parentType: ShareTokenParentType.Task,
parentId: task.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName: task.name });

return { user, school, shareToken, task, course };
Expand Down Expand Up @@ -681,7 +681,7 @@ describe('ShareTokenUC', () => {
parentType: ShareTokenParentType.ColumnBoard,
parentId: columnBoard.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName: columnBoard.title });

columnBoardService.findById.mockResolvedValueOnce(columnBoard);
Expand Down Expand Up @@ -730,7 +730,7 @@ describe('ShareTokenUC', () => {
const schoolEntity = schoolEntityFactory.buildWithId();
const school = schoolFactory.build();
const user = userFactory.buildWithId({ school: schoolEntity });
const shareToken = shareTokenFactory.build({
const shareToken = shareTokenDOFactory.build({
context: { contextType: ShareTokenContextType.School, contextId: schoolEntity.id },
});
const parentName = 'name';
Expand Down Expand Up @@ -769,7 +769,7 @@ describe('ShareTokenUC', () => {
const setup = () => {
const schoolEntity = schoolEntityFactory.buildWithId();
const user = userFactory.buildWithId({ school: schoolEntity });
const shareToken = shareTokenFactory.build({
const shareToken = shareTokenDOFactory.build({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
context: { contextType: 'unknown', contextId: schoolEntity.id },
Expand All @@ -794,7 +794,7 @@ describe('ShareTokenUC', () => {
const setup = () => {
const school = schoolEntityFactory.buildWithId();
const user = userFactory.buildWithId({ school });
const shareToken = shareTokenFactory.build();
const shareToken = shareTokenDOFactory.build();
const parentName = 'name';
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName });
return { user, school, shareToken };
Expand All @@ -821,7 +821,7 @@ describe('ShareTokenUC', () => {
parentType: 'invalid',
parentId: course.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupTokenWithParentName.mockResolvedValueOnce({ shareToken, parentName: 'foo' });

return { user, shareToken };
Expand All @@ -843,7 +843,7 @@ describe('ShareTokenUC', () => {
const user = userFactory.buildWithId({ school });
authorizationService.getUserWithPermissions.mockResolvedValue(user);

const shareToken = shareTokenFactory.build();
const shareToken = shareTokenDOFactory.build();
service.lookupToken.mockResolvedValueOnce(shareToken);

const course = courseFactory.buildWithId();
Expand Down Expand Up @@ -924,7 +924,7 @@ describe('ShareTokenUC', () => {
lessonCopyService.copyLesson.mockResolvedValueOnce(status);

const payload: ShareTokenPayload = { parentType: ShareTokenParentType.Lesson, parentId: lesson._id.toString() };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupToken.mockResolvedValueOnce(shareToken);

return { user, school, shareToken, status, course, lesson };
Expand Down Expand Up @@ -1010,7 +1010,7 @@ describe('ShareTokenUC', () => {
taskCopyService.copyTask.mockResolvedValueOnce(status);

const payload: ShareTokenPayload = { parentType: ShareTokenParentType.Task, parentId: task._id.toString() };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupToken.mockResolvedValueOnce(shareToken);

return { user, school, shareToken, status, course, task };
Expand Down Expand Up @@ -1090,7 +1090,7 @@ describe('ShareTokenUC', () => {
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user);

const payload: ShareTokenPayload = { parentType: ShareTokenParentType.ColumnBoard, parentId: columnBoard.id };
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupToken.mockResolvedValueOnce(shareToken);

return { user, shareToken, school, course, columnBoard };
Expand Down Expand Up @@ -1168,7 +1168,7 @@ describe('ShareTokenUC', () => {
parentType: 'invalid',
parentId: course.id,
};
const shareToken = shareTokenFactory.build({ payload });
const shareToken = shareTokenDOFactory.build({ payload });
service.lookupToken.mockResolvedValueOnce(shareToken);

return { user, shareToken };
Expand Down Expand Up @@ -1200,7 +1200,7 @@ describe('ShareTokenUC', () => {
};
courseCopyService.copyCourse.mockResolvedValueOnce(status);

const shareToken = shareTokenFactory.build({
const shareToken = shareTokenDOFactory.build({
payload: { parentType: ShareTokenParentType.Course, parentId: course.id },
context: { contextType: ShareTokenContextType.School, contextId: schoolEntity.id },
});
Expand Down Expand Up @@ -1237,7 +1237,7 @@ describe('ShareTokenUC', () => {
};
courseCopyService.copyCourse.mockResolvedValueOnce(status);

const shareToken = shareTokenFactory.build({
const shareToken = shareTokenDOFactory.build({
payload: { parentType: ShareTokenParentType.Course, parentId: course.id },
});
service.lookupToken.mockResolvedValueOnce(shareToken);
Expand Down

0 comments on commit d21b84e

Please sign in to comment.