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

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

require 'spec_helper'

RSpec.describe Gitlab::HookData::KeyBuilder do
  let_it_be(:personal_key) { create(:personal_key) }
  let_it_be(:other_key) { create(:key) }

  describe '#build' do
    let(:data) { described_class.new(key).build(event) }
    let(:event_name) { data[:event_name] }
    let(:common_attributes) do
      [
        :event_name, :created_at, :updated_at, :key, :id
      ]
    end

    shared_examples_for 'includes the required attributes' do
      it 'includes the required attributes' do
        expect(data.keys).to contain_exactly(*attributes)

        expect(data[:key]).to eq(key.key)
        expect(data[:id]).to eq(key.id)
        expect(data[:created_at]).to eq(key.created_at.xmlschema)
        expect(data[:updated_at]).to eq(key.updated_at.xmlschema)
      end
    end

    context 'for keys that belong to a user' do
      let(:key) { personal_key }
      let(:attributes) { common_attributes.append(:username) }

      context 'data' do
        context 'on create' do
          let(:event) { :create }

          it { expect(event_name).to eq('key_create') }
          it { expect(data[:username]).to eq(key.user.username) }

          it_behaves_like 'includes the required attributes'
        end

        context 'on destroy' do
          let(:event) { :destroy }

          it { expect(event_name).to eq('key_destroy') }
          it { expect(data[:username]).to eq(key.user.username) }

          it_behaves_like 'includes the required attributes'
        end
      end
    end

    context 'for keys that do not belong to a user' do
      let(:key) { other_key }
      let(:attributes) { common_attributes }

      context 'data' do
        context 'on create' do
          let(:event) { :create }

          it { expect(event_name).to eq('key_create') }

          it_behaves_like 'includes the required attributes'
        end

        context 'on destroy' do
          let(:event) { :destroy }

          it { expect(event_name).to eq('key_destroy') }

          it_behaves_like 'includes the required attributes'
        end
      end
    end
  end
end