Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #17923 - user create/update accepts org/loc name #285

Merged
merged 1 commit into from Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/hammer_cli_foreman/user.rb
Expand Up @@ -51,11 +51,35 @@ def extend_data(user)
build_options
end

module CommonUpdateOptions
def self.included(base)
base.option '--default-organization', 'DEFAULT_ORGANIZATION_NAME', _("Default organization name")
base.option '--default-location', 'DEFAULT_LOCATION_NAME', _("Default location name")
end

def request_params
params = super
org_id = organization_id(option_default_organization)
params['user']['default_organization_id'] ||= org_id unless org_id.nil?
loc_id = location_id(option_default_location)
params['user']['default_location_id'] ||= loc_id unless loc_id.nil?
params
end

def organization_id(name)
resolver.organization_id('option_name' => name) if name
end

def location_id(name)
resolver.location_id('option_name' => name) if name
end
end

class CreateCommand < HammerCLIForeman::CreateCommand
success_message _("User [%{login}] created")
failure_message _("Could not create the user")

include CommonUpdateOptions
build_options
end

Expand All @@ -64,6 +88,7 @@ class UpdateCommand < HammerCLIForeman::UpdateCommand
success_message _("User [%{login}] updated")
failure_message _("Could not update the user")

include CommonUpdateOptions
build_options
end

Expand Down
85 changes: 85 additions & 0 deletions test/functional/user_test.rb
@@ -0,0 +1,85 @@
require File.join(File.dirname(__FILE__), 'test_helper')

describe "user" do
let(:minimal_params) { ['--login', 'jane', '--mail', 'jane@test.org', '--password', 'secret', '--auth-source-id', '1'] }
let(:user) { { 'id' => '32', 'login' => 'jane' } }

def expect_with_minimal_params(action, message, &block)
api_expects(:users, action, message) do |par|
user = par['user']
user['login'] == 'jane' &&
user['mail'] == 'jane@test.org' &&
user['password'] == 'secret' &&
user['auth_source_id'] == '1' &&
yield(par)
end
end

describe "create" do
let(:cmd) { ["user", "create"] }

it 'resolves default organization name to id' do
params = ['--default-organization', 'Org1']

api_expects_search(:organizations, { :name => 'Org1' }).returns(index_response([{ 'id' => '3' }]))
expect_with_minimal_params(:create, 'Create user with default org') do |par|
par['user']['default_organization_id'] == '3'
end.returns(user)

expected_result = success_result("User [jane] created\n")

result = run_cmd(cmd + minimal_params + params)
assert_cmd(expected_result, result)
end

it 'resolves default location name to id' do
params = ['--default-location', 'Loc1']

api_expects_search(:locations, { :name => 'Loc1' }).returns(index_response([{ 'id' => '4' }]))
expect_with_minimal_params(:create, 'Create user with default loc') do |par|
par['user']['default_location_id'] == '4'
end.returns(user)

expected_result = success_result("User [jane] created\n")

result = run_cmd(cmd + minimal_params + params)
assert_cmd(expected_result, result)
end
end

describe "update" do
let(:cmd) { ["user", "update"] }

it 'resolves default organization name to id' do
params = ['--default-organization', 'Org1']

api_expects_search(:users, { :login => 'jane' }).returns(index_response([user]))
api_expects_search(:organizations, { :name => 'Org1' }).returns(index_response([{ 'id' => '3' }]))
expect_with_minimal_params(:update, 'Update user with default org') do |par|
par['id'] == '32' &&
par['user']['default_organization_id'] == '3'
end.returns(user)

expected_result = success_result("User [jane] updated\n")

result = run_cmd(cmd + minimal_params + params)
assert_cmd(expected_result, result)
end

it 'resolves default location name to id' do
params = ['--default-location', 'Loc1']

api_expects_search(:users, { :login => 'jane' }).returns(index_response([user]))
api_expects_search(:locations, { :name => 'Loc1' }).returns(index_response([{ 'id' => '4' }]))
expect_with_minimal_params(:update, 'Update user with default loc') do |par|
par['id'] == '32' &&
par['user']['default_location_id'] == '4'
end.returns(user)

expected_result = success_result("User [jane] updated\n")

result = run_cmd(cmd + minimal_params + params)
assert_cmd(expected_result, result)
end
end
end