lundi 2 mai 2016

Rails - Minitest json API - get :index always redirect me

I need some help in fixing my test

I've the following controller

#app/controllers/api/v1/users_controller.rb
class API::V1::UsersController < ApplicationController

  respond_to :json

  def index
    @users = User.all
  end

end

my routes.rb is:

Rails.application.routes.draw do

  # devise_for and other resources ...

  namespace :api, defaults: { format: 'json' }  do
    namespace :v1 do
      resources :users, only: [:show, :index, :create]
    end
  end

end

My test suite is as follow:

#test/controllers/api/v1
require 'test_helper'

class API::V1::UsersControllerTest < ActionController::TestCase

  test "index with token authentication via query params" do
    get :index, { user_email: "[email]", user_token: "[token]" }
    assert_response :success
  end

  test "index with token authentication via request headers" do
    @request.headers['X-User-Email'] = "[email]"
    @request.headers['X-User-Token'] = "[token]"
    get :index
    assert_response :success
  end

end

When I run rake test I fot the following failures:

1) Failure: API::V1::UsersControllerTest#test_index_with_token_authentication_via_request_headers [/.../test/controllers/api/v1/users_controller_test.rb:17]: Expected response to be a success, but was <302>

2) Failure: API::V1::UsersControllerTest#test_index_with_token_authentication_via_query_params [/.../test/controllers/api/v1/users_controller_test.rb:8]: Expected response to be a success, but was <302>

The user exist in the test DB, and in place of [email] and [psw] I use actual values. Any clue?

Aucun commentaire:

Enregistrer un commentaire