commits_spec.rb 52.4 KB
Newer Older
1 2 3
require 'spec_helper'
require 'mime/types'

4
describe API::Commits do
5 6
  include ProjectForksHelper

7
  let(:user) { create(:user) }
8 9 10 11 12 13 14
  let(:guest) { create(:user).tap { |u| project.add_guest(u) } }
  let(:project) { create(:project, :repository, creator: user, path: 'my.project') }
  let(:branch_with_dot) { project.repository.find_branch('ends-with.json') }
  let(:branch_with_slash) { project.repository.find_branch('improve/awesome') }

  let(:project_id) { project.id }
  let(:current_user) { nil }
15

16
  before do
17
    project.add_maintainer(user)
18
  end
19

20
  describe 'GET /projects/:id/repository/commits' do
21 22
    let(:route) { "/projects/#{project_id}/repository/commits" }

23
    shared_examples_for 'project commits' do |schema: 'public_api/v4/commits'|
24
      it "returns project commits" do
25
        commit = project.repository.commit
26

27
        get api(route, current_user)
28

29
        expect(response).to have_gitlab_http_status(200)
30
        expect(response).to match_response_schema(schema)
31 32 33
        expect(json_response.first['id']).to eq(commit.id)
        expect(json_response.first['committer_name']).to eq(commit.committer_name)
        expect(json_response.first['committer_email']).to eq(commit.committer_email)
34
      end
35 36 37 38

      it 'include correct pagination headers' do
        commit_count = project.repository.count_commits(ref: 'master').to_s

39
        get api(route, current_user)
40 41 42 43 44

        expect(response).to include_pagination_headers
        expect(response.headers['X-Total']).to eq(commit_count)
        expect(response.headers['X-Page']).to eql('1')
      end
45 46
    end

47 48 49 50 51
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'project commits'
    end
52

53 54 55 56
    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
57 58
      end
    end
59

60
    context 'when authenticated', 'as a maintainer' do
61
      let(:current_user) { user }
62

63
      it_behaves_like 'project commits'
64

65 66
      context "since optional parameter" do
        it "returns project commits since provided parameter" do
67
          commits = project.repository.commits("master", limit: 2)
68
          after = commits.second.created_at
69

70
          get api("/projects/#{project_id}/repository/commits?since=#{after.utc.iso8601}", user)
71

72 73 74 75
          expect(json_response.size).to eq 2
          expect(json_response.first["id"]).to eq(commits.first.id)
          expect(json_response.second["id"]).to eq(commits.second.id)
        end
76

77
        it 'include correct pagination headers' do
78
          commits = project.repository.commits("master", limit: 2)
79 80 81 82 83 84 85 86 87
          after = commits.second.created_at
          commit_count = project.repository.count_commits(ref: 'master', after: after).to_s

          get api("/projects/#{project_id}/repository/commits?since=#{after.utc.iso8601}", user)

          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
88
      end
89

90 91
      context "until optional parameter" do
        it "returns project commits until provided parameter" do
92
          commits = project.repository.commits("master", limit: 20)
93
          before = commits.second.created_at
94

95
          get api("/projects/#{project_id}/repository/commits?until=#{before.utc.iso8601}", user)
96

97
          if commits.size == 20
98 99 100 101
            expect(json_response.size).to eq(20)
          else
            expect(json_response.size).to eq(commits.size - 1)
          end
102

103 104 105
          expect(json_response.first["id"]).to eq(commits.second.id)
          expect(json_response.second["id"]).to eq(commits.third.id)
        end
106

107
        it 'include correct pagination headers' do
108
          commits = project.repository.commits("master", limit: 2)
109 110
          before = commits.second.created_at
          commit_count = project.repository.count_commits(ref: 'master', before: before).to_s
111

112
          get api("/projects/#{project_id}/repository/commits?until=#{before.utc.iso8601}", user)
113

114 115 116 117
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
118
      end
119

120 121 122
      context "invalid xmlschema date parameters" do
        it "returns an invalid parameter error message" do
          get api("/projects/#{project_id}/repository/commits?since=invalid-date", user)
123

124
          expect(response).to have_gitlab_http_status(400)
125 126
          expect(json_response['error']).to eq('since is invalid')
        end
127
      end
L
Luis HGO 已提交
128

129 130 131 132
      context "path optional parameter" do
        it "returns project commits matching provided path parameter" do
          path = 'files/ruby/popen.rb'
          commit_count = project.repository.count_commits(ref: 'master', path: path).to_s
L
Luis HGO 已提交
133

134
          get api("/projects/#{project_id}/repository/commits?path=#{path}", user)
L
Luis HGO 已提交
135

136 137 138 139 140
          expect(json_response.size).to eq(3)
          expect(json_response.first["id"]).to eq("570e7b2abdd848b95f2f578043fc23bd6f6fd24d")
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
        end
141

142 143 144
        it 'include correct pagination headers' do
          path = 'files/ruby/popen.rb'
          commit_count = project.repository.count_commits(ref: 'master', path: path).to_s
145

146
          get api("/projects/#{project_id}/repository/commits?path=#{path}", user)
147

148 149 150 151
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eql('1')
        end
152
      end
153

T
Tiago Botelho 已提交
154 155 156 157 158 159 160 161 162 163 164 165
      context 'all optional parameter' do
        it 'returns all project commits' do
          commit_count = project.repository.count_commits(all: true)

          get api("/projects/#{project_id}/repository/commits?all=true", user)

          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count.to_s)
          expect(response.headers['X-Page']).to eql('1')
        end
      end

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      context 'with_stats optional parameter' do
        let(:project) { create(:project, :public, :repository) }

        it_behaves_like 'project commits', schema: 'public_api/v4/commits_with_stats' do
          let(:route) { "/projects/#{project_id}/repository/commits?with_stats=true" }

          it 'include commits details' do
            commit = project.repository.commit
            get api(route, current_user)

            expect(json_response.first['stats']['additions']).to eq(commit.stats.additions)
            expect(json_response.first['stats']['deletions']).to eq(commit.stats.deletions)
            expect(json_response.first['stats']['total']).to eq(commit.stats.total)
          end
        end
      end

183 184 185 186 187 188 189
      context 'with pagination params' do
        let(:page) { 1 }
        let(:per_page) { 5 }
        let(:ref_name) { 'master' }
        let!(:request) do
          get api("/projects/#{project_id}/repository/commits?page=#{page}&per_page=#{per_page}&ref_name=#{ref_name}", user)
        end
190

191 192
        it 'returns correct headers' do
          commit_count = project.repository.count_commits(ref: ref_name).to_s
193

194 195 196 197 198 199
          expect(response).to include_pagination_headers
          expect(response.headers['X-Total']).to eq(commit_count)
          expect(response.headers['X-Page']).to eq('1')
          expect(response.headers['Link']).to match(/page=1&per_page=5/)
          expect(response.headers['Link']).to match(/page=2&per_page=5/)
        end
200

201 202 203
        context 'viewing the first page' do
          it 'returns the first 5 commits' do
            commit = project.repository.commit
204

205 206 207 208
            expect(json_response.size).to eq(per_page)
            expect(json_response.first['id']).to eq(commit.id)
            expect(response.headers['X-Page']).to eq('1')
          end
209 210
        end

211 212
        context 'viewing the third page' do
          let(:page) { 3 }
213

214
          it 'returns the third 5 commits' do
215
            commit = project.repository.commits('HEAD', limit: per_page, offset: (page - 1) * per_page).first
216

217 218 219 220
            expect(json_response.size).to eq(per_page)
            expect(json_response.first['id']).to eq(commit.id)
            expect(response.headers['X-Page']).to eq('3')
          end
221 222 223
        end
      end
    end
224 225
  end

226
  describe "POST /projects/:id/repository/commits" do
227
    let!(:url) { "/projects/#{project_id}/repository/commits" }
M
Marc Siegfriedt 已提交
228 229

    it 'returns a 403 unauthorized for user without permissions' do
230
      post api(url, guest)
M
Marc Siegfriedt 已提交
231

232
      expect(response).to have_gitlab_http_status(403)
M
Marc Siegfriedt 已提交
233 234 235 236 237
    end

    it 'returns a 400 bad request if no params are given' do
      post api(url, user)

238
      expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
239 240
    end

241
    describe 'create' do
R
Robert Schilling 已提交
242
      let(:message) { 'Created a new file with a very very looooooooooooooooooooooooooooooooooooooooooooooong commit message' }
243
      let(:invalid_c_params) do
M
Marc Siegfriedt 已提交
244
        {
245
          branch: 'master',
M
Marc Siegfriedt 已提交
246 247 248 249 250 251 252 253 254 255
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            }
          ]
        }
      end
256
      let(:valid_c_params) do
M
Marc Siegfriedt 已提交
257
        {
258
          branch: 'master',
M
Marc Siegfriedt 已提交
259 260 261 262 263 264 265 266 267 268
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            }
          ]
        }
      end
269
      let(:valid_utf8_c_params) do
270 271 272 273 274 275 276 277 278 279 280 281
        {
          branch: 'master',
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 🦊'
            }
          ]
        }
      end
M
Marc Siegfriedt 已提交
282

T
Tiago Botelho 已提交
283
      it 'does not increment the usage counters using access token authentication' do
284
        expect(::Gitlab::UsageDataCounters::WebIdeCommitsCounter).not_to receive(:increment)
T
Tiago Botelho 已提交
285

B
blackst0ne 已提交
286
        post api(url, user), params: valid_c_params
T
Tiago Botelho 已提交
287 288
      end

M
Marc Siegfriedt 已提交
289
      it 'a new file in project repo' do
B
blackst0ne 已提交
290
        post api(url, user), params: valid_c_params
M
Marc Siegfriedt 已提交
291

292
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
293
        expect(json_response['title']).to eq(message)
294 295
        expect(json_response['committer_name']).to eq(user.name)
        expect(json_response['committer_email']).to eq(user.email)
M
Marc Siegfriedt 已提交
296 297
      end

298
      it 'a new file with utf8 chars in project repo' do
B
blackst0ne 已提交
299
        post api(url, user), params: valid_utf8_c_params
300 301 302 303 304 305 306

        expect(response).to have_gitlab_http_status(201)
        expect(json_response['title']).to eq(message)
        expect(json_response['committer_name']).to eq(user.name)
        expect(json_response['committer_email']).to eq(user.email)
      end

M
Marc Siegfriedt 已提交
307
      it 'returns a 400 bad request if file exists' do
B
blackst0ne 已提交
308
        post api(url, user), params: invalid_c_params
M
Marc Siegfriedt 已提交
309

310
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
311
      end
312

313 314
      context 'with project path containing a dot in URL' do
        let(:url) { "/projects/#{CGI.escape(project.full_path)}/repository/commits" }
315 316

        it 'a new file in project repo' do
B
blackst0ne 已提交
317
          post api(url, user), params: valid_c_params
318

319
          expect(response).to have_gitlab_http_status(201)
320 321
        end
      end
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

      context 'when the API user is a guest' do
        def last_commit_id(project, branch_name)
          project.repository.find_branch(branch_name)&.dereferenced_target&.id
        end

        let(:public_project) { create(:project, :public, :repository) }
        let!(:url) { "/projects/#{public_project.id}/repository/commits" }
        let(:guest) { create(:user).tap { |u| public_project.add_guest(u) } }

        it 'returns a 403' do
          post api(url, guest), params: valid_c_params

          expect(response).to have_gitlab_http_status(403)
        end

        context 'when start_project is provided' do
          context 'when posting to a forked project the user owns' do
            let!(:forked_project) { fork_project(public_project, guest, namespace: guest.namespace, repository: true) }
            let!(:url) { "/projects/#{forked_project.id}/repository/commits" }

            before do
              valid_c_params[:start_branch]  = "master"
              valid_c_params[:branch]        = "patch"
            end

            context 'identified by Integer (id)' do
              before do
                valid_c_params[:start_project] = public_project.id
              end

              it 'adds a new commit to forked_project and returns a 201' do
                expect { post api(url, guest), params: valid_c_params }
                  .to change { last_commit_id(forked_project, valid_c_params[:branch]) }
                  .and not_change { last_commit_id(public_project, valid_c_params[:start_branch]) }

                expect(response).to have_gitlab_http_status(201)
              end
            end

            context 'identified by String (full_path)' do
              before do
                valid_c_params[:start_project] = public_project.full_path
              end

              it 'adds a new commit to forked_project and returns a 201' do
                expect { post api(url, guest), params: valid_c_params }
                  .to change { last_commit_id(forked_project, valid_c_params[:branch]) }
                  .and not_change { last_commit_id(public_project, valid_c_params[:start_branch]) }

                expect(response).to have_gitlab_http_status(201)
              end
            end
          end

          context 'when the target project is not part of the fork network of start_project' do
            let(:unrelated_project) { create(:project, :public, :repository, creator: guest) }
            let!(:url) { "/projects/#{unrelated_project.id}/repository/commits" }

            before do
              valid_c_params[:start_branch]  = "master"
              valid_c_params[:branch]        = "patch"
              valid_c_params[:start_project] = public_project.id
            end

            it 'returns a 403' do
              post api(url, guest), params: valid_c_params

              expect(response).to have_gitlab_http_status(403)
            end
          end
        end

        context 'when posting to a forked project the user does not have write access' do
          let!(:forked_project) { fork_project(public_project, user, namespace: user.namespace, repository: true) }
          let!(:url) { "/projects/#{forked_project.id}/repository/commits" }

          before do
            valid_c_params[:start_branch]  = "master"
            valid_c_params[:branch]        = "patch"
            valid_c_params[:start_project] = public_project.id
          end

          it 'returns a 403' do
            post api(url, guest), params: valid_c_params

            expect(response).to have_gitlab_http_status(403)
          end
        end
      end
M
Marc Siegfriedt 已提交
412 413
    end

414
    describe 'delete' do
M
Marc Siegfriedt 已提交
415
      let(:message) { 'Deleted file' }
416
      let(:invalid_d_params) do
M
Marc Siegfriedt 已提交
417
        {
418
          branch: 'markdown',
M
Marc Siegfriedt 已提交
419 420 421 422 423 424 425 426 427
          commit_message: message,
          actions: [
            {
              action: 'delete',
              file_path: 'doc/api/projects.md'
            }
          ]
        }
      end
428
      let(:valid_d_params) do
M
Marc Siegfriedt 已提交
429
        {
430
          branch: 'markdown',
M
Marc Siegfriedt 已提交
431 432 433 434 435 436 437 438 439 440 441
          commit_message: message,
          actions: [
            {
              action: 'delete',
              file_path: 'doc/api/users.md'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
B
blackst0ne 已提交
442
        post api(url, user), params: valid_d_params
M
Marc Siegfriedt 已提交
443

444
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
445 446 447 448
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
B
blackst0ne 已提交
449
        post api(url, user), params: invalid_d_params
M
Marc Siegfriedt 已提交
450

451
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
452 453 454
      end
    end

455
    describe 'move' do
M
Marc Siegfriedt 已提交
456
      let(:message) { 'Moved file' }
457
      let(:invalid_m_params) do
M
Marc Siegfriedt 已提交
458
        {
459
          branch: 'feature',
M
Marc Siegfriedt 已提交
460 461 462 463 464 465 466 467 468 469 470
          commit_message: message,
          actions: [
            {
              action: 'move',
              file_path: 'CHANGELOG',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            }
          ]
        }
      end
471
      let(:valid_m_params) do
M
Marc Siegfriedt 已提交
472
        {
473
          branch: 'feature',
M
Marc Siegfriedt 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486
          commit_message: message,
          actions: [
            {
              action: 'move',
              file_path: 'VERSION.txt',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
B
blackst0ne 已提交
487
        post api(url, user), params: valid_m_params
M
Marc Siegfriedt 已提交
488

489
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
490 491 492 493
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
B
blackst0ne 已提交
494
        post api(url, user), params: invalid_m_params
M
Marc Siegfriedt 已提交
495

496
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
497 498 499
      end
    end

500
    describe 'update' do
M
Marc Siegfriedt 已提交
501
      let(:message) { 'Updated file' }
502
      let(:invalid_u_params) do
M
Marc Siegfriedt 已提交
503
        {
504
          branch: 'master',
M
Marc Siegfriedt 已提交
505 506 507 508 509 510 511 512 513 514
          commit_message: message,
          actions: [
            {
              action: 'update',
              file_path: 'foo/bar.baz',
              content: 'puts 8'
            }
          ]
        }
      end
515
      let(:valid_u_params) do
M
Marc Siegfriedt 已提交
516
        {
517
          branch: 'master',
M
Marc Siegfriedt 已提交
518 519 520 521 522 523 524 525 526 527 528 529
          commit_message: message,
          actions: [
            {
              action: 'update',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            }
          ]
        }
      end

      it 'an existing file in project repo' do
B
blackst0ne 已提交
530
        post api(url, user), params: valid_u_params
M
Marc Siegfriedt 已提交
531

532
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
533 534 535 536
        expect(json_response['title']).to eq(message)
      end

      it 'returns a 400 bad request if file does not exist' do
B
blackst0ne 已提交
537
        post api(url, user), params: invalid_u_params
M
Marc Siegfriedt 已提交
538

539
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
540 541 542
      end
    end

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    describe 'chmod' do
      let(:message) { 'Chmod +x file' }
      let(:file_path) { 'files/ruby/popen.rb' }
      let(:execute_filemode) { true }
      let(:params) do
        {
          branch: 'master',
          commit_message: message,
          actions: [
            {
              action: 'chmod',
              file_path: file_path,
              execute_filemode: execute_filemode
            }
          ]
        }
      end

      it 'responds with success' do
B
blackst0ne 已提交
562
        post api(url, user), params: params
563 564 565 566 567 568 569 570 571

        expect(response).to have_gitlab_http_status(201)
        expect(json_response['title']).to eq(message)
      end

      context 'when execute_filemode is false' do
        let(:execute_filemode) { false }

        it 'responds with success' do
B
blackst0ne 已提交
572
          post api(url, user), params: params
573 574 575 576 577 578 579 580 581 582

          expect(response).to have_gitlab_http_status(201)
          expect(json_response['title']).to eq(message)
        end
      end

      context "when the file doesn't exists" do
        let(:file_path) { 'foo/bar.baz' }

        it "responds with 400" do
B
blackst0ne 已提交
583
          post api(url, user), params: params
584 585 586 587 588 589 590

          expect(response).to have_gitlab_http_status(400)
          expect(json_response['message']).to eq("A file with this name doesn't exist")
        end
      end
    end

591
    describe 'multiple operations' do
M
Marc Siegfriedt 已提交
592
      let(:message) { 'Multiple actions' }
593
      let(:invalid_mo_params) do
M
Marc Siegfriedt 已提交
594
        {
595
          branch: 'master',
M
Marc Siegfriedt 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
            },
            {
              action: 'delete',
              file_path: 'doc/api/projects.md'
            },
            {
              action: 'move',
              file_path: 'CHANGELOG',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            },
            {
              action: 'update',
              file_path: 'foo/bar.baz',
              content: 'puts 8'
617 618 619 620 621
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
M
Marc Siegfriedt 已提交
622 623 624 625
            }
          ]
        }
      end
626
      let(:valid_mo_params) do
M
Marc Siegfriedt 已提交
627
        {
628
          branch: 'master',
M
Marc Siegfriedt 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
          commit_message: message,
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            },
            {
              action: 'delete',
              file_path: 'Gemfile.zip'
            },
            {
              action: 'move',
              file_path: 'VERSION.txt',
              previous_path: 'VERSION',
              content: '6.7.0.pre'
            },
            {
              action: 'update',
              file_path: 'files/ruby/popen.rb',
              content: 'puts 8'
650 651 652 653 654
            },
            {
              action: 'chmod',
              file_path: 'files/ruby/popen.rb',
              execute_filemode: true
M
Marc Siegfriedt 已提交
655 656 657 658 659
            }
          ]
        }
      end

G
George Tsiolis 已提交
660
      it 'are committed as one in project repo' do
B
blackst0ne 已提交
661
        post api(url, user), params: valid_mo_params
M
Marc Siegfriedt 已提交
662

663
        expect(response).to have_gitlab_http_status(201)
M
Marc Siegfriedt 已提交
664 665 666
        expect(json_response['title']).to eq(message)
      end

667
      it 'includes the commit stats' do
B
blackst0ne 已提交
668
        post api(url, user), params: valid_mo_params
669 670 671 672 673 674

        expect(response).to have_gitlab_http_status(201)
        expect(json_response).to include 'stats'
      end

      it "doesn't include the commit stats when stats is false" do
B
blackst0ne 已提交
675
        post api(url, user), params: valid_mo_params.merge(stats: false)
676 677 678 679 680

        expect(response).to have_gitlab_http_status(201)
        expect(json_response).not_to include 'stats'
      end

M
Marc Siegfriedt 已提交
681
      it 'return a 400 bad request if there are any issues' do
B
blackst0ne 已提交
682
        post api(url, user), params: invalid_mo_params
M
Marc Siegfriedt 已提交
683

684
        expect(response).to have_gitlab_http_status(400)
M
Marc Siegfriedt 已提交
685 686
      end
    end
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

    context 'when committing into a fork as a maintainer' do
      include_context 'merge request allowing collaboration'

      let(:project_id) { forked_project.id }

      def push_params(branch_name)
        {
          branch: branch_name,
          commit_message: 'Hello world',
          actions: [
            {
              action: 'create',
              file_path: 'foo/bar/baz.txt',
              content: 'puts 8'
            }
          ]
        }
      end

      it 'allows pushing to the source branch of the merge request' do
B
blackst0ne 已提交
708
        post api(url, user), params: push_params('feature')
709 710 711 712 713

        expect(response).to have_gitlab_http_status(:created)
      end

      it 'denies pushing to another branch' do
B
blackst0ne 已提交
714
        post api(url, user), params: push_params('other-branch')
715 716 717 718

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
M
Marc Siegfriedt 已提交
719 720
  end

R
Robert Schilling 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  describe 'GET /projects/:id/repository/commits/:sha/refs' do
    let(:project) { create(:project, :public, :repository) }
    let(:tag) { project.repository.find_tag('v1.1.0') }
    let(:commit_id) { tag.dereferenced_target.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/refs" }

    context 'when ref does not exist' do
      let(:commit_id) { 'unknown' }

      it_behaves_like '404 response' do
        let(:request) { get api(route, current_user) }
        let(:message) { '404 Commit Not Found' }
      end
    end

    context 'when repository is disabled' do
      include_context 'disabled repository'

739
      it_behaves_like '404 response' do
R
Robert Schilling 已提交
740 741 742 743 744 745
        let(:request) { get api(route, current_user) }
      end
    end

    context 'for a valid commit' do
      it 'returns all refs with no scope' do
B
blackst0ne 已提交
746
        get api(route, current_user), params: { per_page: 100 }
R
Robert Schilling 已提交
747

748 749
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
        refs.concat(project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]})
R
Robert Schilling 已提交
750

751 752 753 754
        expect(response).to have_gitlab_http_status(200)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
755 756 757
      end

      it 'returns all refs' do
B
blackst0ne 已提交
758
        get api(route, current_user), params: { type: 'all', per_page: 100 }
R
Robert Schilling 已提交
759

760 761
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
        refs.concat(project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]})
R
Robert Schilling 已提交
762

763 764
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
765 766 767
      end

      it 'returns the branch refs' do
B
blackst0ne 已提交
768
        get api(route, current_user), params: { type: 'branch', per_page: 100 }
R
Robert Schilling 已提交
769

770
        refs = project.repository.branch_names_contains(commit_id).map {|name| ['branch', name]}
R
Robert Schilling 已提交
771

772 773
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
774 775 776
      end

      it 'returns the tag refs' do
B
blackst0ne 已提交
777
        get api(route, current_user), params: { type: 'tag', per_page: 100 }
R
Robert Schilling 已提交
778

779
        refs = project.repository.tag_names_contains(commit_id).map {|name| ['tag', name]}
R
Robert Schilling 已提交
780

781 782
        expect(response).to have_gitlab_http_status(200)
        expect(json_response.map { |r| [r['type'], r['name']] }.compact).to eq(refs)
R
Robert Schilling 已提交
783 784 785 786
      end
    end
  end

787 788 789 790
  describe 'GET /projects/:id/repository/commits/:sha' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }
D
dixpac 已提交
791

792 793 794 795 796 797
    shared_examples_for 'ref commit' do
      it 'returns the ref last commit' do
        get api(route, current_user)

        expect(response).to have_gitlab_http_status(200)
        expect(response).to match_response_schema('public_api/v4/commit/detail')
798 799 800 801 802 803 804 805 806 807 808 809 810 811
        expect(json_response['id']).to eq(commit.id)
        expect(json_response['short_id']).to eq(commit.short_id)
        expect(json_response['title']).to eq(commit.title)
        expect(json_response['message']).to eq(commit.safe_message)
        expect(json_response['author_name']).to eq(commit.author_name)
        expect(json_response['author_email']).to eq(commit.author_email)
        expect(json_response['authored_date']).to eq(commit.authored_date.iso8601(3))
        expect(json_response['committer_name']).to eq(commit.committer_name)
        expect(json_response['committer_email']).to eq(commit.committer_email)
        expect(json_response['committed_date']).to eq(commit.committed_date.iso8601(3))
        expect(json_response['parent_ids']).to eq(commit.parent_ids)
        expect(json_response['stats']['additions']).to eq(commit.stats.additions)
        expect(json_response['stats']['deletions']).to eq(commit.stats.deletions)
        expect(json_response['stats']['total']).to eq(commit.stats.total)
812
        expect(json_response['status']).to be_nil
813
        expect(json_response['last_pipeline']).to be_nil
814 815
      end

816 817 818 819 820 821 822
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
823
      end
K
Kamil Trzcinski 已提交
824

825 826
      context 'when repository is disabled' do
        include_context 'disabled repository'
827

828
        it_behaves_like '404 response' do
829 830
          let(:request) { get api(route, current_user) }
        end
K
Kamil Trzcinski 已提交
831
      end
832
    end
K
Kamil Trzcinski 已提交
833

834 835 836 837 838 839 840 841 842 843 844
    context 'when stat param' do
      let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }

      it 'is not present return stats by default' do
        get api(route, user)

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to include 'stats'
      end

      it "is false it does not include stats" do
B
blackst0ne 已提交
845
        get api(route, user), params: { stats: false }
846 847 848 849 850 851

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).not_to include 'stats'
      end

      it "is true it includes stats" do
B
blackst0ne 已提交
852
        get api(route, user), params: { stats: true }
853 854 855 856 857 858

        expect(response).to have_gitlab_http_status(200)
        expect(json_response).to include 'stats'
      end
    end

859 860
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }
861

862 863
      it_behaves_like 'ref commit'
    end
864

865 866 867 868
    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
K
Kamil Trzcinski 已提交
869
      end
870
    end
871

872
    context 'when authenticated', 'as a maintainer' do
873
      let(:current_user) { user }
874

875
      it_behaves_like 'ref commit'
876

877 878 879 880 881
      context 'when branch contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref commit'
882
      end
883

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
      context 'when branch contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref commit'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref commit'

        context 'when branch contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref commit'
        end
      end

      context 'when the ref has a pipeline' do
913
        let!(:pipeline) { project.ci_pipelines.create(source: :push, ref: 'master', sha: commit.sha, protected: false) }
914 915 916 917

        it 'includes a "created" status' do
          get api(route, current_user)

918
          expect(response).to have_gitlab_http_status(200)
919 920
          expect(response).to match_response_schema('public_api/v4/commit/detail')
          expect(json_response['status']).to eq('created')
921 922 923 924
          expect(json_response['last_pipeline']['id']).to eq(pipeline.id)
          expect(json_response['last_pipeline']['ref']).to eq(pipeline.ref)
          expect(json_response['last_pipeline']['sha']).to eq(pipeline.sha)
          expect(json_response['last_pipeline']['status']).to eq(pipeline.status)
925 926 927 928 929 930 931 932 933 934
        end

        context 'when pipeline succeeds' do
          before do
            pipeline.update(status: 'success')
          end

          it 'includes a "success" status' do
            get api(route, current_user)

935
            expect(response).to have_gitlab_http_status(200)
936 937 938 939
            expect(response).to match_response_schema('public_api/v4/commit/detail')
            expect(json_response['status']).to eq('success')
          end
        end
940 941 942 943
      end
    end
  end

944 945 946 947 948 949 950 951 952 953
  describe 'GET /projects/:id/repository/commits/:sha/diff' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/diff" }

    shared_examples_for 'ref diff' do
      it 'returns the diff of the selected commit' do
        get api(route, current_user)

        expect(response).to have_gitlab_http_status(200)
954
        expect(response).to include_pagination_headers
955 956
        expect(json_response.size).to be >= 1
        expect(json_response.first.keys).to include 'diff'
957
      end
958

959 960
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }
961

962 963 964 965
        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
966 967
      end

968 969 970
      context 'when repository is disabled' do
        include_context 'disabled repository'

971
        it_behaves_like '404 response' do
972 973
          let(:request) { get api(route, current_user) }
        end
974 975 976
      end
    end

977 978 979 980 981 982 983 984 985 986 987 988 989
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'ref diff'
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
      end
    end

990
    context 'when authenticated', 'as a maintainer' do
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
      let(:current_user) { user }

      it_behaves_like 'ref diff'

      context 'when branch contains a dot' do
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref diff'
      end

      context 'when branch contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref diff'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref diff'

        context 'when branch contains a dot' do
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref diff'
        end
1025
      end
1026 1027 1028 1029 1030 1031

      context 'when binary diff are treated as text' do
        let(:commit_id) { TestEnv::BRANCH_SHA['add-pdf-text-binary'] }

        it_behaves_like 'ref diff'
      end
1032 1033
    end
  end
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  describe 'GET /projects/:id/repository/commits/:sha/comments' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/comments" }

    shared_examples_for 'ref comments' do
      context 'when ref exists' do
        before do
          create(:note_on_commit, author: user, project: project, commit_id: commit.id, note: 'a comment on a commit')
          create(:note_on_commit, author: user, project: project, commit_id: commit.id, note: 'another comment on a commit')
        end

        it 'returns the diff of the selected commit' do
          get api(route, current_user)

          expect(response).to have_gitlab_http_status(200)
          expect(response).to match_response_schema('public_api/v4/commit_notes')
          expect(json_response.size).to eq(2)
          expect(json_response.first['note']).to eq('a comment on a commit')
          expect(json_response.first['author']['id']).to eq(user.id)
        end
1056 1057
      end

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
          let(:message) { '404 Commit Not Found' }
        end
      end

      context 'when repository is disabled' do
        include_context 'disabled repository'

1070
        it_behaves_like '404 response' do
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
          let(:request) { get api(route, current_user) }
        end
      end
    end

    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like 'ref comments'
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
        let(:request) { get api(route) }
        let(:message) { '404 Project Not Found' }
1086 1087 1088
      end
    end

1089
    context 'when authenticated', 'as a maintainer' do
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
      let(:current_user) { user }

      it_behaves_like 'ref comments'

      context 'when branch contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref comments'
      end

      context 'when branch contains a slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
          let(:request) { get api(route, current_user) }
        end
      end

      context 'when branch contains an escaped slash' do
        let(:commit) { project.repository.commit(branch_with_slash.name) }
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref comments'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref comments'

        context 'when branch contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref comments'
        end
1128 1129
      end
    end
1130 1131

    context 'when the commit is present on two projects' do
1132 1133 1134 1135
      let(:forked_project) { create(:project, :repository, creator: guest, namespace: guest.namespace) }
      let!(:forked_project_note) { create(:note_on_commit, author: guest, project: forked_project, commit_id: forked_project.repository.commit.id, note: 'a comment on a commit for fork') }
      let(:project_id) { forked_project.id }
      let(:commit_id) { forked_project.repository.commit.id }
1136 1137

      it 'returns the comments for the target project' do
1138
        get api(route, guest)
1139

1140 1141 1142
        expect(response).to have_gitlab_http_status(200)
        expect(response).to match_response_schema('public_api/v4/commit_notes')
        expect(json_response.size).to eq(1)
1143
        expect(json_response.first['note']).to eq('a comment on a commit for fork')
1144
        expect(json_response.first['author']['id']).to eq(guest.id)
1145 1146
      end
    end
1147 1148
  end

1149
  describe 'POST :id/repository/commits/:sha/cherry_pick' do
1150 1151 1152 1153 1154 1155 1156 1157
    let(:commit) { project.commit('7d3b0f7cff5f37573aea97cebfd5692ea1689924') }
    let(:commit_id) { commit.id }
    let(:branch) { 'master' }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/cherry_pick" }

    shared_examples_for 'ref cherry-pick' do
      context 'when ref exists' do
        it 'cherry-picks the ref commit' do
B
blackst0ne 已提交
1158
          post api(route, current_user), params: { branch: branch }
1159 1160 1161 1162

          expect(response).to have_gitlab_http_status(201)
          expect(response).to match_response_schema('public_api/v4/commit/basic')
          expect(json_response['title']).to eq(commit.title)
1163
          expect(json_response['message']).to eq(commit.cherry_pick_message(user))
1164 1165 1166 1167
          expect(json_response['author_name']).to eq(commit.author_name)
          expect(json_response['committer_name']).to eq(user.name)
        end
      end
1168

1169 1170
      context 'when repository is disabled' do
        include_context 'disabled repository'
1171

1172
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1173
          let(:request) { post api(route, current_user), params: { branch: 'master' } }
1174
        end
1175
      end
1176
    end
1177

1178 1179
    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }
1180

1181
      it_behaves_like '403 response' do
B
blackst0ne 已提交
1182
        let(:request) { post api(route), params: { branch: 'master' } }
1183 1184 1185 1186 1187
      end
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
B
blackst0ne 已提交
1188
        let(:request) { post api(route), params: { branch: 'master' } }
1189
        let(:message) { '404 Project Not Found' }
1190
      end
1191
    end
1192

1193 1194
    context 'when authenticated', 'as an owner' do
      let(:current_user) { user }
1195

1196
      it_behaves_like 'ref cherry-pick'
1197

1198 1199 1200 1201
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
B
blackst0ne 已提交
1202
          let(:request) { post api(route, current_user), params: { branch: 'master' } }
1203 1204 1205 1206 1207 1208 1209 1210
          let(:message) { '404 Commit Not Found' }
        end
      end

      context 'when branch is missing' do
        it_behaves_like '400 response' do
          let(:request) { post api(route, current_user) }
        end
1211 1212
      end

1213 1214 1215
      context 'when branch is empty' do
        ['', ' '].each do |branch|
          it_behaves_like '400 response' do
B
blackst0ne 已提交
1216
            let(:request) { post api(route, current_user), params: { branch: branch } }
1217 1218 1219 1220
          end
        end
      end

1221 1222
      context 'when branch does not exist' do
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1223
          let(:request) { post api(route, current_user), params: { branch: 'foo' } }
1224 1225 1226
          let(:message) { '404 Branch Not Found' }
        end
      end
1227

1228 1229
      context 'when commit is already included in the target branch' do
        it_behaves_like '400 response' do
B
blackst0ne 已提交
1230
          let(:request) { post api(route, current_user), params: { branch: 'markdown' } }
1231
        end
1232 1233
      end

1234 1235 1236
      context 'when ref contains a dot' do
        let(:commit) { project.repository.commit(branch_with_dot.name) }
        let(:commit_id) { branch_with_dot.name }
1237

1238
        it_behaves_like 'ref cherry-pick'
1239 1240
      end

1241 1242
      context 'when ref contains a slash' do
        let(:commit_id) { branch_with_slash.name }
1243

1244
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1245
          let(:request) { post api(route, current_user), params: { branch: 'master' } }
1246
        end
1247 1248
      end

1249 1250
      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }
1251

1252 1253 1254 1255 1256 1257 1258 1259
        it_behaves_like 'ref cherry-pick'

        context 'when ref contains a dot' do
          let(:commit) { project.repository.commit(branch_with_dot.name) }
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref cherry-pick'
        end
1260 1261 1262
      end
    end

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    context 'when authenticated', 'as a developer' do
      let(:current_user) { guest }

      before do
        project.add_developer(guest)
      end

      context 'when branch is protected' do
        before do
          create(:protected_branch, project: project, name: 'feature')
        end

        it 'returns 400 if you are not allowed to push to the target branch' do
B
blackst0ne 已提交
1276
          post api(route, current_user), params: { branch: 'feature' }
1277

1278 1279
          expect(response).to have_gitlab_http_status(:forbidden)
          expect(json_response['message']).to match(/You are not allowed to push into this branch/)
1280
        end
1281 1282
      end
    end
1283 1284 1285 1286 1287 1288 1289

    context 'when cherry picking to a fork as a maintainer' do
      include_context 'merge request allowing collaboration'

      let(:project_id) { forked_project.id }

      it 'allows access from a maintainer that to the source branch' do
B
blackst0ne 已提交
1290
        post api(route, user), params: { branch: 'feature' }
1291 1292 1293 1294 1295

        expect(response).to have_gitlab_http_status(:created)
      end

      it 'denies cherry picking to another branch' do
B
blackst0ne 已提交
1296
        post api(route, user), params: { branch: 'master' }
1297 1298 1299 1300

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
1301 1302
  end

R
Robert Speicher 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311
  describe 'POST :id/repository/commits/:sha/revert' do
    let(:commit_id) { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' }
    let(:commit)    { project.commit(commit_id) }
    let(:branch)    { 'master' }
    let(:route)     { "/projects/#{project_id}/repository/commits/#{commit_id}/revert" }

    shared_examples_for 'ref revert' do
      context 'when ref exists' do
        it 'reverts the ref commit' do
B
blackst0ne 已提交
1312
          post api(route, current_user), params: { branch: branch }
R
Robert Speicher 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

          expect(response).to have_gitlab_http_status(201)
          expect(response).to match_response_schema('public_api/v4/commit/basic')

          expect(json_response['message']).to eq(commit.revert_message(user))
          expect(json_response['author_name']).to eq(user.name)
          expect(json_response['committer_name']).to eq(user.name)
          expect(json_response['parent_ids']).to contain_exactly(commit_id)
        end
      end

      context 'when repository is disabled' do
        include_context 'disabled repository'

1327
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1328
          let(:request) { post api(route, current_user), params: { branch: branch } }
R
Robert Speicher 已提交
1329 1330 1331 1332 1333 1334 1335 1336
        end
      end
    end

    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like '403 response' do
B
blackst0ne 已提交
1337
        let(:request) { post api(route), params: { branch: branch } }
R
Robert Speicher 已提交
1338 1339 1340 1341 1342
      end
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
B
blackst0ne 已提交
1343
        let(:request) { post api(route), params: { branch: branch } }
R
Robert Speicher 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
        let(:message) { '404 Project Not Found' }
      end
    end

    context 'when authenticated', 'as an owner' do
      let(:current_user) { user }

      it_behaves_like 'ref revert'

      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
B
blackst0ne 已提交
1357
          let(:request) { post api(route, current_user), params: { branch: branch } }
R
Robert Speicher 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
          let(:message) { '404 Commit Not Found' }
        end
      end

      context 'when branch is missing' do
        it_behaves_like '400 response' do
          let(:request) { post api(route, current_user) }
        end
      end

      context 'when branch is empty' do
        ['', ' '].each do |branch|
          it_behaves_like '400 response' do
B
blackst0ne 已提交
1371
            let(:request) { post api(route, current_user), params: { branch: branch } }
R
Robert Speicher 已提交
1372 1373 1374 1375 1376 1377
          end
        end
      end

      context 'when branch does not exist' do
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1378
          let(:request) { post api(route, current_user), params: { branch: 'foo' } }
R
Robert Speicher 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
          let(:message) { '404 Branch Not Found' }
        end
      end

      context 'when ref contains a dot' do
        let(:commit_id) { branch_with_dot.name }
        let(:commit) { project.repository.commit(commit_id) }

        it_behaves_like '400 response' do
          let(:request) { post api(route, current_user) }
        end
      end
    end

    context 'when authenticated', 'as a developer' do
      let(:current_user) { user }

      before do
        project.add_developer(user)
      end

      context 'when branch is protected' do
        before do
          create(:protected_branch, project: project, name: 'feature')
        end

        it 'returns 400 if you are not allowed to push to the target branch' do
B
blackst0ne 已提交
1406
          post api(route, current_user), params: { branch: 'feature' }
R
Robert Speicher 已提交
1407 1408 1409 1410 1411 1412 1413 1414

          expect(response).to have_gitlab_http_status(:forbidden)
          expect(json_response['message']).to match(/You are not allowed to push into this branch/)
        end
      end
    end
  end

1415 1416 1417 1418 1419 1420 1421 1422 1423
  describe 'POST /projects/:id/repository/commits/:sha/comments' do
    let(:commit) { project.repository.commit }
    let(:commit_id) { commit.id }
    let(:note) { 'My comment' }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/comments" }

    shared_examples_for 'ref new comment' do
      context 'when ref exists' do
        it 'creates the comment' do
B
blackst0ne 已提交
1424
          post api(route, current_user), params: { note: note }
1425 1426 1427 1428 1429 1430 1431 1432

          expect(response).to have_gitlab_http_status(201)
          expect(response).to match_response_schema('public_api/v4/commit_note')
          expect(json_response['note']).to eq('My comment')
          expect(json_response['path']).to be_nil
          expect(json_response['line']).to be_nil
          expect(json_response['line_type']).to be_nil
        end
1433 1434
      end

1435 1436 1437
      context 'when repository is disabled' do
        include_context 'disabled repository'

1438
        it_behaves_like '404 response' do
B
blackst0ne 已提交
1439
          let(:request) { post api(route, current_user), params: { note: 'My comment' } }
1440 1441 1442 1443 1444 1445 1446 1447
        end
      end
    end

    context 'when unauthenticated', 'and project is public' do
      let(:project) { create(:project, :public, :repository) }

      it_behaves_like '400 response' do
B
blackst0ne 已提交
1448
        let(:request) { post api(route), params: { note: 'My comment' } }
1449 1450 1451 1452 1453
      end
    end

    context 'when unauthenticated', 'and project is private' do
      it_behaves_like '404 response' do
B
blackst0ne 已提交
1454
        let(:request) { post api(route), params: { note: 'My comment' } }
1455 1456 1457 1458 1459 1460 1461 1462 1463
        let(:message) { '404 Project Not Found' }
      end
    end

    context 'when authenticated', 'as an owner' do
      let(:current_user) { user }

      it_behaves_like 'ref new comment'

1464
      it 'returns the inline comment' do
B
blackst0ne 已提交
1465
        post api(route, current_user), params: { note: 'My comment', path: project.repository.commit.raw_diffs.first.new_path, line: 1, line_type: 'new' }
1466

1467 1468
        expect(response).to have_gitlab_http_status(201)
        expect(response).to match_response_schema('public_api/v4/commit_note')
1469
        expect(json_response['note']).to eq('My comment')
1470
        expect(json_response['path']).to eq(project.repository.commit.raw_diffs.first.new_path)
1471
        expect(json_response['line']).to eq(1)
1472
        expect(json_response['line_type']).to eq('new')
1473 1474
      end

1475 1476 1477 1478
      context 'when ref does not exist' do
        let(:commit_id) { 'unknown' }

        it_behaves_like '404 response' do
B
blackst0ne 已提交
1479
          let(:request) { post api(route, current_user), params: { note: 'My comment' } }
1480 1481 1482 1483
          let(:message) { '404 Commit Not Found' }
        end
      end

1484
      it 'returns 400 if note is missing' do
1485 1486 1487
        post api(route, current_user)

        expect(response).to have_gitlab_http_status(400)
1488 1489
      end

1490 1491 1492 1493
      context 'when ref contains a dot' do
        let(:commit_id) { branch_with_dot.name }

        it_behaves_like 'ref new comment'
1494 1495
      end

1496 1497 1498 1499
      context 'when ref contains a slash' do
        let(:commit_id) { branch_with_slash.name }

        it_behaves_like '404 response' do
B
blackst0ne 已提交
1500
          let(:request) { post api(route, current_user), params: { note: 'My comment' } }
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
        end
      end

      context 'when ref contains an escaped slash' do
        let(:commit_id) { CGI.escape(branch_with_slash.name) }

        it_behaves_like 'ref new comment'
      end

      context 'requesting with the escaped project full path' do
        let(:project_id) { CGI.escape(project.full_path) }

        it_behaves_like 'ref new comment'

        context 'when ref contains a dot' do
          let(:commit_id) { branch_with_dot.name }

          it_behaves_like 'ref new comment'
        end
1520 1521 1522
      end
    end
  end
1523 1524

  describe 'GET /projects/:id/repository/commits/:sha/merge_requests' do
1525 1526
    let(:project) { create(:project, :repository, :private) }
    let(:merged_mr) { create(:merge_request, source_project: project, source_branch: 'master', target_branch: 'feature') }
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
    let(:commit) { merged_mr.merge_request_diff.commits.last }

    it 'returns the correct merge request' do
      get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests", user)

      expect(response).to have_gitlab_http_status(200)
      expect(response).to include_pagination_headers
      expect(json_response.length).to eq(1)
      expect(json_response[0]['id']).to eq(merged_mr.id)
    end

    it 'returns 403 for an unauthorized user' do
      project.add_guest(user)

      get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests", user)

      expect(response).to have_gitlab_http_status(403)
    end

    it 'responds 404 when the commit does not exist' do
      get api("/projects/#{project.id}/repository/commits/a7d26f00c35b/merge_requests", user)

      expect(response).to have_gitlab_http_status(404)
    end
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

    context 'public project' do
      let(:project) { create(:project, :repository, :public, :merge_requests_private) }
      let(:non_member) { create(:user) }

      it 'responds 403 when only members are allowed to read merge requests' do
        get api("/projects/#{project.id}/repository/commits/#{commit.id}/merge_requests", non_member)

        expect(response).to have_gitlab_http_status(403)
      end
    end
1562
  end
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600

  describe 'GET /projects/:id/repository/commits/:sha/signature' do
    let!(:project) { create(:project, :repository, :public) }
    let(:project_id) { project.id }
    let(:commit_id) { project.repository.commit.id }
    let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}/signature" }

    context 'when commit does not exist' do
      let(:commit_id) { 'unknown' }

      it_behaves_like '404 response' do
        let(:request) { get api(route, current_user) }
        let(:message) { '404 Commit Not Found' }
      end
    end

    context 'unsigned commit' do
      it_behaves_like '404 response' do
        let(:request) { get api(route, current_user) }
        let(:message) { '404 GPG Signature Not Found'}
      end
    end

    context 'signed commit' do
      let(:commit) { project.repository.commit(GpgHelpers::SIGNED_COMMIT_SHA) }
      let(:commit_id) { commit.id }

      it 'returns correct JSON' do
        get api(route, current_user)

        expect(response).to have_gitlab_http_status(200)
        expect(json_response['gpg_key_id']).to eq(commit.signature.gpg_key_id)
        expect(json_response['gpg_key_subkey_id']).to eq(commit.signature.gpg_key_subkey_id)
        expect(json_response['gpg_key_primary_keyid']).to eq(commit.signature.gpg_key_primary_keyid)
        expect(json_response['verification_status']).to eq(commit.signature.verification_status)
      end
    end
  end
1601
end