You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.2 KiB

  1. require 'rails_helper'
  2. RSpec.describe Formatter do
  3. let(:account) { Fabricate(:account, username: 'alice') }
  4. let(:local_text) { 'Hello world http://google.com' }
  5. let(:local_status) { Fabricate(:status, text: local_text, account: account) }
  6. let(:remote_status) { Fabricate(:status, text: '<script>alert("Hello")</script> Beep boop', uri: 'beepboop', account: account) }
  7. let(:local_text_with_mention) { "@#{account.username} @#{account.username}@example.com #{local_text}?x=@#{account.username} #hashtag" }
  8. let(:local_status_with_mention) do
  9. Fabricate(
  10. :status,
  11. text: local_text_with_mention,
  12. account: account,
  13. mentions: [Fabricate(:mention, account: account)]
  14. )
  15. end
  16. describe '#format' do
  17. subject { Formatter.instance.format(local_status) }
  18. context 'with standalone status' do
  19. it 'returns a string' do
  20. expect(subject).to be_a String
  21. end
  22. it 'contains plain text' do
  23. expect(subject).to match('Hello world')
  24. end
  25. it 'contains a link' do
  26. expect(subject).to match('<a href="http://google.com/" rel="nofollow noopener" target="_blank"><span class="invisible">http://</span><span class="">google.com/</span><span class="invisible"></span></a>')
  27. end
  28. it 'contains a mention' do
  29. result = Formatter.instance.format(local_status_with_mention)
  30. expect(result).to match "<a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@<span>#{account.username}</span></a></span>"
  31. expect(result).to match %r{href=\"http://google.com/\?x=@#{account.username}}
  32. expect(result).not_to match "href=\"https://example.com/@#{account.username}"
  33. end
  34. it 'contains a hashtag' do
  35. result = Formatter.instance.format(local_status_with_mention)
  36. expect(result).to match('/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>')
  37. end
  38. end
  39. context 'with cashtag' do
  40. let(:local_text) { 'Hello world $AAPL' }
  41. it 'skip cashtag' do
  42. expect(subject).to match '<p>Hello world $AAPL</p>'
  43. end
  44. end
  45. context 'with reblog' do
  46. let(:local_status) { Fabricate(:status, account: account, reblog: Fabricate(:status, text: 'Hello world', account: account)) }
  47. it 'contains credit to original author' do
  48. expect(subject).to include("RT <span class=\"h-card\"><a href=\"#{TagManager.instance.url_for(account)}\" class=\"u-url mention\">@<span>#{account.username}</span></a></span> Hello world")
  49. end
  50. end
  51. context 'matches a stand-alone medium URL' do
  52. let(:local_text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
  53. it 'has valid url' do
  54. expect(subject).to include('href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"')
  55. end
  56. end
  57. context 'matches a stand-alone google URL' do
  58. let(:local_text) { 'http://google.com' }
  59. it 'has valid url' do
  60. expect(subject).to include('href="http://google.com/"')
  61. end
  62. end
  63. context 'matches a stand-alone IDN URL' do
  64. let(:local_text) { 'https://nic.みんな/' }
  65. it 'has valid url' do
  66. expect(subject).to include('href="https://nic.xn--q9jyb4c/"')
  67. end
  68. it 'has display url' do
  69. expect(subject).to include('<span class="">nic.みんな/</span>')
  70. end
  71. end
  72. context 'matches a URL without trailing period' do
  73. let(:local_text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  74. it 'has valid url' do
  75. expect(subject).to include('href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"')
  76. end
  77. end
  78. xit 'matches a URL without closing paranthesis' do
  79. expect(subject.match('(http://google.com/)')[0]).to eq 'http://google.com'
  80. end
  81. context 'matches a URL without exclamation point' do
  82. let(:local_text) { 'http://www.google.com!' }
  83. it 'has valid url' do
  84. expect(subject).to include('href="http://www.google.com/"')
  85. end
  86. end
  87. context 'matches a URL without single quote' do
  88. let(:local_text) { "http://www.google.com'" }
  89. it 'has valid url' do
  90. expect(subject).to include('href="http://www.google.com/"')
  91. end
  92. end
  93. context 'matches a URL without angle brackets' do
  94. let(:local_text) { 'http://www.google.com>' }
  95. it 'has valid url' do
  96. expect(subject).to include('href="http://www.google.com/"')
  97. end
  98. end
  99. context 'matches a URL with a query string' do
  100. let(:local_text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  101. it 'has valid url' do
  102. expect(subject).to include('href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"')
  103. end
  104. end
  105. context 'matches a URL with parenthesis in it' do
  106. let(:local_text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  107. it 'has valid url' do
  108. expect(subject).to include('href="https://en.wikipedia.org/wiki/Diaspora_(software)"')
  109. end
  110. end
  111. context 'contains html (script tag)' do
  112. let(:local_text) { '<script>alert("Hello")</script>' }
  113. it 'has valid url' do
  114. expect(subject).to match '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  115. end
  116. end
  117. context 'contains html (xss attack)' do
  118. let(:local_text) { %q{<img src="javascript:alert('XSS');">} }
  119. it 'has valid url' do
  120. expect(subject).to match '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  121. end
  122. end
  123. context 'contains invalid URL' do
  124. let(:local_text) { 'http://www\.google\.com' }
  125. it 'has valid url' do
  126. expect(subject).to eq '<p>http://www\.google\.com</p>'
  127. end
  128. end
  129. end
  130. describe '#reformat' do
  131. subject { Formatter.instance.format(remote_status) }
  132. it 'returns a string' do
  133. expect(subject).to be_a String
  134. end
  135. it 'contains plain text' do
  136. expect(subject).to match('Beep boop')
  137. end
  138. it 'does not contain scripts' do
  139. expect(subject).to_not match('<script>alert("Hello")</script>')
  140. end
  141. end
  142. end