summaryrefslogtreecommitdiff
path: root/spec/requests/api/merge_request_approvals_spec.rb
blob: fad5c3fb60ec301482ee50b8c6d1c07d03a0f2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::MergeRequestApprovals do
  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :public, :repository, creator: user, namespace: user.namespace) }
  let_it_be(:approver) { create :user }
  let_it_be(:group) { create :group }

  let(:merge_request) { create(:merge_request, :simple, author: user, source_project: project) }

  describe 'GET :id/merge_requests/:merge_request_iid/approvals' do
    it 'retrieves the approval status' do
      project.add_developer(approver)
      project.add_developer(create(:user))

      create(:approval, user: approver, merge_request: merge_request)

      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/approvals", user)

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

  describe 'POST :id/merge_requests/:merge_request_iid/approve' do
    context 'as a valid approver' do
      let_it_be(:approver) { create(:user) }

      before do
        project.add_developer(approver)
        project.add_developer(create(:user))
      end

      def approve(extra_params = {})
        post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/approve", approver), params: extra_params
      end

      context 'when the sha param is not set' do
        it 'approves the merge request' do
          approve

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

      context 'when the sha param is correct' do
        it 'approves the merge request' do
          approve(sha: merge_request.diff_head_sha)

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

      context 'when the sha param is incorrect' do
        it 'does not approve the merge request' do
          approve(sha: merge_request.diff_head_sha.reverse)

          expect(response).to have_gitlab_http_status(:conflict)
          expect(merge_request.approvals).to be_empty
        end
      end
    end
  end

  describe 'POST :id/merge_requests/:merge_request_iid/unapprove' do
    context 'as a user who has approved the merge request' do
      it 'unapproves the merge request' do
        unapprover = create(:user)

        project.add_developer(approver)
        project.add_developer(unapprover)
        project.add_developer(create(:user))

        create(:approval, user: approver, merge_request: merge_request)
        create(:approval, user: unapprover, merge_request: merge_request)

        post api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/unapprove", unapprover)

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