Merge remote-tracking branch 'parent/main' into upstream-20240201

This commit is contained in:
KMY 2024-02-01 11:32:27 +09:00
commit 92ef42d697
179 changed files with 1218 additions and 2902 deletions

View file

@ -0,0 +1,47 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Admin Trends Links Preview Card Providers' do
let(:role) { UserRole.find_by(name: 'Admin') }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
let(:account) { Fabricate(:account) }
let(:preview_card_provider) { Fabricate(:preview_card_provider) }
describe 'GET /api/v1/admin/trends/links/publishers' do
it 'returns http success' do
get '/api/v1/admin/trends/links/publishers', params: { account_id: account.id, limit: 2 }, headers: headers
expect(response).to have_http_status(200)
end
end
describe 'POST /api/v1/admin/trends/links/publishers/:id/approve' do
before do
post "/api/v1/admin/trends/links/publishers/#{preview_card_provider.id}/approve", headers: headers
end
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
expect(response).to have_http_status(200)
end
end
describe 'POST /api/v1/admin/trends/links/publishers/:id/reject' do
before do
post "/api/v1/admin/trends/links/publishers/#{preview_card_provider.id}/reject", headers: headers
end
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
it 'returns http success' do
expect(response).to have_http_status(200)
end
end
end

View file

@ -76,20 +76,14 @@ RSpec.describe 'Media' do
let(:params) { {} }
shared_examples 'a successful media upload' do |media_type|
it 'uploads the file successfully', :aggregate_failures do
it 'uploads the file successfully and returns correct media content', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(MediaAttachment.first).to be_present
expect(MediaAttachment.first).to have_attached_file(:file)
end
it 'returns the correct media content' do
subject
body = body_as_json
expect(body).to match(
expect(body_as_json).to match(
a_hash_including(id: MediaAttachment.first.id.to_s, description: params[:description], type: media_type)
)
end

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Statuses Histories' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'read:statuses' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
context 'with an oauth token' do
describe 'GET /api/v1/statuses/:status_id/history' do
let(:status) { Fabricate(:status, account: user.account) }
before do
get "/api/v1/statuses/#{status.id}/history", headers: headers
end
it 'returns http success' do
expect(response).to have_http_status(200)
expect(body_as_json.size).to_not be 0
end
end
end
end

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Statuses Mutes' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'write:mutes' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
context 'with an oauth token' do
describe 'POST /api/v1/statuses/:status_id/mute' do
let(:status) { Fabricate(:status, account: user.account) }
before do
post "/api/v1/statuses/#{status.id}/mute", headers: headers
end
it 'creates a conversation mute', :aggregate_failures do
expect(response).to have_http_status(200)
expect(ConversationMute.find_by(account: user.account, conversation_id: status.conversation_id)).to_not be_nil
end
end
describe 'POST /api/v1/statuses/:status_id/unmute' do
let(:status) { Fabricate(:status, account: user.account) }
before do
user.account.mute_conversation!(status.conversation)
post "/api/v1/statuses/#{status.id}/unmute", headers: headers
end
it 'destroys the conversation mute', :aggregate_failures do
expect(response).to have_http_status(200)
expect(ConversationMute.find_by(account: user.account, conversation_id: status.conversation_id)).to be_nil
end
end
end
end

View file

@ -0,0 +1,105 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Statuses Reblogs' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'write:statuses' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
context 'with an oauth token' do
describe 'POST /api/v1/statuses/:status_id/reblog' do
let(:status) { Fabricate(:status, account: user.account) }
before do
post "/api/v1/statuses/#{status.id}/reblog", headers: headers
end
context 'with public status' do
it 'reblogs the status', :aggregate_failures do
expect(response).to have_http_status(200)
expect(status.reblogs.count).to eq 1
expect(user.account.reblogged?(status)).to be true
hash_body = body_as_json
expect(hash_body[:reblog][:id]).to eq status.id.to_s
expect(hash_body[:reblog][:reblogs_count]).to eq 1
expect(hash_body[:reblog][:reblogged]).to be true
end
end
context 'with private status of not-followed account' do
let(:status) { Fabricate(:status, visibility: :private) }
it 'returns http not found' do
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v1/statuses/:status_id/unreblog', :sidekiq_inline do
context 'with public status' do
let(:status) { Fabricate(:status, account: user.account) }
before do
ReblogService.new.call(user.account, status)
post "/api/v1/statuses/#{status.id}/unreblog", headers: headers
end
it 'destroys the reblog', :aggregate_failures do
expect(response).to have_http_status(200)
expect(status.reblogs.count).to eq 0
expect(user.account.reblogged?(status)).to be false
hash_body = body_as_json
expect(hash_body[:id]).to eq status.id.to_s
expect(hash_body[:reblogs_count]).to eq 0
expect(hash_body[:reblogged]).to be false
end
end
context 'with public status when blocked by its author' do
let(:status) { Fabricate(:status, account: user.account) }
before do
ReblogService.new.call(user.account, status)
status.account.block!(user.account)
post "/api/v1/statuses/#{status.id}/unreblog", headers: headers
end
it 'destroys the reblog', :aggregate_failures do
expect(response).to have_http_status(200)
expect(status.reblogs.count).to eq 0
expect(user.account.reblogged?(status)).to be false
hash_body = body_as_json
expect(hash_body[:id]).to eq status.id.to_s
expect(hash_body[:reblogs_count]).to eq 0
expect(hash_body[:reblogged]).to be false
end
end
context 'with private status that was not reblogged' do
let(:status) { Fabricate(:status, visibility: :private) }
before do
post "/api/v1/statuses/#{status.id}/unreblog", headers: headers
end
it 'returns http not found' do
expect(response).to have_http_status(404)
end
end
end
end
end

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Statuses Translations' do
let(:user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:scopes) { 'read:statuses' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
context 'with an oauth token' do
describe 'POST /api/v1/statuses/:status_id/translate' do
let(:status) { Fabricate(:status, account: user.account, text: 'Hola', language: 'es') }
before do
translation = TranslationService::Translation.new(text: 'Hello')
service = instance_double(TranslationService::DeepL, translate: [translation])
allow(TranslationService).to receive_messages(configured?: true, configured: service)
Rails.cache.write('translation_service/languages', { 'es' => ['en'] })
post "/api/v1/statuses/#{status.id}/translate", headers: headers
end
it 'returns http success' do
expect(response).to have_http_status(200)
end
end
end
end

View file

@ -0,0 +1,55 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Timelines Antenna' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:statuses' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
let(:antenna) { Fabricate(:antenna, account: user.account) }
context 'with a user context' do
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:lists') }
describe 'GET /api/v1/timelines/antenna/:id' do
before do
subscribe = Fabricate(:antenna_account)
antenna.antenna_accounts << subscribe
PostStatusService.new.call(subscribe.account, text: 'New status for user home timeline.')
end
it 'returns http success' do
get "/api/v1/timelines/antenna/#{antenna.id}", headers: headers
expect(response).to have_http_status(200)
end
end
end
context 'with the wrong user context' do
let(:other_user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: other_user.id, scopes: 'read') }
describe 'GET #show' do
it 'returns http not found' do
get "/api/v1/timelines/antenna/#{antenna.id}", headers: headers
expect(response).to have_http_status(404)
end
end
end
context 'without a user context' do
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: 'read') }
describe 'GET #show' do
it 'returns http unprocessable entity' do
get "/api/v1/timelines/antenna/#{antenna.id}", headers: headers
expect(response).to have_http_status(422)
expect(response.headers['Link']).to be_nil
end
end
end
end

View file

@ -0,0 +1,55 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'API V1 Timelines List' do
let(:user) { Fabricate(:user) }
let(:scopes) { 'read:statuses' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
let(:list) { Fabricate(:list, account: user.account) }
context 'with a user context' do
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:lists') }
describe 'GET /api/v1/timelines/list/:id' do
before do
follow = Fabricate(:follow, account: user.account)
list.accounts << follow.target_account
PostStatusService.new.call(follow.target_account, text: 'New status for user home timeline.')
end
it 'returns http success' do
get "/api/v1/timelines/list/#{list.id}", headers: headers
expect(response).to have_http_status(200)
end
end
end
context 'with the wrong user context' do
let(:other_user) { Fabricate(:user) }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: other_user.id, scopes: 'read') }
describe 'GET #show' do
it 'returns http not found' do
get "/api/v1/timelines/list/#{list.id}", headers: headers
expect(response).to have_http_status(404)
end
end
end
context 'without a user context' do
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: nil, scopes: 'read') }
describe 'GET #show' do
it 'returns http unprocessable entity' do
get "/api/v1/timelines/list/#{list.id}", headers: headers
expect(response).to have_http_status(422)
expect(response.headers['Link']).to be_nil
end
end
end
end

View file

@ -25,10 +25,10 @@ describe 'Public' do
get '/api/v1/timelines/public', headers: headers, params: params
end
let!(:private_status) { Fabricate(:status, visibility: :private) } # rubocop:disable RSpec/LetSetup
let!(:local_status) { Fabricate(:status, account: Fabricate.build(:account, domain: nil)) }
let!(:remote_status) { Fabricate(:status, account: Fabricate.build(:account, domain: 'example.com')) }
let!(:media_status) { Fabricate(:status, media_attachments: [Fabricate.build(:media_attachment)]) }
let!(:private_status) { Fabricate(:status, text: 'ohagi', visibility: :private) } # rubocop:disable RSpec/LetSetup
let!(:local_status) { Fabricate(:status, text: 'ohagi', account: Fabricate.build(:account, domain: nil)) }
let!(:remote_status) { Fabricate(:status, text: 'ohagi', account: Fabricate.build(:account, domain: 'example.com')) }
let!(:media_status) { Fabricate(:status, text: 'ohagi', media_attachments: [Fabricate.build(:media_attachment)]) }
let(:params) { {} }
@ -134,5 +134,61 @@ describe 'Public' do
end
end
end
context 'when user is setting filters' do
subject do
get '/api/v1/timelines/public', headers: headers, params: params
body_as_json.filter { |status| status[:filtered].empty? || status[:filtered][0][:filter][:id] != filter.id.to_s }.map { |status| status[:id].to_i }
end
before do
Fabricate(:custom_filter_keyword, custom_filter: filter, keyword: 'ohagi')
Fabricate(:follow, account: account, target_account: remote_account)
end
let(:exclude_follows) { false }
let(:exclude_localusers) { false }
let(:include_quotes) { false }
let(:account) { user.account }
let(:remote_account) { remote_status.account }
let!(:filter) { Fabricate(:custom_filter, account: account, exclude_follows: exclude_follows, exclude_localusers: exclude_localusers, with_quote: include_quotes) }
let!(:quote_status) { Fabricate(:status, quote: Fabricate(:status, text: 'ohagi')) }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to_not include(local_status.id)
expect(ids).to_not include(remote_status.id)
end
context 'when exclude_followers' do
let(:exclude_follows) { true }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to_not include(local_status.id)
expect(ids).to include(remote_status.id)
end
end
context 'when exclude_localusers' do
let(:exclude_localusers) { true }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to include(local_status.id)
expect(ids).to_not include(remote_status.id)
end
end
context 'when include_quotes' do # rubocop:disable RSpec/MultipleMemoizedHelpers
let(:with_quote) { true }
it 'load statuses', :aggregate_failures do
ids = subject
expect(ids).to_not include(local_status.id)
expect(ids).to include(quote_status.id)
end
end
end
end
end

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'API V1 Trends Links' do
describe 'GET /api/v1/trends/links' do
context 'when trends are disabled' do
before { Setting.trends = false }
it 'returns http success' do
get '/api/v1/trends/links'
expect(response).to have_http_status(200)
end
end
context 'when trends are enabled' do
before { Setting.trends = true }
it 'returns http success' do
prepare_trends
stub_const('Api::V1::Trends::LinksController::DEFAULT_LINKS_LIMIT', 2)
get '/api/v1/trends/links'
expect(response).to have_http_status(200)
expect(response.headers).to include('Link')
end
def prepare_trends
Fabricate.times(3, :preview_card, trendable: true, language: 'en').each do |link|
2.times { |i| Trends.links.add(link, i) }
end
Trends::Links.new(threshold: 1).refresh
end
end
end
end

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'API V1 Trends Statuses' do
describe 'GET /api/v1/trends/statuses' do
context 'when trends are disabled' do
before { Setting.trends = false }
it 'returns http success' do
get '/api/v1/trends/statuses'
expect(response).to have_http_status(200)
end
end
context 'when trends are enabled' do
before { Setting.trends = true }
it 'returns http success' do
prepare_trends
stub_const('Api::BaseController::DEFAULT_STATUSES_LIMIT', 2)
get '/api/v1/trends/statuses'
expect(response).to have_http_status(200)
expect(response.headers).to include('Link')
end
def prepare_trends
Fabricate.times(3, :status, trendable: true, language: 'en').each do |status|
2.times { |i| Trends.statuses.add(status, i) }
end
Trends::Statuses.new(threshold: 1, decay_threshold: -1).refresh
end
end
end
end

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'API V1 Trends Tags' do
describe 'GET /api/v1/trends/tags' do
context 'when trends are disabled' do
before { Setting.trends = false }
it 'returns http success' do
get '/api/v1/trends/tags'
expect(response).to have_http_status(200)
expect(response.headers).to_not include('Link')
end
end
context 'when trends are enabled' do
before { Setting.trends = true }
it 'returns http success' do
prepare_trends
stub_const('Api::V1::Trends::TagsController::DEFAULT_TAGS_LIMIT', 2)
get '/api/v1/trends/tags'
expect(response).to have_http_status(200)
expect(response.headers).to include('Link')
end
def prepare_trends
Fabricate.times(3, :tag, trendable: true).each do |tag|
2.times { |i| Trends.tags.add(tag, i) }
end
Trends::Tags.new(threshold: 1).refresh
end
end
end
end

View file

@ -0,0 +1,91 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'API V2 Admin Accounts' do
let(:role) { UserRole.find_by(name: 'Moderator') }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
let(:account) { Fabricate(:account) }
describe 'GET #index' do
let!(:remote_account) { Fabricate(:account, domain: 'example.org') }
let!(:other_remote_account) { Fabricate(:account, domain: 'foo.bar') }
let!(:suspended_account) { Fabricate(:account, suspended: true) }
let!(:suspended_remote) { Fabricate(:account, domain: 'foo.bar', suspended: true) }
let!(:disabled_account) { Fabricate(:user, disabled: true).account }
let!(:pending_account) { Fabricate(:user, approved: false).account }
let!(:admin_account) { user.account }
let(:params) { {} }
before do
pending_account.user.update(approved: false)
get '/api/v2/admin/accounts', params: params, headers: headers
end
it_behaves_like 'forbidden for wrong scope', 'write:statuses'
it_behaves_like 'forbidden for wrong role', ''
context 'when called with status active and origin local and permissions staff' do
let(:params) { { status: 'active', origin: 'local', permissions: 'staff' } }
it 'returns the correct accounts' do
expect(response).to have_http_status(200)
expect(body_json_ids).to eq([admin_account.id])
end
end
context 'when called with by_domain value and origin remote' do
let(:params) { { by_domain: 'example.org', origin: 'remote' } }
it 'returns the correct accounts' do
expect(response).to have_http_status(200)
expect(body_json_ids).to include(remote_account.id)
expect(body_json_ids).to_not include(other_remote_account.id)
end
end
context 'when called with status suspended' do
let(:params) { { status: 'suspended' } }
it 'returns the correct accounts' do
expect(response).to have_http_status(200)
expect(body_json_ids).to include(suspended_remote.id, suspended_account.id)
end
end
context 'when called with status disabled' do
let(:params) { { status: 'disabled' } }
it 'returns the correct accounts' do
expect(response).to have_http_status(200)
expect(body_json_ids).to include(disabled_account.id)
end
end
context 'when called with status pending' do
let(:params) { { status: 'pending' } }
it 'returns the correct accounts' do
expect(response).to have_http_status(200)
expect(body_json_ids).to include(pending_account.id)
end
end
def body_json_ids
body_as_json.map { |a| a[:id].to_i }
end
context 'with limit param' do
let(:params) { { limit: 1 } }
it 'sets the correct pagination headers' do
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v2_admin_accounts_url(limit: 1, max_id: admin_account.id)
end
end
end
end