2016-09-09 19:04:34 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Formatter do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:local_account) { Fabricate(:account, domain: nil, username: 'alice') }
|
2018-04-25 13:12:28 +01:00
|
|
|
let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }
|
2017-05-05 18:48:22 +01:00
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
shared_examples 'encode and link URLs' do
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a stand-alone medium URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
|
2016-09-09 19:04:34 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
2016-09-09 19:04:34 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a stand-alone google URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'http://google.com' }
|
2016-09-09 19:04:34 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="http://google.com"'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a stand-alone IDN URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'https://nic.みんな/' }
|
2017-02-22 18:35:11 +00:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="https://nic.みんな/"'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
it 'has display URL' do
|
|
|
|
is_expected.to include '<span class="">nic.みんな/</span>'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
2017-05-05 18:48:22 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with a trailing period' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
|
2017-05-24 13:32:53 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL but not the period' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
|
2017-05-24 13:32:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL enclosed with parentheses' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '(http://google.com/)' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL but not the parentheses' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'href="http://google.com/"'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
2017-05-05 18:48:22 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with a trailing exclamation point' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'http://www.google.com!' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL but not the exclamation point' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with a trailing single quote' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { "http://www.google.com'" }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL but not the single quote' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-25 14:03:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with a trailing angle bracket' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'http://www.google.com>' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL but not the angle bracket' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-25 14:03:51 +01:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
2017-04-25 14:03:51 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with a query string' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink"'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with parentheses in it' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with Japanese path string' do
|
2017-09-14 17:03:20 +01:00
|
|
|
let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="https://ja.wikipedia.org/wiki/日本"'
|
2017-09-14 17:03:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with Korean path string' do
|
2017-09-14 17:03:20 +01:00
|
|
|
let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="https://ko.wikipedia.org/wiki/대한민국"'
|
2017-09-14 17:03:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with Simplified Chinese path string' do
|
2017-09-14 17:03:20 +01:00
|
|
|
let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="https://baike.baidu.com/item/中华人民共和国"'
|
2017-09-14 17:03:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL with Traditional Chinese path string' do
|
2017-09-14 17:03:20 +01:00
|
|
|
let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'matches the full URL' do
|
2018-01-03 19:51:33 +00:00
|
|
|
is_expected.to include 'href="https://zh.wikipedia.org/wiki/臺灣"'
|
2017-09-14 17:03:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL containing unsafe code (XSS attack, visible part)' do
|
2017-09-16 20:33:52 +01:00
|
|
|
let(:text) { %q{http://example.com/b<del>b</del>} }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'escapes the HTML in the URL' do
|
2017-09-16 20:33:52 +01:00
|
|
|
is_expected.to include '<del>b</del>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a URL containing unsafe code (XSS attack, invisible part)' do
|
2017-09-16 20:33:52 +01:00
|
|
|
let(:text) { %q{http://example.com/blahblahblahblah/a<script>alert("Hello")</script>} }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'escapes the HTML in the URL' do
|
2017-09-16 20:33:52 +01:00
|
|
|
is_expected.to include '<script>alert("Hello")</script>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given text containing HTML code (script tag)' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'escapes the HTML' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include '<p><script>alert("Hello")</script></p>'
|
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
2017-04-19 13:52:18 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given text containing HTML (XSS attack)' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { %q{<img src="javascript:alert('XSS');">} }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'escapes the HTML' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include '<p><img src="javascript:alert('XSS');"></p>'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given an invalid URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'http://www\.google\.com' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'outputs the raw URL' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq '<p>http://www\.google\.com</p>'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given text containing a hashtag' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '#hashtag' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'creates a hashtag link' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
|
2018-08-31 14:16:59 +01:00
|
|
|
|
|
|
|
describe '#format_spoiler' do
|
|
|
|
subject { Formatter.instance.format_spoiler(status) }
|
|
|
|
|
|
|
|
context 'given a post containing plain text' do
|
|
|
|
let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
|
|
|
|
|
|
|
|
it 'Returns the spoiler text' do
|
|
|
|
is_expected.to eq 'Secret!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
|
|
|
let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
|
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
|
|
|
it 'converts the shortcode to an image tag' do
|
|
|
|
is_expected.to match(/<img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
describe '#format' do
|
|
|
|
subject { Formatter.instance.format(status) }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with local status' do
|
|
|
|
context 'given a reblogged post' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
|
|
|
|
let(:status) { Fabricate(:status, reblog: reblog) }
|
|
|
|
|
|
|
|
it 'returns original status with credit to its author' do
|
|
|
|
is_expected.to include 'RT <span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span> Hello world'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing plain text' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:status) { Fabricate(:status, text: 'text', uri: nil) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'paragraphizes the text' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq '<p>text</p>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing line feeds' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
|
|
|
|
|
|
|
|
it 'removes line feeds' do
|
|
|
|
is_expected.not_to include "\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing linkable mentions' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'creates a mention link' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing unlinkable mentions' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'does not create a mention link' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include '@alice'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context do
|
|
|
|
subject do
|
|
|
|
status = Fabricate(:status, text: text, uri: nil)
|
|
|
|
Formatter.instance.format(status)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'encode and link URLs'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-09-19 01:42:40 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with custom_emojify option' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
|
|
|
let(:status) { Fabricate(:status, account: local_account, text: text) }
|
|
|
|
|
|
|
|
subject { Formatter.instance.format(status, custom_emojify: true) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { 'Beep :coolcat: boop' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { ':coolcat::coolcat:' }
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
is_expected.to match(/:coolcat::coolcat:/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { 'Beep boop :coolcat:' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/boop <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with remote status' do
|
2017-09-06 18:01:28 +01:00
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'reformats the post' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq 'Beep boop'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-09-19 01:42:40 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with custom_emojify option' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: text) }
|
|
|
|
|
|
|
|
subject { Formatter.instance.format(status, custom_emojify: true) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { '<p>:coolcat: Beep boop<br />' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { '<p>Beep :coolcat: boop</p>' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with concatenated emoji' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { '<p>:coolcat::coolcat:</p>' }
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2017-09-19 01:42:40 +01:00
|
|
|
let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2017-09-19 01:42:40 +01:00
|
|
|
is_expected.to match(/<br><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reformat' do
|
|
|
|
subject { Formatter.instance.reformat(text) }
|
2017-04-19 13:52:18 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing plain text' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { 'Beep boop' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'keeps the plain text' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to include 'Beep boop'
|
2017-05-12 16:46:44 +01:00
|
|
|
end
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing script tags' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'strips the scripts' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to_not include '<script>alert("Hello")</script>'
|
2017-04-19 13:52:18 +01:00
|
|
|
end
|
2017-02-22 18:35:11 +00:00
|
|
|
end
|
2017-06-17 19:26:05 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing malicious classes' do
|
2018-01-03 02:54:08 +00:00
|
|
|
let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
|
2017-06-17 19:26:05 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'strips the malicious classes' do
|
2017-06-17 19:26:05 +01:00
|
|
|
is_expected.to_not include 'status__content__spoiler-link'
|
|
|
|
end
|
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
2017-05-09 17:17:41 +01:00
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
describe '#plaintext' do
|
|
|
|
subject { Formatter.instance.plaintext(status) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with local status' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
|
2017-05-12 16:46:44 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'returns the raw text' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
|
2017-05-09 17:17:41 +01:00
|
|
|
end
|
|
|
|
end
|
2017-05-24 14:36:10 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with remote status' do
|
2017-09-06 18:01:28 +01:00
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
|
2017-05-24 14:36:10 +01:00
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
it 'returns tag-stripped text' do
|
|
|
|
is_expected.to eq ''
|
2017-05-24 14:36:10 +01:00
|
|
|
end
|
|
|
|
end
|
2016-09-09 19:04:34 +01:00
|
|
|
end
|
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
describe '#simplified_format' do
|
|
|
|
subject { Formatter.instance.simplified_format(account) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with local status' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:account) { Fabricate(:account, domain: nil, note: text) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing linkable mentions for local accounts' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '@alice' }
|
|
|
|
|
|
|
|
before { local_account }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'creates a mention link' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq '<p><span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span></p>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing linkable mentions for remote accounts' do
|
2018-04-25 13:12:28 +01:00
|
|
|
let(:text) { '@bob@remote.test' }
|
2017-06-04 13:58:57 +01:00
|
|
|
|
|
|
|
before { remote_account }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'creates a mention link' do
|
2018-04-25 13:12:28 +01:00
|
|
|
is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post containing unlinkable mentions' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '@alice' }
|
2016-09-09 19:04:34 +01:00
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'does not create a mention link' do
|
2017-06-04 13:58:57 +01:00
|
|
|
is_expected.to eq '<p>@alice</p>'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with custom_emojify option' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
|
|
|
|
|
|
|
before { account.note = text }
|
|
|
|
subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2018-04-01 22:55:42 +01:00
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { 'Beep :coolcat: boop' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2018-04-01 22:55:42 +01:00
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { ':coolcat::coolcat:' }
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
is_expected.to match(/:coolcat::coolcat:/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { 'Beep boop :coolcat:' }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
it 'converts the shortcode to an image tag' do
|
2018-04-01 22:55:42 +01:00
|
|
|
is_expected.to match(/boop <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
include_examples 'encode and link URLs'
|
2016-09-09 19:04:34 +01:00
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with remote status' do
|
2017-06-04 13:58:57 +01:00
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
|
|
|
let(:account) { Fabricate(:account, domain: 'remote', note: text) }
|
|
|
|
|
|
|
|
it 'reformats' do
|
|
|
|
is_expected.to_not include '<script>alert("Hello")</script>'
|
|
|
|
end
|
2018-04-01 22:55:42 +01:00
|
|
|
|
|
|
|
context 'with custom_emojify option' do
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
|
|
|
|
|
|
|
|
before { remote_account.note = text }
|
|
|
|
|
|
|
|
subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { '<p>:coolcat: Beep boop<br />' }
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { '<p>Beep :coolcat: boop</p>' }
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { '<p>:coolcat::coolcat:</p>' }
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 00:20:56 +01:00
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2018-04-01 22:55:42 +01:00
|
|
|
let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
|
|
|
is_expected.to match(/<br><img draggable="false" class="emojione" alt=":coolcat:"/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-09 19:04:34 +01:00
|
|
|
end
|
2017-06-04 13:58:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sanitize' do
|
|
|
|
let(:html) { '<script>alert("Hello")</script>' }
|
|
|
|
|
|
|
|
subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
|
2016-09-09 19:04:34 +01:00
|
|
|
|
2017-06-04 13:58:57 +01:00
|
|
|
it 'sanitizes' do
|
|
|
|
is_expected.to eq 'alert("Hello")'
|
2016-09-09 19:04:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|