From 62c493cf306041e1a15c211e9d32d7ca1812845c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Fri, 2 Aug 2019 18:05:06 +0200 Subject: [PATCH] FIX: ensure only active users can be on holiday Follow up on a8a560cc675f4a0aa001655128baf99b809d84b6 --- .../scheduled/check_next_regional_holidays.rb | 2 + plugin.rb | 8 +- .../jobs/check_next_regional_holidays_spec.rb | 99 +++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 spec/jobs/check_next_regional_holidays_spec.rb diff --git a/jobs/scheduled/check_next_regional_holidays.rb b/jobs/scheduled/check_next_regional_holidays.rb index b3254e90..cb084a53 100644 --- a/jobs/scheduled/check_next_regional_holidays.rb +++ b/jobs/scheduled/check_next_regional_holidays.rb @@ -46,6 +46,8 @@ module Jobs end users_in_region[region].each do |user_id| + next unless usernames[user_id] + date = if tz = user_timezones[user_id] next_holiday[:date].in_time_zone(tz).iso8601 else diff --git a/plugin.rb b/plugin.rb index b77f0fdf..cb98cfe2 100644 --- a/plugin.rb +++ b/plugin.rb @@ -141,10 +141,12 @@ after_initialize do Post.class_eval do attr_accessor :calendar + def calendar_holidays + custom_fields[DiscourseCalendar::CALENDAR_HOLIDAYS_CUSTOM_FIELD] || [] + end + def calendar_details - details = custom_fields[DiscourseCalendar::CALENDAR_DETAILS_CUSTOM_FIELD] || {} - details = details[0] if details.kind_of?(Array) # investigate why sometimes it has been saved as an array - details + custom_fields[DiscourseCalendar::CALENDAR_DETAILS_CUSTOM_FIELD] || {} end def calendar_details=(val) diff --git a/spec/jobs/check_next_regional_holidays_spec.rb b/spec/jobs/check_next_regional_holidays_spec.rb new file mode 100644 index 00000000..832a47c8 --- /dev/null +++ b/spec/jobs/check_next_regional_holidays_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe DiscourseCalendar::CheckNextRegionalHolidays do + + before do + Jobs.run_immediately! + SiteSetting.calendar_enabled = true + + @op = create_post(raw: "[calendar]\n[/calendar]") + SiteSetting.holiday_calendar_topic_id = @op.topic_id + end + + it "works" do + frenchy = Fabricate(:user) + frenchy.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + frenchy.save! + + freeze_time Time.new(2019, 8, 1) + + subject.execute(nil) + @op.reload + + expect(@op.calendar_holidays).to eq([ + ["fr", "Assomption", "2019-08-15", frenchy.username] + ]) + end + + it "only checks for holidays during business days" do + frenchy = Fabricate(:user) + frenchy.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + frenchy.save! + + freeze_time Time.new(2019, 7, 1) + + subject.execute(nil) + @op.reload + + # The "FĂȘte Nationale" is on July 14th but it's on a Sunday in 2019 + expect(@op.calendar_holidays).to eq([]) + end + + it "only checks for holidays within the current year" do + frenchy = Fabricate(:user) + frenchy.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + frenchy.save! + + freeze_time Time.new(2019, 12, 29) + + subject.execute(nil) + @op.reload + + # We don't want 2020/1/1 + expect(@op.calendar_holidays).to eq([]) + end + + it "uses the user TZ when available" do + frenchy = Fabricate(:user) + frenchy.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + frenchy.custom_fields[DiscourseCalendar::TIMEZONE_CUSTOM_FIELD] = "Europe/Paris" + frenchy.save! + + freeze_time Time.new(2019, 8, 1) + + subject.execute(nil) + @op.reload + + expect(@op.calendar_holidays).to eq([ + ["fr", "Assomption", "2019-08-15T00:00:00+02:00", frenchy.username] + ]) + end + + it "only takes into account active users" do + robot = Fabricate(:user, id: -100) + robot.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + robot.save! + + inactive = Fabricate(:user, active: false) + inactive.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + inactive.save! + + suspended = Fabricate(:user, suspended_till: 1.year.from_now) + suspended.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + suspended.save! + + silenced = Fabricate(:user, silenced_till: 1.year.from_now) + silenced.custom_fields[DiscourseCalendar::REGION_CUSTOM_FIELD] = "fr" + silenced.save! + + freeze_time Time.new(2019, 8, 1) + + subject.execute(nil) + @op.reload + + expect(@op.calendar_holidays).to eq([]) + end + +end