我有一个Rails 5 API唯一的应用程序,并使用敲击来进行JWT身份验证。如何使用RSpec为JWT身份验证应用程序请求规范
完成模型和模型规范后,我开始执行请求规范。
但我不知道如何完成以正确的方式要求规范内部认证,
我的用户控制器,
module V1
class UsersController < ApplicationController
before_action :authenticate_user, except: [:create]
end
end
应用控制器,
class ApplicationController < ActionController::API
include Knock::Authenticable
include ActionController::Serialization
end
我的愚蠢解决方案(也称获得令牌请求,其余请求之前得到JWT),
context 'when the request contains an authentication header' do
it 'should return the user info' do
user = create(:user)
post '/user_token', params: {"auth": {"email": user.email, "password": user.password }}
body = response.body
puts body # {"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODgxMDgxMDYsInN1YiI6MX0.GDBHPzbivclJfwSTswXhDkV0TCFCybJFDrjBnLIfN3Q"}
# use the retrieved JWT for future requests
end
end
任何建议表示赞赏。