闭社主体 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.

127 lines
4.3 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. describe '#format' do
  8. subject { Formatter.instance.format(local_status) }
  9. it 'returns a string' do
  10. expect(subject).to be_a String
  11. end
  12. it 'contains plain text' do
  13. expect(subject).to match('Hello world')
  14. end
  15. it 'contains a link' do
  16. 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>')
  17. end
  18. context 'matches a stand-alone medium URL' do
  19. let(:local_text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
  20. it 'has valid url' do
  21. expect(subject).to include('href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"')
  22. end
  23. end
  24. context 'matches a stand-alone google URL' do
  25. let(:local_text) { 'http://google.com' }
  26. it 'has valid url' do
  27. expect(subject).to include('href="http://google.com/"')
  28. end
  29. end
  30. context 'matches a stand-alone IDN URL' do
  31. let(:local_text) { 'https://nic.みんな/' }
  32. it 'has valid url' do
  33. expect(subject).to include('href="https://nic.xn--q9jyb4c/"')
  34. end
  35. it 'has display url' do
  36. expect(subject).to include('<span class="">nic.みんな/</span>')
  37. end
  38. end
  39. context 'matches a URL without trailing period' do
  40. let(:local_text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  41. it 'has valid url' do
  42. expect(subject).to include('href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"')
  43. end
  44. end
  45. =begin
  46. it 'matches a URL without closing paranthesis' do
  47. expect(subject.match('(http://google.com/)')[0]).to eq 'http://google.com'
  48. end
  49. =end
  50. context 'matches a URL without exclamation point' do
  51. let(:local_text) { 'http://www.google.com!' }
  52. it 'has valid url' do
  53. expect(subject).to include('href="http://www.google.com/"')
  54. end
  55. end
  56. context 'matches a URL without single quote' do
  57. let(:local_text) { "http://www.google.com'" }
  58. it 'has valid url' do
  59. expect(subject).to include('href="http://www.google.com/"')
  60. end
  61. end
  62. context 'matches a URL without angle brackets' do
  63. let(:local_text) { 'http://www.google.com>' }
  64. it 'has valid url' do
  65. expect(subject).to include('href="http://www.google.com/"')
  66. end
  67. end
  68. context 'matches a URL with a query string' do
  69. let(:local_text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  70. it 'has valid url' do
  71. expect(subject).to include('href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"')
  72. end
  73. end
  74. context 'matches a URL with parenthesis in it' do
  75. let(:local_text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  76. it 'has valid url' do
  77. expect(subject).to include('href="https://en.wikipedia.org/wiki/Diaspora_(software)"')
  78. end
  79. end
  80. context 'contains html (script tag)' do
  81. let(:local_text) { '<script>alert("Hello")</script>' }
  82. it 'has valid url' do
  83. expect(subject).to match '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  84. end
  85. end
  86. context 'contains html (xss attack)' do
  87. let(:local_text) { %q{<img src="javascript:alert('XSS');">} }
  88. it 'has valid url' do
  89. expect(subject).to match '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  90. end
  91. end
  92. end
  93. describe '#reformat' do
  94. subject { Formatter.instance.format(remote_status) }
  95. it 'returns a string' do
  96. expect(subject).to be_a String
  97. end
  98. it 'contains plain text' do
  99. expect(subject).to match('Beep boop')
  100. end
  101. it 'does not contain scripts' do
  102. expect(subject).to_not match('<script>alert("Hello")</script>')
  103. end
  104. end
  105. end