1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-30 13:55:31 +03:00

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
No known key found for this signature in database
GPG Key ID: 0E56ED990CDFCB4F
3 changed files with 15 additions and 6 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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('<UserAdd />', () => {
await act(async () => {
wrapper = mountWithContexts(<UserAdd />);
});
UsersAPI.create.mockResolvedValueOnce({ data: {} });
OrganizationsAPI.createUser.mockResolvedValueOnce({ data: {} });
const updatedUserData = {
username: 'sysadmin',
email: 'sysadmin@ansible.com',
@ -30,7 +30,11 @@ describe('<UserAdd />', () => {
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('<UserAdd />', () => {
is_superuser: true,
is_system_auditor: false,
};
UsersAPI.create.mockResolvedValueOnce({
OrganizationsAPI.createUser.mockResolvedValueOnce({
data: {
id: 5,
...userData,