Creating the same account under different provider works, but when
editing provider account the validation problem stil appears.
Dne 31.3.2011 14:09, jzigmund(a)redhat.com napsal(a):
From: Jozef Zigmund<jzigmund(a)redhat.com>
Bugfix for "Not able to add same account under two different providers"
---
src/app/models/provider_account.rb | 8 +++++++-
src/spec/models/provider_account_spec.rb | 7 +++++++
2 files changed, 14 insertions(+), 1 deletions(-)
diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb
index 80072c3..ecf5ffd 100644
--- a/src/app/models/provider_account.rb
+++ b/src/app/models/provider_account.rb
@@ -83,7 +83,13 @@ class ProviderAccount< ActiveRecord::Base
def validate_unique_username
cid = CredentialDefinition.find_by_name('username', :conditions =>
{:provider_type_id => provider.provider_type.id})
- errors.add(:base, "Username has already been taken") unless
Credential.all(:conditions => {:value => credentials_hash['username'],
:credential_definition_id => cid}).empty?
+ Credential.all(:conditions => {:value =>
credentials_hash['username'], :credential_definition_id => cid}).each do |c|
+ if c.provider_account.provider == self.provider
+ errors.add(:base, "Username has already been taken")
+ return false
+ end
+ end
+ return true
end
# Hooks
diff --git a/src/spec/models/provider_account_spec.rb
b/src/spec/models/provider_account_spec.rb
index 3b6ae82..6845ed2 100644
--- a/src/spec/models/provider_account_spec.rb
+++ b/src/spec/models/provider_account_spec.rb
@@ -80,4 +80,11 @@ EOT
}
provider_account.build_credentials.to_s.should eql(expected_xml)
end
+
+ it "should create provider account with same username for different
provider" do
+ provider_account1 = Factory :mock_provider_account
+ provider_account2 = Factory.build(:mock_provider_account, :provider =>
Factory.create(:mock_provider2))
+ provider_account1.credentials_hash.should == provider_account2.credentials_hash
+ provider_account2.should be_valid
+ end
end