OSDN Git Service

Merge branch 'deploy_keys_nonunique' of https://github.com/miks/gitlabhq into miks...
authorAriejan de Vroom <ariejan@ariejan.net>
Thu, 1 Mar 2012 15:00:14 +0000 (16:00 +0100)
committerAriejan de Vroom <ariejan@ariejan.net>
Thu, 1 Mar 2012 15:00:14 +0000 (16:00 +0100)
Added/fixed specs
Update spec/factory to allow Factory#new without opts

Conflicts:
app/models/key.rb

1  2 
app/models/key.rb
db/schema.rb
spec/factory.rb
spec/models/key_spec.rb

@@@ -11,10 -13,22 +13,23 @@@ class Key < ActiveRecord::Bas
              :length   => { :within => 0..5000 }
  
    before_save :set_identifier
+   before_validation :strip_white_space
    after_save :update_repository
    after_destroy :repository_delete_key
 -    self.key = self.key.strip
 +  delegate :name, :email, :to => :user, :prefix => true
+   validate :unique_key
+   def strip_white_space
++    self.key = self.key.strip unless self.key.blank?
+   end
+   def unique_key
+     query = Key.where('key = ?', key)
+     query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id
+     if (query.count > 0)
+       errors.add :key, 'already exist.'
+     end
+   end
  
    def set_identifier
      if is_deploy_key
@@@ -37,7 -54,7 +55,7 @@@
        c.update_projects(projects)
      end
    end
--  
++
    def is_deploy_key
      true if project_id
    end
diff --cc db/schema.rb
@@@ -163,6 -156,7 +163,13 @@@ ActiveRecord::Schema.define(:version =
      t.integer  "project_access", :default => 0, :null => false
    end
  
++  create_table "web_hook_urls", :force => true do |t|
++    t.string   "url"
++    t.integer  "project_id"
++    t.datetime "created_at"
++    t.datetime "updated_at"
++  end
++
    create_table "web_hooks", :force => true do |t|
      t.string   "url"
      t.integer  "project_id"
diff --cc spec/factory.rb
@@@ -10,8 -10,8 +10,8 @@@ class Factor
        new(name, opts).tap(&:save!)
      end
  
--    def new(name, opts)
--      factory = @factories[name]
++    def new(name, opts = {})
++      factory= @factories[name]
        factory[0].new.tap do |obj|
          factory[1].call(obj)
        end.tap do |obj|
@@@ -14,8 -14,8 +14,40 @@@ describe Key d
      it { should respond_to :projects }
    end
  
--  it { Factory.create(:key,
--                      :user => Factory(:user)).should be_valid }
++  context "validation of uniqueness" do
++
++    context "as a deploy key" do
++      let(:project) { Factory.create(:project, path: 'alpha', code: 'alpha') }
++      let(:another_project) { Factory.create(:project, path: 'beta', code: 'beta') }
++
++      before do
++        deploy_key = Factory.create(:key, project: project)
++      end
++
++      it "does not accept the same key twice for a project" do
++        key = Factory.new(:key, project: project)
++        key.should_not be_valid
++      end
++
++      it "does accept the same key for another project" do
++        key = Factory.new(:key, project: another_project)
++        key.should be_valid
++      end
++    end
++
++    context "as a personal key" do
++      let(:user) { Factory.create(:user) }
++
++      it "accepts the key once" do
++        Factory.new(:key, user: user).should be_valid
++      end
++
++      it "does not accepts the key twice" do
++        Factory.create(:key, user: user)
++        Factory.new(:key, user: user).should_not be_valid
++      end
++    end
++  end
  end
  # == Schema Information
  #