Reduce extra round trips in activitypub controller specs (#31041)

This commit is contained in:
Matt Jankowski 2024-07-17 04:09:34 -04:00 committed by GitHub
parent f5e90f3de3
commit 76c2c5c748
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 56 additions and 126 deletions

View file

@ -25,14 +25,12 @@ RSpec.describe ActivityPub::CollectionsController do
context 'without signature' do context 'without signature' do
let(:remote_account) { nil } let(:remote_account) { nil }
it 'returns http success and correct media type' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
end
it_behaves_like 'cacheable response' it_behaves_like 'cacheable response'
it 'returns orderedItems with correct items' do it 'returns http success and correct media type and correct items' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
expect(body_as_json[:orderedItems]) expect(body_as_json[:orderedItems])
.to be_an(Array) .to be_an(Array)
.and have_attributes(size: 3) .and have_attributes(size: 3)
@ -66,14 +64,12 @@ RSpec.describe ActivityPub::CollectionsController do
let(:remote_account) { Fabricate(:account, domain: 'example.com') } let(:remote_account) { Fabricate(:account, domain: 'example.com') }
context 'when getting a featured resource' do context 'when getting a featured resource' do
it 'returns http success and correct media type' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
end
it_behaves_like 'cacheable response' it_behaves_like 'cacheable response'
it 'returns orderedItems with expected items' do it 'returns http success and correct media type and expected items' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
expect(body_as_json[:orderedItems]) expect(body_as_json[:orderedItems])
.to be_an(Array) .to be_an(Array)
.and have_attributes(size: 3) .and have_attributes(size: 3)
@ -92,16 +88,14 @@ RSpec.describe ActivityPub::CollectionsController do
account.block!(remote_account) account.block!(remote_account)
end end
it 'returns http success and correct media type and cache headers' do it 'returns http success and correct media type and cache headers and empty items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
expect(response.headers['Cache-Control']).to include 'private' expect(response.headers['Cache-Control']).to include 'private'
end
it 'returns empty orderedItems' do
expect(body_as_json[:orderedItems]) expect(body_as_json[:orderedItems])
.to be_an(Array) .to be_an(Array)
.and have_attributes(size: 0) .and be_empty
end end
end end
@ -110,16 +104,14 @@ RSpec.describe ActivityPub::CollectionsController do
account.block_domain!(remote_account.domain) account.block_domain!(remote_account.domain)
end end
it 'returns http success and correct media type and cache headers' do it 'returns http success and correct media type and cache headers and empty items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
expect(response.headers['Cache-Control']).to include 'private' expect(response.headers['Cache-Control']).to include 'private'
end
it 'returns empty orderedItems' do
expect(body_as_json[:orderedItems]) expect(body_as_json[:orderedItems])
.to be_an(Array) .to be_an(Array)
.and have_attributes(size: 0) .and be_empty
end end
end end
end end

View file

@ -37,25 +37,18 @@ RSpec.describe ActivityPub::FollowersSynchronizationsController do
let(:body) { body_as_json } let(:body) { body_as_json }
let(:remote_account) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/instance') } let(:remote_account) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/instance') }
it 'returns http success' do it 'returns http success and cache control and activity json types and correct items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json'
end
it 'returns orderedItems with followers from example.com' do
expect(body[:orderedItems]).to be_an Array
expect(body[:orderedItems]).to contain_exactly(
follower_example_com_instance_actor.uri,
follower_example_com_user_a.uri,
follower_example_com_user_b.uri
)
end
it 'returns private Cache-Control header' do
expect(response.headers['Cache-Control']).to eq 'max-age=0, private' expect(response.headers['Cache-Control']).to eq 'max-age=0, private'
expect(response.media_type).to eq 'application/activity+json'
expect(body[:orderedItems])
.to be_an(Array)
.and contain_exactly(
follower_example_com_instance_actor.uri,
follower_example_com_user_a.uri,
follower_example_com_user_b.uri
)
end end
context 'when account is permanently suspended' do context 'when account is permanently suspended' do

View file

@ -25,22 +25,13 @@ RSpec.describe ActivityPub::OutboxesController do
context 'with page not requested' do context 'with page not requested' do
let(:page) { nil } let(:page) { nil }
it 'returns http success' do
expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json'
end
it 'returns totalItems' do
expect(body[:totalItems]).to eq 4
end
it_behaves_like 'cacheable response' it_behaves_like 'cacheable response'
it 'does not have a Vary header' do it 'returns http success and correct media type and headers and items count' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
expect(response.headers['Vary']).to be_nil expect(response.headers['Vary']).to be_nil
expect(body[:totalItems]).to eq 4
end end
context 'when account is permanently suspended' do context 'when account is permanently suspended' do
@ -68,26 +59,18 @@ RSpec.describe ActivityPub::OutboxesController do
context 'with page requested' do context 'with page requested' do
let(:page) { 'true' } let(:page) { 'true' }
it 'returns http success' do it_behaves_like 'cacheable response'
it 'returns http success and correct media type and vary header and items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
end expect(response.headers['Vary']).to include 'Signature'
it 'returns orderedItems with public or unlisted statuses' do
expect(body[:orderedItems]).to be_an Array expect(body[:orderedItems]).to be_an Array
expect(body[:orderedItems].size).to eq 2 expect(body[:orderedItems].size).to eq 2
expect(body[:orderedItems].all? { |item| targets_public_collection?(item) }).to be true expect(body[:orderedItems].all? { |item| targets_public_collection?(item) }).to be true
end end
it_behaves_like 'cacheable response'
it 'returns Vary header with Signature' do
expect(response.headers['Vary']).to include 'Signature'
end
context 'when account is permanently suspended' do context 'when account is permanently suspended' do
before do before do
account.suspend! account.suspend!
@ -120,23 +103,14 @@ RSpec.describe ActivityPub::OutboxesController do
get :show, params: { account_username: account.username, page: page } get :show, params: { account_username: account.username, page: page }
end end
it 'returns http success' do it 'returns http success and correct media type and headers and items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
end
it 'returns orderedItems with public or unlisted statuses' do
json = body_as_json
expect(json[:orderedItems]).to be_an Array
expect(json[:orderedItems].size).to eq 2
expect(json[:orderedItems].all? { |item| targets_public_collection?(item) }).to be true
end
it 'returns private Cache-Control header' do
expect(response.headers['Cache-Control']).to eq 'max-age=60, private' expect(response.headers['Cache-Control']).to eq 'max-age=60, private'
expect(body_as_json[:orderedItems]).to be_an Array
expect(body_as_json[:orderedItems].size).to eq 2
expect(body_as_json[:orderedItems].all? { |item| targets_public_collection?(item) }).to be true
end end
end end
@ -146,23 +120,14 @@ RSpec.describe ActivityPub::OutboxesController do
get :show, params: { account_username: account.username, page: page } get :show, params: { account_username: account.username, page: page }
end end
it 'returns http success' do it 'returns http success and correct media type and headers and items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
end
it 'returns orderedItems with private statuses' do
json = body_as_json
expect(json[:orderedItems]).to be_an Array
expect(json[:orderedItems].size).to eq 3
expect(json[:orderedItems].all? { |item| targets_public_collection?(item) || targets_followers_collection?(item, account) }).to be true
end
it 'returns private Cache-Control header' do
expect(response.headers['Cache-Control']).to eq 'max-age=60, private' expect(response.headers['Cache-Control']).to eq 'max-age=60, private'
expect(body_as_json[:orderedItems]).to be_an Array
expect(body_as_json[:orderedItems].size).to eq 3
expect(body_as_json[:orderedItems].all? { |item| targets_public_collection?(item) || targets_followers_collection?(item, account) }).to be true
end end
end end
@ -172,22 +137,14 @@ RSpec.describe ActivityPub::OutboxesController do
get :show, params: { account_username: account.username, page: page } get :show, params: { account_username: account.username, page: page }
end end
it 'returns http success' do it 'returns http success and correct media type and headers and items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
end
it 'returns empty orderedItems' do
json = body_as_json
expect(json[:orderedItems]).to be_an Array
expect(json[:orderedItems].size).to eq 0
end
it 'returns private Cache-Control header' do
expect(response.headers['Cache-Control']).to eq 'max-age=60, private' expect(response.headers['Cache-Control']).to eq 'max-age=60, private'
expect(body_as_json[:orderedItems])
.to be_an(Array)
.and be_empty
end end
end end
@ -197,22 +154,14 @@ RSpec.describe ActivityPub::OutboxesController do
get :show, params: { account_username: account.username, page: page } get :show, params: { account_username: account.username, page: page }
end end
it 'returns http success' do it 'returns http success and correct media type and headers and items' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
end
it 'returns empty orderedItems' do
json = body_as_json
expect(json[:orderedItems]).to be_an Array
expect(json[:orderedItems].size).to eq 0
end
it 'returns private Cache-Control header' do
expect(response.headers['Cache-Control']).to eq 'max-age=60, private' expect(response.headers['Cache-Control']).to eq 'max-age=60, private'
expect(body_as_json[:orderedItems])
.to be_an(Array)
.and be_empty
end end
end end
end end

View file

@ -66,19 +66,15 @@ RSpec.describe ActivityPub::RepliesController do
context 'when status is public' do context 'when status is public' do
let(:parent_visibility) { :public } let(:parent_visibility) { :public }
let(:json) { body_as_json } let(:page_json) { body_as_json[:first] }
let(:page_json) { json[:first] }
it 'returns http success' do
expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json'
end
it_behaves_like 'cacheable response' it_behaves_like 'cacheable response'
it 'returns http success and correct media type' do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
end
context 'without only_other_accounts' do context 'without only_other_accounts' do
it "returns items with thread author's replies" do it "returns items with thread author's replies" do
expect(page_json).to be_a Hash expect(page_json).to be_a Hash