Skip to content

Commit 0a24688

Browse files
author
Ryan Bigg
committed
Section 7.2.2: Lock down specific projects controller actions for admins only
1 parent ae9e195 commit 0a24688

File tree

6 files changed

+95
-12
lines changed

6 files changed

+95
-12
lines changed

ticketee/app/helpers/application_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ def title(*parts)
66
end
77
end
88
end
9+
10+
def admins_only(&block)
11+
block.call if current_user.try(:admin?)
12+
end
913
end

ticketee/app/views/projects/index.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<%= link_to "New Project", new_project_path, class: "new" %>
1+
<% admins_only do %>
2+
<%= link_to "New Project", new_project_path, class: "new" %>
3+
<% end %>
24

35
<h2>Projects</h2>
46
<ul>

ticketee/app/views/projects/show.html.erb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<% title(@project.name, "Projects") %>
22
<h2><%= @project.name %></h2>
3-
<%= link_to "Edit Project",
4-
edit_project_path(@project),
5-
class: "edit" %>
3+
<% admins_only do %>
4+
<%= link_to "Edit Project",
5+
edit_project_path(@project),
6+
class: "edit" %>
67

7-
<%= link_to "Delete Project",
8-
project_path(@project),
9-
method: :delete,
10-
data: { confirm:
11-
"Are you sure you want to delete this project?"
12-
},
13-
class: "delete" %>
8+
<%= link_to "Delete Project",
9+
project_path(@project),
10+
method: :delete,
11+
data: { confirm:
12+
"Are you sure you want to delete this project?"
13+
},
14+
class: "delete" %>
15+
<% end %>
1416

1517
<%= link_to "New Ticket",
1618
new_project_ticket_path(@project),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require "rails_helper"
2+
3+
feature "hidden links" do
4+
let(:user) { FactoryGirl.create(:user) }
5+
let(:admin) { FactoryGirl.create(:admin_user) }
6+
let(:project) { FactoryGirl.create(:project) }
7+
8+
context "anonymous users" do
9+
scenario "cannot see the New Project link" do
10+
visit "/"
11+
assert_no_link_for "New Project"
12+
end
13+
14+
scenario "cannot see the Edit Project link" do
15+
visit project_path(project)
16+
assert_no_link_for "Edit Project"
17+
end
18+
19+
scenario "cannot see the Delete Project link" do
20+
visit project_path(project)
21+
assert_no_link_for "Delete Project"
22+
end
23+
end
24+
25+
context "regular users" do
26+
before { login_as(user) }
27+
scenario "cannot see the New Project link" do
28+
visit "/"
29+
assert_no_link_for "New Project"
30+
end
31+
32+
scenario "cannot see the Edit Project link" do
33+
visit project_path(project)
34+
assert_no_link_for "Edit Project"
35+
end
36+
37+
scenario "cannot see the Delete Project link" do
38+
visit project_path(project)
39+
assert_no_link_for "Delete Project"
40+
end
41+
end
42+
43+
context "admin users" do
44+
before { login_as(admin) }
45+
scenario "can see the New Project link" do
46+
visit "/"
47+
assert_link_for "New Project"
48+
end
49+
50+
scenario "can see the Edit Project link" do
51+
visit project_path(project)
52+
assert_link_for "Edit Project"
53+
end
54+
55+
scenario "can see the Delete Project link" do
56+
visit project_path(project)
57+
assert_link_for "Delete Project"
58+
end
59+
end
60+
end

ticketee/spec/rails_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# directory. Alternatively, in the individual `*_spec.rb` files, manually
1919
# require only the support files necessary.
2020
#
21-
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
21+
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
2222

2323
# Checks for pending migrations before tests are run.
2424
# If you are not using ActiveRecord, you can remove this line.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module CapybaraHelpers
2+
def assert_no_link_for(text)
3+
expect(page).to_not(have_css("a", :text => text),
4+
"Expected not to see the #{text.inspect} link, but did.")
5+
end
6+
7+
def assert_link_for(text)
8+
expect(page).to(have_css("a", :text => text),
9+
"Expected to see the #{text.inspect} link, but did not.")
10+
end
11+
end
12+
13+
RSpec.configure do |config|
14+
config.include CapybaraHelpers, :type => :feature
15+
end

0 commit comments

Comments
 (0)