Skip to content

Commit

Permalink
Reorganize strategy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
waiting-for-dev committed Dec 20, 2024
1 parent b66f150 commit ca73fa1
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions spec/warden/jwt_auth/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,49 @@

describe '#valid?' do
context 'when Authorization header is valid' do
it 'returns true' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'GET' }
it "returns true when it doesn't match a dispatch request and issuer claim is not present in the token" do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
end

it 'returns false when the current path / method matches a dispatch request path / method' do
it 'returns false when the current path & method match a dispatch request' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end

it 'returns true when the current path matches a dispatch request, but the method does not' do
it 'returns true when only the current path but not the method matches a dispatch request' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'GET' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
end

it 'returns true when the current path does not match a dispatch request path' do
env = { 'HTTP_AUTHORIZATION' => 'Bearer 123', 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).to be_valid
end
end

context 'when Authorization header is not valid' do
it 'returns false' do
env = {}
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end

it 'returns false when the current path matches a dispatch request path' do
env = { 'PATH_INFO' => '/sign_in', 'REQUEST_METHOD' => 'POST' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
end
it 'returns true when issuer claim is configured and it matches the configured issuer' do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer })
env = { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }

it 'returns true when the current path does not match a dispatch request path' do
env = { 'PATH_INFO' => '/users', 'REQUEST_METHOD' => 'GET' }
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
expect(strategy).to be_valid
end
end

context 'when issuer claim is configured and it matches the configured issuer' do
it 'returns true' do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer })
it "returns false when issuer claim is configured and it doesn't match the configured issuer" do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer + 'aaa' })
env = { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }

strategy = described_class.new(env, :user)

expect(strategy).to be_valid
expect(strategy).not_to be_valid
end
end

context "when issuer claim is configured and it doesn't match the configured issuer" do
context 'when Authorization header is not valid' do
it 'returns false' do
token = Warden::JWTAuth::TokenEncoder.new.call({ 'iss' => Warden::JWTAuth.config.issuer + 'aaa' })
env = { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }

env = {}
strategy = described_class.new(env, :user)

expect(strategy).not_to be_valid
Expand Down

0 comments on commit ca73fa1

Please sign in to comment.