1
0
Fork 0

Add module Partynest::RSpecAccountRoleHelpers

This commit is contained in:
Alex Kotov 2018-12-13 09:04:38 +05:00
parent 9b92be20c2
commit 76cfc0f9f7
No known key found for this signature in database
GPG key ID: 4E831250F47DE154
5 changed files with 39 additions and 45 deletions

View file

@ -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

View file

@ -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

View file

@ -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 }

View file

@ -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 }

View file

@ -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