2023-03-04 08:12:54 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
describe 'Directories API' do
|
2023-07-01 12:48:16 -07:00
|
|
|
let(:user) { Fabricate(:user, confirmed_at: nil) }
|
2023-12-14 02:38:06 -08:00
|
|
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
|
|
|
let(:scopes) { 'read:follows' }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
2023-03-04 08:12:54 -08:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
describe 'GET /api/v1/directories' do
|
2023-07-01 12:48:16 -07:00
|
|
|
context 'with no params' do
|
|
|
|
before do
|
2023-09-07 09:55:25 -07:00
|
|
|
local_unconfirmed_account = Fabricate(
|
2023-07-01 12:48:16 -07:00
|
|
|
:account,
|
|
|
|
domain: nil,
|
|
|
|
user: Fabricate(:user, confirmed_at: nil, approved: true),
|
|
|
|
username: 'local_unconfirmed'
|
|
|
|
)
|
2023-09-07 09:55:25 -07:00
|
|
|
local_unconfirmed_account.create_account_stat!
|
2023-03-04 08:12:54 -08:00
|
|
|
|
2023-07-01 12:48:16 -07:00
|
|
|
local_unapproved_account = Fabricate(
|
|
|
|
:account,
|
|
|
|
domain: nil,
|
|
|
|
user: Fabricate(:user, confirmed_at: 10.days.ago),
|
|
|
|
username: 'local_unapproved'
|
|
|
|
)
|
2023-09-07 09:55:25 -07:00
|
|
|
local_unapproved_account.create_account_stat!
|
2023-07-01 12:48:16 -07:00
|
|
|
local_unapproved_account.user.update(approved: false)
|
|
|
|
|
2023-09-07 09:55:25 -07:00
|
|
|
local_undiscoverable_account = Fabricate(
|
2023-07-01 12:48:16 -07:00
|
|
|
:account,
|
|
|
|
domain: nil,
|
|
|
|
user: Fabricate(:user, confirmed_at: 10.days.ago, approved: true),
|
|
|
|
discoverable: false,
|
|
|
|
username: 'local_undiscoverable'
|
|
|
|
)
|
2023-09-07 09:55:25 -07:00
|
|
|
local_undiscoverable_account.create_account_stat!
|
2023-07-01 12:48:16 -07:00
|
|
|
|
|
|
|
excluded_from_timeline_account = Fabricate(
|
|
|
|
:account,
|
|
|
|
domain: 'host.example',
|
|
|
|
discoverable: true,
|
|
|
|
username: 'remote_excluded_from_timeline'
|
|
|
|
)
|
2023-09-07 09:55:25 -07:00
|
|
|
excluded_from_timeline_account.create_account_stat!
|
2023-07-01 12:48:16 -07:00
|
|
|
Fabricate(:block, account: user.account, target_account: excluded_from_timeline_account)
|
|
|
|
|
2023-09-07 09:55:25 -07:00
|
|
|
domain_blocked_account = Fabricate(
|
2023-07-01 12:48:16 -07:00
|
|
|
:account,
|
|
|
|
domain: 'test.example',
|
|
|
|
discoverable: true,
|
|
|
|
username: 'remote_domain_blocked'
|
|
|
|
)
|
2023-09-07 09:55:25 -07:00
|
|
|
domain_blocked_account.create_account_stat!
|
2023-07-01 12:48:16 -07:00
|
|
|
Fabricate(:account_domain_block, account: user.account, domain: 'test.example')
|
2023-12-14 02:38:06 -08:00
|
|
|
|
|
|
|
local_discoverable_account.create_account_stat!
|
|
|
|
eligible_remote_account.create_account_stat!
|
2023-07-01 12:48:16 -07:00
|
|
|
end
|
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
let(:local_discoverable_account) do
|
|
|
|
Fabricate(
|
2023-07-01 12:48:16 -07:00
|
|
|
:account,
|
|
|
|
domain: nil,
|
|
|
|
user: Fabricate(:user, confirmed_at: 10.days.ago, approved: true),
|
|
|
|
discoverable: true,
|
|
|
|
username: 'local_discoverable'
|
|
|
|
)
|
2023-12-14 02:38:06 -08:00
|
|
|
end
|
2023-07-01 12:48:16 -07:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
let(:eligible_remote_account) do
|
|
|
|
Fabricate(
|
2023-07-01 12:48:16 -07:00
|
|
|
:account,
|
|
|
|
domain: 'host.example',
|
|
|
|
discoverable: true,
|
|
|
|
username: 'eligible_remote'
|
|
|
|
)
|
2023-12-14 02:38:06 -08:00
|
|
|
end
|
2023-07-01 12:48:16 -07:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
it 'returns the local discoverable account and the remote discoverable account' do
|
|
|
|
get '/api/v1/directory', headers: headers
|
2023-07-01 12:48:16 -07:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_as_json.size).to eq(2)
|
2023-09-07 06:38:11 -07:00
|
|
|
expect(body_as_json.pluck(:id)).to contain_exactly(eligible_remote_account.id.to_s, local_discoverable_account.id.to_s)
|
2023-07-01 12:48:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when asking for local accounts only' do
|
2023-12-14 02:38:06 -08:00
|
|
|
let(:user) { Fabricate(:user, confirmed_at: 10.days.ago, approved: true) }
|
|
|
|
let(:local_account) { Fabricate(:account, domain: nil, user: user) }
|
|
|
|
let(:remote_account) { Fabricate(:account, domain: 'host.example') }
|
|
|
|
|
|
|
|
before do
|
2023-09-07 09:55:25 -07:00
|
|
|
local_account.create_account_stat!
|
|
|
|
remote_account.create_account_stat!
|
2023-12-14 02:38:06 -08:00
|
|
|
end
|
2023-07-01 12:48:16 -07:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
it 'returns only the local accounts' do
|
|
|
|
get '/api/v1/directory', headers: headers, params: { local: '1' }
|
2023-07-01 12:48:16 -07:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_as_json.size).to eq(1)
|
|
|
|
expect(body_as_json.first[:id]).to include(local_account.id.to_s)
|
|
|
|
expect(response.body).to_not include(remote_account.id.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ordered by active' do
|
|
|
|
it 'returns accounts in order of most recent status activity' do
|
2023-09-07 06:38:11 -07:00
|
|
|
old_stat = Fabricate(:account_stat, last_status_at: 1.day.ago)
|
|
|
|
new_stat = Fabricate(:account_stat, last_status_at: 1.minute.ago)
|
2023-07-01 12:48:16 -07:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
get '/api/v1/directory', headers: headers, params: { order: 'active' }
|
2023-07-01 12:48:16 -07:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_as_json.size).to eq(2)
|
2023-09-07 06:38:11 -07:00
|
|
|
expect(body_as_json.first[:id]).to include(new_stat.account_id.to_s)
|
|
|
|
expect(body_as_json.second[:id]).to include(old_stat.account_id.to_s)
|
2023-07-01 12:48:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when ordered by new' do
|
|
|
|
it 'returns accounts in order of creation' do
|
2023-09-07 09:55:25 -07:00
|
|
|
account_old = Fabricate(:account_stat).account
|
2023-07-01 12:48:16 -07:00
|
|
|
travel_to 10.seconds.from_now
|
2023-09-07 09:55:25 -07:00
|
|
|
account_new = Fabricate(:account_stat).account
|
2023-07-01 12:48:16 -07:00
|
|
|
|
2023-12-14 02:38:06 -08:00
|
|
|
get '/api/v1/directory', headers: headers, params: { order: 'new' }
|
2023-07-01 12:48:16 -07:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_as_json.size).to eq(2)
|
|
|
|
expect(body_as_json.first[:id]).to include(account_new.id.to_s)
|
|
|
|
expect(body_as_json.second[:id]).to include(account_old.id.to_s)
|
|
|
|
end
|
2023-03-04 08:12:54 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|