From cd64a5b2ec93416437850481b0ec8fc1ba43871f Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 21 Dec 2023 05:10:18 -0500 Subject: [PATCH] Clean up of `RSpec/LetSetup` within `api/` (#28448) --- .rubocop_todo.yml | 5 -- .../v1/accounts/statuses_controller_spec.rb | 3 +- .../api/v1/filters_controller_spec.rb | 5 ++ .../api/v2/admin/accounts_controller_spec.rb | 64 +++++++++++++------ .../v2/filters/keywords_controller_spec.rb | 4 ++ .../v2/filters/statuses_controller_spec.rb | 4 ++ 6 files changed, 60 insertions(+), 25 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f834c55620..8bee220d72 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -47,11 +47,6 @@ RSpec/ExampleLength: RSpec/LetSetup: Exclude: - - 'spec/controllers/api/v1/accounts/statuses_controller_spec.rb' - - 'spec/controllers/api/v1/filters_controller_spec.rb' - - 'spec/controllers/api/v2/admin/accounts_controller_spec.rb' - - 'spec/controllers/api/v2/filters/keywords_controller_spec.rb' - - 'spec/controllers/api/v2/filters/statuses_controller_spec.rb' - 'spec/controllers/auth/confirmations_controller_spec.rb' - 'spec/controllers/auth/passwords_controller_spec.rb' - 'spec/controllers/auth/sessions_controller_spec.rb' diff --git a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb index 0e4fa93017..df71e94ace 100644 --- a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb @@ -10,11 +10,11 @@ describe Api::V1::Accounts::StatusesController do before do allow(controller).to receive(:doorkeeper_token) { token } - Fabricate(:status, account: user.account) end describe 'GET #index' do it 'returns expected headers', :aggregate_failures do + Fabricate(:status, account: user.account) get :index, params: { account_id: user.account.id, limit: 1 } expect(response).to have_http_status(200) @@ -30,7 +30,6 @@ describe Api::V1::Accounts::StatusesController do end context 'with exclude replies' do - let!(:older_statuses) { user.account.statuses.destroy_all } let!(:status) { Fabricate(:status, account: user.account) } let!(:status_self_reply) { Fabricate(:status, account: user.account, thread: status) } diff --git a/spec/controllers/api/v1/filters_controller_spec.rb b/spec/controllers/api/v1/filters_controller_spec.rb index 8d5408cf54..b0f64ccf41 100644 --- a/spec/controllers/api/v1/filters_controller_spec.rb +++ b/spec/controllers/api/v1/filters_controller_spec.rb @@ -15,10 +15,15 @@ RSpec.describe Api::V1::FiltersController do describe 'GET #index' do let(:scopes) { 'read:filters' } let!(:filter) { Fabricate(:custom_filter, account: user.account) } + let!(:custom_filter_keyword) { Fabricate(:custom_filter_keyword, custom_filter: filter) } it 'returns http success' do get :index expect(response).to have_http_status(200) + expect(body_as_json) + .to contain_exactly( + include(id: custom_filter_keyword.id.to_s) + ) end end diff --git a/spec/controllers/api/v2/admin/accounts_controller_spec.rb b/spec/controllers/api/v2/admin/accounts_controller_spec.rb index 18b3950140..349f4d2528 100644 --- a/spec/controllers/api/v2/admin/accounts_controller_spec.rb +++ b/spec/controllers/api/v2/admin/accounts_controller_spec.rb @@ -34,28 +34,56 @@ RSpec.describe Api::V2::Admin::AccountsController do it_behaves_like 'forbidden for wrong scope', 'write:statuses' it_behaves_like 'forbidden for wrong role', '' - [ - [{ status: 'active', origin: 'local', permissions: 'staff' }, [:admin_account]], - [{ by_domain: 'example.org', origin: 'remote' }, [:remote_account]], - [{ status: 'suspended' }, [:suspended_remote, :suspended_account]], - [{ status: 'disabled' }, [:disabled_account]], - [{ status: 'pending' }, [:pending_account]], - ].each do |params, expected_results| - context "when called with #{params.inspect}" do - let(:params) { params } + 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 (#{expected_results.inspect})" do - expect(response).to have_http_status(200) - - expect(body_json_ids).to eq(expected_results.map { |symbol| send(symbol).id }) - end - - def body_json_ids - body_as_json.map { |a| a[:id].to_i } - end + 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 } } diff --git a/spec/controllers/api/v2/filters/keywords_controller_spec.rb b/spec/controllers/api/v2/filters/keywords_controller_spec.rb index 5321f787a1..9f25237bcf 100644 --- a/spec/controllers/api/v2/filters/keywords_controller_spec.rb +++ b/spec/controllers/api/v2/filters/keywords_controller_spec.rb @@ -22,6 +22,10 @@ RSpec.describe Api::V2::Filters::KeywordsController do it 'returns http success' do get :index, params: { filter_id: filter.id } expect(response).to have_http_status(200) + expect(body_as_json) + .to contain_exactly( + include(id: keyword.id.to_s) + ) end context "when trying to access another's user filters" do diff --git a/spec/controllers/api/v2/filters/statuses_controller_spec.rb b/spec/controllers/api/v2/filters/statuses_controller_spec.rb index 5c2a623954..d9df803971 100644 --- a/spec/controllers/api/v2/filters/statuses_controller_spec.rb +++ b/spec/controllers/api/v2/filters/statuses_controller_spec.rb @@ -22,6 +22,10 @@ RSpec.describe Api::V2::Filters::StatusesController do it 'returns http success' do get :index, params: { filter_id: filter.id } expect(response).to have_http_status(200) + expect(body_as_json) + .to contain_exactly( + include(id: status_filter.id.to_s) + ) end context "when trying to access another's user filters" do