A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
describe DiscourseEvents::Events::EventSerializer do
before do
Jobs.run_immediately!
SiteSetting.discourse_events_enabled = true
SiteSetting.discourse_post_event_enabled = true
end
fab!(:category)
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:post) { Fabricate(:post, topic: topic) }
context "with a location" do
fab!(:event) { Fabricate(:event, post: post, location: "[RSVP](https://zoom.example.com)") }
it "serializes location_html with rendered links" do
json = DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json
expect(json[:event][:location_html]).to eq(
DiscourseEvents::Events::Parser.cook_inline(event.location, post: event.post),
)
expect(json[:event][:location_html]).to include('href="https://zoom.example.com"')
end
it "omits location_html without a location" do
event.update_columns(location: nil)
json = DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json
expect(json[:event]).not_to have_key(:location_html)
end
end
context "with a recording" do
fab!(:event) { Fabricate(:event, post:, recording_url: "https://youtu.be/abc") }
it "serializes recording_url" do
json = described_class.new(event, scope: Guardian.new).as_json
expect(json[:event][:recording_url]).to eq("https://youtu.be/abc")
end
it "serializes the cached recording onebox" do
Discourse.cache.write(
Oneboxer.onebox_cache_key("https://youtu.be/abc"),
{ onebox: "<aside>recording</aside>" },
)
json = described_class.new(event, scope: Guardian.new).as_json
expect(json[:event][:recording_onebox]).to eq("<aside>recording</aside>")
end
it "omits recording_url without a recording" do
event.update_columns(recording_url: nil)
json = described_class.new(event, scope: Guardian.new).as_json
expect(json[:event]).not_to have_key(:recording_url)
expect(json[:event]).not_to have_key(:recording_onebox)
end
end
context "with reminders" do
def serialized_reminders(reminders)
event = Fabricate(:event, post: post, reminders: reminders)
DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json[:event][
:reminders
]
end
it "serializes value, unit, period and type" do
expect(serialized_reminders("notification.15.minutes,bumpTopic.-3.days")).to eq(
[
{ value: 15, unit: "minutes", period: "before", type: "notification" },
{ value: 3, unit: "days", period: "after", type: "bumpTopic" },
],
)
end
end
context "with a private event" do
fab!(:private_event) do
Fabricate(:event, post: post, status: DiscourseEvents::Events::Event.statuses[:private])
end
fab!(:invitee_1, :user)
fab!(:invitee_2, :user)
fab!(:group_1) do
Fabricate(:group).tap do |g|
g.add(invitee_1)
g.add(invitee_2)
g.save!
end
end
context "when some invited users have not rsvp-ed yet" do
before do
private_event.update_with_params!(raw_invitees: [group_1.name])
DiscourseEvents::Events::Invitee.create_attendance!(invitee_1.id, private_event.id, :going)
private_event.reload
end
it "returns the correct stats" do
json =
DiscourseEvents::Events::EventSerializer.new(private_event, scope: Guardian.new).as_json
expect(json[:event][:stats]).to eq(
going: 1,
going_recurring: 0,
interested: 0,
invited: 2,
not_going: 0,
capacity: nil,
)
end
end
context "with recurring 'going' invitees" do
before do
private_event.update_with_params!(raw_invitees: [group_1.name])
DiscourseEvents::Events::Invitee.create_attendance!(invitee_1.id, private_event.id, :going)
DiscourseEvents::Events::Invitee.create_attendance!(
invitee_2.id,
private_event.id,
:going,
recurring: true,
)
private_event.reload
end
it "counts only recurring 'going' invitees in going_recurring" do
json =
DiscourseEvents::Events::EventSerializer.new(private_event, scope: Guardian.new).as_json
expect(json[:event][:stats]).to eq(
going: 2,
going_recurring: 1,
interested: 0,
invited: 2,
not_going: 0,
capacity: nil,
)
end
end
end
context "with a public event" do
fab!(:event) { Fabricate(:event, post: post) }
it "returns the event category's id" do
json = DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json
expect(json[:event][:category_id]).to eq(category.id)
end
it "includes image_upload key as nil when not set" do
json = DiscourseEvents::Events::EventSerializer.new(event, scope: Guardian.new).as_json
expect(json[:event]).to have_key(:image_upload)
expect(json[:event][:image_upload]).to be_nil
end
context "when event has an image" do
fab!(:upload)
fab!(:event_with_image) do
Fabricate(:event, post: Fabricate(:post, topic: topic), image_upload: upload)
end
it "includes image_upload in serialized output" do
json =
DiscourseEvents::Events::EventSerializer.new(
event_with_image,
scope: Guardian.new,
).as_json
expect(json[:event][:image_upload][:id]).to eq(upload.id)
expect(json[:event][:image_upload][:url]).to include(upload.url)
end
end
context "when event is a livestream" do
let(:livestream_url) { "https://www.youtube.com/live/abc123" }
# The event must live on the topic's first post: `Event#before_save` clears
# the `livestream` flag on any other post.
let(:livestream_topic) { Fabricate(:topic, category: category) }
let(:livestream_post) { Fabricate(:post, topic: livestream_topic) }
let(:livestream_event) do
Fabricate(:event, post: livestream_post, livestream: true, location: livestream_url)
end
before { Jobs.run_later! }
it "does not enqueue onebox warming while serializing" do
livestream_event
expect_not_enqueued_with(job: :warm_livestream_onebox) do
json =
DiscourseEvents::Events::EventSerializer.new(
livestream_event,
scope: Guardian.new,
).as_json
expect(json[:event][:livestream_onebox]).to be_nil
end
end
it "does not fetch the onebox while serializing" do
livestream_event
Oneboxer.expects(:onebox).never
DiscourseEvents::Events::EventSerializer.new(livestream_event, scope: Guardian.new).as_json
end
it "serves the onebox from the cache when it has been warmed" do
livestream_event
Discourse.cache.write(
Oneboxer.onebox_cache_key(livestream_url),
{ onebox: "<aside>cached</aside>" },
)
json =
DiscourseEvents::Events::EventSerializer.new(
livestream_event,
scope: Guardian.new,
).as_json
expect(json[:event][:livestream_onebox]).to eq("<aside>cached</aside>")
end
it "includes the livestream URL and chat channel id" do
livestream_event
json =
DiscourseEvents::Events::EventSerializer.new(
livestream_event,
scope: Guardian.new,
).as_json
expect(json[:event][:livestream_url]).to eq(livestream_url)
expect(json[:event].key?(:livestream_chat_channel_id)).to eq(true)
end
it "is not a Zoom livestream" do
livestream_event
json =
DiscourseEvents::Events::EventSerializer.new(
livestream_event,
scope: Guardian.new,
).as_json
expect(json[:event][:is_zoom_livestream]).to eq(false)
end
end
context "when the event is not a livestream" do
fab!(:plain_post) { Fabricate(:post, topic: topic) }
fab!(:plain_event) { Fabricate(:event, post: plain_post, location: "Room 5") }
it "omits the livestream attributes" do
json =
DiscourseEvents::Events::EventSerializer.new(plain_event, scope: Guardian.new).as_json
expect(json[:event].key?(:livestream_url)).to eq(false)
expect(json[:event].key?(:livestream_onebox)).to eq(false)
expect(json[:event].key?(:livestream_chat_channel_id)).to eq(false)
end
end
context "when the event is a Zoom livestream" do
let(:zoom_url) { "https://us06web.zoom.us/j/123456789?pwd=secret" }
let(:zoom_topic) { Fabricate(:topic, category: category) }
let(:zoom_post) { Fabricate(:post, topic: zoom_topic) }
let(:zoom_event) { Fabricate(:event, post: zoom_post, livestream: true, location: zoom_url) }
before do
SiteSetting.livestream_zoom_enabled = true
Jobs.run_later!
end
it "flags the event as a Zoom livestream" do
json = DiscourseEvents::Events::EventSerializer.new(zoom_event, scope: Guardian.new).as_json
expect(json[:event][:is_zoom_livestream]).to eq(true)
expect(json[:event][:livestream_url]).to eq(zoom_url)
end
it "omits the onebox, since the Zoom entry component renders instead" do
Discourse.cache.write(
Oneboxer.onebox_cache_key(zoom_url),
{ onebox: "<aside>cached</aside>" },
)
json = DiscourseEvents::Events::EventSerializer.new(zoom_event, scope: Guardian.new).as_json
expect(json[:event].key?(:livestream_onebox)).to eq(false)
end
it "falls back to the onebox when Zoom is disabled" do
SiteSetting.livestream_zoom_enabled = false
json = DiscourseEvents::Events::EventSerializer.new(zoom_event, scope: Guardian.new).as_json
expect(json[:event][:is_zoom_livestream]).to eq(false)
expect(json[:event].key?(:livestream_onebox)).to eq(true)
end
end
context "when event has duration" do
fab!(:post_with_duration) { Fabricate(:post, topic: topic) }
fab!(:event_with_duration) do
Fabricate(
:event,
post: post_with_duration,
original_starts_at: "2022-01-15 10:00:00 UTC",
original_ends_at: "2022-01-15 11:30:00 UTC",
)
end
it "includes duration in serialized output" do
json =
DiscourseEvents::Events::EventSerializer.new(
event_with_duration,
scope: Guardian.new,
).as_json
expect(json[:event][:duration]).to eq("01:30:00")
end
end
context "when event has no end time" do
fab!(:post_no_end) { Fabricate(:post, topic: topic) }
fab!(:event_no_end) do
Fabricate(
:event,
post: post_no_end,
original_starts_at: "2022-01-15 10:00:00 UTC",
original_ends_at: nil,
)
end
it "includes default 1-hour duration in serialized output" do
json =
DiscourseEvents::Events::EventSerializer.new(event_no_end, scope: Guardian.new).as_json
expect(json[:event]).to have_key(:duration)
expect(json[:event][:duration]).to eq("01:00:00")
end
end
context "when event is recurring with show_local_time" do
fab!(:post_recurring) { Fabricate(:post, topic:) }
fab!(:recurring_event) do
Fabricate(
:event,
post: post_recurring,
original_starts_at: 2.weeks.ago.change(hour: 14, min: 0, sec: 0),
original_ends_at: 2.weeks.ago.change(hour: 15, min: 0, sec: 0),
recurrence: "every_week",
timezone: "UTC",
show_local_time: true,
)
end
before { recurring_event.set_next_recurrent_event_date }
it "serializes starts_at using the next occurrence" do
json =
DiscourseEvents::Events::EventSerializer.new(
recurring_event.reload,
scope: Guardian.new,
).as_json
starts_at = Time.find_zone("UTC").parse(json[:event][:starts_at])
expect(starts_at).to be > Time.current
end
it "serializes ends_at using the next occurrence" do
json =
DiscourseEvents::Events::EventSerializer.new(
recurring_event.reload,
scope: Guardian.new,
).as_json
ends_at = Time.find_zone("UTC").parse(json[:event][:ends_at])
expect(ends_at).to be > Time.current
end
end
context "when event is all-day" do
fab!(:post_all_day) { Fabricate(:post, topic: topic) }
fab!(:all_day_event) do
Fabricate(
:event,
post: post_all_day,
original_starts_at: "2026-03-12 00:00:00 UTC",
original_ends_at: "2026-03-14 00:00:00 UTC",
all_day: true,
)
end
it "serializes starts_at as date-only string" do
json =
DiscourseEvents::Events::EventSerializer.new(all_day_event, scope: Guardian.new).as_json
expect(json[:event][:starts_at]).to eq("2026-03-12")
end
it "serializes ends_at as date-only string" do
json =
DiscourseEvents::Events::EventSerializer.new(all_day_event, scope: Guardian.new).as_json
expect(json[:event][:ends_at]).to eq("2026-03-14")
end
it "serializes all_day as true" do
json =
DiscourseEvents::Events::EventSerializer.new(all_day_event, scope: Guardian.new).as_json
expect(json[:event][:all_day]).to eq(true)
end
end
end
end