27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Restricting actions to admins only<br />

Scenario: Delete project link is hidden for non-signed-in users<br />

Given I am on the homepage<br />

When I follow "TextMate 2"<br />

Then I should not see the "Delete Project" link<br />

Scenario: Delete project link is hidden for signed-in users<br />

Given I am signed in as "user@ticketee.com"<br />

When I follow "TextMate 2"<br />

Then I should not see the "Delete Project" link<br />

Scenario: Delete project link is shown to admins<br />

Given I am signed in as "admin@ticketee.com"<br />

When I follow "TextMate 2"<br />

Then I should see the "Delete Project" link<br />

To make these steps pass, change the ProjectsController’s show template to wrap<br />

these links in the admins_only helper, as shown in the next listing.<br />

Listing 7.15 app/views/projects/show.html.erb<br />

<br />

<br />

:delete,<br />

:confirm => "Are you sure you want to delete this project?" %><br />

<br />

When you run this entire feature using bin/cucumber features/hidden_links<br />

.feature, all the steps should pass:<br />

9 scenarios (9 passed)<br />

42 steps (42 passed)<br />

All right, that was a little too easy! But that’s Rails.<br />

This is a great point to ensure that everything is still working by running rake<br />

cucumber:ok spec. According to the following output, it is:<br />

25 scenarios (25 passed)<br />

200 steps (200 passed)<br />

# and<br />

11 examples, 0 failures, 5 pending<br />

Let’s commit and push that:<br />

git add .<br />

git commit -<br />

m "Lock down specific projects controller actions for admins only"<br />

git push<br />

In this section, you ensured that only users with the admin attribute set to true can get<br />

to specific actions in your ProjectsController as an example of basic authorization.<br />

Next, you learn to “section off” part of your site using a similar methodology and<br />

explore the concept of namespacing.<br />

147

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!