Login: Adjust error message when user exceed login attempts (#70736)

* Adjust error message when user exceed login attempts

* empty commit to add coauthors

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
Co-authored-by: L-M-K-B <48948963+L-M-K-B@users.noreply.github.com>
Co-authored-by: tskarhed <1438972+tskarhed@users.noreply.github.com>

---------

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
Co-authored-by: L-M-K-B <48948963+L-M-K-B@users.noreply.github.com>
Co-authored-by: tskarhed <1438972+tskarhed@users.noreply.github.com>
This commit is contained in:
RoxanaAnamariaTurc 2023-06-30 11:22:44 +01:00 committed by GitHub
parent 13dd821961
commit ea361f5db4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 1 deletions

View File

@ -169,8 +169,12 @@ function getErrorMessage(err: FetchError<undefined | { messageId?: string; messa
case 'password-auth.empty':
case 'password-auth.failed':
case 'password-auth.invalid':
case 'login-attempt.blocked':
return t('login.error.invalid-user-or-password', 'Invalid username or password');
case 'login-attempt.blocked':
return t(
'login.error.blocked',
'You have exceeded the number of login attempts for this user. Please try again later.'
);
default:
return err.data?.message;
}

View File

@ -8,6 +8,7 @@ import { LoginPage } from './LoginPage';
const postMock = jest.fn();
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
__esModule: true,
getBackendSrv: () => ({
post: postMock,
@ -103,4 +104,48 @@ describe('Login Page', () => {
expect(screen.getByRole('link', { name: 'Sign in with Okta Test' })).toBeInTheDocument();
});
it('shows an error with incorrect password', async () => {
postMock.mockRejectedValueOnce({
data: {
message: 'Invalid username or password',
messageId: 'password-auth.failed',
statusCode: 400,
},
status: 400,
statusText: 'Bad Request',
});
render(<LoginPage />);
await userEvent.type(screen.getByLabelText('Username input field'), 'admin');
await userEvent.type(screen.getByLabelText('Password input field'), 'test');
await userEvent.click(screen.getByRole('button', { name: 'Login button' }));
expect(await screen.findByRole('alert', { name: 'Invalid username or password' })).toBeInTheDocument();
});
it('shows a different error with failed login attempts', async () => {
postMock.mockRejectedValueOnce({
data: {
message: 'Invalid username or password',
messageId: 'login-attempt.blocked',
statusCode: 401,
},
status: 401,
statusText: 'Unauthorized',
});
render(<LoginPage />);
await userEvent.type(screen.getByLabelText('Username input field'), 'admin');
await userEvent.type(screen.getByLabelText('Password input field'), 'test');
await userEvent.click(screen.getByRole('button', { name: 'Login button' }));
expect(
await screen.findByRole('alert', {
name: 'You have exceeded the number of login attempts for this user. Please try again later.',
})
).toBeInTheDocument();
});
});

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "",
"invalid-user-or-password": "",
"unknown": ""
}

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "You have exceeded the number of login attempts for this user. Please try again later.",
"invalid-user-or-password": "Invalid username or password",
"unknown": "Unknown error occurred"
}

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "",
"invalid-user-or-password": "",
"unknown": ""
}

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "",
"invalid-user-or-password": "",
"unknown": ""
}

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "Ÿőū ĥävę ęχčęęđęđ ŧĥę ʼnūmþęř őƒ ľőģįʼn äŧŧęmpŧş ƒőř ŧĥįş ūşęř. Pľęäşę ŧřy äģäįʼn ľäŧęř.",
"invalid-user-or-password": "Ĩʼnväľįđ ūşęřʼnämę őř päşşŵőřđ",
"unknown": "Ůʼnĸʼnőŵʼn ęřřőř őččūřřęđ"
}

View File

@ -192,6 +192,7 @@
},
"login": {
"error": {
"blocked": "",
"invalid-user-or-password": "",
"unknown": ""
}