测试设计用户登录但获得401未经授权的响应

时间:2017-02-20 09:10:50

标签: ruby-on-rails ruby devise minitest rails-api

我正在devise_auth_token应用中使用rails-api gem,我正在尝试测试userlogged_in,然后user被授权{ {1}}中{1}} render行动{/}}。{/}

welcome_controller

index

路线

welcome_controller

welcome_controller_test

class Api::WelcomeController < ApplicationController
  before_action :authenticate_user!

  def index
    render json: {
      data: {
        message: "Welcome #{current_user.first_name}",
        user: current_user
      }
    }, status: 200
  end
end

灯具 users.yml

Rails.application.routes.draw do
  mount_devise_token_auth_for 'User', at: 'auth'
  namespace :api do
    get 'welcome', to: 'welcome#index'
    resources :socities
  end
end

test_helper.rb中

require 'test_helper'

class Api::WelcomeControllerTest < ActionDispatch::IntegrationTest
  def test_index_renders_message
    admin = users :admin
    sign_in admin
    get "/api/welcome"
    assert_response :success
  end
end

错误

admin:
  uid: admin@example.com
  provider: email
  email: admin@example.com
  first_name: Ahmad
  last_name: Hamza
  nickname: nil
  society_id: 1
  encrypted_password: $2a$10$kPXgGIFeHxoek/UgfwtVFeLCLYgIB82Cazj8c6GZz2I/p68ohfp9K #1234test
  confirmed_at: 2017-01-02 08:31:23
  confirmation_sent_at: 2017-01-02 08:30:59
  role: admin

如何检查发生了什么问题?

0 个答案:

没有答案