fork_context_spec.rb 1.78 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe Projects::ForkContext do
  describe :fork_by_user do
    before do
6 7 8 9 10
      @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)
11 12 13
    end

    context 'fork project' do
14 15

      it "successfully creates project in the user namespace" do
16 17
        @to_project = fork_project(@from_project, @to_user)

18 19 20
        @to_project.owner.should == @to_user
        @to_project.namespace.should == @to_user.namespace
      end
21 22 23
    end

    context 'fork project failure' do
24 25 26

      it "fails due to transaction failure" do
        # make the mock gitlab-shell fail
27
        @to_project = fork_project(@from_project, @to_user, false)
28 29 30

        @to_project.errors.should_not be_empty
        @to_project.errors[:base].should include("Fork transaction failed.")
31 32
      end

33 34 35 36 37 38 39 40 41 42 43 44
    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
45 46 47 48 49 50

    end
  end

  def fork_project(from_project, user, fork_success = true)
    context = Projects::ForkContext.new(from_project, user)
skv's avatar
skv committed
51
    shell = double("gitlab_shell")
52 53 54 55 56 57
    shell.stub(fork_repository: fork_success)
    context.stub(gitlab_shell: shell)
    context.execute
  end

end