1
0
Fork 0
forked from gitea/nas

Improve fetch_link_card_service to exclude status references url

This commit is contained in:
KMY 2023-08-27 09:15:19 +09:00
parent b8b569f7cc
commit ad7952ce3d
2 changed files with 63 additions and 0 deletions

View file

@ -10,6 +10,7 @@ RSpec.describe FetchLinkCardService, type: :service do
before do
stub_request(:get, 'http://example.com/html').to_return(headers: { 'Content-Type' => 'text/html' }, body: html)
stub_request(:get, 'http://example.com/html_sub').to_return(headers: { 'Content-Type' => 'text/html' }, body: html)
stub_request(:get, 'http://example.com/not-found').to_return(status: 404, headers: { 'Content-Type' => 'text/html' }, body: html)
stub_request(:get, 'http://example.com/text').to_return(status: 404, headers: { 'Content-Type' => 'text/plain' }, body: 'Hello')
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => 'http://example.com/html' })
@ -234,6 +235,24 @@ RSpec.describe FetchLinkCardService, type: :service do
end
end
end
context 'with URL of reference' do
let(:status) { Fabricate(:status, text: 'RT http://example.com/html') }
it 'creates preview card' do
expect(status.preview_card).to be_nil
end
end
context 'with URL of reference and normal page' do
let(:status) { Fabricate(:status, text: 'RT http://example.com/text http://example.com/html') }
it 'creates preview card' do
expect(status.preview_card).to_not be_nil
expect(status.preview_card.url).to eq 'http://example.com/html'
expect(status.preview_card.title).to eq 'Hello world'
end
end
end
context 'with a remote status' do
@ -254,4 +273,31 @@ RSpec.describe FetchLinkCardService, type: :service do
expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made
end
end
context 'with a remote status of reference' do
let(:status) do
Fabricate(:status, account: Fabricate(:account, domain: 'example.com'), text: <<-TEXT)
RT <a href="http://example.com/html" target="_blank" rel="noopener noreferrer" title="http://example.com/html">Hello</a>&nbsp;
TEXT
end
it 'creates preview card' do
expect(status.preview_card).to be_nil
end
end
context 'with a remote status of reference and normal link' do
let(:status) do
Fabricate(:status, account: Fabricate(:account, domain: 'example.com'), text: <<-TEXT)
RT <a href="http://example.com/html" target="_blank" rel="noopener noreferrer" title="http://example.com/html">Hello</a>&nbsp;
<a href="http://example.com/html_sub" target="_blank" rel="noopener noreferrer" title="http://example.com/html_sub">Hello</a>&nbsp;
TEXT
end
it 'creates preview card' do
expect(status.preview_card).to_not be_nil
expect(status.preview_card.url).to eq 'http://example.com/html_sub'
expect(status.preview_card.title).to eq 'Hello world'
end
end
end