From 24e5bd244cf1bff8d6e997946bc87c527eb68a6a Mon Sep 17 00:00:00 2001 From: Xavier Mouligneau <189600+XavierM@users.noreply.github.com> Date: Thu, 28 Oct 2021 11:21:53 -0400 Subject: [PATCH] [SECURITY] Remove flaky test on edit user page (#116467) * no more flaky test * fix eslint Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../users/edit_user/edit_user_page.test.tsx | 130 +++++------------- 1 file changed, 37 insertions(+), 93 deletions(-) diff --git a/x-pack/plugins/security/public/management/users/edit_user/edit_user_page.test.tsx b/x-pack/plugins/security/public/management/users/edit_user/edit_user_page.test.tsx index 5b5e74cb2c61..f61b2ac2f805 100644 --- a/x-pack/plugins/security/public/management/users/edit_user/edit_user_page.test.tsx +++ b/x-pack/plugins/security/public/management/users/edit_user/edit_user_page.test.tsx @@ -5,13 +5,7 @@ * 2.0. */ -import { - fireEvent, - render, - waitFor, - waitForElementToBeRemoved, - within, -} from '@testing-library/react'; +import { fireEvent, render, waitFor, within } from '@testing-library/react'; import { createMemoryHistory } from 'history'; import React from 'react'; @@ -34,13 +28,22 @@ const userMock = { roles: ['superuser'], }; -// Failing: See https://github.com/elastic/kibana/issues/115473 -describe.skip('EditUserPage', () => { - it('warns when viewing deactivated user', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; +describe('EditUserPage', () => { + const coreStart = coreMock.createStart(); + let history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); + const authc = securityMock.createSetup().authc; + beforeEach(() => { + history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); + authc.getCurrentUser.mockClear(); + coreStart.http.delete.mockClear(); + coreStart.http.get.mockClear(); + coreStart.http.post.mockClear(); + coreStart.notifications.toasts.addDanger.mockClear(); + coreStart.notifications.toasts.addSuccess.mockClear(); + }); + + it('warns when viewing deactivated user', async () => { coreStart.http.get.mockResolvedValueOnce({ ...userMock, enabled: false, @@ -57,10 +60,6 @@ describe.skip('EditUserPage', () => { }); it('warns when viewing deprecated user', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce({ ...userMock, metadata: { @@ -82,14 +81,10 @@ describe.skip('EditUserPage', () => { fireEvent.click(await findByRole('button', { name: 'Back to users' })); - await waitFor(() => expect(history.location.pathname).toBe('/')); + expect(history.location.pathname).toBe('/'); }); it('warns when viewing built-in user', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce({ ...userMock, metadata: { _reserved: true, _deprecated: false }, @@ -106,14 +101,10 @@ describe.skip('EditUserPage', () => { fireEvent.click(await findByRole('button', { name: 'Back to users' })); - await waitFor(() => expect(history.location.pathname).toBe('/')); + expect(history.location.pathname).toBe('/'); }); it('warns when selecting deprecated role', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce({ ...userMock, enabled: false, @@ -140,10 +131,6 @@ describe.skip('EditUserPage', () => { }); it('updates user when submitting form and redirects back', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); coreStart.http.post.mockResolvedValueOnce({}); @@ -175,10 +162,6 @@ describe.skip('EditUserPage', () => { }); it('warns when user form submission fails', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); coreStart.http.post.mockRejectedValueOnce(new Error('Error message')); @@ -214,10 +197,6 @@ describe.skip('EditUserPage', () => { }); it('changes password of other user when submitting form and closes dialog', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); authc.getCurrentUser.mockResolvedValueOnce( @@ -225,24 +204,23 @@ describe.skip('EditUserPage', () => { ); coreStart.http.post.mockResolvedValueOnce({}); - const { getByRole, findByRole } = render( + const { findByRole } = render( ); fireEvent.click(await findByRole('button', { name: 'Change password' })); - - const dialog = getByRole('dialog'); + const dialog = await findByRole('dialog'); fireEvent.change(await within(dialog).findByLabelText('New password'), { target: { value: 'changeme' }, }); - fireEvent.change(within(dialog).getByLabelText('Confirm password'), { + fireEvent.change(await within(dialog).findByLabelText('Confirm password'), { target: { value: 'changeme' }, }); - fireEvent.click(within(dialog).getByRole('button', { name: 'Change password' })); + fireEvent.click(await within(dialog).findByRole('button', { name: 'Change password' })); - await waitForElementToBeRemoved(() => getByRole('dialog')); + expect(await findByRole('dialog')).not.toBeInTheDocument(); expect(coreStart.http.post).toHaveBeenLastCalledWith('/internal/security/users/jdoe/password', { body: JSON.stringify({ newPassword: 'changeme', @@ -251,23 +229,18 @@ describe.skip('EditUserPage', () => { }); it('changes password of current user when submitting form and closes dialog', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); authc.getCurrentUser.mockResolvedValueOnce(mockAuthenticatedUser(userMock)); coreStart.http.post.mockResolvedValueOnce({}); - const { getByRole, findByRole } = render( + const { findByRole } = render( ); fireEvent.click(await findByRole('button', { name: 'Change password' })); - const dialog = await findByRole('dialog'); fireEvent.change(await within(dialog).findByLabelText('Current password'), { target: { value: '123456' }, @@ -280,7 +253,7 @@ describe.skip('EditUserPage', () => { }); fireEvent.click(await within(dialog).findByRole('button', { name: 'Change password' })); - await waitForElementToBeRemoved(() => getByRole('dialog')); + expect(await findByRole('dialog')).not.toBeInTheDocument(); expect(coreStart.http.post).toHaveBeenLastCalledWith('/internal/security/users/jdoe/password', { body: JSON.stringify({ newPassword: 'changeme', @@ -290,10 +263,6 @@ describe.skip('EditUserPage', () => { }); it('warns when change password form submission fails', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); authc.getCurrentUser.mockResolvedValueOnce( @@ -308,7 +277,6 @@ describe.skip('EditUserPage', () => { ); fireEvent.click(await findByRole('button', { name: 'Change password' })); - const dialog = await findByRole('dialog'); fireEvent.change(await within(dialog).findByLabelText('New password'), { target: { value: 'changeme' }, @@ -327,10 +295,6 @@ describe.skip('EditUserPage', () => { }); it('validates change password form', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); authc.getCurrentUser.mockResolvedValueOnce(mockAuthenticatedUser(userMock)); @@ -343,21 +307,17 @@ describe.skip('EditUserPage', () => { ); fireEvent.click(await findByRole('button', { name: 'Change password' })); - const dialog = await findByRole('dialog'); fireEvent.click(await within(dialog).findByRole('button', { name: 'Change password' })); - await within(dialog).findByText(/Enter your current password/i); await within(dialog).findByText(/Enter a new password/i); fireEvent.change(await within(dialog).findByLabelText('Current password'), { target: { value: 'changeme' }, }); - fireEvent.change(await within(dialog).findByLabelText('New password'), { target: { value: '111' }, }); - await within(dialog).findAllByText(/Password must be at least 6 characters/i); fireEvent.change(await within(dialog).findByLabelText('New password'), { @@ -366,44 +326,34 @@ describe.skip('EditUserPage', () => { fireEvent.change(await within(dialog).findByLabelText('Confirm password'), { target: { value: '111' }, }); - await within(dialog).findAllByText(/Passwords do not match/i); }); it('deactivates user when confirming and closes dialog', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); coreStart.http.post.mockResolvedValueOnce({}); - const { getByRole, findByRole } = render( + const { findByRole } = render( ); fireEvent.click(await findByRole('button', { name: 'Deactivate user' })); + const dialog = await findByRole('dialog'); + fireEvent.click(await within(dialog).findByRole('button', { name: 'Deactivate user' })); - const dialog = getByRole('dialog'); - fireEvent.click(within(dialog).getByRole('button', { name: 'Deactivate user' })); - - await waitForElementToBeRemoved(() => getByRole('dialog')); + expect(await findByRole('dialog')).not.toBeInTheDocument(); expect(coreStart.http.post).toHaveBeenLastCalledWith('/internal/security/users/jdoe/_disable'); }); it('activates user when confirming and closes dialog', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce({ ...userMock, enabled: false }); coreStart.http.get.mockResolvedValueOnce([]); coreStart.http.post.mockResolvedValueOnce({}); - const { getByRole, findAllByRole } = render( + const { findByRole, findAllByRole } = render( @@ -411,36 +361,30 @@ describe.skip('EditUserPage', () => { const [enableButton] = await findAllByRole('button', { name: 'Activate user' }); fireEvent.click(enableButton); + const dialog = await findByRole('dialog'); + fireEvent.click(await within(dialog).findByRole('button', { name: 'Activate user' })); - const dialog = getByRole('dialog'); - fireEvent.click(within(dialog).getByRole('button', { name: 'Activate user' })); - - await waitForElementToBeRemoved(() => getByRole('dialog')); + expect(await findByRole('dialog')).not.toBeInTheDocument(); expect(coreStart.http.post).toHaveBeenLastCalledWith('/internal/security/users/jdoe/_enable'); }); it('deletes user when confirming and redirects back', async () => { - const coreStart = coreMock.createStart(); - const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); - const authc = securityMock.createSetup().authc; - coreStart.http.get.mockResolvedValueOnce(userMock); coreStart.http.get.mockResolvedValueOnce([]); coreStart.http.delete.mockResolvedValueOnce({}); - const { getByRole, findByRole } = render( + const { findByRole } = render( ); fireEvent.click(await findByRole('button', { name: 'Delete user' })); + const dialog = await findByRole('dialog'); + fireEvent.click(await within(dialog).findByRole('button', { name: 'Delete user' })); - const dialog = getByRole('dialog'); - fireEvent.click(within(dialog).getByRole('button', { name: 'Delete user' })); - - expect(coreStart.http.delete).toHaveBeenLastCalledWith('/internal/security/users/jdoe'); await waitFor(() => { + expect(coreStart.http.delete).toHaveBeenLastCalledWith('/internal/security/users/jdoe'); expect(history.location.pathname).toBe('/'); }); });