From 76cfc0f9f746cb7a59035bb5d67241bfa91c3fcf Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 13 Dec 2018 09:04:38 +0500 Subject: [PATCH] Add module Partynest::RSpecAccountRoleHelpers --- lib/partynest/rspec_account_role_helpers.rb | 18 +++++++++++++++ spec/policies/application_policy_spec.rb | 17 ++++---------- .../staff/telegram_bot_policy_spec.rb | 22 +++++-------------- .../staff/telegram_chat_policy_spec.rb | 22 +++++-------------- spec/rails_helper.rb | 5 +++++ 5 files changed, 39 insertions(+), 45 deletions(-) create mode 100644 lib/partynest/rspec_account_role_helpers.rb diff --git a/lib/partynest/rspec_account_role_helpers.rb b/lib/partynest/rspec_account_role_helpers.rb new file mode 100644 index 0000000..fab49c3 --- /dev/null +++ b/lib/partynest/rspec_account_role_helpers.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Partynest + module RSpecAccountRoleHelpers + def for_account_types(*account_types, &block) + account_types.each do |account_type| + account_type = :"#{account_type}_account" unless account_type.nil? + + context "when #{account_type || :no_account} is authenticated" do + let(:current_account) { create account_type if account_type } + let(:account) { current_account } + + instance_eval(&block) + end + end + end + end +end diff --git a/spec/policies/application_policy_spec.rb b/spec/policies/application_policy_spec.rb index 9da0ba9..421dfd4 100644 --- a/spec/policies/application_policy_spec.rb +++ b/spec/policies/application_policy_spec.rb @@ -7,19 +7,10 @@ RSpec.describe ApplicationPolicy do let(:record) { nil } - [ - nil, - :guest_account, - :usual_account, - :superuser_account, - ].each do |account_type| - context "when #{account_type || :no_account} is authenticated" do - let(:account) { create account_type if account_type } - - it do - is_expected.to \ - forbid_actions %i[index show new create edit update destroy] - end + for_account_types nil, :guest, :usual, :superuser do + it do + is_expected.to \ + forbid_actions %i[index show new create edit update destroy] end end end diff --git a/spec/policies/staff/telegram_bot_policy_spec.rb b/spec/policies/staff/telegram_bot_policy_spec.rb index f68e34c..4ca3ca8 100644 --- a/spec/policies/staff/telegram_bot_policy_spec.rb +++ b/spec/policies/staff/telegram_bot_policy_spec.rb @@ -13,25 +13,15 @@ RSpec.describe Staff::TelegramBotPolicy do before { create_list :telegram_bot, 3 } - [ - nil, - :guest_account, - :usual_account, - ].each do |account_type| - context "when #{account_type || :no_account} is authenticated" do - let(:account) { create account_type if account_type } + for_account_types nil, :guest, :usual do + it { is_expected.to forbid_actions %i[index show destroy] } + it { is_expected.to forbid_new_and_create_actions } + it { is_expected.to forbid_edit_and_update_actions } - it { is_expected.to forbid_actions %i[index show destroy] } - it { is_expected.to forbid_new_and_create_actions } - it { is_expected.to forbid_edit_and_update_actions } - - specify { expect(resolved_scope).to be_empty } - end + specify { expect(resolved_scope).to be_empty } end - context 'when superuser account is authenticated' do - let(:account) { create :superuser_account } - + for_account_types :superuser do it { is_expected.to permit_actions %i[index show] } it { is_expected.to forbid_new_and_create_actions } diff --git a/spec/policies/staff/telegram_chat_policy_spec.rb b/spec/policies/staff/telegram_chat_policy_spec.rb index 00d129f..fde2b71 100644 --- a/spec/policies/staff/telegram_chat_policy_spec.rb +++ b/spec/policies/staff/telegram_chat_policy_spec.rb @@ -13,25 +13,15 @@ RSpec.describe Staff::TelegramChatPolicy do before { create_list :telegram_chat, 3 } - [ - nil, - :guest_account, - :usual_account, - ].each do |account_type| - context "when #{account_type || :no_account} is authenticated" do - let(:account) { create account_type if account_type } + for_account_types nil, :guest, :usual do + it { is_expected.to forbid_actions %i[index show destroy] } + it { is_expected.to forbid_new_and_create_actions } + it { is_expected.to forbid_edit_and_update_actions } - it { is_expected.to forbid_actions %i[index show destroy] } - it { is_expected.to forbid_new_and_create_actions } - it { is_expected.to forbid_edit_and_update_actions } - - specify { expect(resolved_scope).to be_empty } - end + specify { expect(resolved_scope).to be_empty } end - context 'when superuser account is authenticated' do - let(:account) { create :superuser_account } - + for_account_types :superuser do it { is_expected.to permit_actions %i[index show] } it { is_expected.to forbid_new_and_create_actions } diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c69e3b3..e64bd93 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -15,6 +15,8 @@ require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! +require 'partynest/rspec_account_role_helpers' + # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end @@ -43,6 +45,7 @@ rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end + RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = Rails.root.join('fixtures').to_s @@ -71,4 +74,6 @@ RSpec.configure do |config| config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") + + config.extend Partynest::RSpecAccountRoleHelpers end