From bb08d6e65daf01c6e342d5fdea4db58a6635aee0 Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Tue, 21 Mar 2023 16:50:00 -0400 Subject: [PATCH] correctly read RSS feeds from a url (#2681) --- apps/dashboard/app/models/motd_file.rb | 12 +++++- apps/dashboard/test/models/motd_file_test.rb | 9 ++++ apps/dashboard/test/models/motd_test.rb | 43 -------------------- 3 files changed, 20 insertions(+), 44 deletions(-) delete mode 100644 apps/dashboard/test/models/motd_test.rb diff --git a/apps/dashboard/app/models/motd_file.rb b/apps/dashboard/app/models/motd_file.rb index 7368593a77..8ff16a3d08 100644 --- a/apps/dashboard/app/models/motd_file.rb +++ b/apps/dashboard/app/models/motd_file.rb @@ -61,7 +61,17 @@ def exist? private def load(motd_uri) - motd_uri ? open(motd_uri).read : nil + uri = URI.parse(motd_uri) + + case uri.scheme + when 'http', 'https' + uri.read + when nil + File.read(uri.to_s) + else + Rails.logger.warn("Unknown scheme for #{motd_uri}. No MOTD is loaded") + nil + end rescue Errno::ENOENT Rails.logger.warn "MOTD File is missing; it was expected at #{motd_uri}" nil diff --git a/apps/dashboard/test/models/motd_file_test.rb b/apps/dashboard/test/models/motd_file_test.rb index 7c953028c3..5a6fbc71c3 100644 --- a/apps/dashboard/test/models/motd_file_test.rb +++ b/apps/dashboard/test/models/motd_file_test.rb @@ -40,4 +40,13 @@ class MotdFileTest < ActiveSupport::TestCase assert_equal '', motd_file.content end + test 'when rss is a remote source' do + with_modified_env({ MOTD_PATH: 'https://www.osc.edu/rss.xml', MOTD_FORMAT: 'rss' }) do + formatter = MotdFile.new.formatter + assert_not_nil(formatter.title) + assert_not_nil(formatter.content) + assert_not_nil(formatter.content.items) + end + end + end diff --git a/apps/dashboard/test/models/motd_test.rb b/apps/dashboard/test/models/motd_test.rb deleted file mode 100644 index 7f05e64997..0000000000 --- a/apps/dashboard/test/models/motd_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'test_helper' - -class MotdTest < ActiveSupport::TestCase - - test "test when motd valid" do - - path = "#{Rails.root}/test/fixtures/files/motd_valid" - motd_file = MotdFile.new(path) - expected_file = File.open(path).read - - assert_equal true, motd_file.exist? - assert_equal path, motd_file.motd_path - assert_equal expected_file, motd_file.content - end - - test "test when motd empty" do - path = "#{Rails.root}/test/fixtures/files/motd_empty" - motd_file = MotdFile.new(path) - - assert_equal true, motd_file.exist? - assert_equal path, motd_file.motd_path - assert_equal '', motd_file.content - end - - test "test when motd missing" do - path = "#{Rails.root}/test/fixtures/files/motd_missing" - motd_file = MotdFile.new(path) - - assert_equal false, motd_file.exist? - assert_equal path, motd_file.motd_path - assert_equal '', motd_file.content - end - - test "test when motd nil" do - - motd_file = MotdFile.new(nil) - - assert_equal false, motd_file.exist? - assert_nil motd_file.motd_path - assert_equal '', motd_file.content - end - -end