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

For this feature, at features/editing_tickets.feature, you set up a Permission that<br />

says the user you sign in as has permission to update tickets. To do this, write a step in<br />

the Background directly under the other one that sets up read access, as shown here:<br />

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

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

When you run bin/cucumber features/editing_tickets.feature, it all passes, just<br />

as you expect. This step covers the scenario in which the user has permission to<br />

update tickets; to cover the scenario in which the user doesn’t have permission, you<br />

need to write a couple of specs first.<br />

8.6.1 No updating for you!<br />

In this section, you restrict updating of tickets in the same way you restricted creating<br />

tickets. You start by writing two examples: one to test the edit action and the other to<br />

test the update action. Inside spec/controllers/tickets_controller_spec.rb, within the<br />

“with permission to view the project” context, define a cannot_update_tickets!<br />

method right under the cannot_create_tickets! method, as shown next:<br />

def cannot_update_tickets!<br />

response.should redirect_to(project)<br />

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

end<br />

Then, underneath the existing examples, put the specs, as shown in the following<br />

listing.<br />

Listing 8.9 Update tests for spec/controllers/tickets_controller_spec.rb<br />

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

get :edit, { :project_id => project.id, :id => ticket.id }<br />

cannot_update_tickets!<br />

end<br />

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

put :update, { :project_id => project.id,<br />

:id => ticket.id,<br />

:ticket => {}<br />

}<br />

cannot_update_tickets!<br />

end<br />

These two examples make requests to their respective actions and assert that the user<br />

is redirected away from them with an error message explaining why. With both of<br />

these actions, you need to pass a project_id parameter so the find_project method<br />

can find a project and an id parameter so the find_ticket method can find a ticket.<br />

For the update action, you pass an empty hash so params[:ticket] is set. If you didn’t<br />

do this, you would get a confusing error in your test:<br />

NoMethodError:<br />

undefined method 'stringify_keys' for nil:NilClass<br />

191

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

Saved successfully!

Ooh no, something went wrong!