From 648d2fd79362152e2b07c2f59facf634dde6f550 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 3 Jun 2021 04:36:07 +0300 Subject: [PATCH] DEV: Add test for link watched words (#13251) --- app/models/watched_word.rb | 2 +- spec/models/watched_word_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/watched_word.rb b/app/models/watched_word.rb index ecc239ae67..9251d7d2b6 100644 --- a/app/models/watched_word.rb +++ b/app/models/watched_word.rb @@ -20,7 +20,7 @@ class WatchedWord < ActiveRecord::Base before_validation do self.word = self.class.normalize_word(self.word) if self.action == WatchedWord.actions[:link] && !(self.replacement =~ /^https?:\/\//) - self.replacement = "#{Discourse.base_url}#{self.replacement.starts_with?("/") ? "" : "/"}#{self.replacement}" + self.replacement = "#{Discourse.base_url}#{self.replacement&.starts_with?("/") ? "" : "/"}#{self.replacement}" end end diff --git a/spec/models/watched_word_spec.rb b/spec/models/watched_word_spec.rb index e58a9432b6..a2d425a76c 100644 --- a/spec/models/watched_word_spec.rb +++ b/spec/models/watched_word_spec.rb @@ -90,5 +90,16 @@ describe WatchedWord do expect(w.id).to eq(existing.id) }.to_not change { described_class.count } end + + it "replaces link with absolute URL" do + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta1") + expect(word.replacement).to eq("http://test.localhost/") + + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta2", replacement: "test") + expect(word.replacement).to eq("http://test.localhost/test") + + word = Fabricate(:watched_word, action: described_class.actions[:link], word: "meta3", replacement: "/test") + expect(word.replacement).to eq("http://test.localhost/test") + end end end