|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | + |
| 5 | +class UnitHubCalendarTest < ActiveSupport::TestCase |
| 6 | + setup do |
| 7 | + @student = FactoryBot.create(:user, :student) |
| 8 | + @unit = FactoryBot.create(:unit, with_students: false, task_count: 0) |
| 9 | + @project = @unit.enrol_student(@student, @unit.tutorials.first.campus) |
| 10 | + @webcal = Webcal.create!(user: @student, guid: SecureRandom.uuid) |
| 11 | + @session = @unit.unit_learning_sessions.create!(title: 'HelpHub', start_at: 1.day.from_now, end_at: 1.day.from_now + 1.hour, |
| 12 | + published: true, join_url: 'https://teams.microsoft.com/l/meetup-join/example') |
| 13 | + end |
| 14 | + |
| 15 | + def session_events |
| 16 | + Icalendar::Calendar.parse(@webcal.to_ical.to_ical).first.events.select { |event| event.uid.to_s.start_with?('ontrack-session-') } |
| 17 | + end |
| 18 | + |
| 19 | + def test_calendar_is_explicit_opt_in_and_works_for_units_without_tasks |
| 20 | + assert_not @webcal.include_learning_sessions? |
| 21 | + assert_empty session_events |
| 22 | + @webcal.update!(include_learning_sessions: true) |
| 23 | + event = session_events.first |
| 24 | + assert_equal "ontrack-session-#{@session.id}-0", event.uid.to_s |
| 25 | + assert_equal @session.join_url, event.url.to_s |
| 26 | + assert_equal @session.start_at.utc.to_i, event.dtstart.to_time.to_i |
| 27 | + end |
| 28 | + |
| 29 | + def test_calendar_rechecks_withdrawal_and_exclusions |
| 30 | + @webcal.update!(include_learning_sessions: true) |
| 31 | + assert_equal 1, session_events.length |
| 32 | + @webcal.webcal_unit_exclusions.create!(unit: @unit) |
| 33 | + assert_empty session_events |
| 34 | + @webcal.webcal_unit_exclusions.destroy_all |
| 35 | + @project.update!(enrolled: false) |
| 36 | + assert_empty session_events |
| 37 | + end |
| 38 | + |
| 39 | + def test_other_units_and_drafts_never_enter_subscription |
| 40 | + @webcal.update!(include_learning_sessions: true) |
| 41 | + other = FactoryBot.create(:unit, with_students: false, task_count: 0) |
| 42 | + other.unit_learning_sessions.create!(title: 'Secret', start_at: 1.day.from_now, end_at: 1.day.from_now + 1.hour, published: true) |
| 43 | + @unit.unit_learning_sessions.create!(title: 'Draft', start_at: 1.day.from_now, end_at: 1.day.from_now + 1.hour) |
| 44 | + assert_equal 1, session_events.length |
| 45 | + @unit.update!(active: false) |
| 46 | + assert_empty session_events |
| 47 | + end |
| 48 | + |
| 49 | + def test_sessions_remain_available_on_the_final_day_of_a_unit |
| 50 | + @webcal.update!(include_learning_sessions: true) |
| 51 | + @unit.update!(end_date: Time.zone.today) |
| 52 | + assert_equal [@session.id], @webcal.learning_sessions.pluck(:id) |
| 53 | + end |
| 54 | + |
| 55 | + def test_cancellation_and_rescheduling_keep_stable_uid_and_remove_join_link |
| 56 | + @webcal.update!(include_learning_sessions: true) |
| 57 | + first = session_events.first |
| 58 | + @session.update!(start_at: @session.start_at + 1.hour, end_at: @session.end_at + 1.hour) |
| 59 | + moved = session_events.first |
| 60 | + assert_equal first.uid, moved.uid |
| 61 | + assert_not_equal first.dtstart, moved.dtstart |
| 62 | + assert_operator moved.sequence.to_i, :>, first.sequence.to_i |
| 63 | + @session.update!(cancelled: true) |
| 64 | + cancelled = session_events.first |
| 65 | + assert_equal first.uid, cancelled.uid |
| 66 | + assert_equal 'CANCELLED', cancelled.status.to_s |
| 67 | + assert_nil cancelled.url |
| 68 | + assert_not_includes cancelled.description.to_s, 'https://' |
| 69 | + end |
| 70 | +end |
0 commit comments