-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow :create Entry only for publishers on accounts, not on entries #836
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c518af5
Fix shared example for membership bases permissions
tf 574c1e5
Improve shared example for membership based permissions
tf 82ac17e
Allow :create Entry only for publishers on accounts, not on entries
aviav b130927
Use `of_entry_or_its_account` for readable policy specs
tf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,48 +2,82 @@ | |
|
||
module Pageflow | ||
shared_examples 'a membership-based permission that' do |params| | ||
let(:user) { create(:user) } | ||
let(:topic) { instance_exec(¶ms[:topic]) } | ||
let(:entity) { of.call(topic) } | ||
let(:policy) { described_class.new(user, topic) } | ||
allowed_params = [ | ||
:allows, :forbids, :but_forbids, | ||
:of_account, :of_entry, :of_entry_or_its_account, | ||
:to, :topic | ||
] | ||
|
||
unknown_params = params.keys - allowed_params | ||
|
||
if unknown_params.any? | ||
fail("Unknown params: #{unknown_params * ', '}") | ||
end | ||
|
||
if params[:of_entry_or_its_account] | ||
params[:of_entry] = params[:of_entry_or_its_account] | ||
|
||
params[:of_account] = lambda do |topic| | ||
entry = params[:of_entry_or_its_account].call(topic) | ||
entry.account | ||
end | ||
end | ||
|
||
if !params[:of_entry] && !params[:of_account] | ||
fail('Speficy at least one of the following options: of_entry, of_account') | ||
end | ||
|
||
params[:forbids] ||= params[:but_forbids] | ||
|
||
if !params[:allows] && !params[:forbids] | ||
fail('Specify at least one of the following options: allows, forbids') | ||
end | ||
|
||
topic_class_name = described_class.name.humanize.sub('Pageflow::', '').chomp('policy') | ||
|
||
let(:user) { create(:user) } | ||
let(:topic) { instance_exec(¶ms[:topic]) } | ||
let(:entity) { of.call(topic) } | ||
let(:policy) { described_class.new(user, topic) } | ||
|
||
[:of_entry, :of_account].each do |membership_type| | ||
if params[membership_type] | ||
next unless params[membership_type] | ||
|
||
# This context needs to be here to create a scope for the `let` | ||
# in the next line. Otherwise, when both the `:of_entry` and | ||
# `:of_account` params are present, the second iteration of the | ||
# loop will override the definition of the first, causing the | ||
# specs to be defined for the account case two times. | ||
context do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block has too many lines. [26/25] |
||
let(:of) { params[membership_type] } | ||
|
||
it "allows #{membership_type == :of_account ? 'account' : 'entry'} #{params[:allows]} " \ | ||
"to #{params[:to]} respective " \ | ||
"#{described_class.name.humanize.sub('Pageflow::policies::', '').chomp('policy')}" do | ||
create(:membership, user: user, entity: entity, role: params[:allows]) | ||
if params[:allows].present? | ||
it "allows #{membership_type == :of_account ? 'account' : 'entry'} #{params[:allows]} " \ | ||
"to #{params[:to]} respective #{topic_class_name}" do | ||
create(:membership, user: user, entity: entity, role: params[:allows]) | ||
|
||
expect(policy).to permit_action(params[:to]) | ||
end | ||
expect(policy).to permit_action(params[:to]) | ||
end | ||
|
||
it "does not allow #{membership_type == :of_account ? 'account' : 'entry'} " \ | ||
"#{params[:allows]} to #{params[:to]} off-limits " \ | ||
"#{described_class.name.humanize.sub('Pageflow::policies::', '').chomp('policy')}" do | ||
other_entity = (membership_type == :of_entry ? create(:entry) : create(:account)) | ||
create(:membership, user: user, entity: other_entity, role: params[:allows]) | ||
it "does not allow #{membership_type == :of_account ? 'account' : 'entry'} " \ | ||
"#{params[:allows]} to #{params[:to]} off-limits #{topic_class_name}" do | ||
other_entity = (membership_type == :of_entry ? create(:entry) : create(:account)) | ||
create(:membership, user: user, entity: other_entity, role: params[:allows]) | ||
|
||
expect(policy).not_to permit_action(params[:to]) | ||
expect(policy).not_to permit_action(params[:to]) | ||
end | ||
end | ||
|
||
if params[:but_forbids].present? | ||
if params[:forbids].present? | ||
it "does not allow #{membership_type == :of_account ? 'account' : 'entry'} " \ | ||
"#{params[:but_forbids]} to #{params[:to]} respective " \ | ||
"#{described_class.name.humanize.sub('Pageflow::policies::', '').chomp('policy')}" do | ||
create(:membership, user: user, entity: entity, role: params[:but_forbids]) | ||
"#{params[:forbids]} to #{params[:to]} respective #{topic_class_name}" do | ||
create(:membership, user: user, entity: entity, role: params[:forbids]) | ||
|
||
expect(policy).not_to permit_action(params[:to]) | ||
end | ||
else | ||
it 'does not allow user without membership ' \ | ||
"to #{params[:to]} respective " \ | ||
"#{described_class.name.humanize.sub('Pageflow::policies::', '').chomp('policy')}" do | ||
"to #{params[:to]} respective #{topic_class_name}" do | ||
expect(policy).not_to permit_action(params[:to]) | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use %i or %I for an array of symbols.