Skip to content

Commit e58a2eb

Browse files
committed
test: Add tests for comment policy
Related to #2715
1 parent 393091c commit e58a2eb

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

app/policies/comment_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def show?
99
everyone
1010
end
1111

12-
%i[new? destroy? update? edit?].each do |action|
12+
%i[destroy? update? edit?].each do |action|
1313
define_method(action) { admin? || author? || teacher_in_study_group? }
1414
end
1515

spec/policies/comment_policy_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe CommentPolicy do
6+
let(:user_types) { %i[learner teacher admin] }
7+
8+
permissions :create?, :show?, :index? do
9+
let(:comment) { build_stubbed(:comment) }
10+
11+
it 'grants access to all user types' do
12+
user_types.each do |user_type|
13+
expect(described_class).to permit(build_stubbed(user_type), comment)
14+
end
15+
end
16+
end
17+
18+
permissions :destroy?, :update?, :edit? do
19+
let(:comment) { build_stubbed(:comment) }
20+
21+
it 'grants access to the author' do
22+
expect(described_class).to permit(comment.user, comment)
23+
end
24+
25+
it 'grants no access to other learners' do
26+
expect(described_class).not_to permit(build_stubbed(:learner), comment)
27+
end
28+
29+
it 'grants no access to teachers not in study group' do
30+
expect(described_class).not_to permit(build_stubbed(:teacher), comment)
31+
end
32+
33+
it 'grants access to teachers in study group' do
34+
comment = create(:comment)
35+
teacher = create(:teacher, study_groups: [comment.submission.study_group])
36+
37+
expect(described_class).to permit(teacher, comment)
38+
end
39+
40+
it 'grants access to admins' do
41+
expect(described_class).to permit(build_stubbed(:admin), comment)
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)