From 6699927c6f00ff0f4d9bcb8af0b2907a6d52acc3 Mon Sep 17 00:00:00 2001 From: John Kim Date: Thu, 22 Aug 2024 13:13:42 -0400 Subject: [PATCH] test: successful login with weak or strong password --- .../src/components/DeterminedAuth.test.tsx | 85 +++++++++++++++++++ webui/react/src/components/DeterminedAuth.tsx | 19 +++-- 2 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 webui/react/src/components/DeterminedAuth.test.tsx diff --git a/webui/react/src/components/DeterminedAuth.test.tsx b/webui/react/src/components/DeterminedAuth.test.tsx new file mode 100644 index 000000000000..2f20d65f27ed --- /dev/null +++ b/webui/react/src/components/DeterminedAuth.test.tsx @@ -0,0 +1,85 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import UIProvider, { DefaultTheme } from 'hew/Theme'; +import { useInitApi } from 'hew/Toast'; +import { useState } from 'react'; + +import { login } from 'services/api'; + +import DeterminedAuth, { + PASSWORD_ID, + SUBMIT_ID, + USERNAME_ID, + WEAK_PASSWORD_SUBJECT, +} from './DeterminedAuth'; +import { ThemeProvider } from './ThemeProvider'; + +const USERNAME = 'username'; +const WEAK_PASSWORD = 'password'; +const STRONG_PASSWORD = 'dO2Ccf5ceozXJFuMhjEx32UgtR4X4yZTm1Vv'; + +vi.mock('services/api', () => ({ + login: vi.fn().mockResolvedValue({ token: '', user: {} }), +})); + +const Container = (): JSX.Element => { + const [canceler] = useState(new AbortController()); + useInitApi(); + return ; +}; + +const setup = () => { + render( + + + + + , + ); +}; + +const user = userEvent.setup(); +describe('DeterminedAuth', () => { + it('logs in with strong password', async () => { + setup(); + + await user.type(screen.getByTestId(USERNAME_ID), USERNAME); + await user.type(screen.getByTestId(PASSWORD_ID), STRONG_PASSWORD); + user.click(screen.getByTestId(SUBMIT_ID)); + + await waitFor(() => { + // onFinish begins: + expect(screen.getByRole('button')).toBeDisabled(); + }); + await waitFor(() => { + // onFinish ends: + expect(screen.getByRole('button')).not.toBeDisabled(); + + expect(screen.queryByText(WEAK_PASSWORD_SUBJECT)).not.toBeInTheDocument(); + expect(vi.mocked(login)).toHaveBeenLastCalledWith( + { password: STRONG_PASSWORD, username: USERNAME }, + { + signal: expect.any(AbortSignal), + }, + ); + }); + }); + + it('logs in with weak password', async () => { + setup(); + await user.clear(screen.getByTestId(USERNAME_ID)); + await user.type(screen.getByTestId(USERNAME_ID), USERNAME); + await user.type(screen.getByTestId(PASSWORD_ID), WEAK_PASSWORD); + user.click(screen.getByTestId(SUBMIT_ID)); + + await waitFor(() => { + expect(screen.getByText(WEAK_PASSWORD_SUBJECT)).toBeInTheDocument(); + expect(vi.mocked(login)).toHaveBeenLastCalledWith( + { password: WEAK_PASSWORD, username: USERNAME }, + { + signal: expect.any(AbortSignal), + }, + ); + }); + }); +}); diff --git a/webui/react/src/components/DeterminedAuth.tsx b/webui/react/src/components/DeterminedAuth.tsx index 1ee365bff377..9c465c2c4982 100644 --- a/webui/react/src/components/DeterminedAuth.tsx +++ b/webui/react/src/components/DeterminedAuth.tsx @@ -25,11 +25,16 @@ interface Props { canceler: AbortController; } -interface FromValues { +interface FormValues { password?: string; username?: string; } +export const WEAK_PASSWORD_SUBJECT = 'Weak Password'; +export const USERNAME_ID = 'username_ID'; +export const PASSWORD_ID = 'password'; +export const SUBMIT_ID = 'submit'; + const storage = new StorageManager({ basePath: '/DeterminedAuth', store: window.localStorage }); const STORAGE_KEY_LAST_USERNAME = 'lastUsername'; @@ -41,7 +46,7 @@ const DeterminedAuth: React.FC = ({ canceler }: Props) => { const [isSubmitted, setIsSubmitted] = useState(false); const onFinish = useCallback( - async (creds: FromValues): Promise => { + async (creds: FormValues): Promise => { uiActions.showSpinner(); setCanSubmit(false); setIsSubmitted(true); @@ -62,7 +67,7 @@ const DeterminedAuth: React.FC = ({ canceler }: Props) => { level: ErrorLevel.Warn, publicMessage: 'Your current password is either blank or weak according to current security recommendations. Please change your password.', - publicSubject: 'Weak Password', + publicSubject: WEAK_PASSWORD_SUBJECT, silent: false, type: ErrorType.Input, }); @@ -91,7 +96,7 @@ const DeterminedAuth: React.FC = ({ canceler }: Props) => { [canceler, uiActions, rbacEnabled], ); - const onValuesChange = useCallback((_changes: FromValues, values: FromValues): void => { + const onValuesChange = useCallback((_changes: FormValues, values: FormValues): void => { const hasUsername = !!values.username; setIsBadCredentials(false); setCanSubmit(hasUsername); @@ -113,14 +118,14 @@ const DeterminedAuth: React.FC = ({ canceler }: Props) => { ]}> } /> } /> @@ -130,7 +135,7 @@ const DeterminedAuth: React.FC = ({ canceler }: Props) => { )}