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

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