2023-11-09 14:50:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe 'GET /api/v1/accounts/relationships' do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/accounts/relationships', headers: headers, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:scopes) { 'read:follows' }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
|
|
|
let(:simon) { Fabricate(:account) }
|
|
|
|
let(:lewis) { Fabricate(:account) }
|
|
|
|
let(:bob) { Fabricate(:account, suspended: true) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
user.account.follow!(simon)
|
|
|
|
lewis.follow!(user.account)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when provided only one ID' do
|
|
|
|
let(:params) { { id: simon.id } }
|
|
|
|
|
|
|
|
it 'returns JSON with correct data', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(response)
|
|
|
|
.to have_http_status(200)
|
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
2023-12-11 07:55:07 +00:00
|
|
|
.and contain_exactly(
|
|
|
|
include(
|
2023-11-16 09:55:50 +00:00
|
|
|
following: true,
|
|
|
|
followed_by: false
|
|
|
|
)
|
|
|
|
)
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when provided multiple IDs' do
|
|
|
|
let(:params) { { id: [simon.id, lewis.id, bob.id] } }
|
|
|
|
|
|
|
|
context 'when there is returned JSON data' do
|
|
|
|
context 'with default parameters' do
|
|
|
|
it 'returns an enumerable json with correct elements, excluding suspended accounts', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(response)
|
|
|
|
.to have_http_status(200)
|
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
|
|
|
.and have_attributes(
|
2023-12-11 07:55:07 +00:00
|
|
|
size: 2
|
|
|
|
)
|
|
|
|
.and contain_exactly(
|
|
|
|
include(simon_item),
|
|
|
|
include(lewis_item)
|
2023-11-16 09:55:50 +00:00
|
|
|
)
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with `with_suspended` parameter' do
|
|
|
|
let(:params) { { id: [simon.id, lewis.id, bob.id], with_suspended: true } }
|
|
|
|
|
|
|
|
it 'returns an enumerable json with correct elements, including suspended accounts', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(response)
|
|
|
|
.to have_http_status(200)
|
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
|
|
|
.and have_attributes(
|
2023-12-11 07:55:07 +00:00
|
|
|
size: 3
|
|
|
|
)
|
|
|
|
.and contain_exactly(
|
|
|
|
include(simon_item),
|
|
|
|
include(lewis_item),
|
|
|
|
include(bob_item)
|
2023-11-16 09:55:50 +00:00
|
|
|
)
|
2023-11-23 10:00:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are duplicate IDs in the params' do
|
|
|
|
let(:params) { { id: [simon.id, lewis.id, lewis.id, lewis.id, simon.id] } }
|
|
|
|
|
|
|
|
it 'removes duplicate account IDs from params' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
|
|
|
.and have_attributes(
|
2023-12-11 07:55:07 +00:00
|
|
|
size: 2
|
|
|
|
)
|
|
|
|
.and contain_exactly(
|
|
|
|
include(simon_item),
|
|
|
|
include(lewis_item)
|
2023-11-23 10:00:09 +00:00
|
|
|
)
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
def simon_item
|
|
|
|
{
|
|
|
|
id: simon.id.to_s,
|
|
|
|
following: true,
|
|
|
|
showing_reblogs: true,
|
|
|
|
followed_by: false,
|
|
|
|
muting: false,
|
|
|
|
requested: false,
|
|
|
|
domain_blocking: false,
|
|
|
|
}
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
def lewis_item
|
|
|
|
{
|
|
|
|
id: lewis.id.to_s,
|
|
|
|
following: false,
|
|
|
|
showing_reblogs: false,
|
|
|
|
followed_by: true,
|
|
|
|
muting: false,
|
|
|
|
requested: false,
|
|
|
|
domain_blocking: false,
|
|
|
|
}
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
def bob_item
|
|
|
|
{
|
|
|
|
id: bob.id.to_s,
|
|
|
|
following: false,
|
|
|
|
showing_reblogs: false,
|
|
|
|
followed_by: false,
|
|
|
|
muting: false,
|
|
|
|
requested: false,
|
|
|
|
domain_blocking: false,
|
|
|
|
}
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-14 10:29:33 +00:00
|
|
|
it 'returns JSON with correct data on previously cached requests' do
|
|
|
|
# Initial request including multiple accounts in params
|
|
|
|
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id, lewis.id] }
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(body_as_json)
|
|
|
|
.to have_attributes(size: 2)
|
2023-11-09 14:50:25 +00:00
|
|
|
|
2023-11-14 10:29:33 +00:00
|
|
|
# Subsequent request with different id, should override cache from first request
|
|
|
|
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
|
2023-11-09 14:50:25 +00:00
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(response)
|
|
|
|
.to have_http_status(200)
|
2023-11-09 14:50:25 +00:00
|
|
|
|
2023-11-14 10:29:33 +00:00
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
|
|
|
.and have_attributes(
|
2023-12-11 07:55:07 +00:00
|
|
|
size: 1
|
|
|
|
)
|
|
|
|
.and contain_exactly(
|
|
|
|
include(
|
2023-11-14 10:29:33 +00:00
|
|
|
following: true,
|
|
|
|
showing_reblogs: true
|
|
|
|
)
|
|
|
|
)
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns JSON with correct data after change too' do
|
|
|
|
subject
|
|
|
|
user.account.unfollow!(simon)
|
|
|
|
|
|
|
|
get '/api/v1/accounts/relationships', headers: headers, params: { id: [simon.id] }
|
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(response)
|
|
|
|
.to have_http_status(200)
|
2023-11-09 14:50:25 +00:00
|
|
|
|
2023-11-16 09:55:50 +00:00
|
|
|
expect(body_as_json)
|
|
|
|
.to be_an(Enumerable)
|
2023-12-11 07:55:07 +00:00
|
|
|
.and contain_exactly(
|
|
|
|
include(
|
2023-11-16 09:55:50 +00:00
|
|
|
following: false,
|
|
|
|
showing_reblogs: false
|
|
|
|
)
|
|
|
|
)
|
2023-11-09 14:50:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|