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

Add uniqueness validation on Endpoint#url #12068

Merged
merged 2 commits into from
Oct 20, 2016
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
1 change: 1 addition & 0 deletions app/models/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Endpoint < ApplicationRecord
default_value_for :verify_ssl, OpenSSL::SSL::VERIFY_PEER
validates :verify_ssl, :inclusion => {:in => [OpenSSL::SSL::VERIFY_NONE, OpenSSL::SSL::VERIFY_PEER]}
validates :port, :numericality => {:only_integer => true, :allow_nil => true, :greater_than => 0}
validates :url, :uniqueness => true, :if => :url

def verify_ssl=(val)
val = resolve_verify_ssl_value(val)
Expand Down
18 changes: 7 additions & 11 deletions spec/controllers/provider_foreman_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,13 @@
end
end

context "#save_provider_foreman" do
it "will not save with a duplicate name" do
ManageIQ::Providers::Foreman::Provider.create(:name => "test2Foreman",
:url => "10.8.96.103", :zone => @zone)
provider2 = ManageIQ::Providers::Foreman::Provider.new(:name => "test2Foreman",
:url => "10.8.96.103", :zone => @zone)
controller.instance_variable_set(:@provider_cfgmgmt, provider2)
allow(controller).to receive(:render_flash)
controller.save_provider_foreman
expect(assigns(:flash_array).first[:message]).to include("Configuration_manager.name has already been taken")
end
it "#save_provider_foreman will not save with a duplicate name" do
ManageIQ::Providers::Foreman::Provider.create(:name => "test2Foreman", :url => "server1", :zone => @zone)
provider2 = ManageIQ::Providers::Foreman::Provider.new(:name => "test2Foreman", :url => "server2", :zone => @zone)
controller.instance_variable_set(:@provider_cfgmgmt, provider2)
allow(controller).to receive(:render_flash)
controller.save_provider_foreman
expect(assigns(:flash_array).first[:message]).to include("Configuration_manager.name has already been taken")
Copy link
Member

Choose a reason for hiding this comment

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

Grumble. I guess.

end

context "#edit" do
Expand Down
12 changes: 12 additions & 0 deletions spec/models/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@
end
end
end

context "Uniqueness validation on :url" do
it "is not required" do
expect(Endpoint.create!).to be_truthy
expect(Endpoint.create!).to be_truthy
end

it "raises when provided and already exists" do
Endpoint.create!(:url => "abc")
expect { Endpoint.create!(:url => "abc") }.to raise_error("Validation failed: Url has already been taken")
end
end
Copy link
Member

Choose a reason for hiding this comment

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

TBH, I'm indifferent about these specs. You're essentially testing that ActiveRecord does it's job (which is also why I'm generally against shoulda-matchers. Are you sure you want to add these db writes to the tests? Your choice but thought I'd throw my thoughts out there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was on the fence about writing them since it is just testing rails validations.

My only argument for these kinds of tests is that it shows the intent of the validations for people making future changes. It's essentially testing this use case.

Copy link
Member

Choose a reason for hiding this comment

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

Alrighty 👍

end