Commit 756c6196 authored by Tom Darneix's avatar Tom Darneix
Browse files

fix(UI):User is redirected to the reset password page when the login request...


fix(UI):User is redirected to the reset password page when the login request receive a 500 error (#11110)

* Fix autocomplete that doesn't appear

* Fix wizard layout

* Fix redirection

* Improve test

* Fix autocomplete that doesn't appear

* Fix wizard layout

* fix(readme): remove useless carriage return

* Fix redirection

* Improve test

* Revert some changes

* Improve reability

Co-authored-by: default avatarKevin Duret <kduret@centreon.com>
parent bc65b149
......@@ -147,6 +147,19 @@ const mockPostLoginPasswordExpired = (): void => {
});
};
const labelError = 'This is an error from the server';
const mockPostLoginServerError = (): void => {
mockedAxios.post.mockRejectedValue({
response: {
data: {
message: labelError,
},
status: 500,
},
});
};
const labelInvalidCredentials = 'Invalid credentials';
describe('Login Page', () => {
......@@ -316,4 +329,21 @@ describe('Login Page', () => {
expect(window.location.href).toBe('http://localhost/reset-password');
});
it('stays on the login page when the login request returns a 500 error', async () => {
mockPostLoginServerError();
renderLoginPage();
userEvent.type(screen.getByLabelText(labelAlias), 'admin');
userEvent.type(screen.getByLabelText(labelPassword), 'centreon');
userEvent.click(screen.getByLabelText(labelConnect));
await waitFor(() => {
expect(screen.getByText(labelError)).toBeInTheDocument();
});
expect(window.location.href).not.toBe('http://localhost/reset-password');
expect(screen.getByLabelText(labelAlias)).toBeInTheDocument();
});
});
......@@ -107,20 +107,18 @@ const useLogin = (): UseLoginState => {
error,
) as RedirectAPI;
if (isUserNotAllowed && not(passwordIsExpired)) {
setSubmitting(false);
showErrorMessage(
path(['response', 'data', 'message'], error) as string,
);
if (isUserNotAllowed && passwordIsExpired) {
setPasswordResetInformations({
alias,
});
navigate(routeMap.resetPassword);
showWarningMessage(t(labelPasswordHasExpired));
return;
}
setPasswordResetInformations({
alias,
});
navigate(routeMap.resetPassword);
showWarningMessage(t(labelPasswordHasExpired));
setSubmitting(false);
showErrorMessage(path(['response', 'data', 'message'], error) as string);
},
[],
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment