import { DataSource } from "typeorm"; import { licenseAutoAllocationProcessing } from "../functions/licenseAutoAllocation"; import { LICENSE_ALLOCATED_STATUS, LICENSE_TYPE, USER_ROLES, } from "../constants"; import { DateWithDayEndTime } from "../common/types/types"; import { makeTestAccount, createAndAllocateLicense, makeTestUser, selectLicenseByAllocatedUser, selectLicenseAllocationHistory, } from "./common/utility"; import * as dotenv from "dotenv"; import { InvocationContext } from "@azure/functions"; import { AdB2cService } from "../adb2c/adb2c"; import { SendGridService } from "../sendgrid/sendgrid"; import { SendGridServiceMock } from "./common/sendGrid.mock"; import { AdB2cServiceMock } from "./common/adb2c.mock"; import { createClient } from "redis-mock"; describe("licenseAutoAllocation", () => { dotenv.config({ path: ".env" }); dotenv.config({ path: ".env.local", override: true }); let source: DataSource | null = null; const redisClient = createClient(); beforeEach(async () => { source = new DataSource({ type: "sqlite", database: ":memory:", logging: false, entities: [__dirname + "/../../**/*.entity{.ts,.js}"], synchronize: true, // trueにすると自動的にmigrationが行われるため注意 }); return source.initialize(); }); afterEach(async () => { if (!source) return; await source.destroy(); source = null; }); it("有効期限が本日のライセンスが自動更新されること", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); const currentDateEndTime = new DateWithDayEndTime(); // アカウント const account1 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); const account2 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id6" } ); // 更新対象のユーザー(3role分) const user1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id2", }); const user2 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id3", }); const user3 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.TYPIST}`, external_id: "external_id4", }); // 更新対象ではないユーザー(まだ有効期限が残っている) const user4 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, }); // 更新対象ではないユーザー(auto_renewがfalse) const user5 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, auto_renew: false, }); // 更新対象のユーザー(Author二人目) const user6 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, }); // 更新対象のユーザー(ただしライセンスが足りない) const user7 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id5", }); // 割り当て済みで有効期限が本日のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user1.id, null, null, null ); await createAndAllocateLicense( source, 2, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user2.id, null, null, null ); await createAndAllocateLicense( source, 3, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user3.id, null, null, null ); await createAndAllocateLicense( source, 20, currentDateEndTime, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, account2.admin.id, null, null, null ); await createAndAllocateLicense( source, 5, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user5.id, null, null, null ); await createAndAllocateLicense( source, 6, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user6.id, null, null, null ); await createAndAllocateLicense( source, 7, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user7.id, null, null, null ); // 割り当て済みの更新対象ではないライセンス const nextDate = new Date(); nextDate.setDate(nextDate.getDate() + 1); nextDate.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 nextDate.setMilliseconds(0); await createAndAllocateLicense( source, 4, nextDate, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user4.id, null, null, null ); // 有効期限が先の未割当ライセンスを作成 // idが100のものは有効期限が当日なので自動割り当て対象外 // idが101のものから割り当てられる for (let i = 0; i < 5; i++) { const date = new Date(); date.setDate(date.getDate() + i); date.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 date.setMilliseconds(0); await createAndAllocateLicense( source, i + 100, date, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); } const date = new Date(); date.setDate(date.getDate() + 30); date.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 date.setMilliseconds(0); await createAndAllocateLicense( source, 200, date, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, null, null ); await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock ); const user1Allocated = await selectLicenseByAllocatedUser(source, user1.id); const user2Allocated = await selectLicenseByAllocatedUser(source, user2.id); const user3Allocated = await selectLicenseByAllocatedUser(source, user3.id); const user4Allocated = await selectLicenseByAllocatedUser(source, user4.id); const user5Allocated = await selectLicenseByAllocatedUser(source, user5.id); const user6Allocated = await selectLicenseByAllocatedUser(source, user6.id); const user7Allocated = await selectLicenseByAllocatedUser(source, user7.id); const admin2Allocated = await selectLicenseByAllocatedUser( source, account2.admin.id ); const licenseAllocationHistory = await selectLicenseAllocationHistory( source, user1.id, 104 ); // Author、Typist、Noneの優先順位で割り当てられていることを確認 expect(user1Allocated.license?.id).toBe(104); expect(user2Allocated.license?.id).toBe(101); expect(user3Allocated.license?.id).toBe(103); // 有効期限がまだあるので、ライセンスが更新されていないことを確認 expect(user4Allocated.license?.id).toBe(4); // auto_renewがfalseなので、ライセンスが更新されていないことを確認 expect(user5Allocated.license?.id).toBe(5); // 複数Authorがいる場合、それぞれに割り当てられていることを確認 expect(user6Allocated.license?.id).toBe(102); // ライセンスが足りない場合、ライセンスが更新されていないことを確認 expect(user7Allocated.license?.id).toBe(7); // 複数アカウント分の処理が正常に行われていることの確認 expect(admin2Allocated.license?.id).toBe(200); // ライセンス割り当て履歴テーブルが更新されていることを確認 expect(licenseAllocationHistory.licenseAllocationHistory?.user_id).toBe( user1.id ); expect( licenseAllocationHistory.licenseAllocationHistory?.is_allocated ).toBe(true); expect(licenseAllocationHistory.licenseAllocationHistory?.account_id).toBe( account1.account.id ); expect( licenseAllocationHistory.licenseAllocationHistory?.switch_from_type ).toBe("CARD"); // メール送信が行われていることを確認 expect(spySend).toHaveBeenCalledTimes(4); }); it("新規ライセンスがある場合でも、有効期限が本日のライセンスが自動更新されること", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); const currentDateEndTime = new DateWithDayEndTime(); // アカウント const account1 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); const account2 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id6" } ); // 更新対象のユーザー(3role分) const testNoneUser1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id2", }); const testAuthorUser2 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id3", }); const testTypistUser3 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.TYPIST}`, external_id: "external_id4", }); // 更新対象ではないユーザー(まだ有効期限が残っている) const testNoneUser4ExpirationRemain = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, }); // 更新対象ではないユーザー(auto_renewがfalse) const testNoneUser5AutoRenewFalse = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, auto_renew: false, }); // 更新対象のユーザー(Author二人目) const testAuthorUser6 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id5", }); // 更新対象のユーザー(ただしライセンスが足りない) const testNoneUser7lackofLicenses = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, }); // 割り当て済みで有効期限が本日のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, testNoneUser1.id, null, null, null ); await createAndAllocateLicense( source, 2, currentDateEndTime, account1.account.id, LICENSE_TYPE.NORMAL, LICENSE_ALLOCATED_STATUS.ALLOCATED, testAuthorUser2.id, null, null, null ); await createAndAllocateLicense( source, 3, currentDateEndTime, account1.account.id, LICENSE_TYPE.TRIAL, LICENSE_ALLOCATED_STATUS.ALLOCATED, testTypistUser3.id, null, null, null ); await createAndAllocateLicense( source, 20, currentDateEndTime, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, account2.admin.id, null, null, null ); await createAndAllocateLicense( source, 5, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, testNoneUser5AutoRenewFalse.id, null, null, null ); await createAndAllocateLicense( source, 6, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, testAuthorUser6.id, null, null, null ); await createAndAllocateLicense( source, 7, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, testNoneUser7lackofLicenses.id, null, null, null ); // 割り当て済みの更新対象ではないライセンス const nextDate = new Date(); nextDate.setDate(nextDate.getDate() + 1); nextDate.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 nextDate.setMilliseconds(0); await createAndAllocateLicense( source, 4, nextDate, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, testNoneUser4ExpirationRemain.id, null, null, null ); // 有効期限が先のライセンスを作成 // idが100のものは有効期限が当日なので自動割り当て対象外 // idが101のものから割り当てられる for (let i = 0; i < 4; i++) { const date = new Date(); date.setDate(date.getDate() + i); date.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 date.setMilliseconds(0); await createAndAllocateLicense( source, i + 100, date, account1.account.id, LICENSE_TYPE.TRIAL, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); } // account1用の有効期限が設定されていないライセンスを作成 await createAndAllocateLicense( source, 99, null, account1.account.id, LICENSE_TYPE.NORMAL, LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, null, null ); // account2用の有効期限が設定されていないライセンスを作成 await createAndAllocateLicense( source, 200, null, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, null, null ); await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock ); const testNoneUser1Allocated = await selectLicenseByAllocatedUser( source, testNoneUser1.id ); const testAuthorUser2Allocated = await selectLicenseByAllocatedUser( source, testAuthorUser2.id ); const testTypistUser3Allocated = await selectLicenseByAllocatedUser( source, testTypistUser3.id ); const testNoneUser4ExpirationRemainAllocated = await selectLicenseByAllocatedUser( source, testNoneUser4ExpirationRemain.id ); const testNoneUser5AutoRenewFalseAllocated = await selectLicenseByAllocatedUser( source, testNoneUser5AutoRenewFalse.id ); const testAuthorUser6Allocated = await selectLicenseByAllocatedUser( source, testAuthorUser6.id ); const testNoneUser7lackofLicensesAllocated = await selectLicenseByAllocatedUser( source, testNoneUser7lackofLicenses.id ); const admin2Allocated = await selectLicenseByAllocatedUser( source, account2.admin.id ); const testNoneUser1LicenseAllocationHistory = await selectLicenseAllocationHistory(source, testNoneUser1.id, 99); const testAuthorUser2LicenseAllocationHistory = await selectLicenseAllocationHistory(source, testAuthorUser2.id, 101); const testTypistUser3LicenseAllocationHistory = await selectLicenseAllocationHistory(source, testTypistUser3.id, 103); // Author、Typist、Noneの優先順位で割り当てられていることを確認 // 複数Authorがいる場合、それぞれに割り当てられていることを確認 expect(testAuthorUser2Allocated.license?.id).toBe(101); expect(testAuthorUser6Allocated.license?.id).toBe(102); expect(testTypistUser3Allocated.license?.id).toBe(103); expect(testNoneUser1Allocated.license?.id).toBe(99); // 有効期限がまだあるので、ライセンスが更新されていないことを確認 expect(testNoneUser4ExpirationRemainAllocated.license?.id).toBe(4); // auto_renewがfalseなので、ライセンスが更新されていないことを確認 expect(testNoneUser5AutoRenewFalseAllocated.license?.id).toBe(5); // ライセンスが足りない場合、ライセンスが更新されていないことを確認 expect(testNoneUser7lackofLicensesAllocated.license?.id).toBe(7); // 複数アカウント分の処理が正常に行われていることの確認 expect(admin2Allocated.license?.id).toBe(200); // ライセンス割り当て履歴テーブルが更新されていることを確認 expect( testNoneUser1LicenseAllocationHistory.licenseAllocationHistory?.user_id ).toBe(testNoneUser1.id); expect( testNoneUser1LicenseAllocationHistory.licenseAllocationHistory ?.is_allocated ).toBe(true); expect( testNoneUser1LicenseAllocationHistory.licenseAllocationHistory?.account_id ).toBe(account1.account.id); expect( testNoneUser1LicenseAllocationHistory.licenseAllocationHistory ?.switch_from_type ).toBe("CARD"); expect( testAuthorUser2LicenseAllocationHistory.licenseAllocationHistory?.user_id ).toBe(testAuthorUser2.id); expect( testAuthorUser2LicenseAllocationHistory.licenseAllocationHistory ?.is_allocated ).toBe(true); expect( testAuthorUser2LicenseAllocationHistory.licenseAllocationHistory ?.account_id ).toBe(account1.account.id); expect( testAuthorUser2LicenseAllocationHistory.licenseAllocationHistory ?.switch_from_type ).toBe("NONE"); expect( testTypistUser3LicenseAllocationHistory.licenseAllocationHistory?.user_id ).toBe(testTypistUser3.id); expect( testTypistUser3LicenseAllocationHistory.licenseAllocationHistory ?.is_allocated ).toBe(true); expect( testTypistUser3LicenseAllocationHistory.licenseAllocationHistory ?.account_id ).toBe(account1.account.id); expect( testTypistUser3LicenseAllocationHistory.licenseAllocationHistory ?.switch_from_type ).toBe("TRIAL"); // メール送信が行われていることを確認 expect(spySend).toHaveBeenCalledTimes(5); }); it("有効期限が指定日のライセンスが自動更新されること(リトライ用)", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); // 2023/11/22の日付を作成 const date1122 = new Date(2023, 10, 22, 23, 59, 59); const currentDateEndTime = new DateWithDayEndTime(date1122); // アカウント const account1 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); const account2 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id6" } ); // 更新対象のユーザー(3role分) const user1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id2", }); const user2 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id3", }); const user3 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.TYPIST}`, external_id: "external_id4", }); // 更新対象ではないユーザー(まだ有効期限が残っている) const user4 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, }); // 更新対象ではないユーザー(auto_renewがfalse) const user5 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, auto_renew: false, }); // 割り当て済みで有効期限が11/22のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user1.id, null, null, null ); await createAndAllocateLicense( source, 2, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user2.id, null, null, null ); await createAndAllocateLicense( source, 3, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user3.id, null, null, null ); await createAndAllocateLicense( source, 20, currentDateEndTime, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, account2.admin.id, null, null, null ); await createAndAllocateLicense( source, 5, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user5.id, null, null, null ); // 割り当て済みの更新対象ではないライセンス const nextDate = new Date(); nextDate.setDate(date1122.getDate() + 1); nextDate.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 nextDate.setMilliseconds(0); await createAndAllocateLicense( source, 4, nextDate, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user4.id, null, null, null ); // 有効期限が先の未割当ライセンスを作成 // idが100のものは有効期限が当日なので自動割り当て対象外 // idが101のものから割り当てられる for (let i = 0; i < 10; i++) { // 2023/11/22の日付を作成 const date = new Date(2023, 10, 22, 23, 59, 59); date.setDate(date.getDate() + i); await createAndAllocateLicense( source, i + 100, date, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); } const dateMarch31 = new Date(); dateMarch31.setMonth(12); dateMarch31.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 dateMarch31.setMilliseconds(0); await createAndAllocateLicense( source, 200, dateMarch31, account2.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.REUSABLE, null, null, null, null ); await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock, date1122 ); const user1Allocated = await selectLicenseByAllocatedUser(source, user1.id); const user2Allocated = await selectLicenseByAllocatedUser(source, user2.id); const user3Allocated = await selectLicenseByAllocatedUser(source, user3.id); const user4Allocated = await selectLicenseByAllocatedUser(source, user4.id); const user5Allocated = await selectLicenseByAllocatedUser(source, user5.id); const admin2Allocated = await selectLicenseByAllocatedUser( source, account2.admin.id ); const licenseAllocationHistory = await selectLicenseAllocationHistory( source, user1.id, 103 ); // Author、Typist、Noneの優先順位で割り当てられていることを確認 expect(user1Allocated.license?.id).toBe(103); expect(user2Allocated.license?.id).toBe(101); expect(user3Allocated.license?.id).toBe(102); // 有効期限がまだあるので、ライセンスが更新されていないことを確認 expect(user4Allocated.license?.id).toBe(4); // auto_renewがfalseなので、ライセンスが更新されていないことを確認 expect(user5Allocated.license?.id).toBe(5); // 複数アカウント分の処理が正常に行われていることの確認 expect(admin2Allocated.license?.id).toBe(200); // ライセンス割り当て履歴テーブルが更新されていることを確認 expect(licenseAllocationHistory.licenseAllocationHistory?.user_id).toBe( user1.id ); expect( licenseAllocationHistory.licenseAllocationHistory?.is_allocated ).toBe(true); expect(licenseAllocationHistory.licenseAllocationHistory?.account_id).toBe( account1.account.id ); // メール送信が行われていることを確認 expect(spySend).toHaveBeenCalledTimes(4); }); it("新たに割り当てられるライセンスが存在しないアカウントは、ライセンスが自動更新されない(エラーではない)", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); const currentDateEndTime = new DateWithDayEndTime(); // アカウントと管理者 const account1 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); const account2 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id5" } ); // account1の更新対象のユーザー(3role分) const user1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id2", }); const user2 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id3", }); const user3 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.TYPIST}`, external_id: "external_id4", }); // 割り当て済みで有効期限が本日のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user1.id, null, null, null ); await createAndAllocateLicense( source, 2, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user2.id, null, null, null ); await createAndAllocateLicense( source, 3, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user3.id, null, null, null ); await createAndAllocateLicense( source, 4, currentDateEndTime, account2.account.id, LICENSE_TYPE.TRIAL, LICENSE_ALLOCATED_STATUS.ALLOCATED, account2.admin.id, null, null, null ); // account2の有効期限が先の未割当ライセンスを作成 const nextDate = new Date(); nextDate.setDate(nextDate.getDate() + 1); nextDate.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 nextDate.setMilliseconds(0); await createAndAllocateLicense( source, 100, nextDate, account2.account.id, LICENSE_TYPE.NORMAL, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock ); const user1Allocated = await selectLicenseByAllocatedUser(source, user1.id); const user2Allocated = await selectLicenseByAllocatedUser(source, user2.id); const user3Allocated = await selectLicenseByAllocatedUser(source, user3.id); const account2AdminAllocated = await selectLicenseByAllocatedUser( source, account2.admin.id ); // ライセンスが更新されていないことを確認 expect(user1Allocated.license?.id).toBe(1); expect(user2Allocated.license?.id).toBe(2); expect(user3Allocated.license?.id).toBe(3); expect(account2AdminAllocated.license?.id).toBe(100); // メール送信が行われていないことを確認 expect(spySend).toHaveBeenCalledTimes(1); }); it("tier4のアカウントのため、ライセンスが自動更新されない", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); const currentDateEndTime = new DateWithDayEndTime(); // アカウント const account1 = await makeTestAccount( source, { tier: 4 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); // 更新対象のユーザー(3role分) const user1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id2", }); const user2 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.AUTHOR}`, external_id: "external_id3", }); const user3 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.TYPIST}`, external_id: "external_id4", }); // 割り当て済みで有効期限が本日のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user1.id, null, null, null ); await createAndAllocateLicense( source, 2, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user2.id, null, null, null ); await createAndAllocateLicense( source, 3, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user3.id, null, null, null ); // 有効期限が先の未割当ライセンスを作成 // idが100,101のものは有効期限が当日、翌日なので自動割り当て対象外 // idが102のものから割り当てられる for (let i = 0; i < 10; i++) { const date = new Date(); date.setDate(date.getDate() + i); date.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 date.setMilliseconds(0); await createAndAllocateLicense( source, i + 100, date, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); } await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock ); const user1Allocated = await selectLicenseByAllocatedUser(source, user1.id); const user2Allocated = await selectLicenseByAllocatedUser(source, user2.id); const user3Allocated = await selectLicenseByAllocatedUser(source, user3.id); // ライセンスが更新されていないことを確認 expect(user1Allocated.license?.id).toBe(1); expect(user2Allocated.license?.id).toBe(2); expect(user3Allocated.license?.id).toBe(3); // メール送信が行われていないことを確認 expect(spySend).toHaveBeenCalledTimes(0); }); it("メール送信に失敗しても、有効期限が本日のライセンスが自動更新されること", async () => { if (!source) fail(); const context = new InvocationContext(); const sendgridMock = new SendGridServiceMock() as SendGridService; const adb2cMock = new AdB2cServiceMock() as AdB2cService; // 呼び出し回数でテスト成否を判定 const spySend = jest.spyOn(sendgridMock, "sendMail"); const currentDateEndTime = new DateWithDayEndTime(); // アカウント const account1 = await makeTestAccount( source, { tier: 5 }, { role: `${USER_ROLES.NONE}`, external_id: "external_id1" } ); // 更新対象のユーザー(3role分) const user1 = await makeTestUser(source, { account_id: account1.account.id, role: `${USER_ROLES.NONE}`, external_id: "external_id7", // メール送信失敗用 }); // 割り当て済みで有効期限が本日のライセンス await createAndAllocateLicense( source, 1, currentDateEndTime, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.ALLOCATED, user1.id, null, null, null ); // 有効期限が先の未割当ライセンスを作成 // idが100のものは有効期限が当日なので自動割り当て対象外 // idが101のものから割り当てられる for (let i = 0; i < 5; i++) { const date = new Date(); date.setDate(date.getDate() + i); date.setHours(23, 59, 59); // 時分秒を"23:59:59"に固定 date.setMilliseconds(0); await createAndAllocateLicense( source, i + 100, date, account1.account.id, LICENSE_TYPE.CARD, LICENSE_ALLOCATED_STATUS.UNALLOCATED, null, null, null, null ); } await licenseAutoAllocationProcessing( context, source, redisClient, sendgridMock, adb2cMock ); const user1Allocated = await selectLicenseByAllocatedUser(source, user1.id); const licenseAllocationHistory = await selectLicenseAllocationHistory( source, user1.id, 101 ); // 割り当てられていることを確認 expect(user1Allocated.license?.id).toBe(101); // ライセンス割り当て履歴テーブルが更新されていることを確認 expect(licenseAllocationHistory.licenseAllocationHistory?.user_id).toBe( user1.id ); expect( licenseAllocationHistory.licenseAllocationHistory?.is_allocated ).toBe(true); expect(licenseAllocationHistory.licenseAllocationHistory?.account_id).toBe( account1.account.id ); expect( licenseAllocationHistory.licenseAllocationHistory?.switch_from_type ).toBe("CARD"); // メール送信が行われていないことを確認 expect(spySend).toHaveBeenCalledTimes(0); }); });