Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git_audit_event_spec.rb « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c533b39f550c512170fdefb03b96303a18b94823 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GitAuditEvent, feature_category: :source_code_management do
  let_it_be(:player) { create(:user) }
  let_it_be(:group) { create(:group, :public) }
  let_it_be(:project) { create(:project) }

  subject { described_class.new(player, project) }

  describe '#send_audit_event' do
    let(:msg) { 'valid_msg' }

    context 'with successfully sending' do
      let_it_be(:project) { create(:project, namespace: group) }

      before do
        allow(::Gitlab::Audit::Auditor).to receive(:audit)
      end

      context 'when player is a regular user' do
        it 'sends git audit event' do
          expect(::Gitlab::Audit::Auditor).to receive(:audit).with(a_hash_including(
            name: 'repository_git_operation',
            stream_only: true,
            author: player,
            scope: project,
            target: project,
            message: msg
          )).once

          subject.send_audit_event(msg)
        end
      end

      context 'when player is ::API::Support::GitAccessActor' do
        let_it_be(:user) { player }
        let_it_be(:key) { create(:key, user: user) }
        let_it_be(:git_access_actor) { ::API::Support::GitAccessActor.new(user: user, key: key) }

        subject { described_class.new(git_access_actor, project) }

        it 'sends git audit event' do
          expect(::Gitlab::Audit::Auditor).to receive(:audit).with(a_hash_including(
            name: 'repository_git_operation',
            stream_only: true,
            author: git_access_actor.deploy_key_or_user,
            scope: project,
            target: project,
            message: msg
          )).once

          subject.send_audit_event(msg)
        end
      end
    end

    context 'when user is blank' do
      let_it_be(:player) { nil }

      it 'does not send git audit event' do
        expect(::Gitlab::Audit::Auditor).not_to receive(:audit)

        subject.send_audit_event(msg)
      end
    end

    context 'when project is blank' do
      let_it_be(:project) { nil }

      it 'does not send git audit event' do
        expect(::Gitlab::Audit::Auditor).not_to receive(:audit)

        subject.send_audit_event(msg)
      end
    end
  end
end