creations_controller_spec.rb 6.05 KB
Newer Older
1 2 3
require 'spec_helper'

describe Projects::MergeRequests::CreationsController do
4
  let(:project) { create(:project, :repository) }
5 6
  let(:user)    { project.owner }
  let(:fork_project) { create(:forked_project_with_submodules) }
7 8 9 10 11 12 13 14 15 16
  let(:get_diff_params) do
    {
      namespace_id: fork_project.namespace.to_param,
      project_id: fork_project,
      merge_request: {
        source_branch: 'remove-submodule',
        target_branch: 'master'
      }
    }
  end
17 18

  before do
19
    fork_project.add_master(user)
20
    Projects::ForkService.new(project, user).execute(fork_project)
21 22 23 24 25 26
    sign_in(user)
  end

  describe 'GET new' do
    context 'merge request that removes a submodule' do
      it 'renders new merge request widget template' do
27 28 29 30 31 32 33 34 35 36 37 38 39
        get :new, get_diff_params

        expect(response).to be_success
      end
    end
  end

  describe 'GET diffs' do
    context 'when merge request cannot be created' do
      it 'does not assign diffs var' do
        allow_any_instance_of(MergeRequest).to receive(:can_be_created).and_return(false)

        get :diffs, get_diff_params.merge(format: 'json')
40 41

        expect(response).to be_success
42
        expect(assigns[:diffs]).to be_nil
43 44 45 46 47 48 49 50 51 52 53 54
      end
    end
  end

  describe 'GET pipelines' do
    before do
      create(:ci_pipeline, sha: fork_project.commit('remove-submodule').id,
                           ref: 'remove-submodule',
                           project: fork_project)
    end

    it 'renders JSON including serialized pipelines' do
55
      get :pipelines, get_diff_params.merge(format: 'json')
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

      expect(response).to be_ok
      expect(json_response).to have_key 'pipelines'
      expect(json_response['pipelines']).not_to be_empty
    end
  end

  describe 'GET diff_for_path' do
    def diff_for_path(extra_params = {})
      params = {
        namespace_id: project.namespace.to_param,
        project_id: project,
        format: 'json'
      }

      get :diff_for_path, params.merge(extra_params)
    end

    let(:existing_path) { 'files/ruby/feature.rb' }

    context 'when both branches are in the same project' do
      it 'disables diff notes' do
        diff_for_path(old_path: existing_path, new_path: existing_path, merge_request: { source_branch: 'feature', target_branch: 'master' })

        expect(assigns(:diff_notes_disabled)).to be_truthy
      end

      it 'only renders the diffs for the path given' do
        expect(controller).to receive(:render_diff_for_path).and_wrap_original do |meth, diffs|
          expect(diffs.diff_files.map(&:new_path)).to contain_exactly(existing_path)
          meth.call(diffs)
        end

        diff_for_path(old_path: existing_path, new_path: existing_path, merge_request: { source_branch: 'feature', target_branch: 'master' })
      end
    end

    context 'when the source branch is in a different project to the target' do
94
      let(:other_project) { create(:project, :repository) }
95 96

      before do
97
        other_project.add_master(user)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      end

      context 'when the path exists in the diff' do
        it 'disables diff notes' do
          diff_for_path(old_path: existing_path, new_path: existing_path, merge_request: { source_project: other_project, source_branch: 'feature', target_branch: 'master' })

          expect(assigns(:diff_notes_disabled)).to be_truthy
        end

        it 'only renders the diffs for the path given' do
          expect(controller).to receive(:render_diff_for_path).and_wrap_original do |meth, diffs|
            expect(diffs.diff_files.map(&:new_path)).to contain_exactly(existing_path)
            meth.call(diffs)
          end

          diff_for_path(old_path: existing_path, new_path: existing_path, merge_request: { source_project: other_project, source_branch: 'feature', target_branch: 'master' })
        end
      end

      context 'when the path does not exist in the diff' do
        before do
          diff_for_path(old_path: 'files/ruby/nopen.rb', new_path: 'files/ruby/nopen.rb', merge_request: { source_project: other_project, source_branch: 'feature', target_branch: 'master' })
        end

        it 'returns a 404' do
123
          expect(response).to have_gitlab_http_status(404)
124 125 126 127
        end
      end
    end
  end
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  describe 'GET #branch_to' do
    before do
      allow(Ability).to receive(:allowed?).and_call_original
    end

    it 'fetches the commit if a user has access' do
      expect(Ability).to receive(:allowed?).with(user, :read_project, project) { true }

      get :branch_to,
          namespace_id: fork_project.namespace,
          project_id: fork_project,
          target_project_id: project.id,
          ref: 'master'

      expect(assigns(:commit)).not_to be_nil
      expect(response).to have_gitlab_http_status(200)
    end

    it 'does not load the commit when the user cannot read the project' do
      expect(Ability).to receive(:allowed?).with(user, :read_project, project) { false }

      get :branch_to,
          namespace_id: fork_project.namespace,
          project_id: fork_project,
          target_project_id: project.id,
          ref: 'master'

      expect(assigns(:commit)).to be_nil
      expect(response).to have_gitlab_http_status(200)
    end
  end

  describe 'GET #update_branches' do
    before do
      allow(Ability).to receive(:allowed?).and_call_original
    end

    it 'lists the branches of another fork if the user has access' do
      expect(Ability).to receive(:allowed?).with(user, :read_project, project) { true }

      get :update_branches,
          namespace_id: fork_project.namespace,
          project_id: fork_project,
          target_project_id: project.id

      expect(assigns(:target_branches)).not_to be_empty
      expect(response).to have_gitlab_http_status(200)
    end

    it 'does not list branches when the user cannot read the project' do
      expect(Ability).to receive(:allowed?).with(user, :read_project, project) { false }

      get :update_branches,
          namespace_id: fork_project.namespace,
          project_id: fork_project,
          target_project_id: project.id

      expect(response).to have_gitlab_http_status(200)
      expect(assigns(:target_branches)).to eq([])
    end
  end
190
end