Skip to content

Commit

Permalink
Prevent calling UAA twice when creating new user through /v3/users
Browse files Browse the repository at this point in the history
  • Loading branch information
svkrieger committed Dec 5, 2024
1 parent 39a4b2a commit f7f2b3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 7 additions & 1 deletion app/controllers/v3/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ def create

user = UserCreate.new.create(message:)

render status: :created, json: Presenters::V3::UserPresenter.new(user, uaa_users: User.uaa_users_info([user.guid]))
if message.username && message.origin
render status: :created,
json: Presenters::V3::UserPresenter.new(user,
uaa_users: { user.guid => { 'username' => message.username, 'id' => user.guid, 'origin' => message.origin } })
else
render status: :created, json: Presenters::V3::UserPresenter.new(user, uaa_users: User.uaa_users_info([user.guid]))
end
rescue VCAP::CloudController::UaaUnavailable
raise CloudController::Errors::ApiError.new_from_details('UaaUnavailable')
rescue UserCreate::Error => e
Expand Down
17 changes: 9 additions & 8 deletions spec/request/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -905,18 +905,19 @@
end

before do
allow(uaa_client).to receive(:users_for_ids).with(['new-user-guid']).and_return(
{
user_guid => {
'username' => 'some-user',
'origin' => 'idp.local'
}
}
)
allow(uaa_client).to receive(:create_shadow_user).and_return({ 'id' => user_guid })
end

it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS

it 'constructs the response with the given information without calling the UAA again' do
post '/v3/users', params.to_json, admin_header

expect(last_response).to have_status_code(201)
expect(parsed_response).to match_json_response(user_json)

expect(uaa_client).not_to have_received(:users_for_ids)
end
end

context 'when parameters are invalid' do
Expand Down

0 comments on commit f7f2b3c

Please sign in to comment.