Skip to content

Commit

Permalink
test: add tests for useSessionAuth and getProfileStatus (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterckx committed Nov 5, 2024
1 parent d6c92e6 commit 6169b99
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 0 deletions.
290 changes: 290 additions & 0 deletions tests/getProfileStatus.test.ts
Original file line number Diff line number Diff line change
@@ -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<TerraNIHResponse> = {
isSuccess: true,
isSupported: true,
requestStatus: REQUEST_STATUS.COMPLETED,
response: undefined,
};

const LOGIN_STATUS_TERRA_COMPLETED: LoginStatus<TerraResponse> = {
isSuccess: true,
isSupported: true,
requestStatus: REQUEST_STATUS.COMPLETED,
response: undefined,
};

const LOGIN_STATUS_TOS_COMPLETED: LoginStatus<TerraTermsOfServiceResponse> = {
isSuccess: true,
isSupported: true,
requestStatus: REQUEST_STATUS.COMPLETED,
response: undefined,
};

const LOGIN_STATUS_TOS_COMPLETED_UNSUCCESSFUL: LoginStatus<TerraTermsOfServiceResponse> =
{
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.COMPLETED,
response: undefined,
};

const LOGIN_STATUS_NIH_NOT_STARTED: LoginStatus<TerraNIHResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.NOT_STARTED,
response: undefined,
};

const LOGIN_STATUS_TERRA_NOT_STARTED: LoginStatus<TerraResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.NOT_STARTED,
response: undefined,
};

const LOGIN_STATUS_TOS_NOT_STARTED: LoginStatus<TerraTermsOfServiceResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.NOT_STARTED,
response: undefined,
};

const LOGIN_STATUS_NIH_PENDING: LoginStatus<TerraNIHResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.PENDING,
response: undefined,
};

const LOGIN_STATUS_TERRA_PENDING: LoginStatus<TerraResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.PENDING,
response: undefined,
};

const LOGIN_STATUS_TOS_PENDING: LoginStatus<TerraTermsOfServiceResponse> = {
isSuccess: false,
isSupported: true,
requestStatus: REQUEST_STATUS.PENDING,
response: undefined,
};

const LOGIN_STATUS_NIH_UNSUPPORTED: LoginStatus<TerraNIHResponse> = {
isSuccess: false,
isSupported: false,
requestStatus: REQUEST_STATUS.NOT_STARTED,
response: undefined,
};

const LOGIN_STATUS_TERRA_UNSUPPORTED: LoginStatus<TerraResponse> = {
isSuccess: false,
isSupported: false,
requestStatus: REQUEST_STATUS.NOT_STARTED,
response: undefined,
};

const LOGIN_STATUS_TOS_UNSUPPORTED: LoginStatus<TerraTermsOfServiceResponse> = {
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);
});
});
91 changes: 91 additions & 0 deletions tests/useSessionAuth.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
name: "Test",
},
status: AUTHENTICATION_STATUS.PENDING,
},
{
isAuthenticated: false,
status: AUTH_STATUS.PENDING,
}
);
});

test("auth status SETTLED with profile", async () => {
testAuthenticationState(
{
profile: {
email: "[email protected]",
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);
}

0 comments on commit 6169b99

Please sign in to comment.