2017-02-19 33 views
4

私はRails 5 APIのみのアプリケーションを持ち、JWT認証を行うにはknockを使用しています。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 

アドバイスありがとうございます。

答えて

4
def authenticated_header(user) 
    token = Knock::AuthToken.new(payload: { sub: user.id }).token 
    { 'Authorization': "Bearer #{token}" } 
    end 

    describe 'GET /users?me=true' do 
    URL = '/v1/users?me=true' 
    AUTH_URL = '/user_token' 

    context 'when the request with NO authentication header' do 
     it 'should return unauth for retrieve current user info before login' do 
     get URL 
     expect(response).to have_http_status(:unauthorized) 
     end 
    end 

    context 'when the request contains an authentication header' do 
     it 'should return the user info' do 
     user = create(:user) 

     get URL, headers: authenticated_header(user) 
     puts response.body 
     end 
    end 
    end 
0

Li Xinyangの回答の助けを借りて、私の要求仕様に類似したものを実装することができました。別の実装を見るためにここでそれを共有します。

# spec/requests/locations_spec.rb 
require 'rails_helper' 

RSpec.describe 'Locations API' do 
    let!(:user) { create(:user) } 
    let!(:locations) { create_list(:location, 10, user_id: user.id) } 

    describe 'GET /locations' do 
    it 'reponds with invalid request without JWT' do 
     get '/locations' 
     expect(response).to have_http_status(401) 
     expect(response.body).to match(/Invalid Request/) 
    end 

    it 'responds with JSON with JWT' do 
     jwt = confirm_and_login_user(user) 
     get '/locations', headers: { "Authorization" => "Bearer #{jwt}" } 
     expect(response).to have_http_status(200) 
     expect(json.size).to eq(10) 
    end 
    end 
end 

confirm_and_login_user(user)rails_helper.rbにモジュールとして含まれているrequest_spec_helperに定義されています。

# spec/support/request_spec_helper.rb 

module RequestSpecHelper 
    def json 
    JSON.parse(response.body) 
    end 

    def confirm_and_login_user(user) 
    get '/users/confirm', params: {token: user.confirmation_token} 
    post '/users/login', params: {email: user.email, password: 'password'} 
    return json['auth_token'] 
    end 
end 

このSitePointのチュートリアル(https://www.sitepoint.com/introduction-to-using-jwt-in-rails/

で説明したように、私は私のトークンを生成するためのJWT宝石を使用しています
関連する問題