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

148 lines
5.5 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. xit 'matches a URL without closing paranthesis' do
  59. expect(subject.match('(http://google.com/)')[0]).to eq 'http://google.com'
  60. end
  61. context 'matches a URL without exclamation point' do
  62. let(:local_text) { 'http://www.google.com!' }
  63. it 'has valid url' do
  64. expect(subject).to include('href="http://www.google.com/"')
  65. end
  66. end
  67. context 'matches a URL without single quote' do
  68. let(:local_text) { "http://www.google.com'" }
  69. it 'has valid url' do
  70. expect(subject).to include('href="http://www.google.com/"')
  71. end
  72. end
  73. context 'matches a URL without angle brackets' do
  74. let(:local_text) { 'http://www.google.com>' }
  75. it 'has valid url' do
  76. expect(subject).to include('href="http://www.google.com/"')
  77. end
  78. end
  79. context 'matches a URL with a query string' do
  80. let(:local_text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  81. it 'has valid url' do
  82. expect(subject).to include('href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"')
  83. end
  84. end
  85. context 'matches a URL with parenthesis in it' do
  86. let(:local_text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  87. it 'has valid url' do
  88. expect(subject).to include('href="https://en.wikipedia.org/wiki/Diaspora_(software)"')
  89. end
  90. end
  91. context 'contains html (script tag)' do
  92. let(:local_text) { '<script>alert("Hello")</script>' }
  93. it 'has valid url' do
  94. expect(subject).to match '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  95. end
  96. end
  97. context 'contains html (xss attack)' do
  98. let(:local_text) { %q{<img src="javascript:alert('XSS');">} }
  99. it 'has valid url' do
  100. expect(subject).to match '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  101. end
  102. end
  103. context 'contains invalid URL' do
  104. let(:local_text) { 'http://www\.google\.com' }
  105. it 'has valid url' do
  106. expect(subject).to eq '<p>http://www\.google\.com</p>'
  107. end
  108. end
  109. end
  110. describe '#reformat' do
  111. subject { Formatter.instance.format(remote_status) }
  112. it 'returns a string' do
  113. expect(subject).to be_a String
  114. end
  115. it 'contains plain text' do
  116. expect(subject).to match('Beep boop')
  117. end
  118. it 'does not contain scripts' do
  119. expect(subject).to_not match('<script>alert("Hello")</script>')
  120. end
  121. end
  122. end