From 89751396bf8dce660d92ff9ee284ae7963e8561b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=AF=E6=9C=AC=20=E9=96=8B?= Date: Mon, 28 Aug 2023 10:40:55 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20354:=20=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E7=94=A8=E9=96=A2=E6=95=B0=E3=82=92=E4=BD=9C=E6=88=90?= =?UTF-8?q?=E3=81=99=E3=82=8B(Sprint16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task2383: テスト用関数を作成する(Sprint16)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2383) - WIPレビューで指摘された関数名等を修正 - 汎用関数で問題があった部分を修正 - データ構築ミスしていた箇所を修正 - DB的にnullableな所をテスト用関数でも省略できるよう修正 - 各種Account、User作成関数を汎用関数に置き換え ## レビューポイント - 情報共有 ## 動作確認状況 - npm run test 成功 --- dictation_server/src/common/test/utility.ts | 363 +++++++ .../accounts/accounts.service.spec.ts | 467 +++++---- .../src/features/accounts/test/utility.ts | 253 ----- .../src/features/files/files.service.spec.ts | 240 +++-- .../src/features/files/test/utility.ts | 50 - .../licenses/licenses.service.spec.ts | 143 ++- .../src/features/licenses/test/utility.ts | 50 - .../src/features/tasks/tasks.service.spec.ts | 928 +++++++++--------- .../src/features/tasks/test/utility.ts | 50 - .../src/features/users/test/utility.ts | 173 ---- .../src/features/users/users.service.spec.ts | 808 ++++++++------- 11 files changed, 1728 insertions(+), 1797 deletions(-) create mode 100644 dictation_server/src/common/test/utility.ts diff --git a/dictation_server/src/common/test/utility.ts b/dictation_server/src/common/test/utility.ts new file mode 100644 index 0000000..a2f1005 --- /dev/null +++ b/dictation_server/src/common/test/utility.ts @@ -0,0 +1,363 @@ +import { v4 as uuidv4 } from 'uuid'; +import { DataSource } from 'typeorm'; +import { User } from '../../repositories/users/entity/user.entity'; +import { Account } from '../../repositories/accounts/entity/account.entity'; +import { ADMIN_ROLES, USER_ROLES } from '../../constants'; + +type InitialTestDBState = { + tier1Accounts: { account: Account; users: User[] }[]; + tier2Accounts: { account: Account; users: User[] }[]; + tier3Accounts: { account: Account; users: User[] }[]; + tier4Accounts: { account: Account; users: User[] }[]; + tier5Accounts: { account: Account; users: User[] }[]; +}; + +// 上書きされたら困る項目を除外したAccount型 +type OverrideAccount = Omit< + Account, + 'id' | 'primary_admin_user_id' | 'secondary_admin_user_id' | 'user' +>; + +// 上書きされたら困る項目を除外したUser型 +type OverrideUser = Omit< + User, + 'id' | 'account' | 'license' | 'userGroupMembers' +>; + +type AccountDefault = { [K in keyof OverrideAccount]?: OverrideAccount[K] }; +type UserDefault = { [K in keyof OverrideUser]?: OverrideUser[K] }; + +/** + * テスト ユーティリティ: 1~4階層のアカウントとその管理者ユーザーを作成します + * @param dataSource データソース + * @returns 作成されたデータセット + */ +export const makeHierarchicalAccounts = async ( + datasource: DataSource, +): Promise => { + const state: InitialTestDBState = { + tier1Accounts: [], + tier2Accounts: [], + tier3Accounts: [], + tier4Accounts: [], + tier5Accounts: [], + }; + + // 第1階層を作成 + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 1, + company_name: 'OMDS', + }); + + state.tier1Accounts.push({ + account: account, + users: [admin], + }); + } + // 第2階層を作成 + { + const { account: tier1 } = state.tier1Accounts.slice().shift(); + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 2, + parent_account_id: tier1.id, + company_name: 'OMDS_US', + }); + state.tier2Accounts.push({ + account: account, + users: [admin], + }); + } + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 2, + parent_account_id: tier1.id, + company_name: 'OMDS_EU', + }); + state.tier2Accounts.push({ + account: account, + users: [admin], + }); + } + } + + // 第3階層を作成 + { + for (const v of state.tier2Accounts) { + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 3, + parent_account_id: v.account.id, + company_name: `Agency_${v.account.id}_01`, + }); + state.tier3Accounts.push({ + account: account, + users: [admin], + }); + } + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 3, + parent_account_id: v.account.id, + company_name: `Agency_${v.account.id}_02`, + }); + state.tier3Accounts.push({ + account: account, + users: [admin], + }); + } + } + // 第4階層を作成 + for (const v of state.tier3Accounts) { + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 4, + parent_account_id: v.account.id, + company_name: `Distributor_${v.account.id}_01`, + }); + state.tier4Accounts.push({ + account: account, + users: [admin], + }); + } + { + const { account, admin } = await makeTestAccount(datasource, { + tier: 4, + parent_account_id: v.account.id, + company_name: `Distributor_${v.account.id}_02`, + }); + state.tier4Accounts.push({ + account: account, + users: [admin], + }); + } + } + } + + return state; +}; + +/** + * テスト ユーティリティ: 指定したプロパティを上書きしたアカウントとその管理者ユーザーを作成する + * @param dataSource データソース + * @param defaultUserValue Account型と同等かつoptionalなプロパティを持つ上書き箇所指定用オブジェクト + * @param defaultAdminUserValue User型と同等かつoptionalなプロパティを持つ上書き箇所指定用オブジェクト(account_id等の所属関係が破壊される上書きは無視する) + * @returns 作成したアカウント + */ +export const makeTestAccount = async ( + datasource: DataSource, + defaultAccountValue?: AccountDefault, + defaultAdminUserValue?: UserDefault, +): Promise<{ account: Account; admin: User }> => { + let accountId: number; + let userId: number; + { + const d = defaultAccountValue; + const { identifiers } = await datasource.getRepository(Account).insert({ + tier: d?.tier ?? 1, + parent_account_id: d?.parent_account_id ?? undefined, + country: d?.country ?? 'US', + delegation_permission: d?.delegation_permission ?? false, + locked: d?.locked ?? false, + company_name: d?.company_name ?? 'test inc.', + verified: d?.verified ?? true, + deleted_at: d?.deleted_at ?? '', + created_by: d?.created_by ?? 'test_runner', + created_at: d?.created_at ?? new Date(), + updated_by: d?.updated_by ?? 'updater', + updated_at: d?.updated_at ?? new Date(), + }); + const result = identifiers.pop() as Account; + accountId = result.id; + } + { + const d = defaultAdminUserValue; + const { identifiers } = await datasource.getRepository(User).insert({ + external_id: d?.external_id ?? uuidv4(), + account_id: accountId, + role: d?.role ?? 'admin none', + author_id: d?.author_id ?? undefined, + accepted_terms_version: d?.accepted_terms_version ?? '1.0', + email_verified: d?.email_verified ?? true, + auto_renew: d?.auto_renew ?? true, + license_alert: d?.license_alert ?? true, + notification: d?.notification ?? true, + encryption: d?.encryption ?? true, + encryption_password: d?.encryption_password ?? 'password', + prompt: d?.prompt ?? true, + deleted_at: d?.deleted_at ?? '', + created_by: d?.created_by ?? 'test_runner', + created_at: d?.created_at ?? new Date(), + updated_by: d?.updated_by ?? 'updater', + updated_at: d?.updated_at ?? new Date(), + }); + + const result = identifiers.pop() as User; + userId = result.id; + } + + // Accountの管理者を設定する + await datasource.getRepository(Account).update( + { id: accountId }, + { + primary_admin_user_id: userId, + }, + ); + + const account = await datasource.getRepository(Account).findOne({ + where: { + id: accountId, + }, + }); + + const admin = await datasource.getRepository(User).findOne({ + where: { + id: userId, + }, + }); + + return { + account: account, + admin: admin, + }; +}; + +/** + * テスト ユーティリティ: 指定したプロパティを上書きした管理者ユーザーの存在しないアカウントを作成する + * @param dataSource データソース + * @param defaultUserValue Account型と同等かつoptionalなプロパティを持つ上書き箇所指定用オブジェクト + * @returns 作成したアカウント + */ +export const makeTestSimpleAccount = async ( + datasource: DataSource, + defaultAccountValue?: AccountDefault, +): Promise => { + const d = defaultAccountValue; + const { identifiers } = await datasource.getRepository(Account).insert({ + tier: d?.tier ?? 1, + parent_account_id: d?.parent_account_id ?? undefined, + country: d?.country ?? 'US', + delegation_permission: d?.delegation_permission ?? false, + locked: d?.locked ?? false, + company_name: d?.company_name ?? 'test inc.', + verified: d?.verified ?? true, + deleted_at: d?.deleted_at ?? '', + created_by: d?.created_by ?? 'test_runner', + created_at: d?.created_at ?? new Date(), + updated_by: d?.updated_by ?? 'updater', + updated_at: d?.updated_at ?? new Date(), + }); + const result = identifiers.pop() as Account; + + const account = await datasource.getRepository(Account).findOne({ + where: { + id: result.id, + }, + }); + + return account; +}; + +/** + * テスト ユーティリティ: 指定したプロパティを上書きしたユーザーを作成する + * @param dataSource データソース + * @param defaultUserValue User型と同等かつoptionalなプロパティを持つ上書き箇所指定用オブジェクト + * @returns 作成したユーザー + */ +export const makeTestUser = async ( + datasource: DataSource, + defaultUserValue?: UserDefault, +): Promise => { + const d = defaultUserValue; + const { identifiers } = await datasource.getRepository(User).insert({ + account_id: d?.account_id ?? -1, + external_id: d?.external_id ?? uuidv4(), + role: d?.role ?? `${ADMIN_ROLES.STANDARD} ${USER_ROLES.NONE}`, + author_id: d?.author_id, + accepted_terms_version: d?.accepted_terms_version, + email_verified: d?.email_verified ?? true, + auto_renew: d?.auto_renew ?? true, + license_alert: d?.license_alert ?? true, + notification: d?.notification ?? true, + encryption: d?.encryption ?? true, + encryption_password: d?.encryption_password, + prompt: d?.prompt ?? true, + created_by: d?.created_by ?? 'test_runner', + created_at: d?.created_at ?? new Date(), + updated_by: d?.updated_by ?? 'updater', + updated_at: d?.updated_at ?? new Date(), + }); + const result = identifiers.pop() as User; + + return await datasource.getRepository(User).findOne({ + where: { + id: result.id, + }, + }); +}; + +/** + * テスト ユーティリティ: 指定IDのアカウントを取得する + * @param dataSource データソース + * @param id アカウントID + * @returns 該当アカウント + */ +export const getAccount = async (dataSource: DataSource, id: number) => { + return await dataSource.getRepository(Account).findOne({ + where: { id: id }, + }); +}; + +/** + * テスト ユーティリティ: すべてのアカウントを取得する + * @param dataSource データソース + * @returns 該当アカウント一覧 + */ +export const getAccounts = async ( + dataSource: DataSource, +): Promise => { + return await dataSource.getRepository(Account).find(); +}; + +/** + * テスト ユーティリティ: 指定ExternalIdのユーザーを取得する + * @param dataSource データソース + * @param externalId 外部ID + * @returns 該当ユーザー + */ +export const getUserFromExternalId = async ( + dataSource: DataSource, + externalId: string, +) => { + return await dataSource.getRepository(User).findOne({ + where: { external_id: externalId }, + }); +}; + +/** + * テスト ユーティリティ: 指定Idのユーザーを取得する + * @param dataSource データソース + * @param externalId 外部ID + * @returns 該当ユーザー + */ +export const getUser = async ( + datasource: DataSource, + id: number, +): Promise => { + const user = await datasource.getRepository(User).findOne({ + where: { + id: id, + }, + }); + return user; +}; + +/** + * テスト ユーティリティ: すべてのユーザーを取得する + * @param dataSource データソース + * @returns 該当ユーザー一覧 + */ +export const getUsers = async (dataSource: DataSource): Promise => { + return await dataSource.getRepository(User).find(); +}; diff --git a/dictation_server/src/features/accounts/accounts.service.spec.ts b/dictation_server/src/features/accounts/accounts.service.spec.ts index 924f9e1..a3f6154 100644 --- a/dictation_server/src/features/accounts/accounts.service.spec.ts +++ b/dictation_server/src/features/accounts/accounts.service.spec.ts @@ -12,23 +12,23 @@ import { } from './test/accounts.service.mock'; import { makeDefaultConfigValue } from '../users/test/users.service.mock'; import { - createAccount, createLicense, createLicenseOrder, - createUser, createLicenseSetExpiryDateAndStatus, - getAccount, - getUserFromExternalID, - getAccounts, - getUsers, getSortCriteria, - createAccountAndAdminUser, - createAccountAndAdminUserForTier5, getTypistGroup, getTypistGroupMember, } from './test/utility'; import { DataSource } from 'typeorm'; import { makeTestingModule } from '../../common/test/modules'; +import { + makeTestAccount, + getAccount, + getAccounts, + getUserFromExternalId, + getUsers, + makeTestUser, +} from '../../common/test/utility'; import { AccountsService } from './accounts.service'; import { Context, makeContext } from '../../common/log'; import { TIERS } from '../../constants'; @@ -113,7 +113,7 @@ describe('createAccount', () => { // DB内が想定通りになっているか確認 const account = await getAccount(source, accountId); - const user = await getUserFromExternalID(source, externalUserId); + const user = await getUserFromExternalId(source, externalUserId); expect(account.company_name).toBe(companyName); expect(account.country).toBe(country); expect(account.parent_account_id).toBe(dealerAccountId); @@ -717,8 +717,11 @@ describe('createPartnerAccount', () => { const adminExternalId = 'ADMIN0001'; // 第一階層のアカウントを作成 - const { accountId: parentAccountId, tier } = - await createAccountAndAdminUser(source, adminExternalId); + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: adminExternalId }, + ); const context = makeContext('uuid'); const companyName = 'test_company_name'; @@ -761,7 +764,7 @@ describe('createPartnerAccount', () => { email, adminName, adminExternalId, - tier, + parent.tier, ); // DB上に作成されたデータが想定通りであるか確認 @@ -769,14 +772,14 @@ describe('createPartnerAccount', () => { const accounts = await getAccounts(source); expect(accounts.length).toBe(2); - const createdUser = await getUserFromExternalID( + const createdUser = await getUserFromExternalId( source, pertnerExternalId, ); const createdAccount = await getAccount(source, accountId); expect(createdAccount.company_name).toBe(companyName); expect(createdAccount.country).toBe(country); - expect(createdAccount.parent_account_id).toBe(parentAccountId); + expect(createdAccount.parent_account_id).toBe(parent.id); expect(createdAccount.tier).toBe(2); expect(createdAccount.primary_admin_user_id).toBe(createdUser.id); expect(createdAccount.secondary_admin_user_id).toBe(null); @@ -789,7 +792,11 @@ describe('createPartnerAccount', () => { const adminExternalId = 'ADMIN0001'; // 第一階層のアカウントを作成 - const { tier } = await createAccountAndAdminUser(source, adminExternalId); + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: adminExternalId }, + ); const context = makeContext('uuid'); const companyName = 'test_company_name'; @@ -834,7 +841,7 @@ describe('createPartnerAccount', () => { email, adminName, adminExternalId, - tier, + parent.tier, ); } catch (e) { if (e instanceof HttpException) { @@ -861,7 +868,11 @@ describe('createPartnerAccount', () => { const adminExternalId = 'ADMIN0001'; // 第一階層のアカウントを作成 - const { tier } = await createAccountAndAdminUser(source, adminExternalId); + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: adminExternalId }, + ); const context = makeContext('uuid'); const companyName = 'test_company_name'; @@ -913,7 +924,7 @@ describe('createPartnerAccount', () => { email, adminName, adminExternalId, - tier, + parent.tier, ); } catch (e) { if (e instanceof HttpException) { @@ -944,7 +955,11 @@ describe('createPartnerAccount', () => { const adminExternalId = 'ADMIN0001'; // 第一階層のアカウントを作成 - const { tier } = await createAccountAndAdminUser(source, adminExternalId); + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: adminExternalId }, + ); const context = makeContext('uuid'); const companyName = 'test_company_name'; @@ -996,7 +1011,7 @@ describe('createPartnerAccount', () => { email, adminName, adminExternalId, - tier, + parent.tier, ); } catch (e) { if (e instanceof HttpException) { @@ -1027,7 +1042,12 @@ describe('createPartnerAccount', () => { const parentExternalId = 'parent_external_id'; - await createAccountAndAdminUser(source, parentExternalId); + // 第一階層のアカウントを作成 + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: parentExternalId }, + ); const context = makeContext(parentExternalId); const partnerExternalId = 'partner_external_id'; @@ -1086,7 +1106,7 @@ describe('createPartnerAccount', () => { { const accounts = await getAccounts(source); expect(accounts.length).toBe(1); - expect(accounts[0].tier).toBe(1); + expect(accounts[0].tier).toBe(parent.tier); const users = await getUsers(source); expect(users.length).toBe(1); expect(users[0].external_id).toBe(parentExternalId); @@ -1102,7 +1122,12 @@ describe('createPartnerAccount', () => { const parentExternalId = 'parent_external_id'; - await createAccountAndAdminUser(source, parentExternalId); + // 第一階層のアカウントを作成 + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: parentExternalId }, + ); const context = makeContext(parentExternalId); const partnerExternalId = 'partner_external_id'; @@ -1167,8 +1192,8 @@ describe('createPartnerAccount', () => { // リカバリ処理が失敗した場合、DB上のデータは削除されない const accounts = await getAccounts(source); expect(accounts.length).toBe(2); - expect(accounts[0].tier).toBe(1); - expect(accounts[1].tier).toBe(2); + expect(accounts[0].tier).toBe(parent.tier); + expect(accounts[1].tier).toBe(parent.tier + 1); const users = await getUsers(source); expect(users.length).toBe(2); expect(users[0].external_id).toBe(parentExternalId); @@ -1187,7 +1212,12 @@ describe('createPartnerAccount', () => { const parentExternalId = 'parent_external_id'; - await createAccountAndAdminUser(source, parentExternalId); + // 第一階層のアカウントを作成 + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: parentExternalId }, + ); const context = makeContext(parentExternalId); const partnerExternalId = 'partner_external_id'; @@ -1248,7 +1278,7 @@ describe('createPartnerAccount', () => { // DB上に作成されたデータが想定通りであるか確認 const accounts = await getAccounts(source); expect(accounts.length).toBe(1); - expect(accounts[0].tier).toBe(1); + expect(accounts[0].tier).toBe(parent.tier); const users = await getUsers(source); expect(users.length).toBe(1); expect(users[0].external_id).toBe(parentExternalId); @@ -1272,7 +1302,12 @@ describe('createPartnerAccount', () => { const parentExternalId = 'parent_external_id'; - await createAccountAndAdminUser(source, parentExternalId); + // 第一階層のアカウントを作成 + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: parentExternalId }, + ); const context = makeContext(parentExternalId); const partnerExternalId = 'partner_external_id'; @@ -1338,8 +1373,8 @@ describe('createPartnerAccount', () => { // リカバリ処理が失敗したため、DB上のデータは削除されていない const accounts = await getAccounts(source); expect(accounts.length).toBe(2); - expect(accounts[0].tier).toBe(1); - expect(accounts[1].tier).toBe(2); + expect(accounts[0].tier).toBe(parent.tier); + expect(accounts[1].tier).toBe(parent.tier + 1); const users = await getUsers(source); expect(users.length).toBe(2); expect(users[0].external_id).toBe(parentExternalId); @@ -1361,7 +1396,11 @@ describe('createPartnerAccount', () => { const adminExternalId = 'ADMIN0001'; // 第一階層のアカウントを作成 - const { tier } = await createAccountAndAdminUser(source, adminExternalId); + const { account: parent } = await makeTestAccount( + source, + { tier: 1 }, + { external_id: adminExternalId }, + ); const context = makeContext('uuid'); const companyName = 'test_company_name'; @@ -1407,7 +1446,7 @@ describe('createPartnerAccount', () => { email, adminName, adminExternalId, - tier, + parent.tier, ); } catch (e) { if (e instanceof HttpException) { @@ -1712,38 +1751,45 @@ describe('getPartnerAccount', () => { const module = await makeTestingModule(source); // 親アカウントと子アカウント2つ作成 - const { accountId: parentAccountId } = await createAccount( - source, - 0, - 1, - 'PARENTCORP', - ); - const { accountId: childAccountId1 } = await createAccount( - source, - parentAccountId, - 2, - 'CHILDCORP1', - ); - const { accountId: childAccountId2 } = await createAccount( - source, - parentAccountId, - 2, - 'CHILDCORP2', - ); + const { id: parentAccountId } = ( + await makeTestAccount(source, { + parent_account_id: 0, + tier: 1, + company_name: 'PARENTCORP', + }) + ).account; + + const { id: childAccountId1 } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 2, + company_name: 'CHILDCORP1', + }) + ).account; + + const { id: childAccountId2 } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 2, + company_name: 'CHILDCORP2', + }) + ).account; // 第二にリクエストを投げる用の第三を作成 - const { accountId: childAccountId3 } = await createAccount( - source, - childAccountId1, - 3, - 'CHILDCORP3', - ); - const { accountId: childAccountId4 } = await createAccount( - source, - childAccountId2, - 3, - 'CHILDCORP4', - ); + const { id: childAccountId3 } = ( + await makeTestAccount(source, { + parent_account_id: childAccountId1, + tier: 3, + company_name: 'CHILDCORP3', + }) + ).account; + const { id: childAccountId4 } = ( + await makeTestAccount(source, { + parent_account_id: childAccountId2, + tier: 3, + company_name: 'CHILDCORP4', + }) + ).account; // 所有ライセンスを追加(親:3、子1:1、子2:2) await createLicense(source, parentAccountId); @@ -1819,30 +1865,34 @@ describe('getPartnerAccount', () => { const module = await makeTestingModule(source); // 親アカウントと子アカウント2つ作成 - const { accountId: parentAccountId } = await createAccount( - source, - 0, - 4, - 'PARENTCORP', - ); - const { accountId: childAccountId1 } = await createAccount( - source, - parentAccountId, - 5, - 'CHILDCORP1', - ); - const { accountId: childAccountId2 } = await createAccount( - source, - parentAccountId, - 5, - 'CHILDCORP2', - ); - const { accountId: childAccountId3 } = await createAccount( - source, - parentAccountId, - 5, - 'CHILDCORP3', - ); + const { id: parentAccountId } = ( + await makeTestAccount(source, { + parent_account_id: 0, + tier: 4, + company_name: 'PARENTCORP', + }) + ).account; + const { id: childAccountId1 } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 5, + company_name: 'CHILDCORP1', + }) + ).account; + const { id: childAccountId2 } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 5, + company_name: 'CHILDCORP2', + }) + ).account; + const { id: childAccountId3 } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 5, + company_name: 'CHILDCORP3', + }) + ).account; // 有効期限が14日後のライセンスを追加(5ライセンス) const expiryDate = new Date(); @@ -2062,25 +2112,26 @@ describe('issueLicense', () => { const service = module.get(AccountsService); const now = new Date(); // 親と子アカウントを作成する - const { accountId: parentAccountId } = await createAccount( - source, - 0, - 2, - 'PARENTCORP', - ); - const { accountId: childAccountId } = await createAccount( - source, - parentAccountId, - 3, - 'CHILDCORP1', - ); + const { id: parentAccountId } = ( + await makeTestAccount(source, { + parent_account_id: 0, + tier: 2, + company_name: 'PARENTCORP', + }) + ).account; + const { id: childAccountId } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 3, + company_name: 'CHILDCORP1', + }) + ).account; // 親と子のユーザーを作成する - const { externalId } = await createUser( - source, - parentAccountId, - 'userId-parent', - 'admin', - ); + const { external_id: externalId } = await makeTestUser(source, { + account_id: parentAccountId, + external_id: 'userId-parent', + role: 'admin', + }); // 親のライセンスを作成する(3個) await createLicense(source, parentAccountId); @@ -2121,26 +2172,26 @@ describe('issueLicense', () => { const service = module.get(AccountsService); const now = new Date(); // 親と子アカウントを作成する - const { accountId: parentAccountId } = await createAccount( - source, - 0, - 2, - 'PARENTCORP', - ); - const { accountId: childAccountId } = await createAccount( - source, - parentAccountId, - 3, - 'CHILDCORP1', - ); + const { id: parentAccountId } = ( + await makeTestAccount(source, { + parent_account_id: 0, + tier: 2, + company_name: 'PARENTCORP', + }) + ).account; + const { id: childAccountId } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 3, + company_name: 'CHILDCORP1', + }) + ).account; // 親と子のユーザーを作成する - const { externalId } = await createUser( - source, - parentAccountId, - 'userId-parent', - 'admin', - ); - + const { external_id: externalId } = await makeTestUser(source, { + account_id: parentAccountId, + external_id: 'userId-parent', + role: 'admin', + }); // 親のライセンスを作成する(3個) await createLicense(source, parentAccountId); await createLicense(source, parentAccountId); @@ -2178,25 +2229,26 @@ describe('issueLicense', () => { const service = module.get(AccountsService); const now = new Date(); // 親と子アカウントを作成する - const { accountId: parentAccountId } = await createAccount( - source, - 0, - 2, - 'PARENTCORP', - ); - const { accountId: childAccountId } = await createAccount( - source, - parentAccountId, - 3, - 'CHILDCORP1', - ); + const { id: parentAccountId } = ( + await makeTestAccount(source, { + parent_account_id: 0, + tier: 2, + company_name: 'PARENTCORP', + }) + ).account; + const { id: childAccountId } = ( + await makeTestAccount(source, { + parent_account_id: parentAccountId, + tier: 3, + company_name: 'CHILDCORP1', + }) + ).account; // 親と子のユーザーを作成する - const { externalId } = await createUser( - source, - parentAccountId, - 'userId-parent', - 'admin', - ); + const { external_id: externalId } = await makeTestUser(source, { + account_id: parentAccountId, + external_id: 'userId-parent', + role: 'admin', + }); // 親のライセンスを作成する(3個) await createLicense(source, parentAccountId); @@ -2242,24 +2294,30 @@ describe('getDealers', () => { }); it('Dealerを取得できる', async () => { const module = await makeTestingModule(source); - const { accountId: accountId_1 } = await createAccount( - source, - 1, - TIERS.TIER4, - 'DEALER_1', - ); - const { accountId: accountId_2 } = await createAccount( - source, - 2, - TIERS.TIER4, - 'DEALER_2', - ); - const { accountId: accountId_3 } = await createAccount( - source, - 3, - TIERS.TIER4, - 'DEALER_3', - ); + const { id: accountId_1 } = ( + await makeTestAccount(source, { + parent_account_id: 1, + tier: TIERS.TIER4, + country: 'JP', + company_name: 'DEALER_1', + }) + ).account; + const { id: accountId_2 } = ( + await makeTestAccount(source, { + parent_account_id: 2, + tier: TIERS.TIER4, + country: 'JP', + company_name: 'DEALER_2', + }) + ).account; + const { id: accountId_3 } = ( + await makeTestAccount(source, { + parent_account_id: 3, + tier: TIERS.TIER4, + country: 'JP', + company_name: 'DEALER_3', + }) + ).account; const service = module.get(AccountsService); expect(await service.getDealers()).toEqual({ @@ -2314,10 +2372,13 @@ describe('createTypistGroup', () => { const module = await makeTestingModule(source); const adminExternalId = 'admin-external-id'; // 第五階層のアカウント作成 - const { accountId } = await createAccountAndAdminUserForTier5( - source, - adminExternalId, - ); + const { id: accountId } = ( + await makeTestAccount( + source, + { tier: 5 }, + { external_id: adminExternalId }, + ) + ).account; // 作成したアカウントにユーザーを3名追加する const typiptUserExternalIds = [ 'typist-user-external-id1', @@ -2326,12 +2387,11 @@ describe('createTypistGroup', () => { ]; const userIds: number[] = []; for (const typiptUserExternalId of typiptUserExternalIds) { - const { userId } = await createUser( - source, - accountId, - typiptUserExternalId, - 'typist', - ); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: typiptUserExternalId, + role: 'typist', + }); userIds.push(userId); } //作成したデータを確認 @@ -2371,10 +2431,13 @@ describe('createTypistGroup', () => { const module = await makeTestingModule(source); const adminExternalId = 'admin-external-id'; // 第五階層のアカウント作成 - const { accountId } = await createAccountAndAdminUserForTier5( - source, - adminExternalId, - ); + const { id: accountId } = ( + await makeTestAccount( + source, + { tier: 5 }, + { external_id: adminExternalId }, + ) + ).account; // 作成したアカウントにユーザーを3名追加する const typiptUserExternalIds = [ 'typist-user-external-id1', @@ -2383,12 +2446,14 @@ describe('createTypistGroup', () => { ]; const userIds: number[] = []; for (const typiptUserExternalId of typiptUserExternalIds) { - const { userId } = await createUser( - source, - accountId, - typiptUserExternalId, - typiptUserExternalId === 'typist-user-external-id3' ? 'none' : 'typist', //typist-user-external-id3のみRole:none - ); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: typiptUserExternalId, + role: + typiptUserExternalId === 'typist-user-external-id3' + ? 'none' + : 'typist', //typist-user-external-id3のみRole:none, + }); userIds.push(userId); } //作成したデータを確認 @@ -2419,10 +2484,13 @@ describe('createTypistGroup', () => { const module = await makeTestingModule(source); const adminExternalId = 'admin-external-id'; // 第五階層のアカウント作成 - const { accountId } = await createAccountAndAdminUserForTier5( - source, - adminExternalId, - ); + const { id: accountId } = ( + await makeTestAccount( + source, + { tier: 5 }, + { external_id: adminExternalId }, + ) + ).account; // 作成したアカウントにユーザーを3名追加する const typiptUserExternalIds = [ 'typist-user-external-id1', @@ -2431,12 +2499,11 @@ describe('createTypistGroup', () => { ]; const userIds: number[] = []; for (const typiptUserExternalId of typiptUserExternalIds) { - const { userId } = await createUser( - source, - accountId, - typiptUserExternalId, - 'typist', - ); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: typiptUserExternalId, + role: 'typist', + }); userIds.push(userId); } //作成したデータを確認 @@ -2466,10 +2533,13 @@ describe('createTypistGroup', () => { const module = await makeTestingModule(source); const adminExternalId = 'admin-external-id'; // 第五階層のアカウント作成 - const { accountId } = await createAccountAndAdminUserForTier5( - source, - adminExternalId, - ); + const { id: accountId } = ( + await makeTestAccount( + source, + { tier: 5 }, + { external_id: adminExternalId }, + ) + ).account; // 作成したアカウントにユーザーを3名追加する const typiptUserExternalIds = [ 'typist-user-external-id1', @@ -2478,12 +2548,11 @@ describe('createTypistGroup', () => { ]; const userIds: number[] = []; for (const typiptUserExternalId of typiptUserExternalIds) { - const { userId } = await createUser( - source, - accountId, - typiptUserExternalId, - 'typist', - ); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: typiptUserExternalId, + role: 'typist', + }); userIds.push(userId); } //作成したデータを確認 diff --git a/dictation_server/src/features/accounts/test/utility.ts b/dictation_server/src/features/accounts/test/utility.ts index 2da6207..ef8cdb6 100644 --- a/dictation_server/src/features/accounts/test/utility.ts +++ b/dictation_server/src/features/accounts/test/utility.ts @@ -1,6 +1,4 @@ import { DataSource } from 'typeorm'; -import { User } from '../../../repositories/users/entity/user.entity'; -import { Account } from '../../../repositories/accounts/entity/account.entity'; import { License, LicenseOrder, @@ -9,162 +7,6 @@ import { SortCriteria } from '../../../repositories/sort_criteria/entity/sort_cr import { UserGroup } from '../../../repositories/user_groups/entity/user_group.entity'; import { UserGroupMember } from '../../../repositories/user_groups/entity/user_group_member.entity'; -// TODO: [PBI 2379] 他のUtilityからコピペしてきたもの。後日整理される前提。 -export const createAccountAndAdminUser = async ( - datasource: DataSource, - adminExternalId: string, -): Promise<{ - accountId: number; - adminId: number; - role: string; - tier: number; -}> => { - const { identifiers: account_idf } = await datasource - .getRepository(Account) - .insert({ - tier: 1, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = account_idf.pop() as Account; - - const { identifiers: user_idf } = await datasource - .getRepository(User) - .insert({ - account_id: account.id, - external_id: adminExternalId, - role: 'admin none', - accepted_terms_version: '1.0', - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: true, - encryption_password: 'password', - prompt: true, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = user_idf.pop() as User; - - // Accountの管理者を設定する - await datasource.getRepository(Account).update( - { id: user.account_id }, - { - primary_admin_user_id: user.id, - }, - ); - - const accountResult = await getAccount(datasource, account.id); - const userResult = await getUser(datasource, user.id); - - return { - accountId: account.id, - adminId: user.id, - role: userResult.role, - tier: accountResult.tier, - }; -}; - -export const createAccount = async ( - datasource: DataSource, - parentAccountId: number, - tier: number, - companyName: string, -): Promise<{ accountId: number }> => { - const { identifiers } = await datasource.getRepository(Account).insert({ - parent_account_id: parentAccountId, - tier: tier, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: companyName, - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = identifiers.pop() as Account; - return { accountId: account.id }; -}; - -/** - * テスト ユーティリティ: 指定IDのアカウントを取得する - * @param dataSource データソース - * @param id アカウントID - * @returns 該当アカウント - */ -export const getAccount = async (dataSource: DataSource, id: number) => { - return await dataSource.getRepository(Account).findOne({ - where: { id: id }, - }); -}; - -/** - * テスト ユーティリティ: すべてのアカウントを取得する - * @param dataSource データソース - * @returns 該当アカウント一覧 - */ -export const getAccounts = async ( - dataSource: DataSource, -): Promise => { - return await dataSource.getRepository(Account).find(); -}; - -/** - * テスト ユーティリティ: 指定ExternalIdのユーザーを取得する - * @param dataSource データソース - * @param externalId 外部ID - * @returns 該当ユーザー - */ -export const getUserFromExternalID = async ( - dataSource: DataSource, - externalId: string, -) => { - return await dataSource.getRepository(User).findOne({ - where: { external_id: externalId }, - }); -}; - -/** - * テスト ユーティリティ: 指定Idのユーザーを取得する - * @param dataSource データソース - * @param externalId 外部ID - * @returns 該当ユーザー - */ -export const getUser = async ( - datasource: DataSource, - id: number, -): Promise => { - const user = await datasource.getRepository(User).findOne({ - where: { - id: id, - }, - }); - return user; -}; - -/** - * テスト ユーティリティ: すべてのユーザーを取得する - * @param dataSource データソース - * @returns 該当ユーザー一覧 - */ -export const getUsers = async (dataSource: DataSource): Promise => { - return await dataSource.getRepository(User).find(); -}; - /** * テスト ユーティリティ: すべてのソート条件を取得する * @param dataSource データソース @@ -244,101 +86,6 @@ export const createLicenseOrder = async ( identifiers.pop() as License; }; -export const createUser = async ( - datasource: DataSource, - accountId: number, - external_id: string, - role: string, - author_id?: string | undefined, -): Promise<{ userId: number; externalId: string; authorId: string }> => { - const { identifiers } = await datasource.getRepository(User).insert({ - account_id: accountId, - external_id: external_id, - role: role, - accepted_terms_version: '1.0', - author_id: author_id, - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: false, - prompt: false, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = identifiers.pop() as User; - return { userId: user.id, externalId: external_id, authorId: author_id }; -}; - -// TODO: [PBI 2379] 第五階層のアカウント・管理者を作成する。後日整理される前提。 -export const createAccountAndAdminUserForTier5 = async ( - datasource: DataSource, - adminExternalId: string, -): Promise<{ - accountId: number; - adminId: number; - role: string; - tier: number; -}> => { - const { identifiers: account_idf } = await datasource - .getRepository(Account) - .insert({ - tier: 5, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = account_idf.pop() as Account; - - const { identifiers: user_idf } = await datasource - .getRepository(User) - .insert({ - account_id: account.id, - external_id: adminExternalId, - role: 'none', - accepted_terms_version: '1.0', - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: true, - encryption_password: 'password', - prompt: true, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = user_idf.pop() as User; - - // Accountの管理者を設定する - await datasource.getRepository(Account).update( - { id: user.account_id }, - { - primary_admin_user_id: user.id, - }, - ); - - const accountResult = await getAccount(datasource, account.id); - const userResult = await getUser(datasource, user.id); - - return { - accountId: account.id, - adminId: user.id, - role: userResult.role, - tier: accountResult.tier, - }; -}; - // タイピストグループを取得する export const getTypistGroup = async ( datasource: DataSource, diff --git a/dictation_server/src/features/files/files.service.spec.ts b/dictation_server/src/features/files/files.service.spec.ts index 8ef77b5..b981224 100644 --- a/dictation_server/src/features/files/files.service.spec.ts +++ b/dictation_server/src/features/files/files.service.spec.ts @@ -7,14 +7,10 @@ import { makeFilesServiceMock, } from './test/files.service.mock'; import { DataSource } from 'typeorm'; -import { - createAccount, - createTask, - createUser, - makeTestingModuleWithBlob, -} from './test/utility'; +import { createTask, makeTestingModuleWithBlob } from './test/utility'; import { FilesService } from './files.service'; import { makeContext } from '../../common/log'; +import { makeTestSimpleAccount, makeTestUser } from '../../common/test/utility'; describe('音声ファイルアップロードURL取得', () => { it('アップロードSASトークンが乗っているURLを返却する', async () => { @@ -306,14 +302,17 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('ダウンロードSASトークンが乗っているURLを取得できる', async () => { - const { accountId } = await createAccount(source); - const { externalId, userId, authorId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { + external_id: externalId, + id: userId, + author_id: authorId, + } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/${userId}`; const { audioFileId } = await createTask( @@ -343,20 +342,18 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('Typistの場合、タスクのステータスが[Inprogress,Pending]以外でエラー', async () => { - const { accountId } = await createAccount(source); - const { externalId, userId } = await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId, id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/${authorUserId}`; @@ -388,26 +385,23 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('Typistの場合、自身が担当するタスクでない場合エラー', async () => { - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - ); - const { userId: otherId } = await createUser( - source, - accountId, - 'other-typist-user-external-id', - 'typist', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: otherId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'other-typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/${authorUserId}`; const { audioFileId } = await createTask( @@ -438,14 +432,13 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('Authorの場合、自身が登録したタスクでない場合エラー', async () => { - const { accountId } = await createAccount(source); - const { externalId, userId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId, id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/${userId}`; const { audioFileId } = await createTask( @@ -477,14 +470,13 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('Taskが存在しない場合はエラーとなる', async () => { - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const blobParam = makeBlobstorageServiceMockValue(); @@ -503,14 +495,17 @@ describe('音声ファイルダウンロードURL取得', () => { }); it('blobストレージにファイルが存在しない場合はエラーとなる', async () => { - const { accountId } = await createAccount(source); - const { externalId, userId, authorId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { + external_id: externalId, + id: userId, + author_id: authorId, + } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/${userId}`; const { audioFileId } = await createTask( @@ -561,13 +556,15 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('ダウンロードSASトークンが乗っているURLを取得できる', async () => { - const { accountId } = await createAccount(source); - const { externalId, authorId } = await createUser( + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId, author_id: authorId } = await makeTestUser( source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', + { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }, ); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/Templates`; @@ -598,14 +595,13 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('Typistの場合、タスクのステータスが[Inprogress,Pending]以外でエラー', async () => { - const { accountId } = await createAccount(source); - const { externalId, userId } = await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - undefined, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId, id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + author_id: undefined, + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/Templates`; const { audioFileId } = await createTask( @@ -636,21 +632,19 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('Typistの場合、自身が担当するタスクでない場合エラー', async () => { - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - undefined, - ); - const { userId: otherId } = await createUser( - source, - accountId, - 'other-typist-user-external-id', - 'typist', - undefined, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + author_id: undefined, + }); + const { id: otherId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'other-typist-user-external-id', + role: 'typist', + author_id: undefined, + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/Templates`; const { audioFileId } = await createTask( @@ -681,14 +675,13 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('Authorの場合、自身が登録したタスクでない場合エラー', async () => { - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/Templates`; const { audioFileId } = await createTask( @@ -720,14 +713,13 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('Taskが存在しない場合はエラーとなる', async () => { - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }); const blobParam = makeBlobstorageServiceMockValue(); @@ -746,13 +738,15 @@ describe('テンプレートファイルダウンロードURL取得', () => { }); it('blobストレージにファイルが存在しない場合はエラーとなる', async () => { - const { accountId } = await createAccount(source); - const { externalId, authorId } = await createUser( + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId, author_id: authorId } = await makeTestUser( source, - accountId, - 'author-user-external-id', - 'author', - 'AUTHOR_ID', + { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'AUTHOR_ID', + }, ); const url = `https://saodmsusdev.blob.core.windows.net/account-${accountId}/Templates`; diff --git a/dictation_server/src/features/files/test/utility.ts b/dictation_server/src/features/files/test/utility.ts index a6ccfdc..61772f6 100644 --- a/dictation_server/src/features/files/test/utility.ts +++ b/dictation_server/src/features/files/test/utility.ts @@ -36,58 +36,8 @@ import { BlobstorageServiceMockValue, makeBlobstorageServiceMock, } from './files.service.mock'; -import { User } from '../../../repositories/users/entity/user.entity'; -import { Account } from '../../../repositories/accounts/entity/account.entity'; import { TemplateFile } from '../../../repositories/template_files/entity/template_file.entity'; -export const createAccount = async ( - datasource: DataSource, -): Promise<{ accountId: number }> => { - const { identifiers } = await datasource.getRepository(Account).insert({ - tier: 5, - country: 'US', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = identifiers.pop() as Account; - return { accountId: account.id }; -}; - -export const createUser = async ( - datasource: DataSource, - accountId: number, - external_id: string, - role: string, - author_id?: string | undefined, -): Promise<{ userId: number; externalId: string; authorId: string }> => { - const { identifiers } = await datasource.getRepository(User).insert({ - account_id: accountId, - external_id: external_id, - role: role, - accepted_terms_version: '1.0', - author_id: author_id, - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: false, - prompt: false, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = identifiers.pop() as User; - return { userId: user.id, externalId: external_id, authorId: author_id }; -}; - export const createTask = async ( datasource: DataSource, account_id: number, diff --git a/dictation_server/src/features/licenses/licenses.service.spec.ts b/dictation_server/src/features/licenses/licenses.service.spec.ts index 8438279..f7224c5 100644 --- a/dictation_server/src/features/licenses/licenses.service.spec.ts +++ b/dictation_server/src/features/licenses/licenses.service.spec.ts @@ -23,8 +23,6 @@ import { LicensesService } from './licenses.service'; import { makeTestingModule } from '../../common/test/modules'; import { DataSource } from 'typeorm'; import { - createAccount, - createUser, createCardLicense, createLicense, createCardLicenseIssue, @@ -36,8 +34,8 @@ import { } from './test/utility'; import { UsersService } from '../users/users.service'; import { makeContext } from '../../common/log'; -import { IsNotIn } from 'class-validator'; import { LICENSE_ALLOCATED_STATUS, LICENSE_TYPE } from '../../constants'; +import { makeTestSimpleAccount, makeTestUser } from '../../common/test/utility'; describe('LicensesService', () => { it('ライセンス注文が完了する', async () => { @@ -303,13 +301,13 @@ describe('DBテスト', () => { it('カードライセンス発行が完了する(発行数が合っているか確認)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'userId', - 'admin', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const service = module.get(LicensesService); const issueCount = 500; @@ -321,13 +319,13 @@ describe('DBテスト', () => { it('カードライセンス取り込みが完了する', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'userId', - 'admin', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const cardLicenseKey = 'WZCETXC0Z9PQZ9GKRGGY'; const defaultAccountId = 150; @@ -364,13 +362,13 @@ describe('DBテスト', () => { const module = await makeTestingModule(source); const now = new Date(); - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'userId', - 'admin', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); // ライセンスを作成する // 1件目 @@ -499,8 +497,13 @@ describe('ライセンス割り当て', () => { it('未割当のライセンスに対して、ライセンス割り当てが完了する', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); await createLicense( source, 1, @@ -544,8 +547,13 @@ describe('ライセンス割り当て', () => { it('再割り当て可能なライセンスに対して、ライセンス割り当てが完了する', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -585,8 +593,13 @@ describe('ライセンス割り当て', () => { it('未割当のライセンスに対して、別のライセンスが割り当てられているユーザーの割り当てが完了する', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -661,8 +674,13 @@ describe('ライセンス割り当て', () => { it('割り当て時にライセンス履歴テーブルへの登録が完了する(元がNORMALのとき)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -701,8 +719,13 @@ describe('ライセンス割り当て', () => { it('割り当て時にライセンス履歴テーブルへの登録が完了する(元がCARDのとき)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -741,8 +764,13 @@ describe('ライセンス割り当て', () => { it('割り当て時にライセンス履歴テーブルへの登録が完了する(元がTRIALのとき)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -781,8 +809,13 @@ describe('ライセンス割り当て', () => { it('有効期限が切れているライセンスを割り当てようとした場合、エラーになる', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() - 30); await createLicense( @@ -807,8 +840,13 @@ describe('ライセンス割り当て', () => { it('割り当て不可なライセンスを割り当てようとした場合、エラーになる', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -866,8 +904,13 @@ describe('ライセンス割り当て解除', () => { it('ライセンスの割り当て解除が完了する', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( @@ -915,9 +958,19 @@ describe('ライセンス割り当て解除', () => { it('ライセンスが既に割り当て解除されていた場合、エラーとなる', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { userId } = await createUser(source, accountId, 'userId', 'admin'); - await createUser(source, accountId, 'userId2', 'admin'); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + author_id: undefined, + }); + await makeTestUser(source, { + account_id: accountId, + external_id: 'userId2', + role: 'admin', + author_id: undefined, + }); const date = new Date(); date.setDate(date.getDate() + 30); await createLicense( diff --git a/dictation_server/src/features/licenses/test/utility.ts b/dictation_server/src/features/licenses/test/utility.ts index 352c974..58a3b06 100644 --- a/dictation_server/src/features/licenses/test/utility.ts +++ b/dictation_server/src/features/licenses/test/utility.ts @@ -1,6 +1,4 @@ import { DataSource } from 'typeorm'; -import { User } from '../../../repositories/users/entity/user.entity'; -import { Account } from '../../../repositories/accounts/entity/account.entity'; import { License, CardLicense, @@ -8,54 +6,6 @@ import { LicenseAllocationHistory, } from '../../../repositories/licenses/entity/license.entity'; -export const createAccount = async ( - datasource: DataSource, -): Promise<{ accountId: number }> => { - const { identifiers } = await datasource.getRepository(Account).insert({ - tier: 1, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = identifiers.pop() as Account; - return { accountId: account.id }; -}; - -export const createUser = async ( - datasource: DataSource, - accountId: number, - external_id: string, - role: string, - author_id?: string | undefined, -): Promise<{ userId: number; externalId: string }> => { - const { identifiers } = await datasource.getRepository(User).insert({ - account_id: accountId, - external_id: external_id, - role: role, - accepted_terms_version: '1.0', - author_id: author_id, - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: false, - prompt: false, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = identifiers.pop() as User; - return { userId: user.id, externalId: external_id }; -}; - export const createLicense = async ( datasource: DataSource, licenseId: number, diff --git a/dictation_server/src/features/tasks/tasks.service.spec.ts b/dictation_server/src/features/tasks/tasks.service.spec.ts index 0d47fe7..6b11396 100644 --- a/dictation_server/src/features/tasks/tasks.service.spec.ts +++ b/dictation_server/src/features/tasks/tasks.service.spec.ts @@ -11,10 +11,8 @@ import { makeErrorResponse } from '../../common/error/makeErrorResponse'; import { TasksService } from './tasks.service'; import { DataSource } from 'typeorm'; import { - createAccount, createCheckoutPermissions, createTask, - createUser, createUserGroup, getCheckoutPermissions, getTask, @@ -22,6 +20,7 @@ import { } from './test/utility'; import { Adb2cTooManyRequestsError } from '../../gateways/adb2c/adb2c.service'; import { makeContext } from '../../common/log'; +import { makeTestSimpleAccount, makeTestUser } from '../../common/test/utility'; describe('TasksService', () => { it('タスク一覧を取得できる(admin)', async () => { @@ -611,13 +610,12 @@ describe('TasksService', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { externalId } = await createUser( - source, - accountId, - 'userId', - 'admin', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'admin', + }); const service = module.get(TasksService); const accessToken = { userId: externalId, role: 'admin', tier: 5 }; @@ -646,14 +644,13 @@ describe('TasksService', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { userId } = await createUser( - source, - accountId, - 'userId', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -710,21 +707,19 @@ describe('TasksService', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { userId: userId_1 } = await createUser( - source, - accountId, - 'userId_1', - 'author', - 'AUTHOR_ID_1', - ); - const { userId: userId_2 } = await createUser( - source, - accountId, - 'userId_2', - 'author', - 'AUTHOR_ID_2', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: userId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId_1', + role: 'author', + author_id: 'AUTHOR_ID_1', + }); + const { id: userId_2 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'userId_2', + role: 'author', + author_id: 'AUTHOR_ID_2', + }); await createTask( source, accountId, @@ -798,26 +793,23 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: typistUserId_2 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-2-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -861,26 +853,23 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: typistUserId_2 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-2-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -930,20 +919,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -981,20 +968,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1035,20 +1020,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1089,20 +1072,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); await expect( @@ -1125,20 +1106,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1171,20 +1150,18 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1218,26 +1195,23 @@ describe('changeCheckoutPermission', () => { source, notificationhubServiceMockValue, ); - 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 { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId_1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: typistUserId_2 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-2-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1300,20 +1274,18 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1368,20 +1340,18 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1436,20 +1406,18 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1498,21 +1466,19 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - 'MY_AUTHOR_ID', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + author_id: 'MY_AUTHOR_ID', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1544,21 +1510,19 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'typist-user-external-id', - 'typist', - 'MY_AUTHOR_ID', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + author_id: 'MY_AUTHOR_ID', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1590,14 +1554,13 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1627,14 +1590,13 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1664,14 +1626,13 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); await expect( @@ -1693,14 +1654,13 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); await createTask( source, accountId, @@ -1732,14 +1692,13 @@ describe('checkout', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'none-user-external-id', - 'none', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'none-user-external-id', + role: 'none', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); await expect( @@ -1780,20 +1739,18 @@ describe('checkin', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1829,20 +1786,18 @@ describe('checkin', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1871,21 +1826,23 @@ describe('checkin', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); - const { userId: anotherTypistUserId } = await createUser( - source, - accountId, - 'another-typist-user-external-id', - 'typist', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: anotherTypistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'another-typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -1915,16 +1872,19 @@ describe('checkin', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); - await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); @@ -1961,20 +1921,18 @@ describe('suspend', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2006,20 +1964,18 @@ describe('suspend', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2048,21 +2004,23 @@ describe('suspend', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); - const { userId: anotherTypistUserId } = await createUser( - source, - accountId, - 'another-typist-user-external-id', - 'typist', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: anotherTypistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'another-typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2092,16 +2050,19 @@ describe('suspend', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); - await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); @@ -2138,20 +2099,18 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2188,20 +2147,18 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2238,20 +2195,18 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2289,20 +2244,18 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2340,20 +2293,18 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - 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', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2385,21 +2336,23 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); - const { userId: anotherTypistUserId } = await createUser( - source, - accountId, - 'another-typist-user-external-id', - 'typist', - ); - const { userId: authorUserId } = await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); + const { id: anotherTypistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'another-typist-user-external-id', + role: 'typist', + }); + const { id: authorUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const { taskId } = await createTask( source, accountId, @@ -2432,16 +2385,19 @@ describe('cancel', () => { source, notificationhubServiceMockValue, ); - const { accountId } = await createAccount(source); - await createUser(source, accountId, 'typist-user-external-id', 'typist'); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'typist-user-external-id', + role: 'typist', + }); - await createUser( - source, - accountId, - 'author-user-external-id', - 'author', - 'MY_AUTHOR_ID', - ); + await makeTestUser(source, { + account_id: accountId, + external_id: 'author-user-external-id', + role: 'author', + author_id: 'MY_AUTHOR_ID', + }); const service = module.get(TasksService); diff --git a/dictation_server/src/features/tasks/test/utility.ts b/dictation_server/src/features/tasks/test/utility.ts index 50daad3..8c49279 100644 --- a/dictation_server/src/features/tasks/test/utility.ts +++ b/dictation_server/src/features/tasks/test/utility.ts @@ -1,8 +1,6 @@ import { DataSource } from 'typeorm'; import { Test, TestingModule } from '@nestjs/testing'; import { ConfigModule } from '@nestjs/config'; -import { User } from '../../../repositories/users/entity/user.entity'; -import { Account } from '../../../repositories/accounts/entity/account.entity'; import { Task } from '../../../repositories/tasks/entity/task.entity'; import { AudioFile } from '../../../repositories/audio_files/entity/audio_file.entity'; import { CheckoutPermission } from '../../../repositories/checkout_permissions/entity/checkout_permission.entity'; @@ -102,54 +100,6 @@ export const makeTaskTestingModule = async ( } }; -export const createAccount = async ( - datasource: DataSource, -): Promise<{ accountId: number }> => { - const { identifiers } = await datasource.getRepository(Account).insert({ - tier: 1, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = identifiers.pop() as Account; - return { accountId: account.id }; -}; - -export const createUser = async ( - datasource: DataSource, - accountId: number, - external_id: string, - role: string, - author_id?: string | undefined, -): Promise<{ userId: number; externalId: string }> => { - const { identifiers } = await datasource.getRepository(User).insert({ - account_id: accountId, - external_id: external_id, - role: role, - accepted_terms_version: '1.0', - author_id: author_id, - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: false, - prompt: false, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = identifiers.pop() as User; - return { userId: user.id, externalId: external_id }; -}; - export const createTask = async ( datasource: DataSource, account_id: number, diff --git a/dictation_server/src/features/users/test/utility.ts b/dictation_server/src/features/users/test/utility.ts index 4452f61..7f66b68 100644 --- a/dictation_server/src/features/users/test/utility.ts +++ b/dictation_server/src/features/users/test/utility.ts @@ -29,8 +29,6 @@ import { NotificationhubService } from '../../../gateways/notificationhub/notifi import { FilesService } from '../../../features/files/files.service'; import { LicensesService } from '../../../features/licenses/licenses.service'; import { TasksService } from '../../../features/tasks/tasks.service'; -import { User } from '../../../repositories/users/entity/user.entity'; -import { Account } from '../../../repositories/accounts/entity/account.entity'; import { UserGroup } from '../../../repositories/user_groups/entity/user_group.entity'; import { UserGroupMember } from '../../../repositories/user_groups/entity/user_group_member.entity'; import { License } from '../../../repositories/licenses/entity/license.entity'; @@ -38,150 +36,6 @@ import { AdB2cMockValue, makeAdB2cServiceMock } from './users.service.mock'; import { AdB2cService } from '../../../gateways/adb2c/adb2c.service'; import { LICENSE_ALLOCATED_STATUS, LICENSE_TYPE } from '../../../constants'; -export const createAccountAndAdminUser = async ( - datasource: DataSource, - adminExternalId: string, -): Promise<{ - accountId: number; - adminId: number; - role: string; - tier: number; -}> => { - const { identifiers: account_idf } = await datasource - .getRepository(Account) - .insert({ - tier: 1, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = account_idf.pop() as Account; - - const { identifiers: user_idf } = await datasource - .getRepository(User) - .insert({ - account_id: account.id, - external_id: adminExternalId, - role: 'admin none', - accepted_terms_version: '1.0', - email_verified: true, - auto_renew: true, - license_alert: true, - notification: true, - encryption: true, - encryption_password: 'password', - prompt: true, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = user_idf.pop() as User; - - // Accountの管理者を設定する - await datasource.getRepository(Account).update( - { id: user.account_id }, - { - primary_admin_user_id: user.id, - }, - ); - - const accountResult = await getAccount(datasource, account.id); - const userResult = await getUser(datasource, user.id); - - return { - accountId: account.id, - adminId: user.id, - role: userResult.role, - tier: accountResult.tier, - }; -}; - -export const createAccount = async ( - datasource: DataSource, -): Promise<{ accountId: number }> => { - const { identifiers } = await datasource.getRepository(Account).insert({ - tier: 1, - country: 'JP', - delegation_permission: false, - locked: false, - company_name: 'test inc.', - verified: true, - deleted_at: '', - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const account = identifiers.pop() as Account; - return { accountId: account.id }; -}; - -export const createUser = async ( - datasource: DataSource, - accountId: number, - external_id: string, - role: string, - author_id?: string | undefined, - auto_renew?: boolean, - encryption?: boolean | undefined, - encryption_password?: string | undefined, - prompt?: boolean | undefined, - email_verified?: boolean | undefined, -): Promise<{ userId: number; externalId: string }> => { - const { identifiers } = await datasource.getRepository(User).insert({ - account_id: accountId, - external_id: external_id, - role: role, - accepted_terms_version: '1.0', - author_id: author_id, - email_verified: email_verified ?? true, - auto_renew: auto_renew, - license_alert: true, - notification: true, - encryption: encryption ?? false, - encryption_password: encryption_password, - prompt: prompt ?? false, - created_by: 'test_runner', - created_at: new Date(), - updated_by: 'updater', - updated_at: new Date(), - }); - const user = identifiers.pop() as User; - return { userId: user.id, externalId: external_id }; -}; - -/** - * テスト ユーティリティ: 指定IDのアカウントを取得する - * @param dataSource データソース - * @param id アカウントID - * @returns 該当アカウント - */ -export const getAccount = async (dataSource: DataSource, id: number) => { - return await dataSource.getRepository(Account).findOne({ - where: { id: id }, - }); -}; - -export const getUser = async ( - datasource: DataSource, - id: number, -): Promise => { - const user = await datasource.getRepository(User).findOne({ - where: { - id: id, - }, - }); - return user; -}; - export const getLicenses = async ( datasource: DataSource, account_id: number, @@ -194,33 +48,6 @@ export const getLicenses = async ( return licenses; }; -/** - * テスト ユーティリティ: 指定外部IDを持つユーザーを取得する - * @param dataSource データソース - * @param externalId 外部ID - * @returns 該当ユーザー - */ -export const getUserByExternalId = async ( - datasource: DataSource, - externalId: string, -): Promise => { - const user = await datasource.getRepository(User).findOne({ - where: { - external_id: externalId, - }, - }); - return user; -}; - -/** - * テスト ユーティリティ: すべてのユーザーを取得する - * @param dataSource データソース - * @returns 該当ユーザー一覧 - */ -export const getUsers = async (dataSource: DataSource): Promise => { - return await dataSource.getRepository(User).find(); -}; - /** * * @param datasource diff --git a/dictation_server/src/features/users/users.service.spec.ts b/dictation_server/src/features/users/users.service.spec.ts index fa18ab9..04e5068 100644 --- a/dictation_server/src/features/users/users.service.spec.ts +++ b/dictation_server/src/features/users/users.service.spec.ts @@ -11,15 +11,9 @@ import { } from './test/users.service.mock'; import { EmailAlreadyVerifiedError } from '../../repositories/users/errors/types'; import { - createAccount, - createAccountAndAdminUser, createLicense, - createUser, createUserGroup, getLicenses, - getUser, - getUserByExternalId, - getUsers, makeTestingModuleWithAdb2c, } from './test/utility'; import { DataSource } from 'typeorm'; @@ -41,6 +35,14 @@ import { import { NewTrialLicenseExpirationDate } from '../licenses/types/types'; import { License } from '../../repositories/licenses/entity/license.entity'; import { AdB2cService } from '../../gateways/adb2c/adb2c.service'; +import { + getUser, + getUserFromExternalId, + getUsers, + makeTestAccount, + makeTestSimpleAccount, + makeTestUser, +} from '../../common/test/utility'; describe('UsersService.confirmUser', () => { let source: DataSource = null; @@ -60,33 +62,32 @@ describe('UsersService.confirmUser', () => { it('ユーザの仮登録時に払い出されるトークンにより、未認証のユーザが認証済みになり、トライアルライセンスが100件作成される', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'externalId_user1', - USER_ROLES.NONE, - undefined, - true, - false, - undefined, - false, - false, - ); - const { userId } = await createUser( - source, - accountId, - 'externalId_user2', - USER_ROLES.NONE, - undefined, - true, - false, - undefined, - false, - false, - ); + const { id: accountId } = (await makeTestAccount(source)).account; + const { id: userId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'externalId_user1', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + email_verified: false, + }); + await makeTestUser(source, { + account_id: accountId, + external_id: 'externalId_user2', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + email_verified: false, + }); const service = module.get(UsersService); + // account id:1, user id: 2のトークン const token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50SWQiOjEsInVzZXJJZCI6MiwiZW1haWwiOiJ4eHhAeHh4Lnh4eCIsImlhdCI6MTAwMDAwMDAwMCwiZXhwIjo5MDAwMDAwMDAwfQ.26L6BdNg-3TbyKT62PswlJ6RPMkcTtHzlDXW2Uo9XbMPVSrl2ObcuS6EcXjFFN2DEfNTKbqX_zevIWMpHOAdLNgGhk528nLrBrNvPASqtTjvW9muxMXpjUdjRVkmVbOylBHWW3YpWL9JEbJQ7rAzWDfaIdPhMovdaxumnZt_UwnlnrdaVPLACW7tkH_laEcAU507iSiM4mqxxG8FuTs34t6PEdwRuzZAQPN2IOPYNSvGNdJYryPacSeSNZ_z1xeBYXLOLQfOBZzyTReYDOhXdikhrNUbxjgnZQlSXBCVMlZ9PH42bHfp-LJIeJzW0yqnF6oLklvJP-fo8eW0k5iDOw'; await service.confirmUser(token); @@ -135,31 +136,29 @@ describe('UsersService.confirmUser', () => { it('ユーザが既に認証済みだった場合、認証済みユーザエラーとなる。', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'externalId_user1', - USER_ROLES.NONE, - undefined, - true, - false, - undefined, - false, - false, - ); - await createUser( - source, - accountId, - 'externalId_user2', - USER_ROLES.NONE, - undefined, - true, - false, - undefined, - false, - true, //emailを認証済みにする - ); + const { id: accountId } = (await makeTestAccount(source)).account; + await makeTestUser(source, { + account_id: accountId, + external_id: 'externalId_user1', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + email_verified: true, //emailを認証済みにする + }); + await makeTestUser(source, { + account_id: accountId, + external_id: 'externalId_user2', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + email_verified: false, + }); const service = module.get(UsersService); const token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50SWQiOjEsInVzZXJJZCI6MiwiZW1haWwiOiJ4eHhAeHh4Lnh4eCIsImlhdCI6MTAwMDAwMDAwMCwiZXhwIjo5MDAwMDAwMDAwfQ.26L6BdNg-3TbyKT62PswlJ6RPMkcTtHzlDXW2Uo9XbMPVSrl2ObcuS6EcXjFFN2DEfNTKbqX_zevIWMpHOAdLNgGhk528nLrBrNvPASqtTjvW9muxMXpjUdjRVkmVbOylBHWW3YpWL9JEbJQ7rAzWDfaIdPhMovdaxumnZt_UwnlnrdaVPLACW7tkH_laEcAU507iSiM4mqxxG8FuTs34t6PEdwRuzZAQPN2IOPYNSvGNdJYryPacSeSNZ_z1xeBYXLOLQfOBZzyTReYDOhXdikhrNUbxjgnZQlSXBCVMlZ9PH42bHfp-LJIeJzW0yqnF6oLklvJP-fo8eW0k5iDOw'; @@ -379,11 +378,14 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -437,7 +439,7 @@ describe('UsersService.createUser', () => { ).toEqual(undefined); // 追加されたユーザーが正しくDBに登録されていることを確認 - const user = await getUserByExternalId(source, externalId); + const user = await getUserFromExternalId(source, externalId); expect(user).not.toBeNull(); expect(user.account_id).toEqual(accountId); expect(user.role).toEqual(role); @@ -459,11 +461,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -525,7 +529,7 @@ describe('UsersService.createUser', () => { ).toEqual(undefined); // 追加されたユーザーが正しくDBに登録されていることを確認 - const user = await getUserByExternalId(source, externalId); + const user = await getUserFromExternalId(source, externalId); expect(user).not.toBeNull(); expect(user.account_id).toEqual(accountId); expect(user.role).toEqual(role); @@ -547,11 +551,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -612,7 +618,7 @@ describe('UsersService.createUser', () => { ).toEqual(undefined); // 追加されたユーザーが正しくDBに登録されていることを確認 - const user = await getUserByExternalId(source, externalId); + const user = await getUserFromExternalId(source, externalId); expect(user).not.toBeNull(); expect(user.account_id).toEqual(accountId); expect(user.role).toEqual(role); @@ -634,11 +640,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -692,7 +700,7 @@ describe('UsersService.createUser', () => { ).toEqual(undefined); // 追加されたユーザーが正しくDBに登録されていることを確認 - const user = await getUserByExternalId(source, externalId); + const user = await getUserFromExternalId(source, externalId); expect(user).not.toBeNull(); expect(user.account_id).toEqual(accountId); expect(user.role).toEqual(role); @@ -715,10 +723,13 @@ describe('UsersService.createUser', () => { const service = module.get(UsersService); const b2cService = module.get(AdB2cService); const adminExternalId = 'ADMIN0001'; - const { role: adminRole, tier } = await createAccountAndAdminUser( + const { account, admin } = await makeTestAccount( source, - adminExternalId, + {}, + { external_id: adminExternalId }, ); + const { tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -797,11 +808,13 @@ describe('UsersService.createUser', () => { const service = module.get(UsersService); const b2cService = module.get(AdB2cService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -884,10 +897,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { role: adminRole, tier } = await createAccountAndAdminUser( + const { account, admin } = await makeTestAccount( source, - adminExternalId, + {}, + { external_id: adminExternalId }, ); + const { tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -954,10 +970,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { role: adminRole, tier } = await createAccountAndAdminUser( + const { account, admin } = await makeTestAccount( source, - adminExternalId, + {}, + { external_id: adminExternalId }, ); + const { tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -1028,10 +1047,13 @@ describe('UsersService.createUser', () => { const module = await makeTestingModule(source); const service = module.get(UsersService); const adminExternalId = 'ADMIN0001'; - const { role: adminRole, tier } = await createAccountAndAdminUser( + const { account, admin } = await makeTestAccount( source, - adminExternalId, + {}, + { external_id: adminExternalId }, ); + const { tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -1152,11 +1174,13 @@ describe('UsersService.createUser', () => { const service = module.get(UsersService); const b2cService = module.get(AdB2cService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -1248,11 +1272,13 @@ describe('UsersService.createUser', () => { const b2cService = module.get(AdB2cService); const adminExternalId = 'ADMIN0001'; - const { - accountId, - role: adminRole, - tier, - } = await createAccountAndAdminUser(source, adminExternalId); + const { account, admin } = await makeTestAccount( + source, + {}, + { external_id: adminExternalId }, + ); + const { id: accountId, tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -1331,10 +1357,13 @@ describe('UsersService.createUser', () => { const b2cService = module.get(AdB2cService); const adminExternalId = 'ADMIN0001'; - const { role: adminRole, tier } = await createAccountAndAdminUser( + const { account, admin } = await makeTestAccount( source, - adminExternalId, + {}, + { external_id: adminExternalId }, ); + const { tier } = account; + const { role: adminRole } = admin; const token: AccessToken = { userId: adminExternalId, @@ -1433,36 +1462,42 @@ describe('UsersService.getUsers', () => { const adb2cParam = makeDefaultAdB2cMockValue(); const module = await makeTestingModuleWithAdb2c(source, adb2cParam); - const { accountId } = await createAccount(source); - const { externalId: externalId_author, userId: authorUserId } = - await createUser( - source, - accountId, - 'external_id1', - 'author', - 'AUTHOR_ID', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId_author, id: authorUserId } = + await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: 'author', + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); - const { userId: typistUserId } = await createUser( - source, - accountId, - 'external_id2', - 'typist', - undefined, - true, - ); + const { id: typistUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id2', + role: 'typist', + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); await createUserGroup(source, accountId, 'group1', [typistUserId]); - const { userId: noneUserId } = await createUser( - source, - accountId, - 'external_id3', - 'none', - undefined, - true, - ); + const { id: noneUserId } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id3', + role: 'none', + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -1527,31 +1562,40 @@ describe('UsersService.getUsers', () => { const adb2cParam = makeDefaultAdB2cMockValue(); const module = await makeTestingModuleWithAdb2c(source, adb2cParam); - const { accountId } = await createAccount(source); - const { userId: user1, externalId: external_id1 } = await createUser( + const { id: accountId } = await makeTestSimpleAccount(source); + const { id: user1, external_id: external_id1 } = await makeTestUser( source, - accountId, - 'external_id1', - 'author', - 'AUTHOR_ID1', - true, - ); - const { userId: user2 } = await createUser( - source, - accountId, - 'external_id2', - 'author', - 'AUTHOR_ID2', - true, - ); - const { userId: user3 } = await createUser( - source, - accountId, - 'external_id3', - 'author', - 'AUTHOR_ID3', - false, + { + account_id: accountId, + external_id: 'external_id1', + role: 'author', + author_id: 'AUTHOR_ID1', + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }, ); + const { id: user2 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id2', + role: 'author', + author_id: 'AUTHOR_ID2', + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user3 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id3', + role: 'author', + author_id: 'AUTHOR_ID3', + auto_renew: false, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const date1 = new Date(); date1.setDate(date1.getDate() + LICENSE_EXPIRATION_THRESHOLD_DAYS + 1); @@ -1632,15 +1676,17 @@ describe('UsersService.getUsers', () => { const adb2cParam = makeDefaultAdB2cMockValue(); const module = await makeTestingModuleWithAdb2c(source, adb2cParam); - const { accountId } = await createAccount(source); - await createUser( - source, - accountId, - 'external_id1', - 'author', - 'AUTHOR_ID', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: 'author', + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); await expect(service.getUsers('externalId_failed')).rejects.toEqual( @@ -1653,15 +1699,17 @@ describe('UsersService.getUsers', () => { adb2cParam.getUsers = new Error('ADB2C error'); const module = await makeTestingModuleWithAdb2c(source, adb2cParam); - const { accountId } = await createAccount(source); - const { externalId: externalId_author } = await createUser( - source, - accountId, - 'external_id1', - 'author', - 'AUTHOR_ID', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: externalId_author } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: 'author', + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); await expect(service.getUsers(externalId_author)).rejects.toEqual( @@ -1890,23 +1938,27 @@ describe('UsersService.updateUser', () => { it('ユーザー情報を更新できる(None)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.NONE, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -1942,23 +1994,27 @@ describe('UsersService.updateUser', () => { it('ユーザー情報を更新できる(Typist)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.TYPIST, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.TYPIST, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -1994,26 +2050,27 @@ describe('UsersService.updateUser', () => { it('ユーザー情報を更新できる(Author)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.AUTHOR, - undefined, - true, - true, - 'password', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.AUTHOR, + author_id: undefined, + auto_renew: true, + encryption: true, + encryption_password: 'password', + prompt: true, + }); const service = module.get(UsersService); @@ -2049,23 +2106,27 @@ describe('UsersService.updateUser', () => { it('ユーザーのRoleを更新できる(None⇒Typist)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.NONE, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -2101,23 +2162,27 @@ describe('UsersService.updateUser', () => { it('ユーザーのRoleを更新できる(None⇒Author)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.NONE, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -2153,23 +2218,27 @@ describe('UsersService.updateUser', () => { it('None以外からRoleを変更した場合、エラーとなる(Typist⇒None)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.TYPIST, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.TYPIST, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); const service = module.get(UsersService); @@ -2195,26 +2264,27 @@ describe('UsersService.updateUser', () => { it('Authorがパスワードundefinedで渡されたとき、元のパスワードを維持する(Encryptionがtrue)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.AUTHOR, - 'AUTHOR_ID', - true, - true, - 'password', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.AUTHOR, + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: true, + encryption_password: 'password', + prompt: true, + }); const service = module.get(UsersService); @@ -2250,26 +2320,27 @@ describe('UsersService.updateUser', () => { it('Authorが暗号化なしで更新した場合、パスワードをNULLにする(Encryptionがfalse)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.AUTHOR, - 'AUTHOR_ID', - true, - false, - 'password', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.AUTHOR, + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: false, + encryption_password: 'password', + prompt: true, + }); const service = module.get(UsersService); @@ -2305,26 +2376,27 @@ describe('UsersService.updateUser', () => { it('AuthorのDBにパスワードが設定されていない場合、パスワードundefinedでわたすとエラーとなる(Encryptionがtrue)', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.AUTHOR, - 'AUTHOR_ID', - true, - true, - undefined, - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.AUTHOR, + author_id: 'AUTHOR_ID', + auto_renew: true, + encryption: true, + encryption_password: undefined, + prompt: true, + }); const service = module.get(UsersService); @@ -2350,38 +2422,38 @@ describe('UsersService.updateUser', () => { it('AuthorIdが既存のユーザーと重複した場合、エラーとなる', async () => { const module = await makeTestingModule(source); - const { accountId } = await createAccount(source); - const { externalId: external_id } = await createUser( - source, - accountId, - 'external_id', - USER_ROLES.NONE, - undefined, - true, - ); - const { userId: user1 } = await createUser( - source, - accountId, - 'external_id1', - USER_ROLES.AUTHOR, - 'AUTHOR_ID1', - true, - true, - 'password', - true, - ); + const { id: accountId } = await makeTestSimpleAccount(source); + const { external_id: external_id } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id', + role: USER_ROLES.NONE, + author_id: undefined, + auto_renew: true, + encryption: false, + encryption_password: undefined, + prompt: false, + }); + const { id: user1 } = await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id1', + role: USER_ROLES.AUTHOR, + author_id: 'AUTHOR_ID1', + auto_renew: true, + encryption: true, + encryption_password: 'password', + prompt: true, + }); - await createUser( - source, - accountId, - 'external_id2', - USER_ROLES.AUTHOR, - 'AUTHOR_ID2', - true, - true, - 'password', - true, - ); + await makeTestUser(source, { + account_id: accountId, + external_id: 'external_id2', + role: USER_ROLES.AUTHOR, + author_id: 'AUTHOR_ID2', + auto_renew: true, + encryption: true, + encryption_password: 'password', + prompt: true, + }); const service = module.get(UsersService);