Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: successful login with weak or strong password #9858

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions webui/react/src/components/DeterminedAuth.test.tsx
Original file line number Diff line number Diff line change
@@ -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 <DeterminedAuth canceler={canceler} />;
};

const setup = () => {
render(
<UIProvider theme={DefaultTheme.Light}>
<ThemeProvider>
<Container />
</ThemeProvider>
</UIProvider>,
);
};

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();
Comment on lines +50 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this part isn't in the weak password test, it should probably be in both or neither

keita-determined marked this conversation as resolved.
Show resolved Hide resolved

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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: more descriptive description can make it easier/quicker to diagnose CI failures.

Suggested change
it('logs in with weak password', async () => {
it('logs in with warning when using a 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),
},
);
});
});
});
19 changes: 12 additions & 7 deletions webui/react/src/components/DeterminedAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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';

Expand All @@ -41,7 +46,7 @@ const DeterminedAuth: React.FC<Props> = ({ canceler }: Props) => {
const [isSubmitted, setIsSubmitted] = useState<boolean>(false);

const onFinish = useCallback(
async (creds: FromValues): Promise<void> => {
async (creds: FormValues): Promise<void> => {
uiActions.showSpinner();
setCanSubmit(false);
setIsSubmitted(true);
Expand All @@ -62,7 +67,7 @@ const DeterminedAuth: React.FC<Props> = ({ 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,
});
Expand Down Expand Up @@ -91,7 +96,7 @@ const DeterminedAuth: React.FC<Props> = ({ 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);
Expand All @@ -113,14 +118,14 @@ const DeterminedAuth: React.FC<Props> = ({ canceler }: Props) => {
]}>
<Input
autoFocus
data-testid="username"
data-testid={USERNAME_ID}
placeholder="username"
prefix={<Icon name="user" size="small" title="Username" />}
/>
</Form.Item>
<Form.Item name="password">
<Input.Password
data-testid="password"
data-testid={PASSWORD_ID}
placeholder="password"
prefix={<Icon name="lock" size="small" title="Password" />}
/>
Expand All @@ -130,7 +135,7 @@ const DeterminedAuth: React.FC<Props> = ({ canceler }: Props) => {
)}
<Form.Item>
<Button
data-testid="submit"
data-testid={SUBMIT_ID}
disabled={!canSubmit}
htmlType="submit"
loading={isSubmitted}
Expand Down
Loading