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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AwardEmoji do
describe 'Associations' do
it { is_expected.to belong_to(:awardable) }
it { is_expected.to belong_to(:user) }
end
describe 'modules' do
it { is_expected.to include_module(Participable) }
end
describe "validations" do
it { is_expected.to validate_presence_of(:awardable) }
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:name) }
# To circumvent a bug in the shoulda matchers
describe "scoped uniqueness validation" do
it "rejects duplicate award emoji" do
user = create(:user)
issue = create(:issue)
create(:award_emoji, user: user, awardable: issue)
new_award = build(:award_emoji, user: user, awardable: issue)
expect(new_award).not_to be_valid
end
# Assume User A and User B both created award emoji of the same name
# on the same awardable. When User A is deleted, User A's award emoji
# is moved to the ghost user. When User B is deleted, User B's award emoji
# also needs to be moved to the ghost user - this cannot happen unless
# the uniqueness validation is disabled for ghost users.
it "allows duplicate award emoji for ghost users" do
user = create(:user, :ghost)
issue = create(:issue)
create(:award_emoji, user: user, awardable: issue)
new_award = build(:award_emoji, user: user, awardable: issue)
expect(new_award).to be_valid
end
# Similar to allowing duplicate award emojis for ghost users,
# when Importing a project that has duplicate award emoji placed by
# ghost user we change the author to be importer user and allow
# duplicates, otherwise relation containing such duplicates
# fails to be created
context 'when importing' do
it 'allows duplicate award emoji' do
user = create(:user)
issue = create(:issue)
create(:award_emoji, user: user, awardable: issue)
new_award = build(:award_emoji, user: user, awardable: issue, importing: true)
expect(new_award).to be_valid
end
end
end
context 'custom emoji' do
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:emoji) { create(:custom_emoji, name: 'partyparrot', namespace: group) }
before do
group.add_maintainer(user)
end
%i[issue merge_request note_on_issue snippet].each do |awardable_type|
let_it_be(:project) { create(:project, namespace: group) }
let(:awardable) { create(awardable_type, project: project) }
it "is accepted on #{awardable_type}" do
new_award = build(:award_emoji, user: user, awardable: awardable, name: emoji.name)
expect(new_award).to be_valid
end
end
it 'is accepted on subgroup issue' do
subgroup = create(:group, parent: group)
project = create(:project, namespace: subgroup)
issue = create(:issue, project: project)
new_award = build(:award_emoji, user: user, awardable: issue, name: emoji.name)
expect(new_award).to be_valid
end
it 'is not supported on personal snippet (yet)' do
snippet = create(:personal_snippet)
new_award = build(:award_emoji, user: snippet.author, awardable: snippet, name: 'null')
expect(new_award).not_to be_valid
end
end
end
describe 'scopes' do
let_it_be(:thumbsup) { create(:award_emoji, name: 'thumbsup') }
let_it_be(:thumbsdown) { create(:award_emoji, name: 'thumbsdown') }
describe '.upvotes' do
it { expect(described_class.upvotes).to contain_exactly(thumbsup) }
end
describe '.downvotes' do
it { expect(described_class.downvotes).to contain_exactly(thumbsdown) }
end
describe '.named' do
it { expect(described_class.named('thumbsup')).to contain_exactly(thumbsup) }
it { expect(described_class.named(%w[thumbsup thumbsdown])).to contain_exactly(thumbsup, thumbsdown) }
end
describe '.awarded_by' do
it { expect(described_class.awarded_by(thumbsup.user)).to contain_exactly(thumbsup) }
it { expect(described_class.awarded_by([thumbsup.user, thumbsdown.user])).to contain_exactly(thumbsup, thumbsdown) }
end
end
describe 'expiring ETag cache' do
context 'on a note' do
let(:note) { create(:note_on_issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: note) }
it 'calls expire_etag_cache on the note when saved' do
expect(note).to receive(:expire_etag_cache)
award_emoji.save!
end
it 'calls expire_etag_cache on the note when destroyed' do
expect(note).to receive(:expire_etag_cache)
award_emoji.destroy!
end
end
context 'on another awardable' do
let(:issue) { create(:issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: issue) }
it 'does not call expire_etag_cache on the issue when saved' do
expect(issue).not_to receive(:expire_etag_cache)
award_emoji.save!
end
it 'does not call expire_etag_cache on the issue when destroyed' do
expect(issue).not_to receive(:expire_etag_cache)
award_emoji.destroy!
end
end
end
describe 'bumping updated at' do
let(:note) { create(:note_on_issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: note) }
it 'calls bump_updated_at on the note when saved' do
expect(note).to receive(:bump_updated_at)
award_emoji.save!
end
it 'calls bump_updated_at on the note when destroyed' do
expect(note).to receive(:bump_updated_at)
award_emoji.destroy!
end
context 'on another awardable' do
let(:issue) { create(:issue) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: issue) }
it 'does not error out when saved' do
expect { award_emoji.save! }.not_to raise_error
end
it 'does not error out when destroy' do
expect { award_emoji.destroy! }.not_to raise_error
end
end
end
describe '.award_counts_for_user' do
let(:user) { create(:user) }
before do
create(:award_emoji, user: user, name: 'thumbsup')
create(:award_emoji, user: user, name: 'thumbsup')
create(:award_emoji, user: user, name: 'thumbsdown')
create(:award_emoji, user: user, name: '+1')
end
it 'returns the awarded emoji in descending order' do
awards = described_class.award_counts_for_user(user)
expect(awards).to eq('thumbsup' => 2, 'thumbsdown' => 1, '+1' => 1)
end
it 'limits the returned number of rows' do
awards = described_class.award_counts_for_user(user, 1)
expect(awards).to eq('thumbsup' => 2)
end
end
describe 'updating upvotes_count' do
context 'on an issue' do
let(:issue) { create(:issue) }
let(:upvote) { build(:award_emoji, :upvote, user: build(:user), awardable: issue) }
let(:downvote) { build(:award_emoji, :downvote, user: build(:user), awardable: issue) }
it 'updates upvotes_count on the issue when saved' do
expect(issue).to receive(:update_column).with(:upvotes_count, 1).once
upvote.save!
downvote.save!
end
it 'updates upvotes_count on the issue when destroyed' do
expect(issue).to receive(:update_column).with(:upvotes_count, 0).once
upvote.destroy!
downvote.destroy!
end
end
context 'on another awardable' do
let(:merge_request) { create(:merge_request) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: merge_request) }
it 'does not update upvotes_count on the merge_request when saved' do
expect(merge_request).not_to receive(:update_column)
award_emoji.save!
end
it 'does not update upvotes_count on the merge_request when destroyed' do
expect(merge_request).not_to receive(:update_column)
award_emoji.destroy!
end
end
end
describe '#url' do
let_it_be(:custom_emoji) { create(:custom_emoji) }
let_it_be(:project) { create(:project, namespace: custom_emoji.group) }
let_it_be(:issue) { create(:issue, project: project) }
def build_award(name)
build(:award_emoji, awardable: issue, name: name)
end
it 'is nil for built-in emoji' do
new_award = build_award('tada')
count = ActiveRecord::QueryRecorder.new do
expect(new_award.url).to be_nil
end.count
expect(count).to be_zero
end
it 'is nil for unrecognized emoji' do
new_award = build_award('null')
expect(new_award.url).to be_nil
end
it 'is set for custom emoji' do
new_award = build_award(custom_emoji.name)
expect(new_award.url).to eq(custom_emoji.url)
end
context 'feature flag disabled' do
before do
stub_feature_flags(custom_emoji: false)
end
it 'does not query' do
new_award = build_award(custom_emoji.name)
expect(ActiveRecord::QueryRecorder.new { new_award.url }.count).to be_zero
end
end
end
describe '#to_ability_name' do
let(:merge_request) { create(:merge_request) }
let(:award_emoji) { build(:award_emoji, user: build(:user), awardable: merge_request) }
it 'returns correct ability name' do
expect(award_emoji.to_ability_name).to be('emoji')
end
end
end
|