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 49d286774..0daec691a 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 0f39a80b1..523350e12 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 852a52102..36f6e398c 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 883a55b7b..76e215440 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 9c144d3fa..d9aa06824 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 d25186b37..4d80055ac 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 5ee443d57..0b8eb8c3b 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 aa98ec4c3..20b2909e6 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 762c84af9..a775be170 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 d70e6fc8a..4382cb84e 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 eb7915e77..96000e3ef 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 b3d52311b..7a5ac28c5 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 a24f22be2..d512def86 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 2091ef3dc..d03886c51 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 cd9fc100e..91c3c11f5 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 8a552c1d4..6c171f6e4 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 000000000..d531860ab --- /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