Skip to content

Commit 65d2883

Browse files
authored
Integration tests for EncodedToken (#691)
Integrationish tests
1 parent b035d0b commit 65d2883

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

spec/jwt/encoded_token_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,43 @@
397397
end
398398
end
399399
end
400+
401+
describe 'integration use-cases' do
402+
context 'simple verify HS256 with defaults' do
403+
let(:encoded_token) do
404+
JWT::Token.new(payload: { 'pay' => 'load' })
405+
.tap { |t| t.sign!(algorithm: 'HS256', key: 'secret_signing_key') }
406+
.jwt
407+
end
408+
409+
it 'protects the user from unverified payload access' do
410+
token = described_class.new(encoded_token)
411+
412+
expect(token.unverified_payload).to eq({ 'pay' => 'load' })
413+
expect(token.header).to eq({ 'alg' => 'HS256' })
414+
415+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Verify the token signature before accessing the payload')
416+
417+
expect(token.valid_signature?(algorithm: 'HS256', key: 'invalid_signing_key')).to be(false)
418+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Verify the token signature before accessing the payload')
419+
420+
expect(token.valid_signature?(algorithm: 'HS256', key: 'secret_signing_key')).to be(true)
421+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Verify the token claims before accessing the payload')
422+
423+
expect(token.valid_claims?(iss: 'issuer')).to be(false)
424+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Verify the token claims before accessing the payload')
425+
426+
expect(token.valid_claims?).to be(true)
427+
expect(token.payload).to eq({ 'pay' => 'load' })
428+
429+
token = described_class.new(encoded_token)
430+
431+
expect(token.valid?(signature: { algorithm: 'HS256', key: 'invalid_signing_key' })).to be(false)
432+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Verify the token signature before accessing the payload')
433+
434+
expect(token.valid?(signature: { algorithm: 'HS256', key: 'secret_signing_key' })).to be(true)
435+
expect(token.payload).to eq({ 'pay' => 'load' })
436+
end
437+
end
438+
end
400439
end

0 commit comments

Comments
 (0)