Skip to content

Commit 3f24cb0

Browse files
author
Ryan Bigg
committed
Section 7.2.2: Lock down specific projects controller actions for admins only
1 parent 9732de6 commit 3f24cb0

File tree

9 files changed

+122
-13
lines changed

9 files changed

+122
-13
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
<h1><%= @project.name %></h1>
44
<p><%= @project.description %></p>
55

6-
<%= link_to "Edit Project",
7-
edit_project_path(@project),
8-
class: "edit" %>
6+
<% admins_only do %>
7+
<%= link_to "Edit Project",
8+
edit_project_path(@project),
9+
class: "edit" %>
910

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

1920
<%= link_to "New Ticket",
2021
new_project_ticket_path(@project),

ticketee/spec/controllers/projects_controller_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
require 'rails_helper'
22

33
RSpec.describe ProjectsController, :type => :controller do
4+
let(:user) { FactoryGirl.create(:user) }
5+
6+
context "standard users" do
7+
before do
8+
allow(controller).to receive(:current_user).and_return(user)
9+
end
10+
11+
{ new: :get,
12+
create: :post,
13+
edit: :get,
14+
update: :put,
15+
destroy: :delete }.each do |action, method|
16+
17+
it "cannot access the #{action} action" do
18+
send(method, action, :id => FactoryGirl.create(:project))
19+
20+
expect(response).to redirect_to(root_path)
21+
expect(flash[:alert]).to eql("You must be an admin to do that.")
22+
end
23+
end
24+
end
25+
426
it "displays an error for a missing project" do
527
get :show, id: "not-here"
628

ticketee/spec/features/deleting_projects_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
require "rails_helper"
22

33
feature "Deleting projects" do
4+
before do
5+
login_as(FactoryGirl.create(:admin_user))
6+
end
7+
48
scenario "Deleting a project" do
59
FactoryGirl.create(:project, name: "Sublime Text 3")
610

ticketee/spec/features/editing_projects_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
feature "Editing Projects" do
44
before do
5+
login_as(FactoryGirl.create(:admin_user))
56
FactoryGirl.create(:project, name: "Sublime Text 3")
67

78
visit "/"
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)