From 79c73f00ed1b93f70a3eda0fed7448f9b35453c4 Mon Sep 17 00:00:00 2001 From: Naoki Date: Fri, 19 Jan 2024 18:36:07 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=83=95=E3=83=A9=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B=E5=8D=98=E4=BD=93=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E4=BD=9C=E6=88=90=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.ts | 1 + .../user/sns-user-registration-action.spec.ts | 4 +- .../user/sns-user-registration-action.spec.ts | 58 ++++++-- ...er.spec.ts => user-ccount-manager.spec.ts} | 0 .../user/authenticated-user-provider.spec.ts | 48 +++++-- .../libraries/user/user-registrar.spec.ts | 135 ++++++++++++++++++ .../user/authenticated-user-loader.spec.ts | 33 ++++- .../user/postgres-user-repository.spec.ts | 34 ++++- tests/infrastructure/routes/app/route.spec.ts | 73 +++------- .../routes/auth.register-user/route.spec.ts | 119 +++++++++++++++ tests/libraries/user/user-registrar.spec.ts | 10 +- 11 files changed, 418 insertions(+), 97 deletions(-) rename tests/infrastructure/libraries/authentication/{firebase-user-account-manager.spec.ts => user-ccount-manager.spec.ts} (100%) create mode 100644 tests/infrastructure/libraries/user/user-registrar.spec.ts create mode 100644 tests/infrastructure/routes/auth.register-user/route.spec.ts diff --git a/jest.config.ts b/jest.config.ts index 70c43b4..cd769a3 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -8,6 +8,7 @@ import type {Config} from 'jest'; const config: Config = { preset: 'ts-jest', testEnvironment: 'node', + testTimeout: 60000, // All imported modules in your tests should be mocked automatically // automock: false, diff --git a/tests/actions/user/sns-user-registration-action.spec.ts b/tests/actions/user/sns-user-registration-action.spec.ts index 164f7d1..60696c9 100644 --- a/tests/actions/user/sns-user-registration-action.spec.ts +++ b/tests/actions/user/sns-user-registration-action.spec.ts @@ -11,7 +11,7 @@ let snsUserRegistrationAction: SnsUserRegistrationAction; /** * 認証プロバイダID。 */ -const authenticationProvidedId = "authenticationProvidedId"; +const authenticationProviderId = "authenticationProviderId"; /** * ユーザー名。 @@ -32,7 +32,7 @@ beforeEach(() => { describe("register", () => { test("register should register a user and return true.", async () => { // ユーザーを登録する。 - const response = await snsUserRegistrationAction.register(authenticationProvidedId, userName); + const response = await snsUserRegistrationAction.register(authenticationProviderId, userName); // 結果を検証する。 expect(response).toBe(true); diff --git a/tests/infrastructure/actions/user/sns-user-registration-action.spec.ts b/tests/infrastructure/actions/user/sns-user-registration-action.spec.ts index f50bf2a..d5f3206 100644 --- a/tests/infrastructure/actions/user/sns-user-registration-action.spec.ts +++ b/tests/infrastructure/actions/user/sns-user-registration-action.spec.ts @@ -1,7 +1,14 @@ import { describe, test, expect, beforeEach } from "@jest/globals"; -import MockUserRepository from "../../../repositories/user/mock-user-repository"; +import delayAsync from "../../../test-utilityies/delay-async"; +import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; import UserRegistrar from "../../../../app/libraries/user/user-registrar"; import SnsUserRegistrationAction from "../../../../app/actions/user/sns-user-registration-action"; +import { postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; + +/** + * Postgresのユーザーリポジトリ。 + */ +let postgresUserRepository: PostgresUserRepository; /** * SNSのユーザー登録を行うアクション。 @@ -9,9 +16,9 @@ import SnsUserRegistrationAction from "../../../../app/actions/user/sns-user-reg let snsUserRegistrationAction: SnsUserRegistrationAction; /** - * 認証プロバイダID。 + * プロフィールID。 */ -const authenticationProvidedId = "authenticationProvidedId"; +const profileId = "username_world"; /** * ユーザー名。 @@ -23,10 +30,25 @@ const userName = "UserName@World"; */ const id = 1; -beforeEach(() => { - const mockUserRepository = new MockUserRepository(); - const userAccountManager = new UserRegistrar(mockUserRepository); +beforeEach(async () => { + postgresUserRepository = new PostgresUserRepository(postgresClientProvider); + const userAccountManager = new UserRegistrar(postgresUserRepository); snsUserRegistrationAction = new SnsUserRegistrationAction(userAccountManager); + + // テスト用のユーザー情報が存在する場合、削除する。 + try { + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + const id = responseFindByProfileId.id; + await delayAsync(() => postgresUserRepository.delete(id)); + console.info("テスト用のユーザー情報を削除しました。"); + } catch (error) { + console.info("テスト用のユーザー情報は存在しませんでした。"); + } }); describe("register", () => { @@ -36,9 +58,9 @@ describe("register", () => { return; } - test("register should register a user and return true.", async () => { - // ユーザーを登録する。 - const response = await snsUserRegistrationAction.register(authenticationProvidedId, userName); + test("register should register a new user", async () => { + // テスト用のユーザー情報を作成する。 + const response = await delayAsync(() => snsUserRegistrationAction.register(profileId, userName)); // 結果を検証する。 expect(response).toBe(true); @@ -52,11 +74,21 @@ describe("delete", () => { return; } - test("delete should delete a user and return true.", async () => { - // ユーザーを削除する。 - const response = await snsUserRegistrationAction.delete(id); + test("delete should delete a user", async () => { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => snsUserRegistrationAction.register(profileId, userName)); + + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + // テスト用のユーザー情報を削除する。 + const id = responseFindByProfileId.id; + const responseDelete = await delayAsync(() => snsUserRegistrationAction.delete(id)); // 結果を検証する。 - expect(response).toBe(true); + expect(responseDelete).toBe(true); }); }); \ No newline at end of file diff --git a/tests/infrastructure/libraries/authentication/firebase-user-account-manager.spec.ts b/tests/infrastructure/libraries/authentication/user-ccount-manager.spec.ts similarity index 100% rename from tests/infrastructure/libraries/authentication/firebase-user-account-manager.spec.ts rename to tests/infrastructure/libraries/authentication/user-ccount-manager.spec.ts diff --git a/tests/infrastructure/libraries/user/authenticated-user-provider.spec.ts b/tests/infrastructure/libraries/user/authenticated-user-provider.spec.ts index 8914c2c..3aef921 100644 --- a/tests/infrastructure/libraries/user/authenticated-user-provider.spec.ts +++ b/tests/infrastructure/libraries/user/authenticated-user-provider.spec.ts @@ -3,6 +3,7 @@ import delayAsync from "../../../test-utilityies/delay-async"; import AuthenticatedUserProvider from "../../../../app/libraries/user/authenticated-user-provider"; import FirebaseClient from "../../../../app/libraries/authentication/firebase-client"; import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; +import { postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; /** * Firebaseのクライアント。 @@ -12,7 +13,7 @@ let firebaseClient: FirebaseClient; /** * Postgresのユーザーリポジトリ。 */ -let posgresUserRepository: PostgresUserRepository; +let postgresUserRepository: PostgresUserRepository; /** * 認証済みユーザーを提供するクラス。 @@ -32,12 +33,17 @@ const password = "testPassword123"; /** * プロフィールID。 */ -const profileId = "test_unicorn"; +const profileId = "username_world"; + +/** + * ユーザー名。 + */ +const userName = "UserName@World"; beforeEach(async () => { firebaseClient = new FirebaseClient(); - posgresUserRepository = new PostgresUserRepository(); - authenticatedUserProvider = new AuthenticatedUserProvider(firebaseClient, posgresUserRepository); + postgresUserRepository = new PostgresUserRepository(postgresClientProvider); + authenticatedUserProvider = new AuthenticatedUserProvider(firebaseClient, postgresUserRepository); // テスト用のユーザーが存在する場合、削除する。 try { @@ -55,13 +61,13 @@ beforeEach(async () => { // テスト用のユーザー情報が存在する場合、削除する。 try { // テスト用のユーザー情報を取得する。 - const responseFindByProfileId = await delayAsync(() => posgresUserRepository.findByProfileId(profileId)); + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); // テスト用のユーザー情報が存在しない場合、エラーを投げる。 if (responseFindByProfileId == null) throw new Error("The user does not exist."); const id = responseFindByProfileId.id; - await delayAsync(() => posgresUserRepository.delete(id)); + await delayAsync(() => postgresUserRepository.delete(id)); console.info("テスト用のユーザー情報を削除しました。"); } catch (error) { console.info("テスト用のユーザー情報は存在しませんでした。"); @@ -81,8 +87,7 @@ describe("getUser", () => { // テスト用のユーザー情報を登録する。 const authenticationProviderId = responseSignUp.localId; - const userName = "test@Unicorn"; - await delayAsync(() => posgresUserRepository.create(profileId, authenticationProviderId, userName)); + await delayAsync(() => postgresUserRepository.create(profileId, authenticationProviderId, userName)); // テスト用のユーザー情報を取得する。 const idToken = responseSignUp.idToken; @@ -92,13 +97,10 @@ describe("getUser", () => { if (responseUser === null) throw new Error("The user does not exist."); // 結果を検証する。 - const expectedUser = { - userName: mailAddress, - }; expect(responseUser.id).toBeDefined(); expect(responseUser.profileId).toBe(profileId); - expect(responseUser.authenticationProviderId).toBeDefined(); - expect(responseUser.userName).toBe(expectedUser.userName); + expect(responseUser.authenticationProviderId).toBe(authenticationProviderId); + expect(responseUser.userName).toBe(userName); expect(new Date(responseUser.createdAt)).toBeInstanceOf(Date); }); @@ -113,4 +115,24 @@ describe("getUser", () => { // 結果を検証する。 expect(responseUser).toBeNull(); }); +}); + +describe("getAuthenticationProviderId", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("getAuthenticationProviderId should return an authentication provider ID.", async () => { + // テスト用のユーザーを登録する。 + const responseSignUp = await delayAsync(() => firebaseClient.signUp(mailAddress, password)); + + // ユーザー情報を取得する。 + const idToken = responseSignUp.idToken; + const responseAuthenticationProviderId = await delayAsync(() => authenticatedUserProvider.getAuthenticationProviderId(idToken)); + + // 結果を検証する。 + expect(responseAuthenticationProviderId).toBe(responseSignUp.localId); + }); }); \ No newline at end of file diff --git a/tests/infrastructure/libraries/user/user-registrar.spec.ts b/tests/infrastructure/libraries/user/user-registrar.spec.ts new file mode 100644 index 0000000..3d8d78c --- /dev/null +++ b/tests/infrastructure/libraries/user/user-registrar.spec.ts @@ -0,0 +1,135 @@ +import { describe, test, expect, beforeEach } from "@jest/globals"; +import delayAsync from "../../../test-utilityies/delay-async"; +import UserRegistrar from "../../../../app/libraries/user/user-registrar"; +import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; +import { postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; + +/** + * Postgresのユーザーリポジトリ。 + */ +let postgresUserRepository: PostgresUserRepository; + +/** + * ユーザーの登録を行うクラス。 + */ +let userRegistrar: UserRegistrar; + +/** + * プロフィールID。 + */ +const profileId = "username_world"; + +/** + * ユーザー名。 + */ +const userName = "UserName@World"; + +beforeEach(async () => { + postgresUserRepository = new PostgresUserRepository(postgresClientProvider); + userRegistrar = new UserRegistrar(postgresUserRepository); + + // テスト用のユーザー情報が存在する場合、削除する。 + try { + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + const id = responseFindByProfileId.id; + await delayAsync(() => postgresUserRepository.delete(id)); + console.info("テスト用のユーザー情報を削除しました。"); + } catch (error) { + console.info("テスト用のユーザー情報は存在しませんでした。"); + } +}); + +describe("register", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("register should register a new user", async () => { + // テスト用のユーザー情報を作成する。 + const response = await delayAsync(() => userRegistrar.register(profileId, userName)); + + // 結果を検証する。 + expect(response).toBe(true); + }); + + test("register should throw an error when the authenticationProviderId is empty", async () => { + expect.assertions(1); + try { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => userRegistrar.register("", userName)); + } catch (error) { + // エラーがResponseでない場合、エラーを投げる。 + if (!(error instanceof Error)) { + throw error; + } + + // エラーを検証する。 + expect(error.message).toBe("認証プロバイダIDは必須です。"); + } + }); + + test("register should throw an error when the userName is empty", async () => { + expect.assertions(1); + try { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => userRegistrar.register(profileId, "")); + } catch (error) { + // エラーがResponseでない場合、エラーを投げる。 + if (!(error instanceof Error)) { + throw error; + } + + // エラーを検証する。 + expect(error.message).toBe("ユーザー名は「username@world」で入力してください。"); + } + }); + + test("register should throw an error when the userName is invalid", async () => { + expect.assertions(1); + try { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => userRegistrar.register(profileId, "invalidUserName")); + } catch (error) { + // エラーがResponseでない場合、エラーを投げる。 + if (!(error instanceof Error)) { + throw error; + } + + // エラーを検証する。 + expect(error.message).toBe("ユーザー名は「username@world」で入力してください。"); + } + }); +}); + +describe("delete", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("delete should delete a user", async () => { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => userRegistrar.register(profileId, userName)); + + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + // テスト用のユーザー情報を削除する。 + const id = responseFindByProfileId.id; + const responseDelete = await delayAsync(() => userRegistrar.delete(id)); + + // 結果を検証する。 + expect(responseDelete).toBe(true); + }); +}); \ No newline at end of file diff --git a/tests/infrastructure/loaders/user/authenticated-user-loader.spec.ts b/tests/infrastructure/loaders/user/authenticated-user-loader.spec.ts index eeb74ac..16764b1 100644 --- a/tests/infrastructure/loaders/user/authenticated-user-loader.spec.ts +++ b/tests/infrastructure/loaders/user/authenticated-user-loader.spec.ts @@ -4,6 +4,7 @@ import FirebaseClient from "../../../../app/libraries/authentication/firebase-cl import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; import AuthenticatedUserProvider from "../../../../app/libraries/user/authenticated-user-provider"; import AuthenticatedUserLoader from "../../../../app/loaders/user/authenticated-user-loader"; +import { postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; /** * Firebaseのクライアント。 @@ -38,11 +39,16 @@ const password = "testPassword123"; /** * プロフィールID。 */ -const profileId = "test_unicorn"; +const profileId = "username_world"; + +/** + * ユーザー名。 + */ +const userName = "UserName@World"; beforeEach(async () => { firebaseClient = new FirebaseClient(); - posgresUserRepository = new PostgresUserRepository(); + posgresUserRepository = new PostgresUserRepository(postgresClientProvider); authenticatedUserProvider = new AuthenticatedUserProvider(firebaseClient, posgresUserRepository); authenticatedUserLoader = new AuthenticatedUserLoader(authenticatedUserProvider); @@ -88,7 +94,6 @@ describe("getUser", () => { // テスト用のユーザー情報を登録する。 const authenticationProviderId = responseSignUp.localId; - const userName = "test@Unicorn"; await delayAsync(() => posgresUserRepository.create(profileId, authenticationProviderId, userName)); // 認証済みユーザーを取得する。 @@ -101,8 +106,28 @@ describe("getUser", () => { // 結果を検証する。 expect(response.id).toBeDefined(); expect(response.profileId).toBe(profileId); - expect(response.authenticationProviderId).toBeDefined(); + expect(response.authenticationProviderId).toBe(authenticationProviderId); expect(response.userName).toBe(userName); expect(response.createdAt).toBeInstanceOf(Date); }); +}); + +describe("getAuthenticationProviderId", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("getAuthenticationProviderId should return an authentication provider ID.", async () => { + // テスト用のユーザーを登録する。 + const responseSignUp = await delayAsync(() => firebaseClient.signUp(mailAddress, password)); + + // 認証済みユーザーを取得する。 + const idToken = responseSignUp.idToken; + const response = await delayAsync(() => authenticatedUserLoader.getAuthenticationProviderId(idToken)); + + // 結果を検証する。 + expect(response).toBe(responseSignUp.localId); + }); }); \ No newline at end of file diff --git a/tests/infrastructure/repositories/user/postgres-user-repository.spec.ts b/tests/infrastructure/repositories/user/postgres-user-repository.spec.ts index 9af8579..e56c502 100644 --- a/tests/infrastructure/repositories/user/postgres-user-repository.spec.ts +++ b/tests/infrastructure/repositories/user/postgres-user-repository.spec.ts @@ -1,6 +1,7 @@ import { describe, test, expect, beforeEach } from "@jest/globals"; import delayAsync from "../../../test-utilityies/delay-async"; import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; +import { postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; /** * Postgresのユーザーリポジトリ。 @@ -10,7 +11,7 @@ let postgresUserRepository: PostgresUserRepository; /** * プロフィールID。 */ -const profileId = "test_unicorn"; +const profileId = "username_world"; /** * 認証プロバイダーID。 @@ -23,8 +24,7 @@ const authenticationProviderId = "test_authentication_provider_id"; const userName = "UserName@World"; beforeEach(async () => { - // Postgresのユーザーリポジトリを生成する。 - postgresUserRepository = new PostgresUserRepository(); + postgresUserRepository = new PostgresUserRepository(postgresClientProvider); // テスト用のユーザー情報が存在する場合、削除する。 try { @@ -124,4 +124,32 @@ describe("findByAuthenticationProviderId", () => { // 結果を検証する。 expect(response).toBeNull(); }); +}); + +describe("delete", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("delete should delete a user", async () => { + // テスト用のユーザー情報を作成する。 + await delayAsync(() => postgresUserRepository.create(profileId, authenticationProviderId, userName)); + + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + // テスト用のユーザー情報を削除する。 + const id = responseFindByProfileId.id; + const responseDelete = await delayAsync(() => postgresUserRepository.delete(id)); + const responseFindByProfileIdAfterDelete = await delayAsync(() => postgresUserRepository.findByProfileId(profileId)); + + // 結果を検証する。 + expect(responseDelete).toBe(true); + expect(responseFindByProfileIdAfterDelete).toBeNull(); + }); }); \ No newline at end of file diff --git a/tests/infrastructure/routes/app/route.spec.ts b/tests/infrastructure/routes/app/route.spec.ts index adf5d65..17d6daa 100644 --- a/tests/infrastructure/routes/app/route.spec.ts +++ b/tests/infrastructure/routes/app/route.spec.ts @@ -4,7 +4,7 @@ import FirebaseClient from "../../../../app/libraries/authentication/firebase-cl import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; import { AppLoadContext } from "@netlify/remix-runtime"; import { loader, action } from "../../../../app/routes/app/route"; -import { appLoadContext } from "../../../../app/dependency-injector/get-load-context"; +import { appLoadContext, postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; import { userAuthenticationCookie } from "../../../../app/cookies.server"; /** @@ -30,24 +30,21 @@ const password = "testPassword123"; /** * プロフィールID。 */ -const profileId = "test_unicorn"; +const profileId = "username_world"; /** - * コンテキスト。 + * ユーザー名。 */ -let context: AppLoadContext; +const userName = "UserName@World"; /** - * クッキーが不正なモックリクエスト。 + * コンテキスト。 */ -let requestWithInvalidCookie: Request; +let context: AppLoadContext; beforeEach(async () => { - // Firebaseのクライアントを生成する。 firebaseClient = new FirebaseClient(); - - // Postgresのユーザーリポジトリを生成する。 - posgresUserRepository = new PostgresUserRepository(); + posgresUserRepository = new PostgresUserRepository(postgresClientProvider); // テスト用のユーザーが存在する場合、削除する。 try { @@ -79,16 +76,6 @@ beforeEach(async () => { // コンテキストを設定する。 context = appLoadContext; - - // クッキーが不正なモックリクエストを作成する。 - requestWithInvalidCookie = new Request("https://example.com", { - headers: { - Cookie: await userAuthenticationCookie.serialize({ - idToken: "invalidIdToken", - refreshToken: "invalidRefreshToken", - }), - }, - }); }); describe("loader", () => { @@ -101,6 +88,12 @@ describe("loader", () => { test("loader should return SNS user.", async () => { // テスト用のユーザーを作成する。 const responseSignUp = await delayAsync(() => firebaseClient.signUp(mailAddress, password)); + + // テスト用のユーザー情報を登録する。 + const authenticationProviderId = responseSignUp.localId; + await delayAsync(() => posgresUserRepository.create(profileId, authenticationProviderId, userName)); + + // ローダーを実行し、結果を取得する。 const requestWithCookie = new Request("https://example.com", { headers: { Cookie: await userAuthenticationCookie.serialize({ @@ -109,13 +102,6 @@ describe("loader", () => { }), }, }); - - // テスト用のユーザー情報を登録する。 - const authenticationProviderId = responseSignUp.localId; - const userName = "test@Unicorn"; - await delayAsync(() => posgresUserRepository.create(profileId, authenticationProviderId, userName)); - - // ローダーを実行し、結果を取得する。 const response = await loader({ request: requestWithCookie, params: {}, @@ -127,41 +113,16 @@ describe("loader", () => { // 結果を検証する。 const expectedUser = { - userName: mailAddress, + userName: userName, }; expect(resultUser.userName).toBe(expectedUser.userName); }); - test("loader should redirect to login page if an error occurs.", async () => { - expect.assertions(3); - try { - // ローダーを実行し、エラーを発生させる。 - await loader({ - request: requestWithInvalidCookie, - params: {}, - context, - }); - } catch (error) { - // エラーがResponseでない場合、エラーを投げる。 - if (!(error instanceof Response)) { - throw error; - } - - // 検証に必要な情報を取得する。 - const status = error.status; - const redirect = error.headers.get("Location"); - const cookie = await userAuthenticationCookie.parse(error.headers.get("Set-Cookie")); - - // 結果を検証する。 - expect(status).toBe(302); - expect(redirect).toBe("/auth/login"); - expect(cookie).toStrictEqual({}); - } - }); - test("loader should redirect register user page if user is not loged in.", async () => { // テスト用のユーザーを作成する。 const responseSignUp = await delayAsync(() => firebaseClient.signUp(mailAddress, password)); + + // ローダーを実行し、結果を取得する。 const requestWithCookie = new Request("https://example.com", { headers: { Cookie: await userAuthenticationCookie.serialize({ @@ -170,8 +131,6 @@ describe("loader", () => { }), }, }); - - // ローダーを実行し、結果を取得する。 const response = await loader({ request: requestWithCookie, params: {}, diff --git a/tests/infrastructure/routes/auth.register-user/route.spec.ts b/tests/infrastructure/routes/auth.register-user/route.spec.ts new file mode 100644 index 0000000..147b389 --- /dev/null +++ b/tests/infrastructure/routes/auth.register-user/route.spec.ts @@ -0,0 +1,119 @@ +import { describe, test, expect, beforeEach } from "@jest/globals"; +import delayAsync from "../../../test-utilityies/delay-async"; +import FirebaseClient from "../../../../app/libraries/authentication/firebase-client"; +import PostgresUserRepository from "../../../../app/repositories/user/postgres-user-repository"; +import { AppLoadContext } from "@netlify/remix-runtime"; +import { action } from "../../../../app/routes/auth.register-user/route"; +import { appLoadContext, postgresClientProvider } from "../../../../app/dependency-injector/get-load-context"; +import { userAuthenticationCookie } from "../../../../app/cookies.server"; + +/** + * Firebaseのクライアント。 + */ +let firebaseClient: FirebaseClient; + +/** + * Postgresのユーザーリポジトリ。 + */ +let posgresUserRepository: PostgresUserRepository; + +/** + * テスト用のメールアドレス。 + */ +const mailAddress = "test@example.com"; + +/** + * テスト用のパスワード。 + */ +const password = "testPassword123"; + +/** + * プロフィールID。 + */ +const profileId = "username_world"; + +/** + * ユーザー名。 + */ +const userName = "UserName@World"; + +/** + * コンテキスト。 + */ +let context: AppLoadContext; + +beforeEach(async () => { + firebaseClient = new FirebaseClient(); + posgresUserRepository = new PostgresUserRepository(postgresClientProvider); + + // テスト用のユーザーが存在する場合、削除する。 + try { + // テスト用のユーザーをログインする。 + const responseSignIn = await delayAsync(() => firebaseClient.signInWithEmailPassword(mailAddress, password)); + + // テスト用のユーザーを削除する。 + const idToken = responseSignIn.idToken; + await delayAsync(() => firebaseClient.deleteUser(idToken)); + console.info("テスト用のユーザーを削除しました。"); + } catch (error) { + console.info("テスト用のユーザーは存在しませんでした。"); + } + + // テスト用のユーザー情報が存在する場合、削除する。 + try { + // テスト用のユーザー情報を取得する。 + const responseFindByProfileId = await delayAsync(() => posgresUserRepository.findByProfileId(profileId)); + + // テスト用のユーザー情報が存在しない場合、エラーを投げる。 + if (responseFindByProfileId == null) throw new Error("The user does not exist."); + + const id = responseFindByProfileId.id; + await delayAsync(() => posgresUserRepository.delete(id)); + console.info("テスト用のユーザー情報を削除しました。"); + } catch (error) { + console.info("テスト用のユーザー情報は存在しませんでした。"); + } + + // コンテキストを設定する。 + context = appLoadContext; +}); + +describe("action", () => { + // 環境変数が設定されていない場合、テストをスキップする。 + if (!process.env.RUN_INFRA_TESTS) { + test.skip("Skipping infrastructure tests.", () => {}); + return; + } + + test("action should redirect app page.", async () => { + // テスト用のユーザーを作成する。 + const responseSignUp = await delayAsync(() => firebaseClient.signUp(mailAddress, password)); + + // アクションを実行し、結果を取得する。 + const requestWithCookie = new Request("https://example.com", { + headers: { + Cookie: await userAuthenticationCookie.serialize({ + idToken: responseSignUp.idToken, + refreshToken: responseSignUp.refreshToken, + }), + }, + method: "POST", + body: new URLSearchParams({ + userName: userName, + }), + }); + const response = await action({ + request: requestWithCookie, + params: {}, + context, + }); + + // 検証に必要な情報を取得する。 + const status = response.status; + const location = response.headers.get("Location"); + + // 結果を検証する。 + expect(status).toBe(302); + expect(location).toBe("/app"); + }); +}); \ No newline at end of file diff --git a/tests/libraries/user/user-registrar.spec.ts b/tests/libraries/user/user-registrar.spec.ts index 05591be..ec7de42 100644 --- a/tests/libraries/user/user-registrar.spec.ts +++ b/tests/libraries/user/user-registrar.spec.ts @@ -10,7 +10,7 @@ let userAccountManager: UserRegistrar; /** * 認証プロバイダID。 */ -const authenticationProvidedId = "authenticationProvidedId"; +const authenticationProviderId = "authenticationProviderId"; /** * ユーザー名。 @@ -30,7 +30,7 @@ beforeEach(() => { describe("register", () => { test("register should register a user and return true.", async () => { // ユーザーを登録する。 - const response = await userAccountManager.register(authenticationProvidedId, userName); + const response = await userAccountManager.register(authenticationProviderId, userName); // 結果を検証する。 expect(response).toBe(true); @@ -40,8 +40,8 @@ describe("register", () => { expect.assertions(1); try { // ユーザーを登録する。 - const invalidAuthenticationProvidedId = ""; - await userAccountManager.register(invalidAuthenticationProvidedId, userName) + const invalidAuthenticationProviderId = ""; + await userAccountManager.register(invalidAuthenticationProviderId, userName) } catch (error) { // エラーがErrorでない場合、エラーを投げる。 if (!(error instanceof Error)) { @@ -58,7 +58,7 @@ describe("register", () => { try { // ユーザーを登録する。 const invalidUserName = "invalidUserName"; - await userAccountManager.register(authenticationProvidedId, invalidUserName) + await userAccountManager.register(authenticationProviderId, invalidUserName) } catch (error) { // エラーがErrorでない場合、エラーを投げる。 if (!(error instanceof Error)) {