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

event_entity_spec.rb « profile « serializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3dade4210b3c49efa36be7e377ed2a33a30fac14 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Profile::EventEntity, feature_category: :user_profile do
  let_it_be(:group) { create(:group) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:project) { build(:project_empty_repo, group: group) }
  let_it_be(:user) { create(:user) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
  let_it_be(:note) { build(:note_on_merge_request, noteable: merge_request, project: project) }

  let(:target_user) { user }
  let(:event) { build(:event, :merged, author: user, project: project, target: merge_request) }
  let(:request) { double(described_class, current_user: user, target_user: target_user) } # rubocop:disable RSpec/VerifiedDoubles
  let(:entity) { described_class.new(event, request: request) }

  subject { entity.as_json }

  before do
    group.add_maintainer(user)
  end

  it 'exposes fields', :aggregate_failures do
    expect(subject[:created_at]).to eq(event.created_at)
    expect(subject[:action]).to eq(event.action)
    expect(subject[:author][:id]).to eq(target_user.id)
    expect(subject[:author][:name]).to eq(target_user.name)
    expect(subject[:author][:username]).to eq(target_user.username)
  end

  context 'for push events' do
    let_it_be(:commit_from) { Gitlab::Git::BLANK_SHA }
    let_it_be(:commit_title) { 'My commit' }
    let(:event) { build(:push_event, project: project, author: target_user) }

    it 'exposes ref fields' do
      build(:push_event_payload, event: event, ref_count: 3)

      expect(subject[:ref][:type]).to eq(event.ref_type)
      expect(subject[:ref][:count]).to eq(event.ref_count)
      expect(subject[:ref][:name]).to eq(event.ref_name)
      expect(subject[:ref][:path]).to be_nil
      expect(subject[:ref][:is_new]).to be false
      expect(subject[:ref][:is_removed]).to be false
    end

    shared_examples 'returns ref path' do
      specify do
        expect(subject[:ref][:path]).to be_present
      end
    end

    context 'with tag' do
      before do
        allow(project.repository).to receive(:tag_exists?).and_return(true)
        build(:push_event_payload, event: event, ref_type: :tag)
      end

      it_behaves_like 'returns ref path'
    end

    context 'with branch' do
      before do
        allow(project.repository).to receive(:branch_exists?).and_return(true)
        build(:push_event_payload, event: event, ref_type: :branch)
      end

      it_behaves_like 'returns ref path'
    end

    it 'exposes commit fields' do
      build(:push_event_payload, event: event, commit_title: commit_title, commit_from: commit_from, commit_count: 2)

      compare_path = "/#{group.path}/#{project.path}/-/compare/#{commit_from}...#{event.commit_to}"
      expect(subject[:commit][:compare_path]).to eq(compare_path)
      expect(event.commit_id).to include(subject[:commit][:truncated_sha])
      expect(subject[:commit][:path]).to be_present
      expect(subject[:commit][:title]).to eq(commit_title)
      expect(subject[:commit][:count]).to eq(2)
      expect(commit_from).to include(subject[:commit][:from_truncated_sha])
      expect(event.commit_to).to include(subject[:commit][:to_truncated_sha])
      expect(subject[:commit][:create_mr_path]).to be_nil
    end

    it 'exposes create_mr_path' do
      allow(project).to receive(:default_branch).and_return('main')
      allow(project.repository).to receive(:branch_exists?).and_return(true)
      build(:push_event_payload, event: event, action: :created, commit_from: commit_from, commit_count: 2)

      new_mr_path = "/#{group.path}/#{project.path}/-/merge_requests/new?" \
                    "merge_request%5Bsource_branch%5D=#{event.branch_name}"
      expect(subject[:commit][:create_mr_path]).to eq(new_mr_path)
    end
  end

  context 'for noteable events' do
    let(:event) { build(:event, :commented, project: project, target: note, author: target_user) }

    it 'exposes noteable fields' do
      expect(subject[:noteable][:type]).to eq(note.noteable_type)
      expect(subject[:noteable][:reference_link_text]).to eq(note.noteable.reference_link_text)
      expect(subject[:noteable][:web_url]).to be_present
      expect(subject[:noteable][:first_line_in_markdown]).to be_present
    end
  end

  context 'with target' do
    context 'when target does not responds to :reference_link_text' do
      let(:event) { build(:event, :commented, project: project, target: note, author: target_user) }

      it 'exposes target fields' do
        expect(subject[:target]).not_to include(:reference_link_text)
        expect(subject[:target][:type]).to eq(note.class.to_s)
        expect(subject[:target][:web_url]).to be_present
        expect(subject[:target][:title]).to eq(note.title)
      end
    end

    context 'when target responds to :reference_link_text' do
      it 'exposes reference_link_text' do
        expect(subject[:target][:reference_link_text]).to eq(merge_request.reference_link_text)
      end
    end

    context 'when target is a wiki page' do
      let(:event) { build(:wiki_page_event, :created, project: project, author: target_user) }

      it 'exposes web_url' do
        expect(subject[:target][:web_url]).to be_present
      end
    end

    context 'when target is a work item' do
      let(:incident) { create(:work_item, :incident, author: target_user, project: project) } # rubocop:disable RSpec/FactoryBot/AvoidCreate
      let(:event) do
        build(:event, :created, :for_work_item, author: target_user, project: project, target: incident)
      end

      it 'exposes `issue_type`' do
        expect(subject[:target][:issue_type]).to eq('incident')
      end
    end

    context 'when target is an issue' do
      let(:issue) { build_stubbed(:issue, author: target_user, project: project) }
      let(:event) do
        build(:event, :created, author: target_user, project: project, target: issue)
      end

      it 'exposes `issue_type`' do
        expect(subject[:target][:issue_type]).to eq('issue')
      end
    end
  end

  context 'without target' do
    let(:event) do
      build(:event, :destroyed, author: user, project: project, target_type: Milestone.to_s)
    end

    it 'only exposes target.type' do
      expect(subject[:target][:type]).to eq(Milestone.to_s)
      expect(subject[:target]).not_to include(:web_url)
    end
  end

  context 'with resource parent' do
    it 'exposes resource parent fields' do
      resource_parent = event.resource_parent

      expect(subject[:resource_parent][:type]).to eq('project')
      expect(subject[:resource_parent][:full_name]).to eq(resource_parent.full_name)
      expect(subject[:resource_parent][:full_path]).to eq(resource_parent.full_path)
    end
  end

  context 'for private events' do
    let(:event) { build(:event, :merged, author: target_user) }

    context 'when include_private_contributions? is true' do
      let(:target_user) { build(:user, include_private_contributions: true) }

      it 'exposes only created_at, action, and author', :aggregate_failures do
        expect(subject[:created_at]).to eq(event.created_at)
        expect(subject[:action]).to eq('private')
        expect(subject[:author][:id]).to eq(target_user.id)
        expect(subject[:author][:name]).to eq(target_user.name)
        expect(subject[:author][:username]).to eq(target_user.username)

        is_expected.not_to include(:ref, :commit, :target, :resource_parent)
      end
    end

    context 'when include_private_contributions? is false' do
      let(:target_user) { build(:user, include_private_contributions: false) }

      it { is_expected.to be_empty }
    end
  end
end