merge_request_compliance_entity_spec.rb 3.34 KB
Newer Older
1 2 3 4
# frozen_string_literal: true

require 'spec_helper'

5
RSpec.describe MergeRequestComplianceEntity do
6 7 8 9
  include Gitlab::Routing

  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }
10
  let_it_be(:merge_request) { create(:merge_request, :merged) }
11 12

  let(:request) { double('request', current_user: user, project: project) }
13
  let(:entity) { described_class.new(merge_request.reload, request: request) }
14 15 16 17 18 19

  describe '.as_json' do
    subject { entity.as_json }

    it 'includes merge request attributes for compliance' do
      expect(subject).to include(
20 21 22 23 24 25 26 27 28 29 30 31
        :id,
        :title,
        :merged_at,
        :milestone,
        :path,
        :issuable_reference,
        :author,
        :approved_by_users,
        :approval_status,
        :target_branch,
        :target_branch_uri,
        :source_branch,
32 33
        :source_branch_uri,
        :compliance_management_framework
34 35 36
      )
    end

37
    describe 'with an approver' do
38
      let_it_be(:approver) { create(:user) }
39
      let_it_be(:approval) { create :approval, merge_request: merge_request, user: approver }
40

41
      before_all do
42 43 44 45 46 47 48 49 50 51 52 53 54
        project.add_developer(approver)
      end

      it 'includes only set of approver details' do
        expect(subject[:approved_by_users].count).to eq(1)
      end

      it 'includes approver user details' do
        expect(subject[:approved_by_users][0][:id]).to eq(approver.id)
      end
    end

    describe 'with a head pipeline' do
55
      let_it_be(:pipeline) { create(:ci_empty_pipeline, status: :success, project: project, head_pipeline_of: merge_request) }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

      describe 'and the user cannot read the pipeline' do
        it 'does not include pipeline status attribute' do
          expect(subject).not_to have_key(:pipeline_status)
        end
      end

      describe 'and the user can read the pipeline' do
        before do
          project.add_developer(user)
        end

        it 'includes pipeline status attribute' do
          expect(subject).to have_key(:pipeline_status)
        end
      end
    end
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    context 'with an approval status' do
      let_it_be(:committers_approval_enabled) { false }
      let_it_be(:authors_approval_enabled) { false }
      let_it_be(:approvals_required) { 2 }

      shared_examples 'the approval status' do
        before do
          allow(merge_request).to receive(:authors_can_approve?).and_return(authors_approval_enabled)
          allow(merge_request).to receive(:committers_can_approve?).and_return(committers_approval_enabled)
          allow(merge_request).to receive(:approvals_required).and_return(approvals_required)
        end

        it 'is correct' do
          expect(subject[:approval_status]).to eq(status)
        end
      end

      context 'all approval checks pass' do
        let_it_be(:status) { :success }

        it_behaves_like 'the approval status'
      end

      context 'only some of the approval checks pass' do
        let_it_be(:authors_approval_enabled) { true }
        let_it_be(:status) { :warning }

        it_behaves_like 'the approval status'
      end

      context 'none of the approval checks pass' do
        let_it_be(:committers_approval_enabled) { true }
        let_it_be(:authors_approval_enabled) { true }
        let_it_be(:approvals_required) { 0 }
        let_it_be(:status) { :failed }

        it_behaves_like 'the approval status'
      end
    end
113 114
  end
end