From 6169b99d9592f94c7cd6a0ed190125a159b6b6e0 Mon Sep 17 00:00:00 2001 From: hunterckx <118154470+hunterckx@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:57:17 -0800 Subject: [PATCH] test: add tests for `useSessionAuth` and `getProfileStatus` (#178) --- tests/getProfileStatus.test.ts | 290 +++++++++++++++++++++++++++++++++ tests/useSessionAuth.test.ts | 91 +++++++++++ 2 files changed, 381 insertions(+) create mode 100644 tests/getProfileStatus.test.ts create mode 100644 tests/useSessionAuth.test.ts diff --git a/tests/getProfileStatus.test.ts b/tests/getProfileStatus.test.ts new file mode 100644 index 00000000..3f08a651 --- /dev/null +++ b/tests/getProfileStatus.test.ts @@ -0,0 +1,290 @@ +import { + LoginStatus, + REQUEST_STATUS, +} from "../src/providers/authentication/terra/hooks/common/entities"; +import { TerraNIHResponse } from "../src/providers/authentication/terra/hooks/useFetchTerraNIHProfile"; +import { TerraResponse } from "../src/providers/authentication/terra/hooks/useFetchTerraProfile"; +import { TerraTermsOfServiceResponse } from "../src/providers/authentication/terra/hooks/useFetchTerraTermsOfService"; +import { TERRA_PROFILE_STATUS } from "../src/providers/authentication/terra/types"; +import { getProfileStatus } from "../src/providers/authentication/terra/utils"; + +const LOGIN_STATUS_NIH_COMPLETED: LoginStatus = { + isSuccess: true, + isSupported: true, + requestStatus: REQUEST_STATUS.COMPLETED, + response: undefined, +}; + +const LOGIN_STATUS_TERRA_COMPLETED: LoginStatus = { + isSuccess: true, + isSupported: true, + requestStatus: REQUEST_STATUS.COMPLETED, + response: undefined, +}; + +const LOGIN_STATUS_TOS_COMPLETED: LoginStatus = { + isSuccess: true, + isSupported: true, + requestStatus: REQUEST_STATUS.COMPLETED, + response: undefined, +}; + +const LOGIN_STATUS_TOS_COMPLETED_UNSUCCESSFUL: LoginStatus = + { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.COMPLETED, + response: undefined, + }; + +const LOGIN_STATUS_NIH_NOT_STARTED: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +const LOGIN_STATUS_TERRA_NOT_STARTED: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +const LOGIN_STATUS_TOS_NOT_STARTED: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +const LOGIN_STATUS_NIH_PENDING: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.PENDING, + response: undefined, +}; + +const LOGIN_STATUS_TERRA_PENDING: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.PENDING, + response: undefined, +}; + +const LOGIN_STATUS_TOS_PENDING: LoginStatus = { + isSuccess: false, + isSupported: true, + requestStatus: REQUEST_STATUS.PENDING, + response: undefined, +}; + +const LOGIN_STATUS_NIH_UNSUPPORTED: LoginStatus = { + isSuccess: false, + isSupported: false, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +const LOGIN_STATUS_TERRA_UNSUPPORTED: LoginStatus = { + isSuccess: false, + isSupported: false, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +const LOGIN_STATUS_TOS_UNSUPPORTED: LoginStatus = { + isSuccess: false, + isSupported: false, + requestStatus: REQUEST_STATUS.NOT_STARTED, + response: undefined, +}; + +describe("getProfileStatus", () => { + test("not authenticated, services not started", () => { + expect( + getProfileStatus( + false, + LOGIN_STATUS_NIH_NOT_STARTED, + LOGIN_STATUS_TERRA_NOT_STARTED, + LOGIN_STATUS_TOS_NOT_STARTED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("not authenticated, services unsupported", () => { + expect( + getProfileStatus( + false, + LOGIN_STATUS_NIH_UNSUPPORTED, + LOGIN_STATUS_TERRA_UNSUPPORTED, + LOGIN_STATUS_TOS_UNSUPPORTED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("not authenticated, services completed", () => { + expect( + getProfileStatus( + false, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, services not started", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_NOT_STARTED, + LOGIN_STATUS_TERRA_NOT_STARTED, + LOGIN_STATUS_TOS_NOT_STARTED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, services pending", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_PENDING, + LOGIN_STATUS_TERRA_PENDING, + LOGIN_STATUS_TOS_PENDING + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, nih pending, other services completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_PENDING, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, terra pending, other services completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_PENDING, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, tos pending, other services completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_PENDING + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, nih completed, terra pending, tos not started", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_PENDING, + LOGIN_STATUS_TOS_NOT_STARTED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, nih not started, terra completed, tos pending", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_NOT_STARTED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_PENDING + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, nih pending, terra not started, tos completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_PENDING, + LOGIN_STATUS_TERRA_NOT_STARTED, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.PENDING); + }); + + test("authenticated, services unsupported", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_UNSUPPORTED, + LOGIN_STATUS_TERRA_UNSUPPORTED, + LOGIN_STATUS_TOS_UNSUPPORTED + ) + ).toBe(TERRA_PROFILE_STATUS.AUTHENTICATED); + }); + + test("authenticated, services completed, tos unsuccessful", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED_UNSUCCESSFUL + ) + ).toBe(TERRA_PROFILE_STATUS.UNAUTHENTICATED); + }); + + test("authenticated, nih unsupported, terra completed, tos completed unsuccessfully", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_UNSUPPORTED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED_UNSUCCESSFUL + ) + ).toBe(TERRA_PROFILE_STATUS.UNAUTHENTICATED); + }); + + test("authenticated, nih unsupported, other services completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_UNSUPPORTED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.AUTHENTICATED); + }); + + test("authenticated, terra completed, other services unsupported", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_UNSUPPORTED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_UNSUPPORTED + ) + ).toBe(TERRA_PROFILE_STATUS.UNAUTHENTICATED); + }); + + test("authenticated, services completed", () => { + expect( + getProfileStatus( + true, + LOGIN_STATUS_NIH_COMPLETED, + LOGIN_STATUS_TERRA_COMPLETED, + LOGIN_STATUS_TOS_COMPLETED + ) + ).toBe(TERRA_PROFILE_STATUS.AUTHENTICATED); + }); +}); diff --git a/tests/useSessionAuth.test.ts b/tests/useSessionAuth.test.ts new file mode 100644 index 00000000..bae48ca9 --- /dev/null +++ b/tests/useSessionAuth.test.ts @@ -0,0 +1,91 @@ +import { renderHook } from "@testing-library/react"; +import { useAuthReducer } from "../src/hooks/authentication/auth/useAuthReducer"; +import { useAuthenticationReducer } from "../src/hooks/authentication/authentication/useAuthenticationReducer"; +import { useSessionAuth } from "../src/hooks/authentication/session/useSessionAuth"; +import { + AUTH_STATUS, + AuthState, +} from "../src/providers/authentication/auth/types"; +import { + AUTHENTICATION_STATUS, + AuthenticationState, +} from "../src/providers/authentication/authentication/types"; + +describe("useSessionAuth", () => { + test("auth status SETTLED with no profile", async () => { + testAuthenticationState( + { + profile: undefined, + status: AUTHENTICATION_STATUS.SETTLED, + }, + { + isAuthenticated: false, + status: AUTH_STATUS.SETTLED, + } + ); + }); + + test("auth status PENDING with no profile", async () => { + testAuthenticationState( + { + profile: undefined, + status: AUTHENTICATION_STATUS.PENDING, + }, + { + isAuthenticated: false, + status: AUTH_STATUS.PENDING, + } + ); + }); + + test("auth status PENDING with profile", async () => { + testAuthenticationState( + { + profile: { + email: "test@example.com", + name: "Test", + }, + status: AUTHENTICATION_STATUS.PENDING, + }, + { + isAuthenticated: false, + status: AUTH_STATUS.PENDING, + } + ); + }); + + test("auth status SETTLED with profile", async () => { + testAuthenticationState( + { + profile: { + email: "test@example.com", + name: "Test", + }, + status: AUTHENTICATION_STATUS.SETTLED, + }, + { + isAuthenticated: true, + status: AUTH_STATUS.SETTLED, + } + ); + }); +}); + +function testAuthenticationState( + authenticationState: AuthenticationState, + expectedAuthState: AuthState +): void { + const { result: authenticationReducerResult } = renderHook(() => + useAuthenticationReducer(authenticationState) + ); + const { result: authReducerResult } = renderHook(() => useAuthReducer()); + + renderHook(() => + useSessionAuth({ + authReducer: authReducerResult.current, + authenticationReducer: authenticationReducerResult.current, + }) + ); + + expect(authReducerResult.current.authState).toMatchObject(expectedAuthState); +}