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

build_service_spec.rb « audit_events « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 575ec9e58b8eeda3fd44295f8b8d1bde61309404 (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AuditEvents::BuildService, feature_category: :audit_events do
  let(:author) { build_stubbed(:author, current_sign_in_ip: '127.0.0.1') }
  let(:deploy_token) { build_stubbed(:deploy_token, user: author) }
  let(:scope) { build_stubbed(:group) }
  let(:target) { build_stubbed(:project) }
  let(:ip_address) { '192.168.8.8' }
  let(:message) { 'Added an interesting field from project Gotham' }
  let(:additional_details) { { action: :custom } }

  subject(:service) do
    described_class.new(
      author: author,
      scope: scope,
      target: target,
      message: message,
      additional_details: additional_details,
      ip_address: ip_address
    )
  end

  describe '#execute', :request_store do
    subject(:event) { service.execute }

    before do
      allow(Gitlab::RequestContext.instance).to receive(:client_ip).and_return(ip_address)
    end

    it 'sets correct attributes', :aggregate_failures do
      freeze_time do
        expect(event).to have_attributes(
          author_id: author.id,
          author_name: author.name,
          entity_id: scope.id,
          entity_type: scope.class.name)

        expect(event.details).to eq(
          author_name: author.name,
          author_class: author.class.name,
          target_id: target.id,
          target_type: target.class.name,
          target_details: target.name,
          custom_message: message,
          action: :custom)

        expect(event.ip_address).to be_nil
        expect(event.created_at).to eq(DateTime.current)
      end
    end

    context 'when IP address is not provided' do
      let(:ip_address) { nil }

      it 'uses author current_sign_in_ip' do
        expect(event.ip_address).to be_nil
      end
    end

    context 'when overriding target details' do
      subject(:service) do
        described_class.new(
          author: author,
          scope: scope,
          target: target,
          message: message,
          target_details: "This is my target details"
        )
      end

      it 'uses correct target details' do
        expect(event.target_details).to eq("This is my target details")
      end
    end

    context 'when deploy token is passed as author' do
      let(:service) do
        described_class.new(
          author: deploy_token,
          scope: scope,
          target: target,
          message: message
        )
      end

      it 'expect author to be user' do
        expect(event.author_id).to eq(-2)
        expect(event.author_name).to eq(deploy_token.name)
      end
    end

    context 'when deploy key is passed as author' do
      let(:deploy_key) { build_stubbed(:deploy_key, user: author) }

      let(:service) do
        described_class.new(
          author: deploy_key,
          scope: scope,
          target: target,
          message: message
        )
      end

      it 'expect author to be deploy key' do
        expect(event.author_id).to eq(-3)
        expect(event.author_name).to eq(deploy_key.name)
      end
    end

    context 'when author is passed as UnauthenticatedAuthor' do
      let(:service) do
        described_class.new(
          author: ::Gitlab::Audit::UnauthenticatedAuthor.new,
          scope: scope,
          target: target,
          message: message
        )
      end

      it 'sets author as unauthenticated user' do
        expect(event.author).to be_an_instance_of(::Gitlab::Audit::UnauthenticatedAuthor)
        expect(event.author_name).to eq('An unauthenticated user')
      end
    end

    context 'when attributes are missing' do
      context 'when author is missing' do
        let(:author) { nil }

        it { expect { service }.to raise_error(described_class::MissingAttributeError) }
      end

      context 'when scope is missing' do
        let(:scope) { nil }

        it { expect { service }.to raise_error(described_class::MissingAttributeError) }
      end

      context 'when target is missing' do
        let(:target) { nil }

        it { expect { service }.to raise_error(described_class::MissingAttributeError) }
      end

      context 'when message is missing' do
        let(:message) { nil }

        it { expect { service }.to raise_error(described_class::MissingAttributeError) }
      end
    end
  end
end