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 delete access<br />

Expected to see the "Edit" link, but did not.<br />

(RSpec::Expectations::ExpectationNotMetError)<br />

This time, you edit the file app/views/tickets/show.html.erb. Change the Edit link<br />

from this<br />

<br />

to this:<br />

<br />

<br />

<br />

With this one small change to use the authorized? helper to check for the permission<br />

to edit tickets for the current project, the Hidden Links feature now passes when you<br />

run bin/cucumber features/editing_tickets.feature:<br />

13 scenarios (13 passed)<br />

97 steps (97 passed)<br />

Great! You’ve got one last link to protect now: the Delete Project link on the tickets<br />

show page. Add another three scenarios to this feature, shown in the following listing.<br />

Listing 8.13 features/deleting_tickets.feature<br />

Scenario: Delete ticket link is shown to a user with permission<br />

Given "user@ticketee.com" can view the "TextMate 2" project<br />

And "user@ticketee.com" can delete tickets in the "TextMate 2" project<br />

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

When I follow "TextMate 2"<br />

And I follow "Shiny!"<br />

Then I should see "Delete"<br />

Scenario: Delete ticket link is hidden from a user without permission<br />

Given "user@ticketee.com" can view the "TextMate 2" project<br />

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

When I follow "TextMate 2"<br />

And I follow "Shiny!"<br />

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

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

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

When I follow "TextMate 2"<br />

And I follow "Shiny!"<br />

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

When you run this feature, the middle scenario fails again:<br />

Then I should not see "Delete"<br />

Expected to not see the "Delete" link, but did.<br />

(RSpec::Expectations::ExpectationNotMetError)<br />

To fix it, open or switch back to app/views/tickets/show.html.erb and wrap the Delete<br />

Ticket link in the warm embrace of the authorized? method, just as you did with the<br />

Edit Ticket link:<br />

197

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

Saved successfully!

Ooh no, something went wrong!