Skip to content

Commit

Permalink
improves role tests and adds fullproducer role
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmaciel committed May 17, 2018
1 parent 4577fd6 commit 7a25605
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class User < ApplicationRecord
acts_as_paranoid
has_secure_password

enum user_role: [:admin, :script_writer, :video_producer, :organization_admin, :system_admin, :system_member]
enum user_role: [:admin, :fullproducer, :script_writer, :video_producer, :organization_admin, :system_admin, :system_member]

validates :name, :email, :user_role, :password_digest, presence: true

Expand Down
9 changes: 4 additions & 5 deletions app/policies/access_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def configure
can [:post, :edit, :destroy], Comment
end

role :script_writer, user_role: 'script_writer' do
role :script_writer, lambda { |u| u.user_role.in?(['script_writer', 'fullproducer']) } do
can :manage, Video
can :manage, Task
can :read_collection, Video
Expand All @@ -34,7 +34,7 @@ def configure
end
end

role :video_producer, user_role: 'video_producer' do
role :video_producer, lambda { |u| u.user_role.in?(['video_producer', 'fullproducer']) } do
can [:post, :edit, :destroy], Comment do |target_comment, current_user|
current_user.video_ids.include?(target_comment.video_id)
end
Expand All @@ -44,8 +44,7 @@ def configure
end

can [:create, :destroy], Attachment do |target_attatchment, current_user|
current_user.video_ids.include?(target_attatchment.video_id) &&
target_comment.video.source.organization_id == current_user.organization_id
current_user.video_ids.include?(target_attatchment.video_id)
end

can [
Expand All @@ -67,7 +66,7 @@ def configure
target_user.organization == current_user.organization
end

can [:create, :update, :destroy, :read, :cancel_video, :assign, :send_request], Video do |target_video, current_user|
can [:create, :update, :destroy, :read, :cancel_video, :assign, :send_request, :refused_by_customer], Video do |target_video, current_user|
target_video.system.organization_id == current_user.organization_id
end

Expand Down
49 changes: 31 additions & 18 deletions spec/policies/access_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,44 @@

describe AccessPolicy do
subject(:ability) { described_class.new(current_user) }
let!(:fullrole) { [:create, :update, :destroy, :read] }
fullrole = [:create, :update, :destroy, :read]

describe '#admin abilities' do
let(:current_user) { users(:user_admin) }

describe '#video' do
describe '#video epecial permissions' do
it { expect(ability.can?(:cancel_video, Video)).to be_truthy }
it { expect(ability.can?(:assign, Video)).to be_truthy }
it ':manage' do
fullrole.each do |role|
expect(ability.can?(role, Video)).to be_truthy
end
end
it { expect(ability.can?(:read_collection, Organization)).to be_truthy }
end

describe '#user' do
it ':manage' do
[User, Organization, Video].each do |const|
describe "##{const}" do
fullrole.each do |role|
expect(ability.can?(role, User)).to be_truthy
it role do
expect(ability.can?(role, const)).to be_truthy
end
end
end
end
end

describe '#organization' do
it { expect(ability.can?(:read_collection, Organization)).to be_truthy }
it ':manage' do
fullrole.each { |role| expect(ability.can?(role, User)).to be_truthy }
['script_writer', 'fullproducer'].each do |role|
describe role do
let(:current_user) do
user = users(:user_admin)
user.user_role = role
user
end

[Task, Video].each do |const|
describe "##{const}" do
fullrole.each do |role|
it role do
expect(ability.can?(role, const)).to be_truthy
end
end
end
end
end
end
Expand All @@ -41,16 +52,18 @@
context 'when the target user is from same org' do
let(:same_org_user) { users(:user_system_member) }

it ':manage' do
fullrole.each { |role| expect(ability.can?(role, same_org_user)).to be_truthy }
fullrole.each do |role|
it role do
expect(ability.can?(role, same_org_user)).to be_truthy
end
end
end

context 'when the target user is from same org' do
let(:other_org_user) { users(:software_house_member) }

it 'cannot manage' do
fullrole.each do |role|
fullrole.each do |role|
it role do
expect(ability.can?(role, other_org_user)).to be_falsey
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

ENV['RAILS_ENV'] ||= 'test'
abort("The Rails environment is running in production mode!") if Rails.env.production?

Expand Down

0 comments on commit 7a25605

Please sign in to comment.