FIX: ensure only active users can be on holiday

FIX: ensure only active users can be on holiday

Follow up on a8a560cc675f4a0aa001655128baf99b809d84b6

diff --git a/jobs/scheduled/check_next_regional_holidays.rb b/jobs/scheduled/check_next_regional_holidays.rb
index b3254e9..cb084a5 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 b77f0fd..cb98cfe 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 0000000..832a47c
--- /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

GitHub sha: 62c493cf