27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

196 CHAPTER 8 More authorization<br />

current_user is set (it won’t be set if the user isn’t signed in), and if it is, check to see<br />

if that user is an admin by using the try method, which returns nil. If the method<br />

specified can’t be found on thing, try is called. If it’s found, then you use<br />

block.call, which runs the passed-in block.<br />

With this helper implemented, all three new scenarios should pass. Run bin/<br />

cucumber features/hidden_links.feature to find out:<br />

10 scenarios (10 passed)<br />

60 steps (60 passed)<br />

Great! They’re passing! Now let’s implement another few for testing the Edit link for<br />

tickets. Add the three scenarios from the following listing to the bottom of features/<br />

hidden_links.feature.<br />

Listing 8.12 features/hidden_links.feature<br />

Scenario: Edit 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 edit tickets on 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 the "Edit" link<br />

Scenario: Edit 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 "Edit" link<br />

Scenario: Edit 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 "Edit" link<br />

When you run these scenarios using bin/cucumber feature/hidden_links.feature,<br />

the link to Shiny! can’t be found for any of them:<br />

And I follow "Shiny!"<br />

no link with title, id or text 'Shiny!' found (Capybara::ElementNotFound)<br />

The Shiny! link should be a link to a ticket, but you haven’t yet created this ticket. To<br />

do so, under the line where you created the TextMate 2 project in the Background,<br />

add the following to create a ticket:<br />

And "user@ticketee.com" has created a ticket for this project:<br />

| title | description |<br />

| Shiny! | My eyes! My eyes! |<br />

Now when you run this feature, the middle scenario fails, just like when you implemented<br />

the Create link filtering:

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

Saved successfully!

Ooh no, something went wrong!