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

116 lines
4.0 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 URL without trailing period' do
  31. let(:local_text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  32. it 'has valid url' do
  33. expect(subject).to include('href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"')
  34. end
  35. end
  36. =begin
  37. it 'matches a URL without closing paranthesis' do
  38. expect(subject.match('(http://google.com/)')[0]).to eq 'http://google.com'
  39. end
  40. =end
  41. context 'matches a URL without exclamation point' do
  42. let(:local_text) { 'http://www.google.com!' }
  43. it 'has valid url' do
  44. expect(subject).to include('href="http://www.google.com"')
  45. end
  46. end
  47. context 'matches a URL without single quote' do
  48. let(:local_text) { "http://www.google.com'" }
  49. it 'has valid url' do
  50. expect(subject).to include('href="http://www.google.com"')
  51. end
  52. end
  53. context 'matches a URL without angle brackets' do
  54. let(:local_text) { 'http://www.google.com>' }
  55. it 'has valid url' do
  56. expect(subject).to include('href="http://www.google.com"')
  57. end
  58. end
  59. context 'matches a URL with a query string' do
  60. let(:local_text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  61. it 'has valid url' do
  62. expect(subject).to include('href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"')
  63. end
  64. end
  65. context 'matches a URL with parenthesis in it' do
  66. let(:local_text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  67. it 'has valid url' do
  68. expect(subject).to include('href="https://en.wikipedia.org/wiki/Diaspora_(software)"')
  69. end
  70. end
  71. context 'contains html (script tag)' do
  72. let(:local_text) { '<script>alert("Hello")</script>' }
  73. it 'has valid url' do
  74. expect(subject).to match '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  75. end
  76. end
  77. context 'contains html (xss attack)' do
  78. let(:local_text) { %q{<img src="javascript:alert('XSS');">} }
  79. it 'has valid url' do
  80. expect(subject).to match '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  81. end
  82. end
  83. end
  84. describe '#reformat' do
  85. subject { Formatter.instance.format(remote_status) }
  86. it 'returns a string' do
  87. expect(subject).to be_a String
  88. end
  89. it 'contains plain text' do
  90. expect(subject).to match('Beep boop')
  91. end
  92. it 'does not contain scripts' do
  93. expect(subject).to_not match('<script>alert("Hello")</script>')
  94. end
  95. end
  96. end