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 />

Just like that, you’re finished restricting updating tickets to only some users.<br />

Now run rake cucumber:ok spec to make sure nothing is broken. Everything<br />

should be good:<br />

31 scenarios (31 passed)<br />

290 steps (290 passed)<br />

# and<br />

24 examples, 0 failures, 10 pending<br />

Fantastic! Let’s commit that:<br />

git add .<br />

git commit -m "Restricting ticket updating to only those who have permission"<br />

git push<br />

Good stuff. In this section, you learned how to restrict the edit and update actions<br />

using the permissions you implemented earlier. There’s one last port of call for this<br />

restricting business: the destroy action.<br />

8.7 Restricting delete access<br />

The final action you restrict is the destroy action in the TicketsController. Again,<br />

you can re-use a feature to test this behavior: the Deleting Tickets feature.<br />

As you did with the Creating Tickets and Updating Tickets features, you implement<br />

a step here in the Deleting Tickets feature to give the user permission to delete tickets.<br />

Under the line that grants users permission to view the TextMate 2 project, put another<br />

one to grant them permission to delete tickets, as shown here:<br />

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

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

When you run this feature, the whole thing passes because you already have the step<br />

that supports the different permissions you require:<br />

1 scenario (1 passed)<br />

12 steps (12 passed)<br />

This feature ensures that anybody with permission can delete tickets for projects, but<br />

you need another spec to test that anybody without permission is prevented from deleting<br />

tickets.<br />

8.7.1 Enforcing destroy protection<br />

To ensure that users without permission to delete tickets can’t do so, you write a spec<br />

(shown next) directly under the one for the update action in spec/controllers/<br />

tickets_controller_spec.rb:<br />

it "cannot delete a ticket without permission" do<br />

delete :destroy, { :project_id => project.id, :id => ticket.id }<br />

response.should redirect_to(project)<br />

flash[:alert].should eql("You cannot delete tickets from this project.")<br />

end<br />

193

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

Saved successfully!

Ooh no, something went wrong!