|
6 | 6 | let(:published_event) { build_stubbed :event, :published } |
7 | 7 | let(:public_event) { build_stubbed :event, publicly_visible: true } |
8 | 8 | let(:unpublished_event) { build_stubbed :event, :unpublished } |
| 9 | + let(:ended_event) { build_stubbed :event, :published, :ended } |
9 | 10 | let(:open_registration_event) { build_stubbed :event, registration_close_date: 1.day.from_now } |
10 | 11 | let(:closed_registration_event) { build_stubbed :event, registration_close_date: 1.day.ago } |
11 | 12 |
|
@@ -54,6 +55,36 @@ def policy_for(record: nil, user:) |
54 | 55 | end |
55 | 56 | end |
56 | 57 |
|
| 58 | + context "when event has ended" do |
| 59 | + context "with admin user" do |
| 60 | + subject { policy_for(record: ended_event, user: admin_user) } |
| 61 | + |
| 62 | + it { is_expected.to be_allowed_to(:show?) } |
| 63 | + end |
| 64 | + |
| 65 | + context "with registered user" do |
| 66 | + subject { policy_for(record: ended_event, user: regular_user) } |
| 67 | + |
| 68 | + before { allow(ended_event).to receive(:actively_registered?).with(regular_user.person).and_return(true) } |
| 69 | + |
| 70 | + it { is_expected.to be_allowed_to(:show?) } |
| 71 | + end |
| 72 | + |
| 73 | + context "with unregistered user" do |
| 74 | + subject { policy_for(record: ended_event, user: regular_user) } |
| 75 | + |
| 76 | + before { allow(ended_event).to receive(:actively_registered?).with(regular_user.person).and_return(false) } |
| 77 | + |
| 78 | + it { is_expected.not_to be_allowed_to(:show?) } |
| 79 | + end |
| 80 | + |
| 81 | + context "with no user" do |
| 82 | + subject { policy_for(record: ended_event, user: nil) } |
| 83 | + |
| 84 | + it { is_expected.not_to be_allowed_to(:show?) } |
| 85 | + end |
| 86 | + end |
| 87 | + |
57 | 88 | context "when event is not visible" do |
58 | 89 | context "with admin user" do |
59 | 90 | subject { policy_for(record: unpublished_event, user: admin_user) } |
@@ -175,22 +206,27 @@ def policy_for(record: nil, user:) |
175 | 206 | context "with regular user" do |
176 | 207 | let(:policy) { policy_for(record: Event, user: regular_user) } |
177 | 208 |
|
178 | | - it "returns only visible events with open registration" do |
| 209 | + it "returns only visible events with open registration or active registrations" do |
179 | 210 | scope = policy.apply_scope(Event.all, type: :active_record_relation) |
180 | | - expect(scope.to_sql).to include('`events`.`published` = TRUE') |
181 | | - expect(scope.to_sql).to include('registration_close_date IS NULL OR registration_close_date >=') |
182 | | - expect(scope.to_sql).to include('LEFT OUTER JOIN `event_registrations`') |
| 211 | + sql = scope.to_sql |
| 212 | + expect(sql).to include('`events`.`published` = TRUE') |
| 213 | + expect(sql).to include("events.end_date >=") |
| 214 | + expect(sql).to include("events.registration_close_date IS NULL OR events.registration_close_date >=") |
| 215 | + expect(sql).to include("LEFT OUTER JOIN event_registrations") |
| 216 | + expect(sql).to include("event_registrations.status IN") |
183 | 217 | end |
184 | 218 | end |
185 | 219 |
|
186 | 220 | context "with no user" do |
187 | 221 | let(:policy) { policy_for(record: Event, user: nil) } |
188 | 222 |
|
189 | | - it "returns only visible events with open registration" do |
| 223 | + it "excludes ended events and events with closed registration" do |
190 | 224 | scope = policy.apply_scope(Event.all, type: :active_record_relation) |
191 | | - expect(scope.to_sql).to include('`events`.`published` = TRUE') |
192 | | - expect(scope.to_sql).to include('registration_close_date IS NULL OR registration_close_date >=') |
193 | | - expect(scope.to_sql).not_to include('LEFT OUTER JOIN `registrants`') |
| 225 | + sql = scope.to_sql |
| 226 | + expect(sql).to include('`events`.`published` = TRUE') |
| 227 | + expect(sql).to include("events.end_date >=") |
| 228 | + expect(sql).to include("events.registration_close_date IS NULL OR events.registration_close_date >=") |
| 229 | + expect(sql).not_to include("LEFT OUTER JOIN") |
194 | 230 | end |
195 | 231 | end |
196 | 232 | end |
|
0 commit comments