Skip to content

Commit

Permalink
Fix: mng API to create service account without owner_uid (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
chumaknadya authored Mar 10, 2021
1 parent 486ca86 commit e5d7597
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/api/v2/management/api_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class APIKeys < Grape::API
end

declared_params = declared(params, include_missing: false)
.except(:uid, :sa_uid, :scopes)
.except(:uid, :scopes)
.merge(scope: params[:scopes]&.split(','))
.merge(secret: SecureRandom.hex(16))

Expand Down
14 changes: 10 additions & 4 deletions app/api/v2/management/service_accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,23 @@ class ServiceAccounts < Grape::API
end
params do
requires :service_account_role, type: String, allow_blank: false, desc: 'service_account role'
optional :owner_uid, type: String, allow_blank: false, desc: 'owner uid'
optional :owner_uid, type: String, desc: 'owner uid'
optional :service_account_uid, type: String, allow_blank: false, desc: 'service_account uid'
optional :service_account_email, type: String, allow_blank: false, desc: 'service_account email'
optional :service_account_state, type: String, desc: 'service_account state'
end

post '/create' do
owner = User.find_by(uid: params[:owner_uid])
error!('User doesnt exist', 422) unless owner

s_params = { email: params[:service_account_email], uid: params[:service_account_uid], role: params[:service_account_role] }.compact
service_acc = ServiceAccount.new(s_params.merge(user: owner))
s_params = {
email: params[:service_account_email],
uid: params[:service_account_uid],
role: params[:service_account_role],
state: params[:service_account_state],
user: owner
}.compact
service_acc = ServiceAccount.new(s_params)
error!(service_acc.errors.full_messages, 422) unless service_acc.save

present service_acc, with: API::V2::Entities::ServiceAccounts
Expand Down
41 changes: 41 additions & 0 deletions spec/api/v2/management/service_accounts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,47 @@
end
end
end

context 'when uid doesnt exist' do
let(:params) do
{
service_account_email: '[email protected]',
service_account_uid: 'Fai5aesoLEcx',
service_account_role: 'admin',
}
end

it 'creates a service account' do
expect { do_request }.to change { ServiceAccount.count }.by(1)

expect_status_to_eq 201
result = JSON.parse(response.body)
expect(result.keys).to match_array %w[email uid role level state user created_at updated_at]
expect(result['user']).to eq nil
expect(result['state']).to eq 'pending'
end
end

context 'when owner_uid doesnt exist' do
let(:params) do
{
service_account_email: '[email protected]',
service_account_uid: 'Fai5aesoLEcx',
service_account_role: 'admin',
service_account_state: 'active'
}
end

it 'creates a service account' do
expect { do_request }.to change { ServiceAccount.count }.by(1)

expect_status_to_eq 201
result = JSON.parse(response.body)
expect(result.keys).to match_array %w[email uid role level state user created_at updated_at]
expect(result['user']).to eq nil
expect(result['state']).to eq 'active'
end
end
end

describe 'Update a service account' do
Expand Down

0 comments on commit e5d7597

Please sign in to comment.