From 806a4686001afe24c700e0b0c6d7693ba3d809ae Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Wed, 19 Aug 2020 10:26:53 -0400 Subject: [PATCH 1/2] Use organization api to create users This ensures that the user will be related to the chosen organization when it is created. --- awx/ui_next/src/api/models/Organizations.js | 4 ++++ awx/ui_next/src/screens/User/UserAdd/UserAdd.jsx | 5 +++-- .../src/screens/User/UserAdd/UserAdd.test.jsx | 12 ++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/awx/ui_next/src/api/models/Organizations.js b/awx/ui_next/src/api/models/Organizations.js index 3cbe64c284..267c9aba1e 100644 --- a/awx/ui_next/src/api/models/Organizations.js +++ b/awx/ui_next/src/api/models/Organizations.js @@ -15,6 +15,10 @@ class Organizations extends InstanceGroupsMixin(NotificationsMixin(Base)) { readTeams(id, params) { return this.http.get(`${this.baseUrl}${id}/teams/`, { params }); } + + createUser(id, data) { + return this.http.post(`${this.baseUrl}${id}/users/`, data); + } } export default Organizations; diff --git a/awx/ui_next/src/screens/User/UserAdd/UserAdd.jsx b/awx/ui_next/src/screens/User/UserAdd/UserAdd.jsx index d54da84592..78d18340be 100644 --- a/awx/ui_next/src/screens/User/UserAdd/UserAdd.jsx +++ b/awx/ui_next/src/screens/User/UserAdd/UserAdd.jsx @@ -3,7 +3,7 @@ import { useHistory } from 'react-router-dom'; import { Card, PageSection } from '@patternfly/react-core'; import { CardBody } from '../../../components/Card'; import UserForm from '../shared/UserForm'; -import { UsersAPI } from '../../../api'; +import { OrganizationsAPI } from '../../../api'; function UserAdd() { const [formSubmitError, setFormSubmitError] = useState(null); @@ -11,10 +11,11 @@ function UserAdd() { const handleSubmit = async values => { setFormSubmitError(null); + const { organization, ...userValues } = values; try { const { data: { id }, - } = await UsersAPI.create(values); + } = await OrganizationsAPI.createUser(organization, userValues); history.push(`/users/${id}/details`); } catch (error) { setFormSubmitError(error); diff --git a/awx/ui_next/src/screens/User/UserAdd/UserAdd.test.jsx b/awx/ui_next/src/screens/User/UserAdd/UserAdd.test.jsx index ec18de0974..55327cfcbd 100644 --- a/awx/ui_next/src/screens/User/UserAdd/UserAdd.test.jsx +++ b/awx/ui_next/src/screens/User/UserAdd/UserAdd.test.jsx @@ -6,7 +6,7 @@ import { waitForElement, } from '../../../../testUtils/enzymeHelpers'; import UserAdd from './UserAdd'; -import { UsersAPI } from '../../../api'; +import { OrganizationsAPI } from '../../../api'; jest.mock('../../../api'); let wrapper; @@ -16,7 +16,7 @@ describe('', () => { await act(async () => { wrapper = mountWithContexts(); }); - UsersAPI.create.mockResolvedValueOnce({ data: {} }); + OrganizationsAPI.createUser.mockResolvedValueOnce({ data: {} }); const updatedUserData = { username: 'sysadmin', email: 'sysadmin@ansible.com', @@ -30,7 +30,11 @@ describe('', () => { await act(async () => { wrapper.find('UserForm').prop('handleSubmit')(updatedUserData); }); - expect(UsersAPI.create).toHaveBeenCalledWith(updatedUserData); + + const { organization, ...userData } = updatedUserData; + expect(OrganizationsAPI.createUser.mock.calls).toEqual([ + [organization, userData], + ]); }); test('should navigate to users list when cancel is clicked', async () => { @@ -58,7 +62,7 @@ describe('', () => { is_superuser: true, is_system_auditor: false, }; - UsersAPI.create.mockResolvedValueOnce({ + OrganizationsAPI.createUser.mockResolvedValueOnce({ data: { id: 5, ...userData, From dc9f2441dfde3bf1657e4cc8ad43fd06e2db1bab Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Wed, 19 Aug 2020 10:52:00 -0400 Subject: [PATCH 2/2] Embolden user organization name --- .../screens/User/UserOrganizations/UserOrganizationListItem.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx/ui_next/src/screens/User/UserOrganizations/UserOrganizationListItem.jsx b/awx/ui_next/src/screens/User/UserOrganizations/UserOrganizationListItem.jsx index bc01af942d..d31c1c7670 100644 --- a/awx/ui_next/src/screens/User/UserOrganizations/UserOrganizationListItem.jsx +++ b/awx/ui_next/src/screens/User/UserOrganizations/UserOrganizationListItem.jsx @@ -19,7 +19,7 @@ export default function UserOrganizationListItem({ organization }) { to={`/organizations/${organization.id}/details`} id={labelId} > - {organization.name} + {organization.name} ,