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.

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