From 4c5f62de9976b59e59f01c6be571073703e24640 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 14 Jun 2023 03:34:01 -0400 Subject: [PATCH] Extract shared examples from api specs (#25387) --- .../api/v1/accounts_controller_spec.rb | 8 ------- .../admin/account_actions_controller_spec.rb | 16 ------------- .../api/v1/admin/accounts_controller_spec.rb | 16 ------------- .../preview_card_providers_controller_spec.rb | 16 ------------- .../v1/admin/trends/links_controller_spec.rb | 16 ------------- .../admin/trends/statuses_controller_spec.rb | 16 ------------- .../v1/admin/trends/tags_controller_spec.rb | 16 ------------- .../api/v1/domain_blocks_controller_spec.rb | 8 ------- .../api/v2/admin/accounts_controller_spec.rb | 16 ------------- .../v1/admin/canonical_email_blocks_spec.rb | 20 ---------------- .../api/v1/admin/domain_allows_spec.rb | 20 ---------------- .../api/v1/admin/domain_blocks_spec.rb | 20 ---------------- .../api/v1/admin/email_domain_blocks_spec.rb | 20 ---------------- spec/requests/api/v1/admin/ip_blocks_spec.rb | 20 ---------------- spec/requests/api/v1/admin/reports_spec.rb | 20 ---------------- spec/requests/api/v1/featured_tags_spec.rb | 8 ------- spec/support/examples/api.rb | 23 +++++++++++++++++++ 17 files changed, 23 insertions(+), 256 deletions(-) create mode 100644 spec/support/examples/api.rb diff --git a/spec/controllers/api/v1/accounts_controller_spec.rb b/spec/controllers/api/v1/accounts_controller_spec.rb index 49d2867745..0daec691a5 100644 --- a/spec/controllers/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/accounts_controller_spec.rb @@ -13,14 +13,6 @@ RSpec.describe Api::V1::AccountsController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - 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) } diff --git a/spec/controllers/api/v1/admin/account_actions_controller_spec.rb b/spec/controllers/api/v1/admin/account_actions_controller_spec.rb index 0f39a80b12..523350e123 100644 --- a/spec/controllers/api/v1/admin/account_actions_controller_spec.rb +++ b/spec/controllers/api/v1/admin/account_actions_controller_spec.rb @@ -15,22 +15,6 @@ RSpec.describe Api::V1::Admin::AccountActionsController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'POST #create' do context 'with type of disable' do before do diff --git a/spec/controllers/api/v1/admin/accounts_controller_spec.rb b/spec/controllers/api/v1/admin/accounts_controller_spec.rb index 852a521021..36f6e398cb 100644 --- a/spec/controllers/api/v1/admin/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/admin/accounts_controller_spec.rb @@ -15,22 +15,6 @@ RSpec.describe Api::V1::Admin::AccountsController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do let!(:remote_account) { Fabricate(:account, domain: 'example.org') } let!(:other_remote_account) { Fabricate(:account, domain: 'foo.bar') } diff --git a/spec/controllers/api/v1/admin/trends/links/preview_card_providers_controller_spec.rb b/spec/controllers/api/v1/admin/trends/links/preview_card_providers_controller_spec.rb index 883a55b7b6..76e215440d 100644 --- a/spec/controllers/api/v1/admin/trends/links/preview_card_providers_controller_spec.rb +++ b/spec/controllers/api/v1/admin/trends/links/preview_card_providers_controller_spec.rb @@ -16,22 +16,6 @@ describe Api::V1::Admin::Trends::Links::PreviewCardProvidersController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do it 'returns http success' do get :index, params: { account_id: account.id, limit: 2 } diff --git a/spec/controllers/api/v1/admin/trends/links_controller_spec.rb b/spec/controllers/api/v1/admin/trends/links_controller_spec.rb index 9c144d3faf..d9aa06824d 100644 --- a/spec/controllers/api/v1/admin/trends/links_controller_spec.rb +++ b/spec/controllers/api/v1/admin/trends/links_controller_spec.rb @@ -16,22 +16,6 @@ describe Api::V1::Admin::Trends::LinksController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do it 'returns http success' do get :index, params: { account_id: account.id, limit: 2 } diff --git a/spec/controllers/api/v1/admin/trends/statuses_controller_spec.rb b/spec/controllers/api/v1/admin/trends/statuses_controller_spec.rb index d25186b376..4d80055ac0 100644 --- a/spec/controllers/api/v1/admin/trends/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/admin/trends/statuses_controller_spec.rb @@ -16,22 +16,6 @@ describe Api::V1::Admin::Trends::StatusesController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do it 'returns http success' do get :index, params: { account_id: account.id, limit: 2 } diff --git a/spec/controllers/api/v1/admin/trends/tags_controller_spec.rb b/spec/controllers/api/v1/admin/trends/tags_controller_spec.rb index 5ee443d575..0b8eb8c3b8 100644 --- a/spec/controllers/api/v1/admin/trends/tags_controller_spec.rb +++ b/spec/controllers/api/v1/admin/trends/tags_controller_spec.rb @@ -16,22 +16,6 @@ describe Api::V1::Admin::Trends::TagsController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do it 'returns http success' do get :index, params: { account_id: account.id, limit: 2 } diff --git a/spec/controllers/api/v1/domain_blocks_controller_spec.rb b/spec/controllers/api/v1/domain_blocks_controller_spec.rb index aa98ec4c32..20b2909e63 100644 --- a/spec/controllers/api/v1/domain_blocks_controller_spec.rb +++ b/spec/controllers/api/v1/domain_blocks_controller_spec.rb @@ -13,14 +13,6 @@ RSpec.describe Api::V1::DomainBlocksController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #show' do let(:scopes) { 'read:blocks' } diff --git a/spec/controllers/api/v2/admin/accounts_controller_spec.rb b/spec/controllers/api/v2/admin/accounts_controller_spec.rb index 762c84af94..a775be1709 100644 --- a/spec/controllers/api/v2/admin/accounts_controller_spec.rb +++ b/spec/controllers/api/v2/admin/accounts_controller_spec.rb @@ -15,22 +15,6 @@ RSpec.describe Api::V2::Admin::AccountsController do allow(controller).to receive(:doorkeeper_token) { token } end - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET #index' do let!(:remote_account) { Fabricate(:account, domain: 'example.org') } let!(:other_remote_account) { Fabricate(:account, domain: 'foo.bar') } diff --git a/spec/requests/api/v1/admin/canonical_email_blocks_spec.rb b/spec/requests/api/v1/admin/canonical_email_blocks_spec.rb index d70e6fc8a1..4382cb84e5 100644 --- a/spec/requests/api/v1/admin/canonical_email_blocks_spec.rb +++ b/spec/requests/api/v1/admin/canonical_email_blocks_spec.rb @@ -9,26 +9,6 @@ RSpec.describe 'Canonical Email Blocks' do let(:scopes) { 'admin:read:canonical_email_blocks admin:write:canonical_email_blocks' } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/canonical_email_blocks' do subject do get '/api/v1/admin/canonical_email_blocks', headers: headers, params: params diff --git a/spec/requests/api/v1/admin/domain_allows_spec.rb b/spec/requests/api/v1/admin/domain_allows_spec.rb index eb7915e77a..96000e3ef4 100644 --- a/spec/requests/api/v1/admin/domain_allows_spec.rb +++ b/spec/requests/api/v1/admin/domain_allows_spec.rb @@ -9,26 +9,6 @@ RSpec.describe 'Domain Allows' do let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/domain_allows' do subject do get '/api/v1/admin/domain_allows', headers: headers, params: params diff --git a/spec/requests/api/v1/admin/domain_blocks_spec.rb b/spec/requests/api/v1/admin/domain_blocks_spec.rb index b3d52311b3..7a5ac28c56 100644 --- a/spec/requests/api/v1/admin/domain_blocks_spec.rb +++ b/spec/requests/api/v1/admin/domain_blocks_spec.rb @@ -9,26 +9,6 @@ RSpec.describe 'Domain Blocks' do let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/domain_blocks' do subject do get '/api/v1/admin/domain_blocks', headers: headers, params: params diff --git a/spec/requests/api/v1/admin/email_domain_blocks_spec.rb b/spec/requests/api/v1/admin/email_domain_blocks_spec.rb index a24f22be21..d512def866 100644 --- a/spec/requests/api/v1/admin/email_domain_blocks_spec.rb +++ b/spec/requests/api/v1/admin/email_domain_blocks_spec.rb @@ -10,26 +10,6 @@ RSpec.describe 'Email Domain Blocks' do let(:scopes) { 'admin:read:email_domain_blocks admin:write:email_domain_blocks' } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/email_domain_blocks' do subject do get '/api/v1/admin/email_domain_blocks', headers: headers, params: params diff --git a/spec/requests/api/v1/admin/ip_blocks_spec.rb b/spec/requests/api/v1/admin/ip_blocks_spec.rb index 2091ef3dc6..d03886c51b 100644 --- a/spec/requests/api/v1/admin/ip_blocks_spec.rb +++ b/spec/requests/api/v1/admin/ip_blocks_spec.rb @@ -9,26 +9,6 @@ RSpec.describe 'IP Blocks' do let(:scopes) { 'admin:read:ip_blocks admin:write:ip_blocks' } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/ip_blocks' do subject do get '/api/v1/admin/ip_blocks', headers: headers, params: params diff --git a/spec/requests/api/v1/admin/reports_spec.rb b/spec/requests/api/v1/admin/reports_spec.rb index cd9fc100e7..91c3c11f5d 100644 --- a/spec/requests/api/v1/admin/reports_spec.rb +++ b/spec/requests/api/v1/admin/reports_spec.rb @@ -9,26 +9,6 @@ RSpec.describe 'Reports' do let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - - shared_examples 'forbidden for wrong role' do |wrong_role| - let(:role) { UserRole.find_by(name: wrong_role) } - - it 'returns http forbidden' do - subject - - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/admin/reports' do subject do get '/api/v1/admin/reports', headers: headers, params: params diff --git a/spec/requests/api/v1/featured_tags_spec.rb b/spec/requests/api/v1/featured_tags_spec.rb index 8a552c1d4b..6c171f6e47 100644 --- a/spec/requests/api/v1/featured_tags_spec.rb +++ b/spec/requests/api/v1/featured_tags_spec.rb @@ -8,14 +8,6 @@ RSpec.describe 'FeaturedTags' do let(:scopes) { 'read:accounts write:accounts' } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } - shared_examples 'forbidden for wrong scope' do |wrong_scope| - let(:scopes) { wrong_scope } - - it 'returns http forbidden' do - expect(response).to have_http_status(403) - end - end - describe 'GET /api/v1/featured_tags' do context 'with wrong scope' do before do diff --git a/spec/support/examples/api.rb b/spec/support/examples/api.rb new file mode 100644 index 0000000000..d531860abf --- /dev/null +++ b/spec/support/examples/api.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +shared_examples 'forbidden for wrong scope' do |wrong_scope| + let(:scopes) { wrong_scope } + + it 'returns http forbidden' do + # Some examples have a subject which needs to be called to make a request + subject if request.nil? + + expect(response).to have_http_status(403) + end +end + +shared_examples 'forbidden for wrong role' do |wrong_role| + let(:role) { UserRole.find_by(name: wrong_role) } + + it 'returns http forbidden' do + # Some examples have a subject which needs to be called to make a request + subject if request.nil? + + expect(response).to have_http_status(403) + end +end