Skip to content

Commit d0fcb6d

Browse files
committed
Explicitly state permissions for CommunitySolutionPolicy
1 parent 2ab4877 commit d0fcb6d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
# frozen_string_literal: true
22

33
class CommunitySolutionPolicy < AdminOnlyPolicy
4+
def show?
5+
# We don't have a show action, so no one can show a CommunitySolution directly.
6+
no_one
7+
end
8+
9+
def new?
10+
# We don't have a destroy action, so no one can create a CommunitySolution directly.
11+
no_one
12+
end
13+
14+
def create?
15+
# We don't have a destroy action, so no one can initialize a CommunitySolution directly.
16+
no_one
17+
end
18+
419
def edit?
520
everyone
621
end
722

823
def update?
924
everyone
1025
end
26+
27+
def destroy?
28+
# We don't have a destroy action, so no one can destroy a CommunitySolution directly.
29+
no_one
30+
end
1131
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe CommunitySolutionPolicy do
6+
subject(:policy) { described_class }
7+
8+
permissions(:index?) do
9+
it 'grants access to admins only' do
10+
expect(policy).to permit(build(:admin), Consumer.new)
11+
%i[external_user teacher].each do |factory_name|
12+
expect(policy).not_to permit(create(factory_name), Consumer.new)
13+
end
14+
end
15+
end
16+
17+
%i[edit? update?].each do |action|
18+
permissions(action) do
19+
it 'grants access to anyone' do
20+
%i[admin external_user teacher].each do |factory_name|
21+
expect(policy).to permit(create(factory_name), CommunitySolution.new)
22+
end
23+
end
24+
end
25+
end
26+
27+
%i[create? destroy? new? show?].each do |action|
28+
permissions(action) do
29+
it 'does not grant access to anyone' do
30+
%i[admin external_user teacher].each do |factory_name|
31+
expect(policy).not_to permit(create(factory_name), CommunitySolution.new)
32+
end
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)