forked from railsbridge/bridge_troll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_policy_spec.rb
118 lines (89 loc) · 3.65 KB
/
event_policy_spec.rb
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
# frozen_string_literal: true
require 'rails_helper'
describe EventPolicy do
describe '#publishable / #see_unpublished?' do
let!(:notable_chapter) { create(:chapter) }
let!(:notable_chapter_event) { create(:event, chapter: notable_chapter) }
let!(:other_event) { create(:event) }
it 'does not allow unprivileged users to see any events' do
scope_policy = EventPolicy::Scope.new(create(:user), Event)
expect(scope_policy.publishable).to be_nil
end
it 'allows publishers to see all events' do
publisher = create(:user, publisher: true)
scope_policy = EventPolicy::Scope.new(publisher, Event)
expect(scope_policy.publishable).to match_array([notable_chapter_event, other_event])
policy = described_class.new(publisher, Event)
expect(policy).to be_see_unpublished
end
it 'allows admins to see unpublished events' do
admin = create(:user, admin: true)
scope_policy = EventPolicy::Scope.new(admin, Event)
expect(scope_policy.publishable).to match_array([notable_chapter_event, other_event])
policy = described_class.new(admin, Event)
expect(policy).to be_see_unpublished
end
it 'allows chapter leaders to see unpublished events' do
chapter_leader = create(:user)
notable_chapter.chapter_leaderships.create(user: chapter_leader)
scope_policy = EventPolicy::Scope.new(chapter_leader, Event)
expect(scope_policy.publishable).to match_array([notable_chapter_event])
policy = described_class.new(chapter_leader, Event)
expect(policy).to be_see_unpublished
end
it 'allows organization leaders to see unpublished events' do
organization_leader = create(:user)
notable_chapter.organization.organization_leaderships.create(user: organization_leader)
scope_policy = EventPolicy::Scope.new(organization_leader, Event)
expect(scope_policy.publishable).to match_array([notable_chapter_event])
policy = described_class.new(organization_leader, Event)
expect(policy).to be_see_unpublished
end
end
describe '#publish?' do
let!(:event) { create(:event) }
it 'allows admins to publish an event' do
policy = described_class.new(create(:user, admin: true), event)
expect(policy).to be_publish
end
it 'allows chapter leaders to publish an event' do
leader = create(:user)
event.chapter.leaders << leader
policy = described_class.new(leader, event)
expect(policy).to be_publish
end
it 'allows organization leaders to publish an event' do
leader = create(:user)
event.chapter.organization.leaders << leader
policy = described_class.new(leader, event)
expect(policy).to be_publish
end
it 'does not allow strangers to edit an event' do
policy = described_class.new(create(:user), event)
expect(policy).not_to be_publish
end
end
describe '#update?' do
let!(:event) { create(:event) }
it 'allows admins to edit an event' do
policy = described_class.new(create(:user, admin: true), event)
expect(policy).to be_update
end
it 'allows organizers to edit an event' do
organizer = create(:user)
event.organizers << organizer
policy = described_class.new(organizer, event)
expect(policy).to be_update
end
it 'allows chapter leaders to edit an event' do
leader = create(:user)
event.chapter.leaders << leader
policy = described_class.new(leader, event)
expect(policy).to be_update
end
it 'does not allow strangers to edit an event' do
policy = described_class.new(create(:user), event)
expect(policy).not_to be_update
end
end
end