ruby on rails - RSPEC: How to test that a JSON Web Token is returned by controller action -


i using devise , jwt's authenticate users in project writing. having hard time figuring out how write useful test expect jwt response.body (since each encrypted).

the thing can think of test structured jwt should (a 3 segment, '.' delimited string).

has encountered testing random/hashed returns , come better solution?

describe sessiontokenscontroller, type: :controller   let(:current_user) { factorygirl.create(:user) }    before(:each)     sign_in current_user   end    describe '#create'     'responds jwt'       post :create       token = json.parse(response.body)['token']        expect(token).to be_kind_of(string)       segments = token.split('.')       expect(segments.size).to eql(3)     end   end end 

it depends on want test.

if want test if returned token exists , valid can following:

it 'responds valid jwt'   post :create   token = json.parse(response.body)['token']    expect { jwt.decode(token, key) }.to_not raise_error(jwt::decodeerror) end 

although seems more useful validate claims token includes:

let(:claims) { jwt.decode(json.parse(response.body)['token'], key) }  'returns jwt valid claims'   post :create   expect(claims['user_id']).to eq(123) end 

in latter example can validate exact claims included in jwt.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -