discourse/spec/models/post_stripper_spec.rb
Andrei Prigorshnev be2eb3df44
FIX: user got notified about a mention inside a chat message quote (#24229)
When quoting a chat message in a post, if that message contains a mention, 
that mention should be ignored. But we've been detecting them and sending 
notifications to users. This PR fixes the problem. Since this fix is for 
the chat plugin, I had to introduce a new API for plugins:

    # We strip posts before detecting mentions, oneboxes, attachments etc. 
    # We strip those elements that shouldn't be detected. For example, 
    # a mention inside a quote should be ignored, so we strip it off. 
    # Using this API plugins can register their own post strippers. 
    def register_post_stripper(&block) 
    end
2023-11-08 23:13:25 +04:00

55 lines
1.5 KiB
Ruby

# frozen_string_literal: true
RSpec.describe PostStripper do
it "strips mentions in quotes" do
mention = '<a class="mention">@andrei</a>'
cooked = "<aside class='quote'>#{mention}</aside>"
fragment = Nokogiri::HTML5.fragment(cooked)
PostStripper.strip(fragment)
expect(fragment.to_s).to_not include(mention)
end
it "strips group mentions in quotes" do
group_mention = '<a class="mention-group">@moderators</a>'
cooked = "<aside class='quote'>#{group_mention}</aside>"
fragment = Nokogiri::HTML5.fragment(cooked)
PostStripper.strip(fragment)
expect(fragment.to_s).to_not include(group_mention)
end
it "strips oneboxes" do
onebox =
'<aside class="onebox">
Onebox content
</aside>'
cooked = "<p>#{onebox}</p>"
fragment = Nokogiri::HTML5.fragment(cooked)
PostStripper.strip(fragment)
expect(fragment.to_s).to_not include(onebox)
end
context "with plugins" do
after { DiscoursePluginRegistry.reset_register!(:post_strippers) }
it "runs strippers registered by plugins" do
plugin_element = '<div class="plugin_class"></div>'
block = Proc.new { |nokogiri_fragment| nokogiri_fragment.css(".plugin_class").remove }
plugin = OpenStruct.new(enabled?: true)
DiscoursePluginRegistry.register_post_stripper({ block: block }, plugin)
fragment = Nokogiri::HTML5.fragment("<p>#{plugin_element}</p>")
PostStripper.strip(fragment)
expect(fragment.to_s).to_not include(plugin_element)
end
end
end