Spec speed ups on AccountsController spec (#25391)

This commit is contained in:
Matt Jankowski 2023-10-17 07:32:10 -04:00 committed by GitHub
parent d54fec24e5
commit 12bb7be8b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,449 +7,319 @@ RSpec.describe AccountsController do
let(:account) { Fabricate(:account) } let(:account) { Fabricate(:account) }
describe 'GET #show' do shared_examples 'unapproved account check' do
let(:format) { 'html' } before { account.user.update(approved: false) }
let!(:status) { Fabricate(:status, account: account) } it 'returns http not found' do
let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) } get :show, params: { username: account.username, format: format }
let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) }
let!(:status_media) { Fabricate(:status, account: account) }
let!(:status_pinned) { Fabricate(:status, account: account) }
let!(:status_private) { Fabricate(:status, account: account, visibility: :private) }
let!(:status_direct) { Fabricate(:status, account: account, visibility: :direct) }
let!(:status_reblog) { Fabricate(:status, account: account, reblog: Fabricate(:status)) }
expect(response).to have_http_status(404)
end
end
shared_examples 'permanently suspended account check' do
before do before do
status_media.media_attachments << Fabricate(:media_attachment, account: account, type: :image) account.suspend!
account.pinned_statuses << status_pinned account.deletion_request.destroy
account.pinned_statuses << status_private
end end
shared_examples 'preliminary checks' do it 'returns http gone' do
context 'when account is not approved' do get :show, params: { username: account.username, format: format }
before do
account.user.update(approved: false)
end
it 'returns http not found' do expect(response).to have_http_status(410)
get :show, params: { username: account.username, format: format } end
expect(response).to have_http_status(404) end
end
shared_examples 'temporarily suspended account check' do |code: 403|
before { account.suspend! }
it 'returns appropriate http response code' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(code)
end
end
describe 'GET #show' do
context 'with basic account status checks' do
context 'with HTML' do
let(:format) { 'html' }
it_behaves_like 'unapproved account check'
it_behaves_like 'permanently suspended account check'
it_behaves_like 'temporarily suspended account check'
end
context 'with JSON' do
let(:format) { 'json' }
it_behaves_like 'unapproved account check'
it_behaves_like 'permanently suspended account check'
it_behaves_like 'temporarily suspended account check', code: 200
end
context 'with RSS' do
let(:format) { 'rss' }
it_behaves_like 'unapproved account check'
it_behaves_like 'permanently suspended account check'
it_behaves_like 'temporarily suspended account check'
end end
end end
context 'with HTML' do context 'with existing statuses' do
let(:format) { 'html' } let!(:status) { Fabricate(:status, account: account) }
let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) }
it_behaves_like 'preliminary checks' let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) }
let!(:status_media) { Fabricate(:status, account: account) }
context 'when account is permanently suspended' do let!(:status_pinned) { Fabricate(:status, account: account) }
before do let!(:status_private) { Fabricate(:status, account: account, visibility: :private) }
account.suspend! let!(:status_direct) { Fabricate(:status, account: account, visibility: :direct) }
account.deletion_request.destroy let!(:status_reblog) { Fabricate(:status, account: account, reblog: Fabricate(:status)) }
end
it 'returns http gone' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(410)
end
end
context 'when account is temporarily suspended' do
before do
account.suspend!
end
it 'returns http forbidden' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(403)
end
end
shared_examples 'common response characteristics' do
it 'returns http success' do
expect(response).to have_http_status(200)
end
it 'returns Link header' do
expect(response.headers['Link'].to_s).to include ActivityPub::TagManager.instance.uri_for(account)
end
it 'renders show template' do
expect(response).to render_template(:show)
end
end
context 'with a normal account in an HTML request' do
before do
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics'
end
context 'with replies' do
before do
allow(controller).to receive(:replies_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics'
end
context 'with media' do
before do
allow(controller).to receive(:media_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics'
end
context 'with tag' do
let(:tag) { Fabricate(:tag) }
let!(:status_tag) { Fabricate(:status, account: account) }
before do
allow(controller).to receive(:tag_requested?).and_return(true)
status_tag.tags << tag
get :show, params: { username: account.username, format: format, tag: tag.to_param }
end
it_behaves_like 'common response characteristics'
end
end
context 'with JSON' do
let(:authorized_fetch_mode) { false }
let(:format) { 'json' }
before do before do
allow(controller).to receive(:authorized_fetch_mode?).and_return(authorized_fetch_mode) status_media.media_attachments << Fabricate(:media_attachment, account: account, type: :image)
account.pinned_statuses << status_pinned
account.pinned_statuses << status_private
end end
it_behaves_like 'preliminary checks' context 'with HTML' do
let(:format) { 'html' }
context 'when account is suspended permanently' do shared_examples 'common HTML response' do
before do it 'returns a standard HTML response', :aggregate_failures do
account.suspend! expect(response).to have_http_status(200)
account.deletion_request.destroy
expect(response.headers['Link'].to_s).to include ActivityPub::TagManager.instance.uri_for(account)
expect(response).to render_template(:show)
end
end end
it 'returns http gone' do context 'with a normal account in an HTML request' do
get :show, params: { username: account.username, format: format } before do
expect(response).to have_http_status(410) get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common HTML response'
end
context 'with replies' do
before do
allow(controller).to receive(:replies_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common HTML response'
end
context 'with media' do
before do
allow(controller).to receive(:media_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common HTML response'
end
context 'with tag' do
let(:tag) { Fabricate(:tag) }
let!(:status_tag) { Fabricate(:status, account: account) }
before do
allow(controller).to receive(:tag_requested?).and_return(true)
status_tag.tags << tag
get :show, params: { username: account.username, format: format, tag: tag.to_param }
end
it_behaves_like 'common HTML response'
end end
end end
context 'when account is suspended temporarily' do context 'with JSON' do
let(:authorized_fetch_mode) { false }
let(:format) { 'json' }
before do before do
account.suspend! allow(controller).to receive(:authorized_fetch_mode?).and_return(authorized_fetch_mode)
end end
it 'returns http success' do context 'with a normal account in a JSON request' do
get :show, params: { username: account.username, format: format } before do
expect(response).to have_http_status(200) get :show, params: { username: account.username, format: format }
end end
end
context 'with a normal account in a JSON request' do it 'returns a JSON version of the account', :aggregate_failures do
before do expect(response).to have_http_status(200)
get :show, params: { username: account.username, format: format }
expect(response.media_type).to eq 'application/activity+json'
expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie'
context 'with authorized fetch mode' do
let(:authorized_fetch_mode) { true }
it 'returns http unauthorized' do
expect(response).to have_http_status(401)
end
end
end end
it 'returns http success' do context 'when signed in' do
expect(response).to have_http_status(200) let(:user) { Fabricate(:user) }
before do
sign_in(user)
get :show, params: { username: account.username, format: format }
end
it 'returns a private JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
expect(response.headers['Cache-Control']).to include 'private'
expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
end end
it 'returns application/activity+json' do context 'with signature' do
expect(response.media_type).to eq 'application/activity+json' let(:remote_account) { Fabricate(:account, domain: 'example.com') }
end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' before do
allow(controller).to receive(:signed_request_actor).and_return(remote_account)
get :show, params: { username: account.username, format: format }
end
it 'renders account' do it 'returns a JSON version of the account', :aggregate_failures do
json = body_as_json expect(response).to have_http_status(200)
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
context 'with authorized fetch mode' do expect(response.media_type).to eq 'application/activity+json'
let(:authorized_fetch_mode) { true }
it 'returns http unauthorized' do expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
expect(response).to have_http_status(401) end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie'
context 'with authorized fetch mode' do
let(:authorized_fetch_mode) { true }
it 'returns a private signature JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/activity+json'
expect(response.headers['Cache-Control']).to include 'private'
expect(response.headers['Vary']).to include 'Signature'
expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
end end
end end
end end
context 'when signed in' do context 'with RSS' do
let(:user) { Fabricate(:user) } let(:format) { 'rss' }
before do
sign_in(user)
get :show, params: { username: account.username, format: format }
end
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 private Cache-Control header' do
expect(response.headers['Cache-Control']).to include 'private'
end
it 'renders account' do
json = body_as_json
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
end
context 'with signature' do
let(:remote_account) { Fabricate(:account, domain: 'example.com') }
before do
allow(controller).to receive(:signed_request_actor).and_return(remote_account)
get :show, params: { username: account.username, format: format }
end
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', expects_vary: 'Accept, Accept-Language, Cookie'
it 'renders account' do
json = body_as_json
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end
context 'with authorized fetch mode' do
let(:authorized_fetch_mode) { true }
shared_examples 'common RSS response' do
it 'returns http success' do it 'returns http success' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'returns application/activity+json' do it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie'
expect(response.media_type).to eq 'application/activity+json' end
context 'with a normal account in an RSS request' do
before do
get :show, params: { username: account.username, format: format }
end end
it 'returns private Cache-Control header' do it_behaves_like 'common RSS response'
expect(response.headers['Cache-Control']).to include 'private'
end
it 'returns Vary header with Signature' do it 'responds with correct statuses', :aggregate_failures do
expect(response.headers['Vary']).to include 'Signature' expect(response.body).to include_status_tag(status_media)
end expect(response.body).to include_status_tag(status_self_reply)
expect(response.body).to include_status_tag(status)
it 'renders account' do expect(response.body).to_not include_status_tag(status_direct)
json = body_as_json expect(response.body).to_not include_status_tag(status_private)
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary) expect(response.body).to_not include_status_tag(status_reblog.reblog)
expect(response.body).to_not include_status_tag(status_reply)
end end
end end
end
end
context 'with RSS' do context 'with replies' do
let(:format) { 'rss' } before do
allow(controller).to receive(:replies_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'preliminary checks' it_behaves_like 'common RSS response'
context 'when account is permanently suspended' do it 'responds with correct statuses with replies', :aggregate_failures do
before do expect(response.body).to include_status_tag(status_media)
account.suspend! expect(response.body).to include_status_tag(status_reply)
account.deletion_request.destroy expect(response.body).to include_status_tag(status_self_reply)
expect(response.body).to include_status_tag(status)
expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to_not include_status_tag(status_private)
expect(response.body).to_not include_status_tag(status_reblog.reblog)
end
end end
it 'returns http gone' do context 'with media' do
get :show, params: { username: account.username, format: format } before do
expect(response).to have_http_status(410) allow(controller).to receive(:media_requested?).and_return(true)
end get :show, params: { username: account.username, format: format }
end end
context 'when account is temporarily suspended' do it_behaves_like 'common RSS response'
before do
account.suspend! it 'responds with correct statuses with media', :aggregate_failures do
expect(response.body).to include_status_tag(status_media)
expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to_not include_status_tag(status_private)
expect(response.body).to_not include_status_tag(status_reblog.reblog)
expect(response.body).to_not include_status_tag(status_reply)
expect(response.body).to_not include_status_tag(status_self_reply)
expect(response.body).to_not include_status_tag(status)
end
end end
it 'returns http forbidden' do context 'with tag' do
get :show, params: { username: account.username, format: format } let(:tag) { Fabricate(:tag) }
expect(response).to have_http_status(403)
end
end
shared_examples 'common response characteristics' do let!(:status_tag) { Fabricate(:status, account: account) }
it 'returns http success' do
expect(response).to have_http_status(200)
end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' before do
end allow(controller).to receive(:tag_requested?).and_return(true)
status_tag.tags << tag
get :show, params: { username: account.username, format: format, tag: tag.to_param }
end
context 'with a normal account in an RSS request' do it_behaves_like 'common RSS response'
before do
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics' it 'responds with correct statuses with a tag', :aggregate_failures do
expect(response.body).to include_status_tag(status_tag)
it 'renders public status' do expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status)) expect(response.body).to_not include_status_tag(status_media)
end expect(response.body).to_not include_status_tag(status_private)
expect(response.body).to_not include_status_tag(status_reblog.reblog)
it 'renders self-reply' do expect(response.body).to_not include_status_tag(status_reply)
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply)) expect(response.body).to_not include_status_tag(status_self_reply)
end expect(response.body).to_not include_status_tag(status)
end
it 'renders status with media' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
end
it 'does not render reblog' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
end
it 'does not render private status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
end
it 'does not render direct status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
end
it 'does not render reply to someone else' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
end
end
context 'with replies' do
before do
allow(controller).to receive(:replies_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics'
it 'renders public status' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status))
end
it 'renders self-reply' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply))
end
it 'renders status with media' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
end
it 'does not render reblog' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
end
it 'does not render private status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
end
it 'does not render direct status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
end
it 'renders reply to someone else' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_reply))
end
end
context 'with media' do
before do
allow(controller).to receive(:media_requested?).and_return(true)
get :show, params: { username: account.username, format: format }
end
it_behaves_like 'common response characteristics'
it 'does not render public status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status))
end
it 'does not render self-reply' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply))
end
it 'renders status with media' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_media))
end
it 'does not render reblog' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
end
it 'does not render private status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
end
it 'does not render direct status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
end
it 'does not render reply to someone else' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
end
end
context 'with tag' do
let(:tag) { Fabricate(:tag) }
let!(:status_tag) { Fabricate(:status, account: account) }
before do
allow(controller).to receive(:tag_requested?).and_return(true)
status_tag.tags << tag
get :show, params: { username: account.username, format: format, tag: tag.to_param }
end
it_behaves_like 'common response characteristics'
it 'does not render public status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status))
end
it 'does not render self-reply' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply))
end
it 'does not render status with media' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_media))
end
it 'does not render reblog' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reblog.reblog))
end
it 'does not render private status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_private))
end
it 'does not render direct status' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_direct))
end
it 'does not render reply to someone else' do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_reply))
end
it 'renders status with tag' do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_tag))
end end
end end
end end
end end
def include_status_tag(status)
include ActivityPub::TagManager.instance.url_for(status)
end
end end