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

Fixed validation while adding user with multiple groups. #427

Merged
merged 2 commits into from
Jul 24, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ def validate_self_user_data(data = {})

def validate_user_create_data(data)
validate_user_data(data)
req_attrs = %w(name userid group)
req_attrs = %w(name userid)
req_attrs << "password" if ::Settings.authentication.mode == "database"
bad_attrs = []
req_attrs.each { |attr| bad_attrs << attr if data[attr].blank? }
bad_attrs << "group or miq_groups" if !data['group'] && !data['miq_groups']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abellotti @gtanzillo what do you think about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hyperkid123 , @skateman - @abellotti and I looked at this and we are both ok with this change. We would just need some tests before merging.

raise BadRequestError, "Missing attribute(s) #{bad_attrs.join(', ')} for creating a user" if bad_attrs.present?
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

let(:sample_user1) { {:userid => "user1", :name => "User1", :password => "password1", :group => {"id" => group1.id}} }
let(:sample_user2) { {:userid => "user2", :name => "User2", :password => "password2", :group => {"id" => group2.id}} }
let(:sample_user3) { {:userid => "user3", :name => "User3", :password => "password3", :miq_groups => [{"id" => group1.id}, {"id" => group2.id}]} }

let(:user1) { FactoryGirl.create(:user, sample_user1.except(:group).merge(:miq_groups => [group1])) }
let(:user2) { FactoryGirl.create(:user, sample_user2.except(:group).merge(:miq_groups => [group2])) }
Expand Down Expand Up @@ -199,6 +200,34 @@
expect(user1_hash["current_group_id"]).to eq(group1.id.to_s)
expect(user2_hash["current_group_id"]).to eq(group2.id.to_s)
end

it "supports creating user with multiple groups" do
api_basic_authorize collection_action_identifier(:users, :create)

post(api_users_url, :params => gen_request(:create, sample_user3))

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_keys("results", expected_attributes)

user_id = response.parsed_body["results"].first["id"]
expect(User.exists?(user_id)).to be_truthy
end

it "rejects user creation with missing group attribute" do
api_basic_authorize collection_action_identifier(:users, :create)

post(api_users_url, :params => sample_user2.except(:group))

expect_bad_request(/Missing attribute/i)
end

it "rejects user creation with missing miq_groups attribute" do
api_basic_authorize collection_action_identifier(:users, :create)

post(api_users_url, :params => sample_user3.except(:miq_groups))

expect_bad_request(/Missing attribute/i)
end
end

describe "users edit" do
Expand Down