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

This commit is contained in:
KMY 2023-11-20 12:50:02 +09:00
commit abec232dd7
85 changed files with 1314 additions and 458 deletions

View file

@ -1,300 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::V1::AccountsController do
render_views
let(:user) { Fabricate(:user) }
let(:scopes) { '' }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
before do
allow(controller).to receive(:doorkeeper_token) { token }
end
describe 'POST #create' do
let(:app) { Fabricate(:application) }
let(:token) { Doorkeeper::AccessToken.find_or_create_for(application: app, resource_owner: nil, scopes: 'read write', use_refresh_token: false) }
let(:agreement) { nil }
before do
post :create, params: { username: 'test', password: '12345678', email: 'hello@world.tld', agreement: agreement }
end
context 'when given truthy agreement' do
let(:agreement) { 'true' }
it 'creates a user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(body_as_json[:access_token]).to_not be_blank
user = User.find_by(email: 'hello@world.tld')
expect(user).to_not be_nil
expect(user.created_by_application_id).to eq app.id
end
end
context 'when given no agreement' do
it 'returns http unprocessable entity' do
expect(response).to have_http_status(422)
end
end
end
describe 'POST #follow' do
let(:scopes) { 'write:follows' }
let(:my_actor_type) { 'Person' }
let(:lock_follow_from_bot) { false }
let(:other_account) { Fabricate(:account, username: 'bob', locked: locked) }
context 'when posting to an other account' do
before do
other_account.user.settings['lock_follow_from_bot'] = lock_follow_from_bot
other_account.user.save!
user.account.update!(actor_type: my_actor_type)
post :follow, params: { id: other_account.id }
end
context 'with unlocked account' do
let(:locked) { false }
it 'creates a following relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
json = body_as_json
expect(json[:following]).to be true
expect(json[:requested]).to be false
expect(user.account.following?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
context 'with locked account' do
let(:locked) { true }
it 'creates a follow request relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
json = body_as_json
expect(json[:following]).to be false
expect(json[:requested]).to be true
expect(user.account.requested?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
context 'with unlocked account from bot' do
let(:locked) { false }
let(:lock_follow_from_bot) { true }
let(:my_actor_type) { 'Service' }
it 'returns http success' do
expect(response).to have_http_status(200)
end
it 'returns JSON with following=false and requested=true' do
json = body_as_json
expect(json[:following]).to be false
expect(json[:requested]).to be true
end
it 'creates a follow request relation between user and target user' do
expect(user.account.requested?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
end
context 'when modifying follow options' do
let(:locked) { false }
before do
user.account.follow!(other_account, reblogs: false, notify: false)
end
it 'changes reblogs option' do
post :follow, params: { id: other_account.id, reblogs: true }
json = body_as_json
expect(json[:following]).to be true
expect(json[:showing_reblogs]).to be true
expect(json[:notifying]).to be false
end
it 'changes notify option' do
post :follow, params: { id: other_account.id, notify: true }
json = body_as_json
expect(json[:following]).to be true
expect(json[:showing_reblogs]).to be false
expect(json[:notifying]).to be true
end
it 'changes languages option' do
post :follow, params: { id: other_account.id, languages: %w(en es) }
json = body_as_json
expect(json[:following]).to be true
expect(json[:showing_reblogs]).to be false
expect(json[:notifying]).to be false
expect(json[:languages]).to match_array %w(en es)
end
end
end
describe 'POST #unfollow' do
let(:scopes) { 'write:follows' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.follow!(other_account)
post :unfollow, params: { id: other_account.id }
end
it 'removes the following relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.following?(other_account)).to be false
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #remove_from_followers' do
let(:scopes) { 'write:follows' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
other_account.follow!(user.account)
post :remove_from_followers, params: { id: other_account.id }
end
it 'removes the followed relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.followed_by?(other_account)).to be false
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #block' do
let(:scopes) { 'write:blocks' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.follow!(other_account)
post :block, params: { id: other_account.id }
end
it 'creates a blocking relation', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.following?(other_account)).to be false
expect(user.account.blocking?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #unblock' do
let(:scopes) { 'write:blocks' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.block!(other_account)
post :unblock, params: { id: other_account.id }
end
it 'removes the blocking relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.blocking?(other_account)).to be false
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #mute' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.follow!(other_account)
post :mute, params: { id: other_account.id }
end
it 'mutes notifications', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.following?(other_account)).to be true
expect(user.account.muting?(other_account)).to be true
expect(user.account.muting_notifications?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #mute with notifications set to false' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.follow!(other_account)
post :mute, params: { id: other_account.id, notifications: false }
end
it 'does not mute notifications', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.following?(other_account)).to be true
expect(user.account.muting?(other_account)).to be true
expect(user.account.muting_notifications?(other_account)).to be false
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #mute with nonzero duration set' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.follow!(other_account)
post :mute, params: { id: other_account.id, duration: 300 }
end
it 'mutes notifications', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.following?(other_account)).to be true
expect(user.account.muting?(other_account)).to be true
expect(user.account.muting_notifications?(other_account)).to be true
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
describe 'POST #unmute' do
let(:scopes) { 'write:mutes' }
let(:other_account) { Fabricate(:account, username: 'bob') }
before do
user.account.mute!(other_account)
post :unmute, params: { id: other_account.id }
end
it 'removes the muting relation between user and target user', :aggregate_failures do
expect(response).to have_http_status(200)
expect(user.account.muting?(other_account)).to be false
end
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
end
end

View file

@ -1,326 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Api::V1::StatusesController do
render_views
let(:user) { Fabricate(:user) }
let(:app) { Fabricate(:application, name: 'Test app', website: 'http://testapp.com') }
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, application: app, scopes: scopes) }
context 'with an oauth token' do
before do
allow(controller).to receive(:doorkeeper_token) { token }
end
describe 'GET #show' do
let(:scopes) { 'read:statuses' }
let(:status) { Fabricate(:status, account: user.account) }
it 'returns http success' do
get :show, params: { id: status.id }
expect(response).to have_http_status(200)
end
context 'when post includes filtered terms' do
let(:status) { Fabricate(:status, text: 'this toot is about that banned word') }
before do
user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
end
it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id }
json = body_as_json
expect(response).to have_http_status(200)
expect(json[:filtered][0]).to include({
filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s,
title: 'filter1',
filter_action: 'hide',
}),
keyword_matches: ['banned'],
})
end
end
context 'when post is explicitly filtered' do
let(:status) { Fabricate(:status, text: 'hello world') }
before do
filter = user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide)
filter.statuses.create!(status_id: status.id)
end
it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id }
json = body_as_json
expect(response).to have_http_status(200)
expect(json[:filtered][0]).to include({
filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s,
title: 'filter1',
filter_action: 'hide',
}),
status_matches: [status.id.to_s],
})
end
end
context 'when reblog includes filtered terms' do
let(:status) { Fabricate(:status, reblog: Fabricate(:status, text: 'this toot is about that banned word')) }
before do
user.account.custom_filters.create!(phrase: 'filter1', context: %w(home), action: :hide, keywords_attributes: [{ keyword: 'banned' }, { keyword: 'irrelevant' }])
end
it 'returns filter information', :aggregate_failures do
get :show, params: { id: status.id }
json = body_as_json
expect(response).to have_http_status(200)
expect(json[:reblog][:filtered][0]).to include({
filter: a_hash_including({
id: user.account.custom_filters.first.id.to_s,
title: 'filter1',
filter_action: 'hide',
}),
keyword_matches: ['banned'],
})
end
end
end
describe 'GET #context' do
let(:scopes) { 'read:statuses' }
let(:status) { Fabricate(:status, account: user.account) }
let!(:thread) { Fabricate(:status, account: user.account, thread: status) }
it 'returns http success' do
get :context, params: { id: status.id }
expect(response).to have_http_status(200)
end
context 'when has also reference' do
before do
Fabricate(:status_reference, status: thread, target_status: status)
end
it 'returns unique ancestors' do
get :context, params: { id: thread.id }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }
expect(status_ids).to eq [status.id]
end
it 'returns unique references' do
get :context, params: { id: thread.id, with_reference: true }
ancestor_status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }
reference_status_ids = body_as_json[:references].map { |ref| ref[:id].to_i }
expect(ancestor_status_ids).to eq [status.id]
expect(reference_status_ids).to eq []
end
end
end
context 'with reference' do
let(:status) { Fabricate(:status, account: user.account) }
let(:scopes) { 'read:statuses' }
let(:referred) { Fabricate(:status) }
let(:referred_private) { Fabricate(:status, visibility: :private) }
let(:referred_private_following) { Fabricate(:status, visibility: :private) }
before do
user.account.follow!(referred_private_following.account)
Fabricate(:status_reference, status: status, target_status: referred)
Fabricate(:status_reference, status: status, target_status: referred_private)
Fabricate(:status_reference, status: status, target_status: referred_private_following)
end
it 'returns http success' do
get :context, params: { id: status.id }
expect(response).to have_http_status(200)
end
it 'returns empty references' do
get :context, params: { id: status.id }
status_ids = body_as_json[:references].map { |ref| ref[:id].to_i }
expect(status_ids).to eq []
end
it 'contains referred status' do
get :context, params: { id: status.id }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }
expect(status_ids).to include referred.id
expect(status_ids).to include referred_private_following.id
end
it 'does not contain private status' do
get :context, params: { id: status.id }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }
expect(status_ids).to_not include referred_private.id
end
context 'when with_reference is enabled' do
it 'returns http success' do
get :context, params: { id: status.id, with_reference: true }
expect(response).to have_http_status(200)
end
it 'returns empty ancestors' do
get :context, params: { id: status.id, with_reference: true }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }
expect(status_ids).to eq []
end
it 'contains referred status' do
get :context, params: { id: status.id, with_reference: true }
status_ids = body_as_json[:references].map { |ref| ref[:id].to_i }
expect(status_ids).to include referred.id
end
end
end
describe 'POST #create' do
let(:scopes) { 'write:statuses' }
context 'with a basic status body' do
before do
post :create, params: { status: 'Hello world' }
end
it 'returns rate limit headers', :aggregate_failures do
expect(response).to have_http_status(200)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
expect(response.headers['X-RateLimit-Remaining']).to eq (RateLimiter::FAMILIES[:statuses][:limit] - 1).to_s
end
end
context 'with a safeguard' do
let!(:alice) { Fabricate(:account, username: 'alice') }
let!(:bob) { Fabricate(:account, username: 'bob') }
before do
post :create, params: { status: '@alice hm, @bob is really annoying lately', allowed_mentions: [alice.id] }
end
it 'returns serialized extra accounts in body', :aggregate_failures do
expect(response).to have_http_status(422)
expect(body_as_json[:unexpected_accounts].map { |a| a.slice(:id, :acct) }).to eq [{ id: bob.id.to_s, acct: bob.acct }]
end
end
context 'with missing parameters' do
before do
post :create, params: {}
end
it 'returns rate limit headers', :aggregate_failures do
expect(response).to have_http_status(422)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
end
end
context 'when exceeding rate limit' do
before do
rate_limiter = RateLimiter.new(user.account, family: :statuses)
300.times { rate_limiter.record! }
post :create, params: { status: 'Hello world' }
end
it 'returns rate limit headers', :aggregate_failures do
expect(response).to have_http_status(429)
expect(response.headers['X-RateLimit-Limit']).to eq RateLimiter::FAMILIES[:statuses][:limit].to_s
expect(response.headers['X-RateLimit-Remaining']).to eq '0'
end
end
end
describe 'DELETE #destroy' do
let(:scopes) { 'write:statuses' }
let(:status) { Fabricate(:status, account: user.account) }
before do
post :destroy, params: { id: status.id }
end
it 'removes the status', :aggregate_failures do
expect(response).to have_http_status(200)
expect(Status.find_by(id: status.id)).to be_nil
end
end
describe 'PUT #update' do
let(:scopes) { 'write:statuses' }
let(:status) { Fabricate(:status, account: user.account) }
before do
put :update, params: { id: status.id, status: 'I am updated' }
end
it 'updates the status', :aggregate_failures do
expect(response).to have_http_status(200)
expect(status.reload.text).to eq 'I am updated'
end
end
end
context 'without an oauth token' do
before do
allow(controller).to receive(:doorkeeper_token).and_return(nil)
end
context 'with a private status' do
let(:status) { Fabricate(:status, account: user.account, visibility: :private) }
describe 'GET #show' do
it 'returns http unauthorized' do
get :show, params: { id: status.id }
expect(response).to have_http_status(404)
end
end
describe 'GET #context' do
before do
Fabricate(:status, account: user.account, thread: status)
end
it 'returns http unauthorized' do
get :context, params: { id: status.id }
expect(response).to have_http_status(404)
end
end
end
context 'with a public status' do
let(:status) { Fabricate(:status, account: user.account, visibility: :public) }
describe 'GET #show' do
it 'returns http success' do
get :show, params: { id: status.id }
expect(response).to have_http_status(200)
end
end
describe 'GET #context' do
before do
Fabricate(:status, account: user.account, thread: status)
end
it 'returns http success' do
get :context, params: { id: status.id }
expect(response).to have_http_status(200)
end
end
end
end
end