From 96f8070e79bd5609ebdb437f66928af499fef033 Mon Sep 17 00:00:00 2001 From: Mukesh Chaudhary Date: Sat, 28 Dec 2019 01:27:59 +0530 Subject: [PATCH] Add specs for Policies (#392) * Add specs for Policies * Fix CI issues --- spec/factories/users.rb | 4 ++++ spec/policies/contact_policy_spec.rb | 31 ++++++++++++++++++++++++++++ spec/policies/inbox_policy_spec.rb | 31 ++++++++++++++++++++++++++++ spec/policies/user_policy_spec.rb | 31 ++++++++++++++++++++++++++++ spec/rails_helper.rb | 1 + 5 files changed, 98 insertions(+) create mode 100644 spec/policies/contact_policy_spec.rb create mode 100644 spec/policies/inbox_policy_spec.rb create mode 100644 spec/policies/user_policy_spec.rb diff --git a/spec/factories/users.rb b/spec/factories/users.rb index aa9b718d3..ef2ae752c 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -22,5 +22,9 @@ FactoryBot.define do trait :with_avatar do avatar { Rack::Test::UploadedFile.new('spec/assets/avatar.png', 'image/png') } end + + trait :administrator do + role { 'administrator' } + end end end diff --git a/spec/policies/contact_policy_spec.rb b/spec/policies/contact_policy_spec.rb new file mode 100644 index 000000000..f6cad9718 --- /dev/null +++ b/spec/policies/contact_policy_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ContactPolicy, type: :policy do + subject(:contact_policy) { described_class } + + let(:administrator) { create(:user, :administrator) } + let(:agent) { create(:user) } + let(:contact) { create(:contact) } + + permissions :index?, :show?, :update? do + context 'when administrator' do + it { expect(contact_policy).to permit(administrator, contact) } + end + + context 'when agent' do + it { expect(contact_policy).not_to permit(agent, contact) } + end + end + + permissions :create? do + context 'when administrator' do + it { expect(contact_policy).to permit(administrator, contact) } + end + + context 'when agent' do + it { expect(contact_policy).to permit(agent, contact) } + end + end +end diff --git a/spec/policies/inbox_policy_spec.rb b/spec/policies/inbox_policy_spec.rb new file mode 100644 index 000000000..a963f4aec --- /dev/null +++ b/spec/policies/inbox_policy_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe InboxPolicy, type: :policy do + subject(:inbox_policy) { described_class } + + let(:administrator) { create(:user, :administrator) } + let(:agent) { create(:user) } + let(:inbox) { create(:inbox) } + + permissions :create?, :destroy? do + context 'when administrator' do + it { expect(inbox_policy).to permit(administrator, inbox) } + end + + context 'when agent' do + it { expect(inbox_policy).not_to permit(agent, inbox) } + end + end + + permissions :index? do + context 'when administrator' do + it { expect(inbox_policy).to permit(administrator, inbox) } + end + + context 'when agent' do + it { expect(inbox_policy).to permit(agent, inbox) } + end + end +end diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb new file mode 100644 index 000000000..95634a0a7 --- /dev/null +++ b/spec/policies/user_policy_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UserPolicy, type: :policy do + subject(:user_policy) { described_class } + + let(:administrator) { create(:user, :administrator) } + let(:agent) { create(:user) } + let(:user) { create(:user) } + + permissions :create?, :update?, :destroy? do + context 'when administrator' do + it { expect(user_policy).to permit(administrator, user) } + end + + context 'when agent' do + it { expect(user_policy).not_to permit(agent, user) } + end + end + + permissions :index? do + context 'when administrator' do + it { expect(user_policy).to permit(administrator, user) } + end + + context 'when agent' do + it { expect(user_policy).to permit(agent, user) } + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 8f4e454e6..9f5d94278 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -4,6 +4,7 @@ require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort('The Rails environment is running in production mode!') if Rails.env.production? require 'rspec/rails' +require 'pundit/rspec' # Add additional requires below this line. Rails is not loaded until this point!