0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/plugins/discourse-calendar/spec/integration/recurrence_spec.rb
Martin Brennan 57779cc4be
FIX: can_create_discourse_post_event? not checking user groups correctly (#42284)
This commit fixes `can_create_discourse_post_event?` manually checking
a user's groups, which doesn't account for pseudogroups like
`logged_in_users`. We should always use `user.in_any_groups?` for this.

In addition, the opportunity is taken here for some cleanup, moving
all the guardian extensions from plugin.rb into a proper
`GuardianExtensions` file for calendar, adding extra specs along the
way.

Finally, I've added a `Group.refresh_automatic_groups_for_user!`
method as a single-user variant of `Group.refresh_automatic_groups!`
that can use in fabricators for testing, to make this more
thorough/reliable
than manually adding group user records in `after_create` in the
fabricators
2026-08-04 16:09:50 +10:00

166 lines
4.8 KiB
Ruby
Vendored

# frozen_string_literal: true
describe "discourse_post_event_recurrence" do
let(:user_1) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
let(:topic_1) { Fabricate(:topic, user: user_1) }
let(:post_1) { Fabricate(:post, topic: topic_1) }
let(:starts_at) { Time.zone.parse("2020-09-10 19:00") }
let(:post_event_1) do
Fabricate(
:event,
post: post_1,
original_starts_at: starts_at,
original_ends_at: starts_at + 1.hour,
)
end
before do
freeze_time(starts_at + 1.minute)
SiteSetting.calendar_enabled = true
SiteSetting.discourse_post_event_enabled = true
end
it "delete previous notifications before creating a new one for invites" do
going_user = Fabricate(:user)
DiscoursePostEvent::Invitee.create_attendance!(going_user.id, post_event_1.id, :going)
post_event_1.update!(original_starts_at: starts_at + 10.minutes)
post_event_1.set_next_date
post_event_1.update!(original_starts_at: starts_at - 10.minutes)
post_event_1.set_next_date
expect(
going_user
.notifications
.where(notification_type: Notification.types[:event_invitation])
.count,
).to eq(1)
end
describe "every_month" do
before { post_event_1.update!(recurrence: "every_month") }
it "sets the next month at the same weekday" do
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))
end
end
describe "every_week" do
before { post_event_1.update!(recurrence: "every_week") }
it "sets the next week at the same weekday" do
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-17 19:00"))
end
end
describe "every_two_weeks" do
before { post_event_1.update!(recurrence: "every_two_weeks") }
it "sets in two weeks at the same weekday" do
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-24 19:00"))
end
end
describe "every_four_weeks" do
before { post_event_1.update!(recurrence: "every_four_weeks") }
it "sets in four weeks at the same weekday" do
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))
end
end
describe "every_day" do
before { post_event_1.update!(recurrence: "every_day") }
it "sets the next day" do
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-11 19:00"))
end
end
describe "every_weekday" do
before do
post_event_1.update!(
original_starts_at: Time.zone.parse("2020-09-11 19:00"),
original_ends_at: Time.zone.parse("2020-09-11 19:00") + 1.hour,
recurrence: "every_weekday",
)
end
it "sets the next day" do
freeze_time(post_event_1.original_starts_at + 1.minute)
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-09-14 19:00"))
end
end
context "when the event has a timezone" do
describe "every_month" do
before { post_event_1.update!(recurrence: "every_month", timezone: "America/New_York") }
it "sets the next month at the same weekday" do
freeze_time(starts_at + 1.day)
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(Time.zone.parse("2020-10-08 19:00"))
end
end
end
describe "resetting invitees on recurrence advance" do
fab!(:going_once_user, :user)
fab!(:going_recurring_user, :user)
before { post_event_1.update!(recurrence: "every_week") }
it "clears non-recurring going invitees and keeps recurring ones" do
DiscoursePostEvent::Invitee.create_attendance!(going_once_user.id, post_event_1.id, :going)
DiscoursePostEvent::Invitee.create_attendance!(
going_recurring_user.id,
post_event_1.id,
:going,
recurring: true,
)
post_event_1.set_next_date
once = post_event_1.invitees.find_by(user_id: going_once_user.id)
recurring = post_event_1.invitees.find_by(user_id: going_recurring_user.id)
expect(once.status).to be_nil
expect(recurring.status).to eq(DiscoursePostEvent::Invitee.statuses[:going])
expect(recurring.recurring).to eq(true)
end
end
context "when the recurring event is closed" do
before { post_event_1.update!(recurrence: "every_week", closed: true) }
it "does not generate the next occurrence" do
initial_starts_at = post_event_1.starts_at
post_event_1.set_next_date
expect(post_event_1.starts_at).to eq_time(initial_starts_at)
end
it "does not schedule a topic bump" do
post_event_1.update!(reminders: "bumpTopic.10.minutes")
expect { post_event_1.set_topic_bump }.not_to change {
Jobs::DiscoursePostEventBumpTopic.jobs.size
}
end
end
end