2017-02-19 1 views
4

Ich habe eine Rails 5 API nur App und mit klopfen, um JWT authentifizieren.Wie Request-Spezifikation für JWT authentifizieren App mit RSpec

Nachdem ich die Modell- und Modellspezifikation fertiggestellt habe, beginne ich mit der Anforderungsspezifikation.

Aber ich habe keine Ahnung, wie die Authentifizierung innerhalb der Anforderungsspezifikation in der richtigen Weise zu vervollständigen,

Meine Benutzer-Controller,

module V1 
    class UsersController < ApplicationController 
    before_action :authenticate_user, except: [:create] 
    end 
end 

Application Controller,

class ApplicationController < ActionController::API 
    include Knock::Authenticable 
    include ActionController::Serialization 
end 

Mein dümmste Lösung (rufen Sie die Get-Token-Anforderung ab, um den JWT vor der Ruheanforderung abzurufen),

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 

Alle Ratschläge sind willkommen.

Antwort

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

Mit Hilfe von Li Xinyangs Antwort konnte ich etwas Ähnliches für meine Anfragespezifikation implementieren. Teilen Sie es hier, damit andere eine alternative Implementierung sehen können.

# 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) in einem request_spec_helper definiert, die als Modul in rails_helper.rb enthalten ist:

# 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 

Ich bin mit dem jwt gem meine Token zur Erzeugung wie in diesem Tutorial beschrieben Sitepoint (https://www.sitepoint.com/introduction-to-using-jwt-in-rails/)

Verwandte Themen