Skip to content

Commit

Permalink
Add tests for keyfinder logic to ensure the argument count does not m…
Browse files Browse the repository at this point in the history
…atter
  • Loading branch information
anakinj committed Jan 9, 2022
1 parent 2da9d98 commit 769bd66
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/jwt_spec.rb
Expand Up @@ -695,6 +695,31 @@
end
end

describe 'when keyfinder given with 1 argument' do
let(:token) { JWT.encode(payload, 'HS256', 'HS256') }
it 'decodes the token' do
expect(JWT.decode(token, nil, true, algorithm: 'HS256') { |header| header['alg'] }).to include(payload)
end
end

describe 'when keyfinder given with 2 arguments' do
let(:token) { JWT.encode(payload, payload['user_id'], 'HS256') }
it 'decodes the token' do
expect(JWT.decode(token, nil, true, algorithm: 'HS256') { |_header, payload| payload['user_id'] }).to include(payload)
end
end

describe 'when keyfinder given with 3 arguments' do
let(:token) { JWT.encode(payload, 'HS256', 'HS256') }
it 'decodes the token but does not pass the payload' do
expect(JWT.decode(token, nil, true, algorithm: 'HS256') do |header, token_payload, nothing|
expect(token_payload).to eq(nil) # This behaviour is not correct, the payload should be available in the keyfinder
expect(nothing).to eq(nil)
header['alg']
end).to include(payload)
end
end

describe 'when none token is and decoding without key and with verification' do
let(:none_token) { ::JWT.encode(payload, nil, 'none') }
it 'decodes the token' do
Expand Down

0 comments on commit 769bd66

Please sign in to comment.