diff --git a/dictation_server/src/features/tasks/tasks.service.spec.ts b/dictation_server/src/features/tasks/tasks.service.spec.ts index a619585..ee38b41 100644 --- a/dictation_server/src/features/tasks/tasks.service.spec.ts +++ b/dictation_server/src/features/tasks/tasks.service.spec.ts @@ -683,49 +683,386 @@ describe('TasksService', () => { }); describe('changeCheckoutPermission', () => { - // TODO sqliteを用いたテストを別途実装予定 - /* - 指定したユーザーグループがない場合 - 指定したユーザーがいない場合 - 指定したタスクがない場合 - タスクのステータスがUploadedでない場合 - */ - it('タスクのチェックアウト権限を変更できる', async () => { - const tasksRepositoryMockValue = makeDefaultTasksRepositoryMockValue(); - const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue(); - const adb2cServiceMockValue = makeDefaultAdb2cServiceMockValue(); - const service = await makeTasksServiceMock( - tasksRepositoryMockValue, - usersRepositoryMockValue, - adb2cServiceMockValue, - ); + let source: DataSource = null; + beforeEach(async () => { + source = new DataSource({ + type: 'sqlite', + database: ':memory:', + logging: false, + entities: [__dirname + '/../../**/*.entity{.ts,.js}'], + synchronize: true, // trueにすると自動的にmigrationが行われるため注意 + }); + return source.initialize(); + }); - expect( - await service.tasksService.changeCheckoutPermission( - 1, - [], - 'xxx-xxx-xxxx', - ['admin'], - ), - ).toEqual(undefined); + afterEach(async () => { + await source.destroy(); + source = null; + }); + + it('タスクのチェックアウト権限を変更できる。(個人指定)', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId_1 } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: typistUserId_2 } = await createUser( + source, + accountId, + 'typist-user-2-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const { taskId } = await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const { userGroupId } = await createUserGroup( + source, + accountId, + 'USER_GROUP_A', + [typistUserId_1], + ); + await createCheckoutPermissions(source, taskId, typistUserId_1); + await createCheckoutPermissions(source, taskId, undefined, userGroupId); + const service = module.get(TasksService); + await service.changeCheckoutPermission( + 1, + [{ typistName: 'typist-user-2', typistUserId: typistUserId_2 }], + 'author-user-external-id', + ['admin'], + ); + const permisions = await getCheckoutPermissions(source, taskId); + expect(permisions.length).toEqual(1); + expect(permisions[0]).toEqual({ + id: 3, + task_id: taskId, + user_id: typistUserId_2, + user_group_id: null, + }); + }); + + it('タスクのチェックアウト権限を変更できる。(グループ指定)', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId_1 } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: typistUserId_2 } = await createUser( + source, + accountId, + 'typist-user-2-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const { taskId } = await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const { userGroupId: userGroupId_1 } = await createUserGroup( + source, + accountId, + 'USER_GROUP_A', + [typistUserId_1], + ); + const { userGroupId: userGroupId_2 } = await createUserGroup( + source, + accountId, + 'USER_GROUP_B', + [typistUserId_2], + ); + await createCheckoutPermissions(source, taskId, typistUserId_1); + await createCheckoutPermissions(source, taskId, undefined, userGroupId_1); + const service = module.get(TasksService); + await service.changeCheckoutPermission( + 1, + [{ typistName: 'USER_GROUP_B', typistGroupId: userGroupId_2 }], + 'author-user-external-id', + ['admin'], + ); + const permisions = await getCheckoutPermissions(source, taskId); + expect(permisions.length).toEqual(1); + expect(permisions[0]).toEqual({ + id: 3, + task_id: taskId, + user_id: null, + user_group_id: userGroupId_2, + }); + }); + + it('タスクのチェックアウト権限を変更できる。(チェックアウト権限を外す)', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId_1 } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const { taskId } = await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const { userGroupId } = await createUserGroup( + source, + accountId, + 'USER_GROUP_A', + [typistUserId_1], + ); + await createCheckoutPermissions(source, taskId, typistUserId_1); + await createCheckoutPermissions(source, taskId, undefined, userGroupId); + const service = module.get(TasksService); + await service.changeCheckoutPermission(1, [], 'author-user-external-id', [ + 'admin', + ]); + const permisions = await getCheckoutPermissions(source, taskId); + expect(permisions.length).toEqual(0); }); it('ユーザーが存在しない場合、タスクのチェックアウト権限を変更できない', async () => { - const tasksRepositoryMockValue = makeDefaultTasksRepositoryMockValue(); - const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue(); - const adb2cServiceMockValue = makeDefaultAdb2cServiceMockValue(); - tasksRepositoryMockValue.changeCheckoutPermission = - new TasksNotFoundError(); - const service = await makeTasksServiceMock( - tasksRepositoryMockValue, - usersRepositoryMockValue, - adb2cServiceMockValue, + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId_1 } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const { taskId } = await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const { userGroupId } = await createUserGroup( + source, + accountId, + 'USER_GROUP_A', + [typistUserId_1], + ); + await createCheckoutPermissions(source, taskId, typistUserId_1); + await createCheckoutPermissions(source, taskId, undefined, userGroupId); + const service = module.get(TasksService); await expect( - service.tasksService.changeCheckoutPermission(1, [], 'xxx-xxxx-xxxx', [ - 'admin', - ]), + service.changeCheckoutPermission( + 1, + [{ typistName: 'not-exist-user', typistUserId: 999 }], + 'author-user-external-id', + ['admin'], + ), + ).rejects.toEqual( + new HttpException(makeErrorResponse('E010204'), HttpStatus.BAD_REQUEST), + ); + }); + + it('ユーザーグループが存在しない場合、タスクのチェックアウト権限を変更できない', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId_1 } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const { taskId } = await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const { userGroupId } = await createUserGroup( + source, + accountId, + 'USER_GROUP_A', + [typistUserId_1], + ); + await createCheckoutPermissions(source, taskId, typistUserId_1); + await createCheckoutPermissions(source, taskId, undefined, userGroupId); + const service = module.get(TasksService); + + await expect( + service.changeCheckoutPermission( + 1, + [{ typistName: 'not-exist-user-group', typistGroupId: 999 }], + 'author-user-external-id', + ['admin'], + ), + ).rejects.toEqual( + new HttpException(makeErrorResponse('E010204'), HttpStatus.BAD_REQUEST), + ); + }); + + it('タスクが存在しない場合、タスクのチェックアウト権限を変更できない', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + const service = module.get(TasksService); + + await expect( + service.changeCheckoutPermission( + 1, + [{ typistName: 'typist-user', typistUserId: typistUserId }], + 'author-user-external-id', + ['admin'], + ), + ).rejects.toEqual( + new HttpException(makeErrorResponse('E010601'), HttpStatus.BAD_REQUEST), + ); + }); + + it('タスクのステータスがUploadedでない場合、タスクのチェックアウト権限を変更できない', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + await createTask( + source, + accountId, + authorUserId, + 'MY_AUTHOR_ID', + '', + '01', + '00000001', + 'Inprogress', + ); + const service = module.get(TasksService); + + await expect( + service.changeCheckoutPermission( + 1, + [{ typistName: 'typist-user', typistUserId: typistUserId }], + 'author-user-external-id', + ['admin'], + ), + ).rejects.toEqual( + new HttpException(makeErrorResponse('E010601'), HttpStatus.BAD_REQUEST), + ); + }); + + it('ユーザーのRoleがAuthorでタスクのAuthorIDと自身のAuthorIDが一致しない場合、タスクのチェックアウト権限を変更できない', async () => { + const module = await makeTestingModule(source); + const { accountId } = await createAccount(source); + const { userId: typistUserId } = await createUser( + source, + accountId, + 'typist-user-external-id', + 'typist', + ); + const { userId: authorUserId } = await createUser( + source, + accountId, + 'author-user-external-id', + 'author', + 'MY_AUTHOR_ID', + ); + await createTask( + source, + accountId, + authorUserId, + 'OTHER_AUTHOR_ID', + '', + '01', + '00000001', + 'Uploaded', + ); + const service = module.get(TasksService); + + await expect( + service.changeCheckoutPermission( + 1, + [{ typistName: 'typist-user', typistUserId: typistUserId }], + 'author-user-external-id', + ['author'], + ), ).rejects.toEqual( new HttpException(makeErrorResponse('E010601'), HttpStatus.BAD_REQUEST), ); @@ -779,7 +1116,7 @@ describe('checkout', () => { source, accountId, 'USER_GROUP_A', - typistUserId, + [typistUserId], ); await createCheckoutPermissions(source, taskId, typistUserId); await createCheckoutPermissions(source, taskId, undefined, userGroupId); @@ -805,7 +1142,7 @@ describe('checkout', () => { user_id: 1, user_group_id: null, }); - }, 600000); + }); it('ユーザーのRoleがTypistで、タスクのチェックアウト権限がグループ指定である時、タスクをチェックアウトできる', async () => { const module = await makeTestingModule(source); @@ -837,7 +1174,7 @@ describe('checkout', () => { source, accountId, 'USER_GROUP_A', - typistUserId, + [typistUserId], ); await createCheckoutPermissions(source, taskId, typistUserId); await createCheckoutPermissions(source, taskId, undefined, userGroupId); diff --git a/dictation_server/src/features/tasks/test/utility.ts b/dictation_server/src/features/tasks/test/utility.ts index e527fe8..c46dd4e 100644 --- a/dictation_server/src/features/tasks/test/utility.ts +++ b/dictation_server/src/features/tasks/test/utility.ts @@ -129,7 +129,7 @@ export const createUserGroup = async ( datasource: DataSource, account_id: number, user_group_name: string, - user_id: number, + user_id: number[], ): Promise<{ userGroupId: number }> => { const { identifiers: userGroupIdentifiers } = await datasource .getRepository(UserGroup) @@ -140,12 +140,15 @@ export const createUserGroup = async ( updated_by: 'test', }); const userGroup = userGroupIdentifiers.pop() as UserGroup; - await datasource.getRepository(UserGroupMember).insert({ - user_group_id: userGroup.id, - user_id: user_id, - created_by: 'test', - updated_by: 'test', + const userGroupMenber = user_id.map((id) => { + return { + user_group_id: userGroup.id, + user_id: id, + created_by: 'test', + updated_by: 'test', + }; }); + await datasource.getRepository(UserGroupMember).save(userGroupMenber); return { userGroupId: userGroup.id }; };