闭社主体 forked from https://github.com/tootsuite/mastodon
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.

293 lines
8.5 KiB

  1. require 'rails_helper'
  2. RSpec.describe Formatter do
  3. let(:local_account) { Fabricate(:account, domain: nil, username: 'alice') }
  4. let(:remote_account) { Fabricate(:account, domain: 'remote', username: 'bob', url: 'https://remote/') }
  5. shared_examples 'encode and link URLs' do
  6. context 'matches a stand-alone medium URL' do
  7. let(:text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
  8. it 'has valid URL' do
  9. is_expected.to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
  10. end
  11. end
  12. context 'matches a stand-alone google URL' do
  13. let(:text) { 'http://google.com' }
  14. it 'has valid URL' do
  15. is_expected.to include 'href="http://google.com/"'
  16. end
  17. end
  18. context 'matches a stand-alone IDN URL' do
  19. let(:text) { 'https://nic.みんな/' }
  20. it 'has valid URL' do
  21. is_expected.to include 'href="https://nic.xn--q9jyb4c/"'
  22. end
  23. it 'has display URL' do
  24. is_expected.to include '<span class="">nic.みんな/</span>'
  25. end
  26. end
  27. context 'matches a URL without trailing period' do
  28. let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  29. it 'has valid URL' do
  30. is_expected.to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
  31. end
  32. end
  33. context 'matches a URL without closing paranthesis' do
  34. let(:text) { '(http://google.com/)' }
  35. it 'has valid URL' do
  36. is_expected.to include 'href="http://google.com/"'
  37. end
  38. end
  39. context 'matches a URL without exclamation point' do
  40. let(:text) { 'http://www.google.com!' }
  41. it 'has valid URL' do
  42. is_expected.to include 'href="http://www.google.com/"'
  43. end
  44. end
  45. context 'matches a URL without single quote' do
  46. let(:text) { "http://www.google.com'" }
  47. it 'has valid URL' do
  48. is_expected.to include 'href="http://www.google.com/"'
  49. end
  50. end
  51. context 'matches a URL without angle brackets' do
  52. let(:text) { 'http://www.google.com>' }
  53. it 'has valid URL' do
  54. is_expected.to include 'href="http://www.google.com/"'
  55. end
  56. end
  57. context 'matches a URL with a query string' do
  58. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  59. it 'has valid URL' do
  60. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"'
  61. end
  62. end
  63. context 'matches a URL with parenthesis in it' do
  64. let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  65. it 'has valid URL' do
  66. is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
  67. end
  68. end
  69. context 'contains HTML (script tag)' do
  70. let(:text) { '<script>alert("Hello")</script>' }
  71. it 'has escaped HTML' do
  72. is_expected.to include '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  73. end
  74. end
  75. context 'contains HTML (XSS attack)' do
  76. let(:text) { %q{<img src="javascript:alert('XSS');">} }
  77. it 'has escaped HTML' do
  78. is_expected.to include '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  79. end
  80. end
  81. context 'contains invalid URL' do
  82. let(:text) { 'http://www\.google\.com' }
  83. it 'has raw URL' do
  84. is_expected.to eq '<p>http://www\.google\.com</p>'
  85. end
  86. end
  87. context 'contains a hashtag' do
  88. let(:text) { '#hashtag' }
  89. it 'has a link' do
  90. is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
  91. end
  92. end
  93. end
  94. describe '#format' do
  95. subject { Formatter.instance.format(status) }
  96. context 'with local status' do
  97. context 'with reblog' do
  98. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  99. let(:status) { Fabricate(:status, reblog: reblog) }
  100. it 'returns original status with credit to its author' do
  101. is_expected.to include 'RT <span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span> Hello world'
  102. end
  103. end
  104. context 'contains plain text' do
  105. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  106. it 'paragraphizes' do
  107. is_expected.to eq '<p>text</p>'
  108. end
  109. end
  110. context 'contains line feeds' do
  111. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  112. it 'removes line feeds' do
  113. is_expected.not_to include "\n"
  114. end
  115. end
  116. context 'contains linkable mentions' do
  117. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  118. it 'links' do
  119. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  120. end
  121. end
  122. context 'contains unlinkable mentions' do
  123. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  124. it 'does not link' do
  125. is_expected.to include '@alice'
  126. end
  127. end
  128. context do
  129. subject do
  130. status = Fabricate(:status, text: text, uri: nil)
  131. Formatter.instance.format(status)
  132. end
  133. include_examples 'encode and link URLs'
  134. end
  135. end
  136. context 'with remote status' do
  137. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  138. it 'reformats' do
  139. is_expected.to eq 'Beep boop'
  140. end
  141. end
  142. end
  143. describe '#reformat' do
  144. subject { Formatter.instance.reformat(text) }
  145. context 'contains plain text' do
  146. let(:text) { 'Beep boop' }
  147. it 'contains plain text' do
  148. is_expected.to include 'Beep boop'
  149. end
  150. end
  151. context 'contains scripts' do
  152. let(:text) { '<script>alert("Hello")</script>' }
  153. it 'strips scripts' do
  154. is_expected.to_not include '<script>alert("Hello")</script>'
  155. end
  156. end
  157. context 'contains malicious classes' do
  158. let(:text) { '<span class="status__content__spoiler-link">Show more</span>' }
  159. it 'strips malicious classes' do
  160. is_expected.to_not include 'status__content__spoiler-link'
  161. end
  162. end
  163. end
  164. describe '#plaintext' do
  165. subject { Formatter.instance.plaintext(status) }
  166. context 'with local status' do
  167. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  168. it 'returns raw text' do
  169. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  170. end
  171. end
  172. context 'with remote status' do
  173. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  174. it 'returns tag-stripped text' do
  175. is_expected.to eq ''
  176. end
  177. end
  178. end
  179. describe '#simplified_format' do
  180. subject { Formatter.instance.simplified_format(account) }
  181. context 'with local status' do
  182. let(:account) { Fabricate(:account, domain: nil, note: text) }
  183. context 'contains linkable mentions for local accounts' do
  184. let(:text) { '@alice' }
  185. before { local_account }
  186. it 'links' do
  187. is_expected.to eq '<p><span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span></p>'
  188. end
  189. end
  190. context 'contains linkable mentions for remote accounts' do
  191. let(:text) { '@bob@remote' }
  192. before { remote_account }
  193. it 'links' do
  194. is_expected.to eq '<p><span class="h-card"><a href="https://remote/" class="u-url mention">@<span>bob</span></a></span></p>'
  195. end
  196. end
  197. context 'contains unlinkable mentions' do
  198. let(:text) { '@alice' }
  199. it 'returns raw mention texts' do
  200. is_expected.to eq '<p>@alice</p>'
  201. end
  202. end
  203. include_examples 'encode and link URLs'
  204. end
  205. context 'with remote status' do
  206. let(:text) { '<script>alert("Hello")</script>' }
  207. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  208. it 'reformats' do
  209. is_expected.to_not include '<script>alert("Hello")</script>'
  210. end
  211. end
  212. end
  213. describe '#sanitize' do
  214. let(:html) { '<script>alert("Hello")</script>' }
  215. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  216. it 'sanitizes' do
  217. is_expected.to eq 'alert("Hello")'
  218. end
  219. end
  220. end