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.

356 CHAPTER 13 Designing an API<br />

In the before block you create one project that the user should not have access to<br />

read. Inside the test itself, you’re still using the for scope on the Project class to get<br />

only the projects that the specified user has access to. Let’s add a couple more lines to<br />

your example now to check that this user cannot see the Access Denied project:<br />

projects.any? do |p|<br />

p["project"]["name"] == "Access Denied"<br />

end.should be_false<br />

When you run this spec with bin/rspec spec/api/v1/projects_spec.rb you’ll see<br />

that the JSON returned from your controller still contains both projects:<br />

expected "[[Ticketee hash]]"<br />

got "[[Ticketee hash], [Access Denied hash]]"<br />

To make this test pass, you’re going to need to stop returning all the projects in the<br />

index action of Api::V1::ProjectsController and only return the projects that this<br />

user should be able to see. Let’s now open app/controllers/api/v1/projects<br />

_controller.rb and change the index action to use the for method and pass in the<br />

current_user, rather than the all method:<br />

def index<br />

respond_with(Project.for(current_user))<br />

end<br />

This will now return only the list of projects that the user should be able to see, which<br />

should be enough to get this test passing. You can find out with another quick run of<br />

bin/rspec spec/api/v1/projects_spec.rb:<br />

1 example, 0 failures<br />

Great, now you’ve got your API finding a user based on the token that you’ve gathered<br />

in your spec. One thing you haven’t tested for yet is: what happens when an invalid (or<br />

no) token is given? Well, you should return an error when that happens. This is the<br />

final change you’ll be making before you make a commit, because it’s been a little too<br />

long since you’ve last done that. 8<br />

13.1.4 Error reporting<br />

Something will inevitably go wrong in your application, and when that happens you’re<br />

going to want to provide useful error messages to your users. One of the things that<br />

could go wrong in your API is that the user uses an invalid token to authenticate<br />

against your API. When a user makes a request with an invalid token, you should<br />

inform them of their mistake, which you can do by returning JSON that looks like this:<br />

{ error: "Token is invalid." }<br />

To test this behavior, you’re going to make a request without a token and then fix up<br />

your projects_spec.rb test to pass in a token. You’ll write your first test now in a new<br />

8 As a reminder: you should commit after every safe point so that if you stuff something up (it happens!) you<br />

won’t have to roll back as much.

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

Saved successfully!

Ooh no, something went wrong!