Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

24.2 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"

RSpec.describe Meetings::IcalendarBuilder,
               with_settings: { mail_from: "[email protected]", app_title: "OpenProject Testing" } do
  let(:timezone) { ActiveSupport::TimeZone["Europe/Berlin"] }

  context "without any meetings" do
    subject(:builder) { described_class.new(timezone:) }

    let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

    it "sets the calendar properties" do
      expect(parsed_calendar.prodid).to eq("-//OpenProject GmbH//#{OpenProject::VERSION}//Meeting//EN")
      expect(parsed_calendar.version).to eq("2.0")
      expect(parsed_calendar.calscale).to eq("GREGORIAN")
      expect(parsed_calendar.refresh_interval.value_ical).to eq("PT6H")
    end

    it "allows setting a custom calendar title" do
      builder.calendar_title = "Custom Title"

      calendar = Icalendar::Calendar.parse(builder.to_ical).first
      expect(calendar.x_wr_calname.first).to eq("Custom Title")
    end
  end

  context "with a single meeting" do
    let(:meeting) { create(:meeting, :author_participates, start_time: Time.zone.parse("2025-08-30 10:00")) }

    context "when current user needs to take action" do
      subject(:builder) { described_class.new(timezone:, user: meeting.author) }

      let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

      it "sets PARTSTAT to NEEDS-ACTION and RSVP to TRUE for current user" do
        builder.add_single_meeting_event(meeting:)
        builder.update_calendar_status(cancelled: false)

        event = parsed_calendar.events.first
        expect(event.attendee).not_to be_empty

        # Find the current user's attendee entry
        current_user_attendee = event.attendee.find { |a| a.to_s.include?(meeting.author.mail) }
        expect(current_user_attendee).to be_present
        expect(current_user_attendee.ical_params["partstat"]).to eq(["NEEDS-ACTION"])
        expect(current_user_attendee.ical_params["rsvp"]).to eq(["TRUE"])
        expect(current_user_attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
        expect(current_user_attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
      end

      it "sets created and last_modified timestamps correctly" do
        builder.add_single_meeting_event(meeting:)
        builder.update_calendar_status(cancelled: false)

        event = parsed_calendar.events.first
        expect(event.created.to_time).to be_within(1.second).of(meeting.created_at.utc)
        expect(event.last_modified.to_time).to be_within(1.second).of(meeting.updated_at.utc)
      end
    end

    context "when current user has accepted all invitations" do
      subject(:builder) do
        described_class.new(timezone:, user: meeting.author).tap(&:treat_participations_from_user_as_accepted!)
      end

      let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

      it "sets PARTSTAT to ACCEPTED and RSVP to FALSE for all attendees" do
        builder.add_single_meeting_event(meeting:)
        builder.update_calendar_status(cancelled: false)

        event = parsed_calendar.events.first
        expect(event.attendee).not_to be_empty

        event.attendee.each do |attendee|
          expect(attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
          expect(attendee.ical_params["rsvp"]).to eq(["FALSE"])
          expect(attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
          expect(attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
        end
      end
    end

    context "when current user is not a participant" do
      let(:other_user) { create(:user) }
      let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

      subject(:builder) { described_class.new(timezone:, user: other_user) }

      it "sets PARTSTAT to ACCEPTED and RSVP to FALSE for all attendees" do
        builder.add_single_meeting_event(meeting:)
        builder.update_calendar_status(cancelled: false)

        event = parsed_calendar.events.first
        expect(event.attendee).not_to be_empty

        event.attendee.each do |attendee|
          expect(attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
          expect(attendee.ical_params["rsvp"]).to eq(["FALSE"])
          expect(attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
          expect(attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
        end
      end
    end

    context "with multiple participants" do
      let(:user1) { create(:user, firstname: "John", lastname: "Doe", mail: "[email protected]") }
      let(:user2) { create(:user, firstname: "Jane", lastname: "Smith", mail: "[email protected]") }
      let(:meeting_with_participants) do
        meeting = create(:meeting, start_time: Time.zone.parse("2025-08-30 10:00"))
        create(:meeting_participant, meeting:, user: user1)
        create(:meeting_participant, meeting:, user: user2)
        meeting
      end

      context "when current user needs to take action" do
        subject(:builder) { described_class.new(timezone:, user: user1) }

        let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

        it "sets PARTSTAT to NEEDS-ACTION and RSVP to TRUE for current user, ACCEPTED and FALSE for others" do
          builder.add_single_meeting_event(meeting: meeting_with_participants)
          builder.update_calendar_status(cancelled: false)

          event = parsed_calendar.events.first
          expect(event.attendee.count).to eq(2)

          # Find attendees by email
          john_attendee = event.attendee.find { |a| a.to_s.include?("[email protected]") }
          jane_attendee = event.attendee.find { |a| a.to_s.include?("[email protected]") }

          expect(john_attendee).to be_present
          expect(jane_attendee).to be_present

          # John is the current user, so he should have NEEDS-ACTION and RSVP=TRUE
          expect(john_attendee.ical_params["partstat"]).to eq(["NEEDS-ACTION"])
          expect(john_attendee.ical_params["rsvp"]).to eq(["TRUE"])
          expect(john_attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
          expect(john_attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])

          # Jane is not the current user, so she should have ACCEPTED and RSVP=FALSE
          expect(jane_attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
          expect(jane_attendee.ical_params["rsvp"]).to eq(["FALSE"])
          expect(jane_attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
          expect(jane_attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
        end
      end

      context "when current user has accepted all invitations" do
        subject(:builder) do
          described_class.new(timezone:, user: user1).tap(&:treat_participations_from_user_as_accepted!)
        end

        let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

        it "sets PARTSTAT to ACCEPTED and RSVP to FALSE for all multiple attendees" do
          builder.add_single_meeting_event(meeting: meeting_with_participants)
          builder.update_calendar_status(cancelled: false)

          event = parsed_calendar.events.first
          expect(event.attendee.count).to eq(2)

          # Find attendees by email
          john_attendee = event.attendee.find { |a| a.to_s.include?("[email protected]") }
          jane_attendee = event.attendee.find { |a| a.to_s.include?("[email protected]") }

          expect(john_attendee).to be_present
          expect(jane_attendee).to be_present

          [john_attendee, jane_attendee].each do |attendee|
            expect(attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
            expect(attendee.ical_params["rsvp"]).to eq(["FALSE"])
            expect(attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
            expect(attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
          end
        end
      end
    end
  end

  context "with recurring meeting series" do
    let(:project) { create(:project) }
    let(:user1) { create(:user, firstname: "John", lastname: "Doe", member_with_permissions: { project => [:view_meetings] }) }
    let(:user2) { create(:user, firstname: "John", lastname: "Doe", member_with_permissions: { project => [:view_meetings] }) }

    let(:recurring_meeting) do
      create(:recurring_meeting,
             start_time: Time.zone.parse("2025-08-25 09:00"),
             iterations: 10,
             project: project,
             end_after: :iterations,
             time_zone: timezone.tzinfo.name).tap do |recurring_meeting|
        create(:meeting_participant, :invitee, meeting: recurring_meeting.template, user: user1)
        create(:meeting_participant, :invitee, meeting: recurring_meeting.template, user: user2)
      end
    end

    let!(:second_occurrence) do
      # Cancel second occurrence
      create(:scheduled_meeting,
             :cancelled,
             recurring_meeting:,
             start_time: recurring_meeting.start_time + 1.week)
    end

    let!(:third_occurence) do
      # Third occurrence instantiated and moved by +10 minutes
      base_start = recurring_meeting.start_time + 2.weeks
      create(:scheduled_meeting,
             recurring_meeting:,
             start_time: base_start)

      result = RecurringMeetings::InitOccurrenceService
          .new(user: User.system, recurring_meeting:)
          .call(start_time: base_start)

      meeting = result.result

      # Reschedule meeting to be 10 minutes later. It should still have the correct recurrence
      meeting.update(start_time: base_start + 10.minutes)

      meeting.scheduled_meeting
    end

    context "when using the cache" do
      subject(:builder) { described_class.new(timezone:) }

      before do
        builder.preload_for_recurring_meetings(recurring_meetings: [recurring_meeting])
      end

      it "preloads the correct caches" do
        builder.add_series_event(recurring_meeting:)

        expect(builder.instance_variable_get(:@excluded_dates_cache)).to eq(
          recurring_meeting.id => [second_occurrence.start_time]
        )

        expect(builder.instance_variable_get(:@instantiated_occurrences_cache)).to eq(
          recurring_meeting.id => [third_occurence]
        )

        expect(builder.instance_variable_get(:@series_cache_loaded)).to be true
      end

      it "puts the correct EXDATEs in the generated event (REGRESSION: #68068)" do
        builder.add_series_event(recurring_meeting:)

        parsed_calendar = Icalendar::Calendar.parse(builder.to_ical).first
        event = parsed_calendar.events.find { |e| e.uid == recurring_meeting.uid }

        expect(event.exdate).not_to be_empty
        exdate_values = event.exdate.map(&:value)
        expect(exdate_values).to contain_exactly(second_occurrence.start_time)
      end
    end

    context "when current user needs to take action" do
      subject(:builder) { described_class.new(timezone:, user: user1) }

      let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

      it "sets PARTSTAT to NEEDS-ACTION and RSVP to TRUE for current user in recurring meeting series" do
        builder.add_series_event(recurring_meeting:)

        master = parsed_calendar.events.find { |e| e.rrule.present? && e.recurrence_id.blank? }
        overrides = parsed_calendar.events.select { |e| e.recurrence_id.present? }

        # Check master event attendees
        expect(master.attendee).not_to be_empty
        current_user_attendee = master.attendee.find { |a| a.to_s.include?(user1.mail) }
        other_user_attendee = master.attendee.find { |a| a.to_s.include?(user2.mail) }

        # Current user should have NEEDS-ACTION and RSVP=TRUE
        expect(current_user_attendee.ical_params["partstat"]).to eq(["NEEDS-ACTION"])
        expect(current_user_attendee.ical_params["rsvp"]).to eq(["TRUE"])
        expect(current_user_attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
        expect(current_user_attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])

        # Other user should have ACCEPTED and RSVP=FALSE
        expect(other_user_attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
        expect(other_user_attendee.ical_params["rsvp"]).to eq(["FALSE"])
        expect(other_user_attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
        expect(other_user_attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])

        # Check override event attendees
        overrides.each do |override_event|
          expect(override_event.attendee).not_to be_empty
          current_user_override = override_event.attendee.find { |a| a.to_s.include?(user1.mail) }
          other_user_override = override_event.attendee.find { |a| a.to_s.include?(user2.mail) }

          expect(current_user_override.ical_params["partstat"]).to eq(["NEEDS-ACTION"])
          expect(current_user_override.ical_params["rsvp"]).to eq(["TRUE"])
          expect(other_user_override.ical_params["partstat"]).to eq(["ACCEPTED"])
          expect(other_user_override.ical_params["rsvp"]).to eq(["FALSE"])
        end
      end

      it "sets created and last_modified timestamps correctly for recurring series" do
        builder.add_series_event(recurring_meeting:)

        master = parsed_calendar.events.find { |e| e.rrule.present? && e.recurrence_id.blank? }
        overrides = parsed_calendar.events.select { |e| e.recurrence_id.present? }

        # Check master event timestamps
        expect(master.created.to_time).to be_within(1.second).of(recurring_meeting.template.created_at.utc)
        expect(master.last_modified.to_time).to be_within(1.second).of(recurring_meeting.template.updated_at.utc)

        # Check override event timestamps
        overrides.each do |override_event|
          # Find the corresponding scheduled meeting for this override
          scheduled_meeting = [second_occurrence, third_occurence].find do |sm|
            sm.meeting && override_event.recurrence_id.to_time.utc.to_i == sm.start_time.utc.to_i
          end

          if scheduled_meeting&.meeting
            expect(override_event.created.to_time).to be_within(1.second).of(scheduled_meeting.meeting.created_at.utc)
            expect(override_event.last_modified.to_time).to be_within(1.second).of(scheduled_meeting.meeting.updated_at.utc)
          end
        end
      end
    end

    context "when current user has accepted all invitations" do
      subject(:builder) do
        described_class.new(timezone:, user: user1).tap(&:treat_participations_from_user_as_accepted!)
      end

      let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

      it "sets PARTSTAT to ACCEPTED and RSVP to FALSE for all attendees in recurring meeting series" do
        builder.add_series_event(recurring_meeting:)

        master = parsed_calendar.events.find { |e| e.rrule.present? && e.recurrence_id.blank? }
        overrides = parsed_calendar.events.select { |e| e.recurrence_id.present? }

        # Check master event attendees
        expect(master.attendee).not_to be_empty
        master.attendee.each do |attendee|
          expect(attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
          expect(attendee.ical_params["rsvp"]).to eq(["FALSE"])
          expect(attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
          expect(attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
        end

        # Check override event attendees
        overrides.each do |override_event|
          expect(override_event.attendee).not_to be_empty
          override_event.attendee.each do |attendee|
            expect(attendee.ical_params["partstat"]).to eq(["ACCEPTED"])
            expect(attendee.ical_params["rsvp"]).to eq(["FALSE"])
            expect(attendee.ical_params["cutype"]).to eq(["INDIVIDUAL"])
            expect(attendee.ical_params["role"]).to eq(["REQ-PARTICIPANT"])
          end
        end
      end

      it "sets created and last_modified timestamps correctly for recurring series when accepted" do
        builder.add_series_event(recurring_meeting:)

        master = parsed_calendar.events.find { |e| e.rrule.present? && e.recurrence_id.blank? }
        overrides = parsed_calendar.events.select { |e| e.recurrence_id.present? }

        # Check master event timestamps
        expect(master.created.to_time).to be_within(1.second).of(recurring_meeting.template.created_at.utc)
        expect(master.last_modified.to_time).to be_within(1.second).of(recurring_meeting.template.updated_at.utc)

        # Check override event timestamps
        overrides.each do |override_event|
          # Find the corresponding scheduled meeting for this override
          scheduled_meeting = [second_occurrence, third_occurence].find do |sm|
            sm.meeting && override_event.recurrence_id.to_time.utc.to_i == sm.start_time.utc.to_i
          end

          if scheduled_meeting&.meeting
            expect(override_event.created.to_time).to be_within(1.second).of(scheduled_meeting.meeting.created_at.utc)
            expect(override_event.last_modified.to_time).to be_within(1.second).of(scheduled_meeting.meeting.updated_at.utc)
          end
        end
      end
    end
  end

  context "for timezone component" do
    let(:meeting) { create(:meeting, start_time: Time.zone.parse("2025-10-01 10:00")) }
    let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

    subject(:builder) { described_class.new(timezone:) }

    it "includes a VTIMEZONE with TZID" do
      builder.add_single_meeting_event(meeting:)
      expect(parsed_calendar.timezones.size).to eq(1)
      tz = parsed_calendar.timezones.first
      expect(tz.tzid).to eq(timezone.tzinfo.canonical_identifier)
    end
  end

  context "with recurring meetings in multiple timezones" do
    subject(:builder) { described_class.new(timezone:) }

    let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

    let(:recurring_meeting) do
      create(:recurring_meeting,
             start_time: Time.zone.parse("2025-08-25 09:00"),
             iterations: 10,
             end_after: :iterations,
             time_zone: "Europe/Paris")
    end

    let(:other_recurring_meeting) do
      create(:recurring_meeting,
             start_time: Time.zone.parse("2025-08-25 09:00"),
             iterations: 10,
             end_after: :iterations,
             time_zone: "America/New_York")
    end

    before do
      builder.add_series_event(recurring_meeting: recurring_meeting)
      builder.add_series_event(recurring_meeting: other_recurring_meeting)
    end

    it "adds timezone definitions for both timezones of recurring meetings, but not builder timezone" do
      expect(parsed_calendar.timezones.size).to eq(2)
      tzids = parsed_calendar.timezones.map(&:tzid)
      expect(tzids).to contain_exactly("Europe/Paris", "America/New_York")
      # Berlin is not included because there is no event in that timezone
      # We only have recurring meetings, and those are not converted to the builder timezone
    end

    context "when adding another single meeting" do
      let(:single_meeting) { create(:meeting, start_time: Time.zone.parse("2025-10-01 10:00")) }

      before do
        builder.add_single_meeting_event(meeting: single_meeting)
      end

      it "also adds the builder timezone because single meetings are converted" do
        expect(parsed_calendar.timezones.size).to eq(3)
        tzids = parsed_calendar.timezones.map(&:tzid)
        expect(tzids).to contain_exactly("Europe/Paris", "America/New_York", timezone.tzinfo.canonical_identifier)
      end
    end
  end

  context "for timezone transitions across multiple years" do
    subject(:builder) { described_class.new(timezone:) }

    let(:parsed_calendar) { Icalendar::Calendar.parse(builder.to_ical).first }

    # We pick dates spread over multiple DST changes
    let!(:meetings) do
      [
        Time.zone.parse("2025-03-30 10:00"), # Around spring DST change
        Time.zone.parse("2026-01-15 11:00"),
        Time.zone.parse("2026-07-15 09:30"), # Summer time
        Time.zone.parse("2027-02-10 14:00"),
        Time.zone.parse("2027-10-30 10:00")  # Around autumn DST change
      ].map do |ts|
        create(:meeting, :author_participates, start_time: ts, duration: 1.0)
      end
    end

    it "emits exactly one VTIMEZONE block" do
      meetings.each { |m| builder.add_single_meeting_event(meeting: m) }
      expect(parsed_calendar.timezones.size).to eq(1)
    end

    it "contains multiple STANDARD and/or DAYLIGHT components (DST transitions)" do
      meetings.each { |m| builder.add_single_meeting_event(meeting: m) }
      ics = builder.to_ical
      vtimezone_block = ics[/BEGIN:VTIMEZONE.*?END:VTIMEZONE/m]
      expect(vtimezone_block).to be_present
      standard_count = vtimezone_block.scan("BEGIN:STANDARD").size
      daylight_count = vtimezone_block.scan("BEGIN:DAYLIGHT").size
      expect(standard_count).to eq(4)
      expect(daylight_count).to eq(4)
    end

    it "does not add the timezone multiple times when `to_ical` is called multiple times" do
      meetings.each { |m| builder.add_single_meeting_event(meeting: m) }
      builder.to_ical
      builder.to_ical
      expect(parsed_calendar.timezones.size).to eq(1)
    end
  end

  context "with mutlipple recurring meetings in different timezones" do
    let(:project) { create(:project) }
    let(:user) do
      create(:user, firstname: "John", lastname: "Doe", member_with_permissions: { project => [:view_meetings] })
    end

    let(:recurring_new_york) do
      create(:recurring_meeting,
             uid: "ny-series-uid",
             start_time: ActiveSupport::TimeZone["America/New_York"].parse("2025-08-25 09:00"),
             iterations: 10,
             project: project,
             end_after: :iterations,
             time_zone: "America/New_York").tap do |recurring_meeting|
        create(:meeting_participant, :invitee, meeting: recurring_meeting.template, user: user)
      end
    end

    let(:recurring_tokyo) do
      create(:recurring_meeting,
             uid: "tokyo-series-uid",
             start_time: ActiveSupport::TimeZone["Asia/Tokyo"].parse("2025-08-25 09:00"),
             iterations: 10,
             project: project,
             end_after: :iterations,
             time_zone: "Asia/Tokyo").tap do |recurring_meeting|
        create(:meeting_participant, :invitee, meeting: recurring_meeting.template, user: user)
      end
    end

    subject(:builder) { described_class.new(timezone:) }

    before do
      builder.add_series_event(recurring_meeting: recurring_new_york)
      builder.add_series_event(recurring_meeting: recurring_tokyo)
    end

    it "builds the meetings with their respective timezones" do
      parsed_calendar = Icalendar::Calendar.parse(builder.to_ical).first
      expect(parsed_calendar.events.size).to eq(2)

      ny_event = parsed_calendar.events.find { |e| e.uid == "ny-series-uid" }
      tokyo_event = parsed_calendar.events.find { |e| e.uid == "tokyo-series-uid" }

      expect(ny_event).to be_present
      expect(tokyo_event).to be_present

      expect(ny_event.dtstart.ical_params["tzid"].first).to eq("America/New_York")
      expect(ny_event.dtstart.value).to eq(ActiveSupport::TimeZone["America/New_York"].parse("2025-08-25 09:00"))

      expect(tokyo_event.dtstart.ical_params["tzid"].first).to eq("Asia/Tokyo")
      expect(tokyo_event.dtstart.value).to eq(ActiveSupport::TimeZone["Asia/Tokyo"].parse("2025-08-25 09:00"))
    end
  end
end