formatter_spec.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rails_helper'
  2. RSpec.describe Formatter do
  3. let(:account) { Fabricate(:account, username: 'alice') }
  4. let(:local_status) { Fabricate(:status, text: 'Hello world http://google.com', account: account) }
  5. let(:remote_status) { Fabricate(:status, text: '<script>alert("Hello")</script> Beep boop', uri: 'beepboop', account: account) }
  6. describe '#format' do
  7. subject { Formatter.instance.format(local_status) }
  8. it 'returns a string' do
  9. expect(subject).to be_a String
  10. end
  11. it 'contains plain text' do
  12. expect(subject).to match('Hello world')
  13. end
  14. it 'contains a link' do
  15. 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>')
  16. end
  17. =begin
  18. it 'matches a stand-alone medium URL' do
  19. expect(subject.match('https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4')[0]).to eq 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4'
  20. end
  21. it 'matches a stand-alone google URL' do
  22. expect(subject.match('http://google.com')[0]).to eq 'http://google.com'
  23. end
  24. it 'matches a URL without trailing period' do
  25. expect(subject.match('http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ')[0]).to eq 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona'
  26. end
  27. it 'matches a URL without closing paranthesis' do
  28. expect(subject.match('(http://google.com/)')[0]).to eq 'http://google.com'
  29. end
  30. it 'matches a URL without exclamation point' do
  31. expect(subject.match('http://www.google.com! ')[0]).to eq 'http://www.google.com'
  32. end
  33. it 'matches a URL with a query string' do
  34. expect(subject.match('https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink')[0]).to eq 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink'
  35. end
  36. it 'matches a URL with parenthesis in it' do
  37. expect(subject.match('https://en.wikipedia.org/wiki/Diaspora_(software)')[0]).to eq 'https://en.wikipedia.org/wiki/Diaspora_(software)'
  38. end
  39. =end
  40. end
  41. describe '#reformat' do
  42. subject { Formatter.instance.format(remote_status) }
  43. it 'returns a string' do
  44. expect(subject).to be_a String
  45. end
  46. it 'contains plain text' do
  47. expect(subject).to match('Beep boop')
  48. end
  49. it 'does not contain scripts' do
  50. expect(subject).to_not match('<script>alert("Hello")</script>')
  51. end
  52. end
  53. end