Use organization api to create users

This ensures that the user will be related to the chosen organization
when it is created.
This commit is contained in:
Jake McDermott
2020-08-19 10:26:53 -04:00
parent d452c1d7a9
commit 806a468600
3 changed files with 15 additions and 6 deletions

View File

@@ -15,6 +15,10 @@ class Organizations extends InstanceGroupsMixin(NotificationsMixin(Base)) {
readTeams(id, params) { readTeams(id, params) {
return this.http.get(`${this.baseUrl}${id}/teams/`, { 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; export default Organizations;

View File

@@ -3,7 +3,7 @@ import { useHistory } from 'react-router-dom';
import { Card, PageSection } from '@patternfly/react-core'; import { Card, PageSection } from '@patternfly/react-core';
import { CardBody } from '../../../components/Card'; import { CardBody } from '../../../components/Card';
import UserForm from '../shared/UserForm'; import UserForm from '../shared/UserForm';
import { UsersAPI } from '../../../api'; import { OrganizationsAPI } from '../../../api';
function UserAdd() { function UserAdd() {
const [formSubmitError, setFormSubmitError] = useState(null); const [formSubmitError, setFormSubmitError] = useState(null);
@@ -11,10 +11,11 @@ function UserAdd() {
const handleSubmit = async values => { const handleSubmit = async values => {
setFormSubmitError(null); setFormSubmitError(null);
const { organization, ...userValues } = values;
try { try {
const { const {
data: { id }, data: { id },
} = await UsersAPI.create(values); } = await OrganizationsAPI.createUser(organization, userValues);
history.push(`/users/${id}/details`); history.push(`/users/${id}/details`);
} catch (error) { } catch (error) {
setFormSubmitError(error); setFormSubmitError(error);

View File

@@ -6,7 +6,7 @@ import {
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import UserAdd from './UserAdd'; import UserAdd from './UserAdd';
import { UsersAPI } from '../../../api'; import { OrganizationsAPI } from '../../../api';
jest.mock('../../../api'); jest.mock('../../../api');
let wrapper; let wrapper;
@@ -16,7 +16,7 @@ describe('<UserAdd />', () => {
await act(async () => { await act(async () => {
wrapper = mountWithContexts(<UserAdd />); wrapper = mountWithContexts(<UserAdd />);
}); });
UsersAPI.create.mockResolvedValueOnce({ data: {} }); OrganizationsAPI.createUser.mockResolvedValueOnce({ data: {} });
const updatedUserData = { const updatedUserData = {
username: 'sysadmin', username: 'sysadmin',
email: 'sysadmin@ansible.com', email: 'sysadmin@ansible.com',
@@ -30,7 +30,11 @@ describe('<UserAdd />', () => {
await act(async () => { await act(async () => {
wrapper.find('UserForm').prop('handleSubmit')(updatedUserData); 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 () => { test('should navigate to users list when cancel is clicked', async () => {
@@ -58,7 +62,7 @@ describe('<UserAdd />', () => {
is_superuser: true, is_superuser: true,
is_system_auditor: false, is_system_auditor: false,
}; };
UsersAPI.create.mockResolvedValueOnce({ OrganizationsAPI.createUser.mockResolvedValueOnce({
data: { data: {
id: 5, id: 5,
...userData, ...userData,