generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
371b7d0
commit ac76ba2
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe ApplicationHelper, type: :helper do | ||
describe "#sanitized_notes" do | ||
let(:valid_html) do | ||
'<p class="text-bold">This is <strong>bold</strong> and <em>italic</em>. <a href="http://example.com" target="_blank">Link</a></p>' | ||
end | ||
|
||
let(:invalid_html) do | ||
'<script>alert("XSS")</script><p>This is safe content</p>' | ||
end | ||
|
||
let(:mixed_html) do | ||
'<p>This is <strong>bold</strong> and <script>alert("XSS")</script> <a href="http://example.com">Link</a></p>' | ||
end | ||
|
||
it "allows specific tags and attributes" do | ||
result = helper.sanitized_notes(valid_html) | ||
expect(result).to include('<p class="text-bold">') | ||
expect(result).to include('<strong>bold</strong>') | ||
expect(result).to include('<em>italic</em>') | ||
expect(result).to include('<a href="http://example.com" target="_blank">Link</a>') | ||
end | ||
|
||
it "removes disallowed tags" do | ||
result = helper.sanitized_notes(invalid_html) | ||
expect(result).not_to include('<script>') | ||
end | ||
|
||
it "removes disallowed tags but keeps allowed tags and attributes" do | ||
result = helper.sanitized_notes(mixed_html) | ||
expect(result).to include('<p>This is <strong>bold</strong>') | ||
expect(result).not_to include('<script>') | ||
expect(result).to include('<a href="http://example.com">Link</a>') | ||
end | ||
|
||
it "does not remove allowed attributes from tags" do | ||
html_with_attributes = '<img src="image.png" alt="example image" style="width:100px;">' | ||
result = helper.sanitized_notes(html_with_attributes) | ||
expect(result).to eq('<img src="image.png" alt="example image" style="width:100px;">') | ||
end | ||
end | ||
end |