Reputation: 12923
So I am writing some API controllers that return and deal with json. One particular is a sessions controller. I set up the routes as such:
Note: (I am only showing you the destroy method cause that's the only one I am having issues with...)
namespace :api do
namespace :v1 do
resource :users
resources :sessions
end
end
Then I have routes such as:
api_v1_sessions GET /api/v1/sessions(.:format) api/v1/sessions#index
POST /api/v1/sessions(.:format) api/v1/sessions#create
new_api_v1_session GET /api/v1/sessions/new(.:format) api/v1/sessions#new
edit_api_v1_session GET /api/v1/sessions/:id/edit(.:format) api/v1/sessions#edit
api_v1_session GET /api/v1/sessions/:id(.:format) api/v1/sessions#show
PATCH /api/v1/sessions/:id(.:format) api/v1/sessions#update
PUT /api/v1/sessions/:id(.:format) api/v1/sessions#update
DELETE /api/v1/sessions/:id(.:format) api/v1/sessions#destroy
And finally I have destroy code that looks like this, for api/v1/sessions#destroy
:
def destroy
@user = User.find_by auth_token: param[:auth_token]
return invalid_user_crendentials unless @user
cookies.delete(@user.auth_token)
render json: { user_id: @user.id }, status: 200
end
Then I wrote the following spec tests for this particular action:
context "Destroy a session" do
it "should NOT destroy a session" do
post :destroy
response.response_code.should == 401
end
it "should NOT destroy a session with invalid token" do
post :destroy, auth_token: '34534534'
response.response_code.should == 401
end
it "should destroy a session based on valid data" do
post :destory, auth_token: @user.auth_token
json = JSON.parse response.body
json.to_json.should have_json_path('user_id')
response.response_code.should == 200
end
end
But when I run it I get:
No route matches {:auth_token=>"34534534", :controller=>"api/v1/sessions", :action=>"destroy"}
No route matches {:auth_token=>"QfEg4oa0UdhJF4K1VIBADg", :controller=>"api/v1/sessions", :action=>"destory"}
No route matches {:controller=>"api/v1/sessions", :action=>"destroy"}
How does no route match? at all?
Upvotes: 0
Views: 1487
Reputation: 25029
Your routes are all expecting an id
parameter to be passed in.
DELETE /api/v1/sessions/:id
Your tests are passing in an auth_token
instead. Change the tests to pass in an id
.
Note: Your tests should indeed be delete :destroy
instead of post
, and your third test also has a typo in :destroy
.
Upvotes: 1
Reputation: 3627
Change post
to delete
context "Destroy a session" do
it "should NOT destroy a session" do
delete :destroy
response.response_code.should == 401
end
it "should NOT destroy a session with invalid token" do
delete :destroy, auth_token: '34534534'
response.response_code.should == 401
end
it "should destroy a session based on valid data" do
delete :destory, auth_token: @user.auth_token
json = JSON.parse response.body
json.to_json.should have_json_path('user_id')
response.response_code.should == 200
end
end
You can see in your rake routes
output that your destroy action only accepts delete
requests:
DELETE /api/v1/sessions/:id(.:format) api/v1/sessions#destroy
It's also looking for an :id
param. I think you want to change param[:auth_token]
to param[:id]
in your controller and auth_token
to id
in your spec.
Upvotes: 1