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,9 +7,67 @@ RSpec.describe AccountsController do
let(:account) { Fabricate(:account) } let(:account) { Fabricate(:account) }
shared_examples 'unapproved account check' do
before { account.user.update(approved: false) }
it 'returns http not found' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(404)
end
end
shared_examples 'permanently suspended account check' do
before do
account.suspend!
account.deletion_request.destroy
end
it 'returns http gone' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(410)
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 describe 'GET #show' do
context 'with basic account status checks' do
context 'with HTML' do
let(:format) { 'html' } 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
context 'with existing statuses' do
let!(:status) { Fabricate(:status, account: account) } let!(:status) { Fabricate(:status, account: account) }
let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) } let!(:status_reply) { Fabricate(:status, account: account, thread: Fabricate(:status)) }
let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) } let!(:status_self_reply) { Fabricate(:status, account: account, thread: status) }
@ -25,57 +83,15 @@ RSpec.describe AccountsController do
account.pinned_statuses << status_private account.pinned_statuses << status_private
end end
shared_examples 'preliminary checks' do
context 'when account is not approved' do
before do
account.user.update(approved: false)
end
it 'returns http not found' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(404)
end
end
end
context 'with HTML' do context 'with HTML' do
let(:format) { 'html' } let(:format) { 'html' }
it_behaves_like 'preliminary checks' shared_examples 'common HTML response' do
it 'returns a standard HTML response', :aggregate_failures do
context 'when account is permanently suspended' do
before do
account.suspend!
account.deletion_request.destroy
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) 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) 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) expect(response).to render_template(:show)
end end
end end
@ -85,7 +101,7 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common HTML response'
end end
context 'with replies' do context 'with replies' do
@ -94,7 +110,7 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common HTML response'
end end
context 'with media' do context 'with media' do
@ -103,7 +119,7 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common HTML response'
end end
context 'with tag' do context 'with tag' do
@ -117,7 +133,7 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format, tag: tag.to_param } get :show, params: { username: account.username, format: format, tag: tag.to_param }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common HTML response'
end end
end end
@ -129,51 +145,21 @@ RSpec.describe AccountsController do
allow(controller).to receive(:authorized_fetch_mode?).and_return(authorized_fetch_mode) allow(controller).to receive(:authorized_fetch_mode?).and_return(authorized_fetch_mode)
end end
it_behaves_like 'preliminary checks'
context 'when account is suspended permanently' do
before do
account.suspend!
account.deletion_request.destroy
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 suspended temporarily' do
before do
account.suspend!
end
it 'returns http success' do
get :show, params: { username: account.username, format: format }
expect(response).to have_http_status(200)
end
end
context 'with a normal account in a JSON request' do context 'with a normal account in a JSON request' do
before do before do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it 'returns http success' do it 'returns a JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' 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 context 'with authorized fetch mode' do
let(:authorized_fetch_mode) { true } let(:authorized_fetch_mode) { true }
@ -191,21 +177,14 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it 'returns http success' do it 'returns a private JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' 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' expect(response.headers['Cache-Control']).to include 'private'
end
it 'renders account' do expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
json = body_as_json
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end end
end end
@ -217,43 +196,29 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it 'returns http success' do it 'returns a JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' expect(response.media_type).to eq 'application/activity+json'
expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end end
it_behaves_like 'cacheable response', expects_vary: 'Accept, Accept-Language, Cookie' 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 context 'with authorized fetch mode' do
let(:authorized_fetch_mode) { true } let(:authorized_fetch_mode) { true }
it 'returns http success' do it 'returns a private signature JSON version of the account', :aggregate_failures do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end
it 'returns application/activity+json' do
expect(response.media_type).to eq 'application/activity+json' 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' expect(response.headers['Cache-Control']).to include 'private'
end
it 'returns Vary header with Signature' do
expect(response.headers['Vary']).to include 'Signature' expect(response.headers['Vary']).to include 'Signature'
end
it 'renders account' do expect(body_as_json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
json = body_as_json
expect(json).to include(:id, :type, :preferredUsername, :inbox, :publicKey, :name, :summary)
end end
end end
end end
@ -262,32 +227,7 @@ RSpec.describe AccountsController do
context 'with RSS' do context 'with RSS' do
let(:format) { 'rss' } let(:format) { 'rss' }
it_behaves_like 'preliminary checks' shared_examples 'common RSS response' do
context 'when account is permanently suspended' do
before do
account.suspend!
account.deletion_request.destroy
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 it 'returns http success' do
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
@ -300,34 +240,16 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common RSS response'
it 'renders public status' do it 'responds with correct statuses', :aggregate_failures do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status)) 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 self-reply' do expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply)) expect(response.body).to_not include_status_tag(status_private)
end expect(response.body).to_not include_status_tag(status_reblog.reblog)
expect(response.body).to_not include_status_tag(status_reply)
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
end end
@ -337,34 +259,16 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common RSS response'
it 'renders public status' do it 'responds with correct statuses with replies', :aggregate_failures do
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status)) expect(response.body).to include_status_tag(status_media)
end expect(response.body).to include_status_tag(status_reply)
expect(response.body).to include_status_tag(status_self_reply)
it 'renders self-reply' do expect(response.body).to include_status_tag(status)
expect(response.body).to include(ActivityPub::TagManager.instance.url_for(status_self_reply)) expect(response.body).to_not include_status_tag(status_direct)
end expect(response.body).to_not include_status_tag(status_private)
expect(response.body).to_not include_status_tag(status_reblog.reblog)
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
end end
@ -374,34 +278,16 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format } get :show, params: { username: account.username, format: format }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common RSS response'
it 'does not render public status' do it 'responds with correct statuses with media', :aggregate_failures do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status)) expect(response.body).to include_status_tag(status_media)
end expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to_not include_status_tag(status_private)
it 'does not render self-reply' do expect(response.body).to_not include_status_tag(status_reblog.reblog)
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply)) expect(response.body).to_not include_status_tag(status_reply)
end expect(response.body).to_not include_status_tag(status_self_reply)
expect(response.body).to_not include_status_tag(status)
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
end end
@ -416,40 +302,24 @@ RSpec.describe AccountsController do
get :show, params: { username: account.username, format: format, tag: tag.to_param } get :show, params: { username: account.username, format: format, tag: tag.to_param }
end end
it_behaves_like 'common response characteristics' it_behaves_like 'common RSS response'
it 'does not render public status' do it 'responds with correct statuses with a tag', :aggregate_failures do
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status)) expect(response.body).to include_status_tag(status_tag)
expect(response.body).to_not include_status_tag(status_direct)
expect(response.body).to_not include_status_tag(status_media)
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
end
end end
it 'does not render self-reply' do def include_status_tag(status)
expect(response.body).to_not include(ActivityPub::TagManager.instance.url_for(status_self_reply)) include ActivityPub::TagManager.instance.url_for(status)
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