2022-01-16 13:13:07 -08:00
# frozen_string_literal: true
2021-12-30 11:12:05 -08:00
require " rails_helper "
2022-01-16 13:13:07 -08:00
describe MarkdownHelper , type : :helper do
2021-12-30 11:12:05 -08:00
before do
stub_const ( " APP_CONFIG " , {
2022-01-16 13:13:07 -08:00
" hostname " = > " example.com " ,
" https " = > true ,
" items_per_page " = > 5 ,
" allowed_hosts " = > [
" twitter.com "
]
} )
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
describe " # markdown " do
it " should return the expected text " do
expect ( markdown ( " **Strong text** " ) ) . to eq ( " <p><strong>Strong text</strong></p> " )
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
it " should transform mentions into links " do
2022-07-22 14:20:21 -07:00
expect ( markdown ( " @jake_weary " ) ) . to eq ( '<p><a href="/@jake_weary">@jake_weary</a></p>' )
2021-12-30 11:12:05 -08:00
end
2022-07-11 10:37:53 -07:00
it " should escape text in links " do
expect ( markdown ( " [It's a link](https://example.com) " ) ) . to eq ( '<p><a href="/linkfilter?url=https%3A%2F%2Fexample.com" target="_blank" rel="nofollow">It\'s a link</a></p>' )
expect ( markdown ( " [It's >a link](https://example.com) " ) ) . to eq ( '<p><a href="/linkfilter?url=https%3A%2F%2Fexample.com" target="_blank" rel="nofollow">It\'s >a link</a></p>' )
end
it " should escape HTML tags " do
expect ( markdown ( " I'm <h1>a test</h1> " ) ) . to eq ( " <p>I'm <h1>a test</h1></p> " )
end
2023-02-15 14:51:59 -08:00
it " should turn line breaks into <br> tags " do
expect ( markdown ( " Some \n text " ) ) . to eq ( " <p>Some<br> \n text</p> " )
end
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
describe " # strip_markdown " do
it " should not return formatted text " do
2022-01-16 13:23:13 -08:00
expect ( strip_markdown ( " **Strong text** " ) ) . to eq ( " Strong text " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # twitter_markdown " do
2023-02-05 12:13:37 -08:00
it " should not transform the mention " do
expect ( twitter_markdown ( " @test " ) ) . to eq ( " test " )
2021-12-30 11:12:05 -08:00
end
2022-07-30 10:41:47 -07:00
it " should not strip weird hearts " do
expect ( twitter_markdown ( " <///3 " ) ) . to eq ( " <///3 " )
end
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
describe " # question_markdown " do
it " should link allowed links without the linkfilter " do
expect ( question_markdown ( " https://twitter.com/retrospring " ) ) . to eq ( '<p><a href="https://twitter.com/retrospring" target="_blank" rel="nofollow">https://twitter.com/retrospring</a></p>' )
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
it " should link untrusted links with the linkfilter " do
expect ( question_markdown ( " https://rrerr.net " ) ) . to eq ( '<p><a href="/linkfilter?url=https%3A%2F%2Frrerr.net" target="_blank" rel="nofollow">https://rrerr.net</a></p>' )
2021-12-31 13:24:15 -08:00
end
2022-01-16 13:13:07 -08:00
it " should not process any markup aside of links " do
expect ( question_markdown ( " **your account has been disabled**, [click here to enable it again](https://evil.example.com) " ) ) . to eq ( '<p>your account has been disabled, <a href="/linkfilter?url=https%3A%2F%2Fevil.example.com" target="_blank" rel="nofollow">https://evil.example.com</a></p>' )
2022-01-16 13:28:20 -08:00
expect ( question_markdown ( " > the important question is: will it blend? " ) ) . to eq ( " <p>the important question is: will it blend?</p> " )
2021-12-30 11:12:05 -08:00
end
2022-01-02 10:16:04 -08:00
2022-01-16 13:13:07 -08:00
it " should not raise an exception if an invalid link is processed " do
expect { question_markdown ( " https://example.com/example.質問 " ) } . not_to raise_error
2022-01-02 10:16:04 -08:00
end
2022-01-16 13:13:07 -08:00
it " should not process invalid links " do
expect ( question_markdown ( " https://example.com/example.質問 " ) ) . to eq ( " <p>https://example.com/example.質問</p> " )
2022-01-02 10:16:04 -08:00
end
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
describe " # raw_markdown " do
it " should return the expected text " do
expect ( raw_markdown ( " # Heading " ) ) . to eq ( " <h1>Heading</h1> \n " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # get_markdown " do
it " should return the contents of the specified file " do
expect ( get_markdown ( " spec/fixtures/markdown/test.md " ) ) . to eq ( " # Heading " )
2021-12-30 11:12:05 -08:00
end
2022-01-16 13:13:07 -08:00
it " should return an error message on error " do
expect ( get_markdown ( " non/existant/path.md " ) ) . to eq ( " # Error reading #{ Rails . root . join ( 'non/existant/path.md' ) } " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # markdown_io " do
it " should return the expected text " do
expect ( markdown_io ( " spec/fixtures/markdown/io.md " ) ) . to eq ( " <p><strong>Strong text</strong></p> " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # strip_markdown_io " do
it " should return the expected text " do
expect ( strip_markdown_io ( " spec/fixtures/markdown/io.md " ) ) . to eq ( " Strong text " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # twitter_markdown_io " do
2021-12-30 11:12:05 -08:00
let ( :user ) { FactoryBot . create ( :user ) }
2022-01-16 13:13:07 -08:00
it " should return the expected text " do
2023-02-05 12:13:37 -08:00
expect ( twitter_markdown_io ( " spec/fixtures/markdown/twitter.md " ) ) . to eq ( " test " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
describe " # raw_markdown_io " do
it " should return the expected text " do
expect ( raw_markdown_io ( " spec/fixtures/markdown/test.md " ) ) . to eq ( " <h1>Heading</h1> \n " )
2021-12-30 11:12:05 -08:00
end
end
2022-01-16 13:13:07 -08:00
end