Skip to content

Commit d0b039a

Browse files
committed
Memorize use of configuration in Policy
1 parent 031d7d1 commit d0b039a

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

app/policies/request_for_comment_policy.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class RequestForCommentPolicy < ApplicationPolicy
4+
REPORT_RECEIVER_CONFIGURED = CodeOcean::Config.new(:code_ocean).read.dig(:content_moderation, :report_emails).present?
5+
46
def create?
57
everyone
68
end
@@ -42,15 +44,11 @@ def rfcs_with_my_comments?
4244
end
4345

4446
def report?
45-
report_receiver_configured? && show? && !author?
47+
REPORT_RECEIVER_CONFIGURED && show? && !author?
4648
end
4749

4850
private
4951

50-
def report_receiver_configured?
51-
CodeOcean::Config.new(:code_ocean).read.dig(:content_moderation, :report_emails).present?
52-
end
53-
5452
def rfc_visibility
5553
# The consumer with the most restricted visibility determines the visibility of the RfC
5654
case [@user.consumer.rfc_visibility, @record.author.consumer.rfc_visibility]

spec/policies/request_for_comment_policy_spec.rb

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
RSpec.describe RequestForCommentPolicy do
66
subject(:policy) { described_class }
77

8-
let(:report_emails) { [] }
8+
let(:reports_enabled) { false }
99

1010
before do
11-
codeocean_config = instance_double(CodeOcean::Config)
12-
allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config)
13-
allow(codeocean_config).to receive(:read).and_return({
14-
content_moderation: {report_emails:},
15-
})
11+
stub_const('RequestForCommentPolicy::REPORT_RECEIVER_CONFIGURED', reports_enabled)
1612
end
1713

1814
context 'when the RfC visibility is not considered' do
@@ -59,7 +55,7 @@
5955

6056
permissions(:report?) do
6157
context 'when report emails are configured' do
62-
let(:report_emails) { ['[email protected]'] }
58+
let(:reports_enabled) { true }
6359

6460
it 'allows anyone to report RfCs' do
6561
%i[admin external_user teacher].each do |factory_name|
@@ -69,8 +65,6 @@
6965
end
7066

7167
context 'when no report email is configured' do
72-
let(:report_emails) { [] }
73-
7468
it 'does not allow reports from anyone' do
7569
%i[admin external_user teacher].each do |factory_name|
7670
expect(policy).not_to permit(create(factory_name), RequestForComment.new)
@@ -172,7 +166,7 @@
172166
end
173167

174168
permissions(:report?) do
175-
let(:report_emails) { ['[email protected]'] }
169+
let(:reports_enabled) { true }
176170

177171
it_behaves_like 'grants access to everyone', {block_author: true}
178172
end
@@ -189,7 +183,7 @@
189183
end
190184

191185
permissions(:report?) do
192-
let(:report_emails) { ['[email protected]'] }
186+
let(:reports_enabled) { true }
193187

194188
it_behaves_like 'grants access to admins only'
195189
end
@@ -206,7 +200,7 @@
206200
end
207201

208202
permissions(:report?) do
209-
let(:report_emails) { ['[email protected]'] }
203+
let(:reports_enabled) { true }
210204

211205
it_behaves_like 'grants access to admins only'
212206
end
@@ -230,7 +224,7 @@
230224
end
231225

232226
permissions(:report?) do
233-
let(:report_emails) { ['[email protected]'] }
227+
let(:reports_enabled) { true }
234228

235229
it_behaves_like 'grants access to everyone', {block_author: true}
236230
end
@@ -250,7 +244,7 @@
250244
end
251245

252246
permissions(:report?) do
253-
let(:report_emails) { ['[email protected]'] }
247+
let(:reports_enabled) { true }
254248

255249
it_behaves_like 'grants access to everyone', {block_author: true}
256250
end
@@ -273,7 +267,7 @@
273267
end
274268

275269
permissions(:report?) do
276-
let(:report_emails) { ['[email protected]'] }
270+
let(:reports_enabled) { true }
277271

278272
it_behaves_like 'grants access to admins only'
279273
end
@@ -290,7 +284,7 @@
290284
end
291285

292286
permissions(:report?) do
293-
let(:report_emails) { ['[email protected]'] }
287+
let(:reports_enabled) { true }
294288

295289
it_behaves_like 'grants access to admins only'
296290
end
@@ -307,7 +301,7 @@
307301
end
308302

309303
permissions(:report?) do
310-
let(:report_emails) { ['[email protected]'] }
304+
let(:reports_enabled) { true }
311305

312306
it_behaves_like 'grants access to admins only'
313307
end
@@ -331,7 +325,7 @@
331325
end
332326

333327
permissions(:report?) do
334-
let(:report_emails) { ['[email protected]'] }
328+
let(:reports_enabled) { true }
335329

336330
it_behaves_like 'grants access to everyone', {block_author: true}
337331
end
@@ -351,7 +345,7 @@
351345
end
352346

353347
permissions(:report?) do
354-
let(:report_emails) { ['[email protected]'] }
348+
let(:reports_enabled) { true }
355349

356350
it_behaves_like 'grants access to everyone', {block_author: true}
357351
end
@@ -374,7 +368,7 @@
374368
end
375369

376370
permissions(:report?) do
377-
let(:report_emails) { ['[email protected]'] }
371+
let(:reports_enabled) { true }
378372

379373
it_behaves_like 'grants access to admins only'
380374
end
@@ -391,7 +385,7 @@
391385
end
392386

393387
permissions(:report?) do
394-
let(:report_emails) { ['[email protected]'] }
388+
let(:reports_enabled) { true }
395389

396390
it_behaves_like 'grants access to admins only'
397391
end
@@ -408,7 +402,7 @@
408402
end
409403

410404
permissions(:report?) do
411-
let(:report_emails) { ['[email protected]'] }
405+
let(:reports_enabled) { true }
412406

413407
it_behaves_like 'grants access to admins only'
414408
end
@@ -428,7 +422,7 @@
428422
end
429423

430424
permissions(:report?) do
431-
let(:report_emails) { ['[email protected]'] }
425+
let(:reports_enabled) { true }
432426

433427
it_behaves_like 'grants access to admins only'
434428
end
@@ -448,7 +442,7 @@
448442
end
449443

450444
permissions(:report?) do
451-
let(:report_emails) { ['[email protected]'] }
445+
let(:reports_enabled) { true }
452446

453447
it_behaves_like 'grants access to everyone', {block_author: true}
454448
end

spec/system/request_for_comments_filter_system_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747

4848
describe 'reporting of user content' do
4949
before do
50+
stub_const('RequestForCommentPolicy::REPORT_RECEIVER_CONFIGURED', reports_enabled)
5051
visit(request_for_comment_path(create(:rfc)))
5152
end
5253

5354
context 'when reporting is enabled' do
54-
let(:report_emails) { ['[email protected]'] }
55+
let(:reports_enabled) { true }
5556

5657
it 'allows reporting of RfCs', :js do
5758
accept_confirm do
@@ -63,6 +64,8 @@
6364
end
6465

6566
context 'when reporting is disabled' do
67+
let(:reports_enabled) { false }
68+
6669
it 'dose not display the report button' do
6770
expect(page).to have_no_button(I18n.t('request_for_comments.report.report'))
6871
end

0 commit comments

Comments
 (0)