The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

611 rivejä
20 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.test', username: 'bob', url: 'https://remote.test/') }
  5. shared_examples 'encode and link URLs' do
  6. context 'given 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 'matches the full 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 'given a stand-alone google URL' do
  13. let(:text) { 'http://google.com' }
  14. it 'matches the full URL' do
  15. is_expected.to include 'href="http://google.com"'
  16. end
  17. end
  18. context 'given a stand-alone IDN URL' do
  19. let(:text) { 'https://nic.みんな/' }
  20. it 'matches the full URL' do
  21. is_expected.to include 'href="https://nic.みんな/"'
  22. end
  23. it 'has display URL' do
  24. is_expected.to include '<span class="">nic.みんな/</span>'
  25. end
  26. end
  27. context 'given a URL with a trailing period' do
  28. let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  29. it 'matches the full URL but not the period' 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 'given a URL enclosed with parentheses' do
  34. let(:text) { '(http://google.com/)' }
  35. it 'matches the full URL but not the parentheses' do
  36. is_expected.to include 'href="http://google.com/"'
  37. end
  38. end
  39. context 'given a URL with a trailing exclamation point' do
  40. let(:text) { 'http://www.google.com!' }
  41. it 'matches the full URL but not the exclamation point' do
  42. is_expected.to include 'href="http://www.google.com"'
  43. end
  44. end
  45. context 'given a URL with a trailing single quote' do
  46. let(:text) { "http://www.google.com'" }
  47. it 'matches the full URL but not the single quote' do
  48. is_expected.to include 'href="http://www.google.com"'
  49. end
  50. end
  51. context 'given a URL with a trailing angle bracket' do
  52. let(:text) { 'http://www.google.com>' }
  53. it 'matches the full URL but not the angle bracket' do
  54. is_expected.to include 'href="http://www.google.com"'
  55. end
  56. end
  57. context 'given a URL with a query string' do
  58. context 'with escaped unicode character' do
  59. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  60. it 'matches the full URL' do
  61. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"'
  62. end
  63. end
  64. context 'with unicode character' do
  65. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓&q=autolink' }
  66. it 'matches the full URL' do
  67. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓&amp;q=autolink"'
  68. end
  69. end
  70. context 'with unicode character at the end' do
  71. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓' }
  72. it 'matches the full URL' do
  73. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓"'
  74. end
  75. end
  76. context 'with escaped and not escaped unicode characters' do
  77. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink' }
  78. it 'preserves escaped unicode characters' do
  79. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;utf81=✓&amp;q=autolink"'
  80. end
  81. end
  82. end
  83. context 'given a URL with parentheses in it' do
  84. let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  85. it 'matches the full URL' do
  86. is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
  87. end
  88. end
  89. context 'given a URL in quotation marks' do
  90. let(:text) { '"https://example.com/"' }
  91. it 'does not match the quotation marks' do
  92. is_expected.to include 'href="https://example.com/"'
  93. end
  94. end
  95. context 'given a URL in angle brackets' do
  96. let(:text) { '<https://example.com/>' }
  97. it 'does not match the angle brackets' do
  98. is_expected.to include 'href="https://example.com/"'
  99. end
  100. end
  101. context 'given a URL with Japanese path string' do
  102. let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
  103. it 'matches the full URL' do
  104. is_expected.to include 'href="https://ja.wikipedia.org/wiki/日本"'
  105. end
  106. end
  107. context 'given a URL with Korean path string' do
  108. let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
  109. it 'matches the full URL' do
  110. is_expected.to include 'href="https://ko.wikipedia.org/wiki/대한민국"'
  111. end
  112. end
  113. context 'given a URL with a full-width space' do
  114. let(:text) { 'https://example.com/ abc123' }
  115. it 'does not match the full-width space' do
  116. is_expected.to include 'href="https://example.com/"'
  117. end
  118. end
  119. context 'given a URL in Japanese quotation marks' do
  120. let(:text) { '「[https://example.org/」' }
  121. it 'does not match the quotation marks' do
  122. is_expected.to include 'href="https://example.org/"'
  123. end
  124. end
  125. context 'given a URL with Simplified Chinese path string' do
  126. let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
  127. it 'matches the full URL' do
  128. is_expected.to include 'href="https://baike.baidu.com/item/中华人民共和国"'
  129. end
  130. end
  131. context 'given a URL with Traditional Chinese path string' do
  132. let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
  133. it 'matches the full URL' do
  134. is_expected.to include 'href="https://zh.wikipedia.org/wiki/臺灣"'
  135. end
  136. end
  137. context 'given a URL containing unsafe code (XSS attack, visible part)' do
  138. let(:text) { %q{http://example.com/b<del>b</del>} }
  139. it 'does not include the HTML in the URL' do
  140. is_expected.to include '"http://example.com/b"'
  141. end
  142. it 'escapes the HTML' do
  143. is_expected.to include '&lt;del&gt;b&lt;/del&gt;'
  144. end
  145. end
  146. context 'given a URL containing unsafe code (XSS attack, invisible part)' do
  147. let(:text) { %q{http://example.com/blahblahblahblah/a<script>alert("Hello")</script>} }
  148. it 'does not include the HTML in the URL' do
  149. is_expected.to include '"http://example.com/blahblahblahblah/a"'
  150. end
  151. it 'escapes the HTML' do
  152. is_expected.to include '&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;'
  153. end
  154. end
  155. context 'given text containing HTML code (script tag)' do
  156. let(:text) { '<script>alert("Hello")</script>' }
  157. it 'escapes the HTML' do
  158. is_expected.to include '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  159. end
  160. end
  161. context 'given text containing HTML (XSS attack)' do
  162. let(:text) { %q{<img src="javascript:alert('XSS');">} }
  163. it 'escapes the HTML' do
  164. is_expected.to include '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  165. end
  166. end
  167. context 'given an invalid URL' do
  168. let(:text) { 'http://www\.google\.com' }
  169. it 'outputs the raw URL' do
  170. is_expected.to eq '<p>http://www\.google\.com</p>'
  171. end
  172. end
  173. context 'given text containing a hashtag' do
  174. let(:text) { '#hashtag' }
  175. it 'creates a hashtag link' do
  176. is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
  177. end
  178. end
  179. context 'given text containing a hashtag with Unicode chars' do
  180. let(:text) { '#hashtagタグ' }
  181. it 'creates a hashtag link' do
  182. is_expected.to include '/tags/hashtag%E3%82%BF%E3%82%B0" class="mention hashtag" rel="tag">#<span>hashtagタグ</span></a>'
  183. end
  184. end
  185. context 'given a stand-alone xmpp: URI' do
  186. let(:text) { 'xmpp:user@instance.com' }
  187. it 'matches the full URI' do
  188. is_expected.to include 'href="xmpp:user@instance.com"'
  189. end
  190. end
  191. context 'given a an xmpp: URI with a query-string' do
  192. let(:text) { 'please join xmpp:muc@instance.com?join right now' }
  193. it 'matches the full URI' do
  194. is_expected.to include 'href="xmpp:muc@instance.com?join"'
  195. end
  196. end
  197. end
  198. describe '#format_spoiler' do
  199. subject { Formatter.instance.format_spoiler(status) }
  200. context 'given a post containing plain text' do
  201. let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
  202. it 'Returns the spoiler text' do
  203. is_expected.to eq 'Secret!'
  204. end
  205. end
  206. context 'given a post with an emoji shortcode at the start' do
  207. let!(:emoji) { Fabricate(:custom_emoji) }
  208. let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
  209. let(:text) { ':coolcat: Beep boop' }
  210. it 'converts the shortcode to an image tag' do
  211. is_expected.to match(/<img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  212. end
  213. end
  214. end
  215. describe '#format' do
  216. subject { Formatter.instance.format(status) }
  217. context 'given a post with local status' do
  218. context 'given a reblogged post' do
  219. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  220. let(:status) { Fabricate(:status, reblog: reblog) }
  221. it 'returns original status with credit to its author' do
  222. 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'
  223. end
  224. end
  225. context 'given a post containing plain text' do
  226. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  227. it 'paragraphizes the text' do
  228. is_expected.to eq '<p>text</p>'
  229. end
  230. end
  231. context 'given a post containing line feeds' do
  232. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  233. it 'removes line feeds' do
  234. is_expected.not_to include "\n"
  235. end
  236. end
  237. context 'given a post containing linkable mentions' do
  238. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  239. it 'creates a mention link' do
  240. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  241. end
  242. end
  243. context 'given a post containing unlinkable mentions' do
  244. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  245. it 'does not create a mention link' do
  246. is_expected.to include '@alice'
  247. end
  248. end
  249. context do
  250. subject do
  251. status = Fabricate(:status, text: text, uri: nil)
  252. Formatter.instance.format(status)
  253. end
  254. include_examples 'encode and link URLs'
  255. end
  256. context 'given a post with custom_emojify option' do
  257. let!(:emoji) { Fabricate(:custom_emoji) }
  258. let(:status) { Fabricate(:status, account: local_account, text: text) }
  259. subject { Formatter.instance.format(status, custom_emojify: true) }
  260. context 'given a post with an emoji shortcode at the start' do
  261. let(:text) { ':coolcat: Beep boop' }
  262. it 'converts the shortcode to an image tag' do
  263. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  264. end
  265. end
  266. context 'given a post with an emoji shortcode in the middle' do
  267. let(:text) { 'Beep :coolcat: boop' }
  268. it 'converts the shortcode to an image tag' do
  269. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  270. end
  271. end
  272. context 'given a post with concatenated emoji shortcodes' do
  273. let(:text) { ':coolcat::coolcat:' }
  274. it 'does not touch the shortcodes' do
  275. is_expected.to match(/:coolcat::coolcat:/)
  276. end
  277. end
  278. context 'given a post with an emoji shortcode at the end' do
  279. let(:text) { 'Beep boop :coolcat:' }
  280. it 'converts the shortcode to an image tag' do
  281. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  282. end
  283. end
  284. end
  285. end
  286. context 'given a post with remote status' do
  287. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  288. it 'reformats the post' do
  289. is_expected.to eq 'Beep boop'
  290. end
  291. context 'given a post with custom_emojify option' do
  292. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  293. let(:status) { Fabricate(:status, account: remote_account, text: text) }
  294. subject { Formatter.instance.format(status, custom_emojify: true) }
  295. context 'given a post with an emoji shortcode at the start' do
  296. let(:text) { '<p>:coolcat: Beep boop<br />' }
  297. it 'converts the shortcode to an image tag' do
  298. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  299. end
  300. end
  301. context 'given a post with an emoji shortcode in the middle' do
  302. let(:text) { '<p>Beep :coolcat: boop</p>' }
  303. it 'converts the shortcode to an image tag' do
  304. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  305. end
  306. end
  307. context 'given a post with concatenated emoji' do
  308. let(:text) { '<p>:coolcat::coolcat:</p>' }
  309. it 'does not touch the shortcodes' do
  310. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  311. end
  312. end
  313. context 'given a post with an emoji shortcode at the end' do
  314. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  315. it 'converts the shortcode to an image tag' do
  316. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  317. end
  318. end
  319. end
  320. end
  321. end
  322. describe '#reformat' do
  323. subject { Formatter.instance.reformat(text) }
  324. context 'given a post containing plain text' do
  325. let(:text) { 'Beep boop' }
  326. it 'keeps the plain text' do
  327. is_expected.to include 'Beep boop'
  328. end
  329. end
  330. context 'given a post containing script tags' do
  331. let(:text) { '<script>alert("Hello")</script>' }
  332. it 'strips the scripts' do
  333. is_expected.to_not include '<script>alert("Hello")</script>'
  334. end
  335. end
  336. context 'given a post containing malicious classes' do
  337. let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
  338. it 'strips the malicious classes' do
  339. is_expected.to_not include 'status__content__spoiler-link'
  340. end
  341. end
  342. end
  343. describe '#plaintext' do
  344. subject { Formatter.instance.plaintext(status) }
  345. context 'given a post with local status' do
  346. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  347. it 'returns the raw text' do
  348. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  349. end
  350. end
  351. context 'given a post with remote status' do
  352. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  353. it 'returns tag-stripped text' do
  354. is_expected.to eq ''
  355. end
  356. end
  357. end
  358. describe '#simplified_format' do
  359. subject { Formatter.instance.simplified_format(account) }
  360. context 'given a post with local status' do
  361. let(:account) { Fabricate(:account, domain: nil, note: text) }
  362. context 'given a post containing linkable mentions for local accounts' do
  363. let(:text) { '@alice' }
  364. before { local_account }
  365. it 'creates a mention link' do
  366. 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>'
  367. end
  368. end
  369. context 'given a post containing linkable mentions for remote accounts' do
  370. let(:text) { '@bob@remote.test' }
  371. before { remote_account }
  372. it 'creates a mention link' do
  373. is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
  374. end
  375. end
  376. context 'given a post containing unlinkable mentions' do
  377. let(:text) { '@alice' }
  378. it 'does not create a mention link' do
  379. is_expected.to eq '<p>@alice</p>'
  380. end
  381. end
  382. context 'given a post with custom_emojify option' do
  383. let!(:emoji) { Fabricate(:custom_emoji) }
  384. before { account.note = text }
  385. subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
  386. context 'given a post with an emoji shortcode at the start' do
  387. let(:text) { ':coolcat: Beep boop' }
  388. it 'converts the shortcode to an image tag' do
  389. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  390. end
  391. end
  392. context 'given a post with an emoji shortcode in the middle' do
  393. let(:text) { 'Beep :coolcat: boop' }
  394. it 'converts the shortcode to an image tag' do
  395. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  396. end
  397. end
  398. context 'given a post with concatenated emoji shortcodes' do
  399. let(:text) { ':coolcat::coolcat:' }
  400. it 'does not touch the shortcodes' do
  401. is_expected.to match(/:coolcat::coolcat:/)
  402. end
  403. end
  404. context 'given a post with an emoji shortcode at the end' do
  405. let(:text) { 'Beep boop :coolcat:' }
  406. it 'converts the shortcode to an image tag' do
  407. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  408. end
  409. end
  410. end
  411. include_examples 'encode and link URLs'
  412. end
  413. context 'given a post with remote status' do
  414. let(:text) { '<script>alert("Hello")</script>' }
  415. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  416. it 'reformats' do
  417. is_expected.to_not include '<script>alert("Hello")</script>'
  418. end
  419. context 'with custom_emojify option' do
  420. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  421. before { remote_account.note = text }
  422. subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
  423. context 'given a post with an emoji shortcode at the start' do
  424. let(:text) { '<p>:coolcat: Beep boop<br />' }
  425. it 'converts shortcode to image tag' do
  426. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  427. end
  428. end
  429. context 'given a post with an emoji shortcode in the middle' do
  430. let(:text) { '<p>Beep :coolcat: boop</p>' }
  431. it 'converts shortcode to image tag' do
  432. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  433. end
  434. end
  435. context 'given a post with concatenated emoji shortcodes' do
  436. let(:text) { '<p>:coolcat::coolcat:</p>' }
  437. it 'does not touch the shortcodes' do
  438. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  439. end
  440. end
  441. context 'given a post with an emoji shortcode at the end' do
  442. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  443. it 'converts shortcode to image tag' do
  444. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  445. end
  446. end
  447. end
  448. end
  449. end
  450. describe '#sanitize' do
  451. let(:html) { '<script>alert("Hello")</script>' }
  452. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  453. it 'sanitizes' do
  454. is_expected.to eq ''
  455. end
  456. end
  457. end