summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/hook_data/issuable_builder_spec.rb
blob: 676396697fb7b7e380f159b65ddb9aaab6e8a6b4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::HookData::IssuableBuilder do
  let_it_be(:user) { create(:user) }

  # This shared example requires a `builder` and `user` variable
  shared_examples 'issuable hook data' do |kind, hook_data_issuable_builder_class|
    let(:data) { builder.build(user: user) }

    include_examples 'project hook data' do
      let(:project) { builder.issuable.project }
    end

    include_examples 'deprecated repository hook data'

    context "with a #{kind}" do
      it 'contains issuable data' do
        expect(data[:object_kind]).to eq(kind)
        expect(data[:user]).to eq(user.hook_attrs)
        expect(data[:project]).to eq(builder.issuable.project.hook_attrs)
        expect(data[:object_attributes]).to eq(hook_data_issuable_builder_class.new(issuable).build)
        expect(data[:changes]).to eq({})
        expect(data[:repository]).to eq(builder.issuable.project.hook_attrs.slice(:name, :url, :description, :homepage))
      end

      it 'does not contain certain keys' do
        expect(data).not_to have_key(:assignees)
        expect(data).not_to have_key(:assignee)
      end

      describe 'changes are given' do
        let(:changes) do
          {
            cached_markdown_version: %w[foo bar],
            description: ['A description', 'A cool description'],
            description_html: %w[foo bar],
            in_progress_merge_commit_sha: %w[foo bar],
            lock_version: %w[foo bar],
            merge_jid: %w[foo bar],
            title: ['A title', 'Hello World'],
            title_html: %w[foo bar],
            labels: [
              [{ id: 1, title: 'foo' }],
              [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
            ],
            total_time_spent: [1, 2],
            assignees: [
              [],
              [{
                name: "Foo Bar",
                username: "foobar",
                avatar_url: "http://www.example.com/my-avatar.jpg"
              }]
            ]
          }
        end

        let(:data) { builder.build(user: user, changes: changes) }

        it 'populates the :changes hash' do
          expect(data[:changes]).to match(hash_including({
            title: { previous: 'A title', current: 'Hello World' },
            description: { previous: 'A description', current: 'A cool description' },
            labels: {
              previous: [{ id: 1, title: 'foo' }],
              current: [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
            },
            total_time_spent: {
              previous: 1,
              current: 2
            },
            assignees: {
              previous: [],
                current: [{
                  name: "Foo Bar",
                  username: "foobar",
                  avatar_url: "http://www.example.com/my-avatar.jpg"
                }]
            }
          }))
        end

        it 'does not contain certain keys' do
          expect(data[:changes]).not_to have_key('cached_markdown_version')
          expect(data[:changes]).not_to have_key('description_html')
          expect(data[:changes]).not_to have_key('lock_version')
          expect(data[:changes]).not_to have_key('title_html')
          expect(data[:changes]).not_to have_key('in_progress_merge_commit_sha')
          expect(data[:changes]).not_to have_key('merge_jid')
        end
      end
    end
  end

  describe '#build' do
    it_behaves_like 'issuable hook data', 'issue', Gitlab::HookData::IssueBuilder do
      let(:issuable) { create(:issue, description: 'A description') }
      let(:builder) { described_class.new(issuable) }
    end

    it_behaves_like 'issuable hook data', 'merge_request', Gitlab::HookData::MergeRequestBuilder do
      let(:issuable) { create(:merge_request, description: 'A description') }
      let(:builder) { described_class.new(issuable) }
    end

    context 'issue is assigned' do
      let(:issue) { create(:issue, assignees: [user]) }
      let(:data) { described_class.new(issue).build(user: user) }

      it 'returns correct hook data' do
        expect(data[:object_attributes]['assignee_id']).to eq(user.id)
        expect(data[:assignees].first).to eq(user.hook_attrs)
        expect(data).not_to have_key(:assignee)
      end
    end

    context 'merge_request is assigned' do
      let(:merge_request) { create(:merge_request, assignees: [user]) }
      let(:data) { described_class.new(merge_request).build(user: user) }

      it 'returns correct hook data' do
        expect(data[:object_attributes]['assignee_id']).to eq(user.id)
        expect(data[:assignees].first).to eq(user.hook_attrs)
        expect(data).not_to have_key(:assignee)
      end
    end
  end
end