From 75e568e075b66c8818c461dd855508fe2516731f Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 13 Dec 2018 08:01:50 +0500 Subject: [PATCH] Test Pundit policies --- Gemfile | 3 +++ Gemfile.lock | 3 +++ lib/templates/rspec/policy/policy_spec.rb | 20 +------------------ spec/policies/application_policy_spec.rb | 20 +++++++++++++++++++ spec/policies/membership_app_policy_spec.rb | 20 +------------------ .../account_telegram_contact_policy_spec.rb | 20 +------------------ .../passport_confirmation_policy_spec.rb | 20 +------------------ spec/policies/staff/passport_policy_spec.rb | 20 +------------------ .../staff/telegram_bot_policy_spec.rb | 20 +------------------ .../staff/telegram_chat_policy_spec.rb | 20 +------------------ spec/rails_helper.rb | 2 +- 11 files changed, 34 insertions(+), 134 deletions(-) create mode 100644 spec/policies/application_policy_spec.rb diff --git a/Gemfile b/Gemfile index 9b4f4a7..e2d525c 100644 --- a/Gemfile +++ b/Gemfile @@ -204,4 +204,7 @@ group :test do # It aims to mimic the behaviour of a real user, # and as such interacts with the HTML of the application. gem 'selenium-webdriver', '~> 3.14' + + # A set of RSpec matchers for testing Pundit authorization policies. + gem 'pundit-matchers', '~> 1.6' end diff --git a/Gemfile.lock b/Gemfile.lock index 3e89d51..713d957 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -250,6 +250,8 @@ GEM puma (3.12.0) pundit (2.0.0) activesupport (>= 3.0.0) + pundit-matchers (1.6.0) + rspec-rails (>= 3.0.0) rack (2.0.6) rack-attack (5.4.2) rack (>= 1.0, < 3) @@ -459,6 +461,7 @@ DEPENDENCIES pry-rails (~> 0.3) puma (~> 3.11) pundit (~> 2.0) + pundit-matchers (~> 1.6) rack-attack (~> 5.4) rails (~> 5.2.1) rails-erd (~> 1.5) diff --git a/lib/templates/rspec/policy/policy_spec.rb b/lib/templates/rspec/policy/policy_spec.rb index 909fd7e..a6408ae 100644 --- a/lib/templates/rspec/policy/policy_spec.rb +++ b/lib/templates/rspec/policy/policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe <%= class_name %>Policy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/application_policy_spec.rb b/spec/policies/application_policy_spec.rb new file mode 100644 index 0000000..43c8d87 --- /dev/null +++ b/spec/policies/application_policy_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ApplicationPolicy do + subject { described_class.new context, record } + + let :context do + described_class::Context.new account: account, guest_account: guest_account + end + + let(:account) { create :superuser_account } + let(:guest_account) { create :guest_account } + + let(:record) { nil } + + it do + is_expected.to forbid_actions %i[index show new create edit update destroy] + end +end diff --git a/spec/policies/membership_app_policy_spec.rb b/spec/policies/membership_app_policy_spec.rb index b91c39a..1304f91 100644 --- a/spec/policies/membership_app_policy_spec.rb +++ b/spec/policies/membership_app_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe MembershipAppPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/settings/account_telegram_contact_policy_spec.rb b/spec/policies/settings/account_telegram_contact_policy_spec.rb index 3e50898..b334822 100644 --- a/spec/policies/settings/account_telegram_contact_policy_spec.rb +++ b/spec/policies/settings/account_telegram_contact_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe Settings::AccountTelegramContactPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/staff/passport_confirmation_policy_spec.rb b/spec/policies/staff/passport_confirmation_policy_spec.rb index ae06cc8..e9bfbe9 100644 --- a/spec/policies/staff/passport_confirmation_policy_spec.rb +++ b/spec/policies/staff/passport_confirmation_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe Staff::PassportConfirmationPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/staff/passport_policy_spec.rb b/spec/policies/staff/passport_policy_spec.rb index 12d191d..b0c1690 100644 --- a/spec/policies/staff/passport_policy_spec.rb +++ b/spec/policies/staff/passport_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe Staff::PassportPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/staff/telegram_bot_policy_spec.rb b/spec/policies/staff/telegram_bot_policy_spec.rb index d772021..175afbc 100644 --- a/spec/policies/staff/telegram_bot_policy_spec.rb +++ b/spec/policies/staff/telegram_bot_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe Staff::TelegramBotPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/policies/staff/telegram_chat_policy_spec.rb b/spec/policies/staff/telegram_chat_policy_spec.rb index 5508061..3ce4804 100644 --- a/spec/policies/staff/telegram_chat_policy_spec.rb +++ b/spec/policies/staff/telegram_chat_policy_spec.rb @@ -3,23 +3,5 @@ require 'rails_helper' RSpec.describe Staff::TelegramChatPolicy do - permissions '.scope' do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :show? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :create? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :update? do - pending "add some examples to (or delete) #{__FILE__}" - end - - permissions :destroy? do - pending "add some examples to (or delete) #{__FILE__}" - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 7fae015..6a04d55 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -15,7 +15,7 @@ require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! -require 'pundit/rspec' +require 'pundit/matchers' # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are