summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-05-02 23:41:07 -0700
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-05-02 23:41:07 -0700
commitf40d4e6685ca749c69bfc480a747a430f6c9825f (patch)
tree0cfdb3e6276c6b4cce664b89c4da37f55adaac55 /spec
parent36efe0f5807e92c2a0b6ec71b828387e6684a9ab (diff)
parentec63804831d1a55171abfb7fc0894af20d4298e8 (diff)
downloadgitlab-ce-f40d4e6685ca749c69bfc480a747a430f6c9825f.tar.gz
Merge pull request #3597 from amacarthur/fork-pull-request
updated fork feature to use gitlab-shell for v5 of gitlab
Diffstat (limited to 'spec')
-rw-r--r--spec/contexts/fork_context_spec.rb57
-rw-r--r--spec/factories/forked_project_links.rb8
-rw-r--r--spec/lib/gitlab/backend/shell_spec.rb1
-rw-r--r--spec/models/forked_project_link_spec.rb56
-rw-r--r--spec/models/project_spec.rb1
-rw-r--r--spec/routing/project_routing_spec.rb5
6 files changed, 128 insertions, 0 deletions
diff --git a/spec/contexts/fork_context_spec.rb b/spec/contexts/fork_context_spec.rb
new file mode 100644
index 00000000000..ed51b0c3f8e
--- /dev/null
+++ b/spec/contexts/fork_context_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+describe Projects::ForkContext do
+ describe :fork_by_user do
+ before do
+ @from_namespace = create(:namespace)
+ @from_user = create(:user, namespace: @from_namespace )
+ @from_project = create(:project, creator_id: @from_user.id, namespace: @from_namespace)
+ @to_namespace = create(:namespace)
+ @to_user = create(:user, namespace: @to_namespace)
+ end
+
+ context 'fork project' do
+
+ it "successfully creates project in the user namespace" do
+ @to_project = fork_project(@from_project, @to_user)
+
+ @to_project.owner.should == @to_user
+ @to_project.namespace.should == @to_user.namespace
+ end
+ end
+
+ context 'fork project failure' do
+
+ it "fails due to transaction failure" do
+ # make the mock gitlab-shell fail
+ @to_project = fork_project(@from_project, @to_user, false)
+
+ @to_project.errors.should_not be_empty
+ @to_project.errors[:base].should include("Fork transaction failed.")
+ end
+
+ end
+
+ context 'project already exists' do
+
+ it "should fail due to validation, not transaction failure" do
+ @existing_project = create(:project, creator_id: @to_user.id, name: @from_project.name, namespace: @to_namespace)
+ @to_project = fork_project(@from_project, @to_user)
+
+ @existing_project.persisted?.should be_true
+ @to_project.errors[:base].should include("Invalid fork destination")
+ @to_project.errors[:base].should_not include("Fork transaction failed.")
+ end
+
+ end
+ end
+
+ def fork_project(from_project, user, fork_success = true)
+ context = Projects::ForkContext.new(from_project, user)
+ shell = mock("gitlab_shell")
+ shell.stub(fork_repository: fork_success)
+ context.stub(gitlab_shell: shell)
+ context.execute
+ end
+
+end
diff --git a/spec/factories/forked_project_links.rb b/spec/factories/forked_project_links.rb
new file mode 100644
index 00000000000..64bcdf09429
--- /dev/null
+++ b/spec/factories/forked_project_links.rb
@@ -0,0 +1,8 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :forked_project_link do
+ association :forked_to_project, factory: :project
+ association :forked_from_project, factory: :project
+ end
+end
diff --git a/spec/lib/gitlab/backend/shell_spec.rb b/spec/lib/gitlab/backend/shell_spec.rb
index 3c04f4bbeb6..f00ec0fa401 100644
--- a/spec/lib/gitlab/backend/shell_spec.rb
+++ b/spec/lib/gitlab/backend/shell_spec.rb
@@ -12,6 +12,7 @@ describe Gitlab::Shell do
it { should respond_to :remove_key }
it { should respond_to :add_repository }
it { should respond_to :remove_repository }
+ it { should respond_to :fork_repository }
it { gitlab_shell.url_to_repo('diaspora').should == Gitlab.config.gitlab_shell.ssh_path_prefix + "diaspora.git" }
end
diff --git a/spec/models/forked_project_link_spec.rb b/spec/models/forked_project_link_spec.rb
new file mode 100644
index 00000000000..c362b21f088
--- /dev/null
+++ b/spec/models/forked_project_link_spec.rb
@@ -0,0 +1,56 @@
+require 'spec_helper'
+
+describe ForkedProjectLink, "add link on fork" do
+ let(:project_from) {create(:project)}
+ let(:namespace) {create(:namespace)}
+ let(:user) {create(:user, namespace: namespace)}
+
+ before do
+ @project_to = fork_project(project_from, user)
+ end
+
+ it "project_to should know it is forked" do
+ @project_to.forked?.should be_true
+ end
+
+ it "project should know who it is forked from" do
+ @project_to.forked_from_project.should == project_from
+ end
+end
+
+describe :forked_from_project do
+ let(:forked_project_link) {build(:forked_project_link)}
+ let(:project_from) {create(:project)}
+ let(:project_to) {create(:project, forked_project_link: forked_project_link)}
+
+
+ before :each do
+ forked_project_link.forked_from_project = project_from
+ forked_project_link.forked_to_project = project_to
+ forked_project_link.save!
+ end
+
+
+ it "project_to should know it is forked" do
+ project_to.forked?.should be_true
+ end
+
+ it "project_from should not be forked" do
+ project_from.forked?.should be_false
+ end
+
+ it "project_to.destroy should destroy fork_link" do
+ forked_project_link.should_receive(:destroy)
+ project_to.destroy
+ end
+
+end
+
+def fork_project(from_project, user)
+ context = Projects::ForkContext.new(from_project, user)
+ shell = mock("gitlab_shell")
+ shell.stub(fork_repository: true)
+ context.stub(gitlab_shell: shell)
+ context.execute
+end
+
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index fedf17b1ba0..b7eb7391072 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -40,6 +40,7 @@ describe Project do
it { should have_many(:deploy_keys).dependent(:destroy) }
it { should have_many(:hooks).dependent(:destroy) }
it { should have_many(:protected_branches).dependent(:destroy) }
+ it { should have_one(:forked_project_link).dependent(:destroy) }
end
describe "Mass assignment" do
diff --git a/spec/routing/project_routing_spec.rb b/spec/routing/project_routing_spec.rb
index 064177f8f21..dd4fb54af69 100644
--- a/spec/routing/project_routing_spec.rb
+++ b/spec/routing/project_routing_spec.rb
@@ -55,6 +55,7 @@ end
# projects POST /projects(.:format) projects#create
# new_project GET /projects/new(.:format) projects#new
+# fork_project POST /:id/fork(.:format) projects#fork
# wall_project GET /:id/wall(.:format) projects#wall
# files_project GET /:id/files(.:format) projects#files
# edit_project GET /:id/edit(.:format) projects#edit
@@ -70,6 +71,10 @@ describe ProjectsController, "routing" do
get("/projects/new").should route_to('projects#new')
end
+ it "to #fork" do
+ post("/gitlabhq/fork").should route_to('projects#fork', id: 'gitlabhq')
+ end
+
it "to #wall" do
get("/gitlabhq/wall").should route_to('walls#show', project_id: 'gitlabhq')
end