2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-02 17:41:01 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe ActivityPub::FetchFeaturedCollectionService, type: :service do
|
2023-02-20 05:24:14 +01:00
|
|
|
subject { described_class.new }
|
|
|
|
|
2022-05-02 17:41:01 +02:00
|
|
|
let(:actor) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/account', featured_collection_url: 'https://example.com/account/pinned') }
|
|
|
|
|
|
|
|
let!(:known_status) { Fabricate(:status, account: actor, uri: 'https://example.com/account/pinned/1') }
|
|
|
|
|
2023-06-14 16:44:37 +02:00
|
|
|
let(:status_json_pinned_known) do
|
2022-05-02 17:41:01 +02:00
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'Note',
|
2023-06-14 16:44:37 +02:00
|
|
|
id: 'https://example.com/account/pinned/known',
|
2022-05-02 17:41:01 +02:00
|
|
|
content: 'foo',
|
|
|
|
attributedTo: actor.uri,
|
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-06-14 16:44:37 +02:00
|
|
|
let(:status_json_pinned_unknown_inlined) do
|
2022-05-02 17:41:01 +02:00
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'Note',
|
2023-06-14 16:44:37 +02:00
|
|
|
id: 'https://example.com/account/pinned/unknown-inlined',
|
2022-05-02 17:41:01 +02:00
|
|
|
content: 'foo',
|
|
|
|
attributedTo: actor.uri,
|
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-01-19 13:43:10 +01:00
|
|
|
let(:status_json_pinned_unknown_reachable) do
|
2022-05-02 17:41:01 +02:00
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'Note',
|
2023-06-14 16:44:37 +02:00
|
|
|
id: 'https://example.com/account/pinned/unknown-reachable',
|
2022-05-02 17:41:01 +02:00
|
|
|
content: 'foo',
|
|
|
|
attributedTo: actor.uri,
|
|
|
|
to: 'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-10-27 04:36:22 +02:00
|
|
|
let(:featured_with_null) do
|
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
id: 'https://example.com/account/collections/featured',
|
|
|
|
totalItems: 0,
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-05-02 17:41:01 +02:00
|
|
|
let(:items) do
|
|
|
|
[
|
2023-06-14 16:44:37 +02:00
|
|
|
'https://example.com/account/pinned/known', # known
|
|
|
|
status_json_pinned_unknown_inlined, # unknown inlined
|
|
|
|
'https://example.com/account/pinned/unknown-unreachable', # unknown unreachable
|
|
|
|
'https://example.com/account/pinned/unknown-reachable', # unknown reachable
|
2023-10-27 04:36:22 +02:00
|
|
|
'https://example.com/account/collections/featured', # featured with null
|
2022-05-02 17:41:01 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'Collection',
|
|
|
|
id: actor.featured_collection_url,
|
|
|
|
items: items,
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'sets pinned posts' do
|
|
|
|
before do
|
2023-06-14 16:44:37 +02:00
|
|
|
stub_request(:get, 'https://example.com/account/pinned/known').to_return(status: 200, body: Oj.dump(status_json_pinned_known))
|
|
|
|
stub_request(:get, 'https://example.com/account/pinned/unknown-inlined').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_inlined))
|
|
|
|
stub_request(:get, 'https://example.com/account/pinned/unknown-unreachable').to_return(status: 404)
|
2024-01-19 13:43:10 +01:00
|
|
|
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
|
2023-10-27 04:36:22 +02:00
|
|
|
stub_request(:get, 'https://example.com/account/collections/featured').to_return(status: 200, body: Oj.dump(featured_with_null))
|
2022-05-02 17:41:01 +02:00
|
|
|
|
2022-10-21 11:48:22 +02:00
|
|
|
subject.call(actor, note: true, hashtag: false)
|
2022-05-02 17:41:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets expected posts as pinned posts' do
|
2023-06-14 16:44:37 +02:00
|
|
|
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
|
|
|
|
'https://example.com/account/pinned/known',
|
|
|
|
'https://example.com/account/pinned/unknown-inlined',
|
|
|
|
'https://example.com/account/pinned/unknown-reachable'
|
|
|
|
)
|
2023-12-22 13:57:29 +01:00
|
|
|
expect(actor.pinned_statuses).to_not include(known_status)
|
2022-05-02 17:41:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
context 'when the endpoint is a Collection' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sets pinned posts'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the endpoint is an OrderedCollection' do
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'OrderedCollection',
|
|
|
|
id: actor.featured_collection_url,
|
|
|
|
orderedItems: items,
|
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sets pinned posts'
|
2024-01-19 13:43:10 +01:00
|
|
|
|
|
|
|
context 'when there is a single item, with the array compacted away' do
|
|
|
|
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
|
|
|
|
subject.call(actor, note: true, hashtag: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets expected posts as pinned posts' do
|
|
|
|
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
|
|
|
|
'https://example.com/account/pinned/unknown-reachable'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-05-02 17:41:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the endpoint is a paginated Collection' do
|
|
|
|
let(:payload) do
|
|
|
|
{
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
type: 'Collection',
|
|
|
|
id: actor.featured_collection_url,
|
|
|
|
first: {
|
|
|
|
type: 'CollectionPage',
|
|
|
|
partOf: actor.featured_collection_url,
|
|
|
|
items: items,
|
2023-02-18 15:33:41 +01:00
|
|
|
},
|
2022-05-02 17:41:01 +02:00
|
|
|
}.with_indifferent_access
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, actor.featured_collection_url).to_return(status: 200, body: Oj.dump(payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'sets pinned posts'
|
2024-01-19 13:43:10 +01:00
|
|
|
|
|
|
|
context 'when there is a single item, with the array compacted away' do
|
|
|
|
let(:items) { 'https://example.com/account/pinned/unknown-reachable' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, 'https://example.com/account/pinned/unknown-reachable').to_return(status: 200, body: Oj.dump(status_json_pinned_unknown_reachable))
|
|
|
|
subject.call(actor, note: true, hashtag: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets expected posts as pinned posts' do
|
|
|
|
expect(actor.pinned_statuses.pluck(:uri)).to contain_exactly(
|
|
|
|
'https://example.com/account/pinned/unknown-reachable'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-05-02 17:41:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|