Skip to content

Commit 900ec15

Browse files
committed
Less confusing example of authorization
Currently, the route `payments#index` exists but is disallowed to all users. This is probably in order to provide a context to the authorization spec, but I think it's confusing. Now that we have an authentication mechanism, we can use this to provide a clearer (I hope) authorization spec and a less confusing app.
1 parent 89ff922 commit 900ec15

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

spec/example_app/app/policies/payment_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PaymentPolicy < ApplicationPolicy
22
def index?
3-
false
3+
user.admin?
44
end
55

66
def create?

spec/features/authorization_spec.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,25 @@ def show?
2222
Product.policy_class = @original_product_policy
2323
end
2424

25-
it "shows link to resource for which index? is authorized" do
26-
visit admin_customers_path
27-
navigation = find(".navigation")
28-
expect(navigation).to have_link("Products")
29-
end
25+
describe "navigation" do
26+
def navigation
27+
visit admin_customers_path
28+
find(".navigation")
29+
end
30+
31+
def become_user(customer)
32+
visit become_admin_customer_path(customer)
33+
end
3034

31-
it "hides link to resource for which index? is not authorized" do
32-
visit admin_customers_path
33-
navigation = find(".navigation")
34-
expect(navigation).not_to have_link("Payments")
35+
it "shows links to sections with authorized index" do
36+
expect(navigation).to have_link("Payments")
37+
end
38+
39+
it "hides links to sections without authorized index" do
40+
customer = create(:customer, name: "Non Admin")
41+
become_user(customer)
42+
expect(navigation).not_to have_link("Payments")
43+
end
3544
end
3645

3746
it "renders all results yielded by the scope" do

0 commit comments

Comments
 (0)