Skip to content

Commit

Permalink
Fix sso issues and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouder committed Jul 3, 2024
1 parent c46dc01 commit 3e3b12c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 112 deletions.
137 changes: 45 additions & 92 deletions src/hooks/use-auth-sso.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,136 +7,89 @@ interface ContextWrapperProps {
}

vi.mock('react-oidc-context', () => ({
useAuth: vi.fn().mockImplementation(() => ({
signinRedirect: vi.fn().mockResolvedValue(true),
signoutRedirect: vi.fn().mockResolvedValue(true),
useAuth: () => ({
isAuthenticated: true,
isLoading: false,
user: {
profile: undefined,
},
})),
}));

vi.mock('@src/utils/auth', () => ({
getSignInRedirectUrl: vi.fn(() => 'mocked-redirect-url'), // Replace with the expected URL
signinRedirect: vi.fn(),
signoutRedirect: vi.fn(),
}),
}));

describe('useAuth', () => {
const OLD_ENV = process.env;
beforeEach(() => {
process.env = { ...OLD_ENV };
afterEach(() => {
vi.clearAllMocks();
});

const contextWrapper = ({ children }: ContextWrapperProps) => (
<RecoilRoot>{children}</RecoilRoot>
);

test('should call signIn with SSO and no configs', async () => {
it('should set isSignedIn to true when authenticated with sso', async () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

await act(async () => {
result.current.signIn(true);
});
expect(result.current.signIn).toBeTruthy();
});

test('should call signIn with SSO and available configs', async () => {
process.env.SSO_AUTHORITY = 'http://localhost';
process.env.SSO_CLIENT_ID = 'dev-client';
expect(result.current.isSignedIn).toBe(true);
});

it('should set isSignedIn to true when authenticated without sso', () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

await act(async () => {
result.current.signIn(true);
act(() => {
result.current.signIn(false);
});
expect(result.current.signIn).toBeTruthy();

expect(result.current.isSignedIn).toBe(true);
});

it('should set isSignedIn to true when authenticated with sso', () => {
it('should sign out and set isSignedIn to false when authenticated', () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

act(() => {
result.current.signOut();
});

expect(result.current.isSignedIn).toBe(false);
});

it('should set isSignedIn to true when authenticated and with profile', async () => {
vi.mock('react-oidc-context', () => ({
useAuth: () => ({
isAuthenticated: true,
isLoading: false,
user: {
profile: {
firstName: 'John',
lastName: 'Doe',
displayName: 'John Doe',
emailAddress: '[email protected]',
phoneNumber: '1234567890',
},
},
signinRedirect: vi.fn(),
signoutRedirect: vi.fn(),
}),
}));

const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

await act(async () => {
result.current.signIn(true);
});

expect(result.current.isSignedIn).toBe(true);
});

// it('should not authenticated with sso and error', () => {
// vi.mock('react-oidc-context', () => ({
// useAuth: vi.fn().mockImplementation(() => ({
// signinRedirect: vi.fn().mockRejectedValue(true),
// signoutRedirect: vi.fn(),
// isAuthenticated: false,
// })),
// }));
// const { result } = renderHook(() => useAuth(), {
// wrapper: contextWrapper,
// });

// act(() => {
// result.current.signIn(true);
// });

// expect(result.current.isSignedIn).toBe(true);
// });

// it('should set isSignedIn to true when authenticated without sso', () => {
// const { result } = renderHook(() => useAuth(), {
// wrapper: contextWrapper,
// });

// act(() => {
// result.current.signIn(false);
// });

// expect(result.current.isSignedIn).toBe(true);
// });

// it('should sign out and set isSignedIn to false when authenticated', () => {
// const { result } = renderHook(() => useAuth(), {
// wrapper: contextWrapper,
// });

// act(() => {
// result.current.signOut();
// });

// expect(result.current.isSignedIn).toBe(false);
// });

// it('should set isSignedIn to true when authenticated and with profile', () => {
// vi.mock('react-oidc-context', () => ({
// useAuth: vi.fn().mockImplementation(() => ({
// signinRedirect: vi.fn().mockResolvedValue(true),
// signoutRedirect: vi.fn().mockResolvedValue(true),
// isAuthenticated: true,
// user: {
// profile: {
// firstName: 'John',
// lastName: 'Doe',
// displayName: 'John Doe',
// emailAddress: '[email protected]',
// phoneNumber: '1234567890',
// },
// },
// })),
// }));
// const { result } = renderHook(() => useAuth(), {
// wrapper: contextWrapper,
// });

// act(() => {
// result.current.signIn(false);
// });

// expect(result.current.isSignedIn).toBe(true);
// });
});
39 changes: 19 additions & 20 deletions src/hooks/use-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const useAuth = () => {
const [isSignedIn, setIsSignedIn] = useRecoilState<boolean>(signedIn);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>();
const [currentUserData, setCurrentUserDate] = useRecoilState<
const [currentUserData, setCurrentUserData] = useRecoilState<
User | undefined
>(currentUser);

Expand All @@ -32,48 +32,47 @@ const useAuth = () => {

useEffect(() => {
setIsLoading(auth.isLoading);
}, [auth.isLoading, setIsSignedIn]);
}, [auth.isLoading, setIsLoading]);

useEffect(() => {
const profile = auth.user?.profile;
if (profile) {
setCurrentUserDate({
if (profile && !currentUserData) {
setCurrentUserData({
firstName: profile.given_name,
lastName: profile.family_name,
displayName: profile.name,
emailAddress: profile.email,
phoneNumber: profile.phone_number,
});
}
}, [auth.user?.profile, setCurrentUserDate]);
}, [auth.user?.profile, currentUserData, setCurrentUserData]);

useEffect(() => {
if (auth.error) {
setError(auth.error.message);
setIsSignedIn(false);
}
}, [auth.error, setIsSignedIn]);

const signIn = (isSso: boolean): void => {
if (isSso) {
auth
.signinRedirect({ redirect_uri: getSignInRedirectUrl() })
.catch((err) => {
setError(err);
});
auth.signinRedirect({ redirect_uri: getSignInRedirectUrl() });
} else {
setIsSignedIn(true);
setCurrentUserDate(userData);
setCurrentUserData(userData);
}
};

const signOut = (): void => {
setIsSignedIn(false);
setCurrentUserDate({} as User);
setCurrentUserData({} as User);
if (auth.isAuthenticated) {
auth
.signoutRedirect({
post_logout_redirect_uri: getSignInRedirectUrl(),
})
.catch((err) => {
setError(err);
});
auth.signoutRedirect({
post_logout_redirect_uri: getSignInRedirectUrl(),
});
} else {
setIsSignedIn(false);
setCurrentUserDate({} as User);
setCurrentUserData({} as User);
}
};

Expand Down

0 comments on commit 3e3b12c

Please sign in to comment.