-
Notifications
You must be signed in to change notification settings - Fork 0
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
refactor: refactors company controller policies and tests to include … #45
Merged
+107
−38
Merged
Changes from 1 commit
Commits
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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
class CompanyPolicy < ApplicationPolicy | ||
# NOTE: Up to Pundit v2.3.1, the inheritance was declared as | ||
# `Scope < Scope` rather than `Scope < ApplicationPolicy::Scope`. | ||
# In most cases the behavior will be identical, but if updating existing | ||
# code, beware of possible changes to the ancestors: | ||
# https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5 | ||
def create? | ||
user.present? | ||
end | ||
|
||
def index? | ||
user.present? | ||
end | ||
|
||
stefanjbloom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# record.user == user | ||
|
||
class Scope < ApplicationPolicy::Scope | ||
# NOTE: Be explicit about which records you allow access to! | ||
# def resolve | ||
# scope.all | ||
# end | ||
def resolve | ||
return scope.none unless user | ||
|
||
if admin? | ||
scope.all | ||
else | ||
scope.where(user_id: user.id) | ||
end | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,27 +1,90 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe CompanyPolicy, type: :policy do | ||
let(:user) { User.new } | ||
|
||
subject { described_class } | ||
|
||
permissions ".scope" do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
let(:user) { User.create!(name: "user", email: "[email protected]", password: "123") } | ||
let(:other_user) { User.create!(name: "other_user", email: "[email protected]", password: "234") } | ||
let(:admin) { User.create!(name: "admin", email: "[email protected]", password: "456") } | ||
|
||
let(:company) do | ||
Company.create!( | ||
name: "Test Company", | ||
website: "https://testcompany.com", | ||
street_address: "123 Main St", | ||
city: "Testville", | ||
state: "TS", | ||
zip_code: "12345", | ||
notes: "A test company", | ||
user: user | ||
) | ||
end | ||
|
||
let(:other_company) do | ||
Company.create!( | ||
name: "Other Company", | ||
website: "https://othercompany.com", | ||
street_address: "456 Elm St", | ||
city: "Othertown", | ||
state: "OT", | ||
zip_code: "67890", | ||
notes: "Another test company", | ||
user: other_user | ||
) | ||
end | ||
|
||
permissions :show? do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
before(:each) do | ||
admin.set_role(:admin) | ||
end | ||
|
||
permissions :create? do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end | ||
it "allows any logged-in user to create a company" do | ||
expect(subject).to permit(user, Company.new) | ||
expect(subject).to permit(admin, Company.new) | ||
end | ||
|
||
permissions :update? do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
it "does not allow guests to create a company" do | ||
expect(subject).not_to permit(nil, Company.new) | ||
end | ||
end | ||
|
||
permissions :destroy? do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
permissions ".scope" do | ||
let(:scope) { Pundit.policy_scope!(current_user, Company) } | ||
|
||
context "when the user is an admin" do | ||
let(:current_user) { admin } | ||
|
||
it "returns all companies" do | ||
company | ||
other_company | ||
expect(scope).to match_array(Company.all) | ||
end | ||
end | ||
|
||
context "when the user is a regular user" do | ||
let(:current_user) { user } | ||
|
||
it "returns only the user's companies" do | ||
company | ||
other_company | ||
expect(scope).to match_array([company]) | ||
end | ||
|
||
it "does not return other users' companies" do | ||
company | ||
other_company | ||
expect(scope).not_to include(other_company) | ||
end | ||
end | ||
|
||
context "when no user is logged in" do | ||
let(:current_user) { nil } | ||
|
||
it "returns an empty relation" do | ||
company | ||
other_company | ||
expect(scope).to match_array([]) | ||
end | ||
end | ||
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.
Not relevant for this PR, but I wanted to make note of this so we have it on the record. There is a discrepancy in how different controllers are handling params, I know that
Contacts
andJobApplications
handle this private method as followNote the
require(:model)
in the case above. The Companies controller is only usingparams.permit
.I don't think its inherently wrong, and like I said it isn't explicitly related to this ticket but we should be aware of the inconsistency moving forward.