merge_requests_controller_spec.rb 6.51 KB
Newer Older
1 2
require 'spec_helper'

3
describe Projects::MergeRequestsController do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  let(:project) { create(:project) }
5
  let(:user)    { create(:user) }
6
  let(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
7 8 9

  before do
    sign_in(user)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10
    project.team << [user, :master]
11 12
  end

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  describe '#new' do
    context 'merge request that removes a submodule' do
      render_views

      let(:fork_project) { create(:forked_project_with_submodules) }

      before do
        fork_project.team << [user, :master]
      end

      it 'renders it' do
        get :new,
            namespace_id: fork_project.namespace.to_param,
            project_id: fork_project.to_param,
            merge_request: {
              source_branch: 'remove-submodule',
              target_branch: 'master'
            }

        expect(response).to be_success
      end
    end
  end

37
  describe "#show" do
38
    shared_examples "export merge as" do |format|
39
      it "should generally work" do
40 41 42 43 44
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
45 46 47 48 49

        expect(response).to be_success
      end

      it "should generate it" do
50
        expect_any_instance_of(MergeRequest).to receive(:"to_#{format}")
51

52 53 54 55 56
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
57 58 59
      end

      it "should render it" do
60 61 62 63 64
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
65

66
        expect(response.body).to eq((merge_request.send(:"to_#{format}",user)).to_s)
67 68 69
      end

      it "should not escape Html" do
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
70 71
        allow_any_instance_of(MergeRequest).to receive(:"to_#{format}").
          and_return('HTML entities &<>" ')
72

73 74 75 76 77
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
78

79 80 81 82
        expect(response.body).not_to include('&amp;')
        expect(response.body).not_to include('&gt;')
        expect(response.body).not_to include('&lt;')
        expect(response.body).not_to include('&quot;')
83 84 85 86
      end
    end

    describe "as diff" do
87
      include_examples "export merge as", :diff
88 89 90
      let(:format) { :diff }

      it "should really only be a git diff" do
91 92 93 94 95
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
96 97 98 99 100 101

        expect(response.body).to start_with("diff --git")
      end
    end

    describe "as patch" do
102
      include_examples "export merge as", :patch
103 104 105
      let(:format) { :patch }

      it "should really be a git email patch with commit" do
106 107 108 109
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid, format: format)
110

111
        expect(response.body[0..100]).to start_with("From #{merge_request.commits.last.id}")
112 113 114
      end

      it "should contain git diffs" do
115 116 117 118 119
        get(:show,
            namespace_id: project.namespace.to_param,
            project_id: project.to_param,
            id: merge_request.iid,
            format: format)
120 121 122 123 124

        expect(response.body).to match(/^diff --git/)
      end
    end
  end
125

126 127
  describe 'GET diffs' do
    def go(format: 'html')
128 129 130 131 132
      get :diffs,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: merge_request.iid,
          format: format
133 134
    end

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
    context 'as html' do
      it 'renders the diff template' do
        go

        expect(response).to render_template('diffs')
      end
    end

    context 'as json' do
      it 'renders the diffs template to a string' do
        go format: 'json'

        expect(response).to render_template('projects/merge_requests/show/_diffs')
        expect(JSON.parse(response.body)).to have_key('html')
      end
    end

    context 'with forked projects with submodules' do
      render_views

      let(:project) { create(:project) }
      let(:fork_project) { create(:forked_project_with_submodules) }
      let(:merge_request) { create(:merge_request_with_diffs, source_project: fork_project, source_branch: 'add-submodule-version-bump', target_branch: 'master', target_project: project) }

      before do
        fork_project.build_forked_project_link(forked_to_project_id: fork_project.id, forked_from_project_id: project.id)
        fork_project.save
        merge_request.reload
      end

      it 'renders' do
        go format: 'json'

        expect(response).to be_success
        expect(response.body).to have_content('Subproject commit')
      end
    end
  end

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  describe 'GET diffs with ignore_whitespace_change' do
    def go(format: 'html')
      get :diffs,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: merge_request.iid,
          format: format,
          w: 1
    end

    context 'as html' do
      it 'renders the diff template' do
        go

        expect(response).to render_template('diffs')
      end
    end
191

192 193 194 195 196 197 198 199 200 201
    context 'as json' do
      it 'renders the diffs template to a string' do
        go format: 'json'

        expect(response).to render_template('projects/merge_requests/show/_diffs')
        expect(JSON.parse(response.body)).to have_key('html')
      end
    end
  end

202 203 204 205 206 207 208 209 210 211 212 213
  describe 'GET diffs with view' do
    it 'saves the preferred diff view in a cookie' do
      get :diffs,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: merge_request.iid,
          view: 'parallel'

      expect(response.cookies['diff_view']).to eq('parallel')
    end
  end

214 215
  describe 'GET commits' do
    def go(format: 'html')
216 217 218 219 220
      get :commits,
          namespace_id: project.namespace.to_param,
          project_id: project.to_param,
          id: merge_request.iid,
          format: format
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    end

    context 'as html' do
      it 'renders the show template' do
        go

        expect(response).to render_template('show')
      end
    end

    context 'as json' do
      it 'renders the commits template to a string' do
        go format: 'json'

        expect(response).to render_template('projects/merge_requests/show/_commits')
        expect(JSON.parse(response.body)).to have_key('html')
      end
238 239
    end
  end
240
end