DEV: Add test for link watched words (#13251)

This commit is contained in:
Bianca Nenciu 2021-06-03 04:36:07 +03:00 committed by GitHub
parent 8cfe203383
commit 648d2fd793
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -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

View File

@ -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