Skip to content

Commit f7f2b3c

Browse files
committed
Prevent calling UAA twice when creating new user through /v3/users
1 parent 39a4b2a commit f7f2b3c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

app/controllers/v3/users_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def create
4747

4848
user = UserCreate.new.create(message:)
4949

50-
render status: :created, json: Presenters::V3::UserPresenter.new(user, uaa_users: User.uaa_users_info([user.guid]))
50+
if message.username && message.origin
51+
render status: :created,
52+
json: Presenters::V3::UserPresenter.new(user,
53+
uaa_users: { user.guid => { 'username' => message.username, 'id' => user.guid, 'origin' => message.origin } })
54+
else
55+
render status: :created, json: Presenters::V3::UserPresenter.new(user, uaa_users: User.uaa_users_info([user.guid]))
56+
end
5157
rescue VCAP::CloudController::UaaUnavailable
5258
raise CloudController::Errors::ApiError.new_from_details('UaaUnavailable')
5359
rescue UserCreate::Error => e

spec/request/users_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -905,18 +905,19 @@
905905
end
906906

907907
before do
908-
allow(uaa_client).to receive(:users_for_ids).with(['new-user-guid']).and_return(
909-
{
910-
user_guid => {
911-
'username' => 'some-user',
912-
'origin' => 'idp.local'
913-
}
914-
}
915-
)
916908
allow(uaa_client).to receive(:create_shadow_user).and_return({ 'id' => user_guid })
917909
end
918910

919911
it_behaves_like 'permissions for single object endpoint', ALL_PERMISSIONS
912+
913+
it 'constructs the response with the given information without calling the UAA again' do
914+
post '/v3/users', params.to_json, admin_header
915+
916+
expect(last_response).to have_status_code(201)
917+
expect(parsed_response).to match_json_response(user_json)
918+
919+
expect(uaa_client).not_to have_received(:users_for_ids)
920+
end
920921
end
921922

922923
context 'when parameters are invalid' do

0 commit comments

Comments
 (0)