-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
151 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module Rack | ||
class PrxAuth | ||
VERSION = "0.0.5" | ||
VERSION = "0.0.6" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
require 'test_helper' | ||
|
||
describe Rack::PrxAuth::Certificate do | ||
let(:subject) { Rack::PrxAuth::Certificate.new } | ||
let(:certificate) { subject } | ||
|
||
describe '#initialize' do | ||
it 'allows setting the location of the certificates' do | ||
cert = Rack::PrxAuth::Certificate.new('http://example.com') | ||
cert.cert_location.must_equal URI('http://example.com') | ||
end | ||
|
||
it 'defaults to DEFAULT_CERT_LOC' do | ||
certificate.cert_location.must_equal Rack::PrxAuth::Certificate::DEFAULT_CERT_LOC | ||
end | ||
end | ||
|
||
describe '#valid?' do | ||
it 'validates the token with the public key' do | ||
token, key = nil, nil | ||
certificate.stub(:public_key, :public_key) do | ||
JSON::JWT.stub(:decode, Proc.new {|t, k| token, key = t, k }) do | ||
certificate.valid?(:token) | ||
end | ||
end | ||
|
||
token.must_equal :token | ||
key.must_equal :public_key | ||
end | ||
|
||
it 'returns false if verification fails' do | ||
JSON::JWT.stub(:decode, Proc.new do |t, k| | ||
raise JSON::JWT::VerificationFailed | ||
end) do | ||
certificate.stub(:public_key, :foo) do | ||
certificate.wont_be :valid?, :token | ||
end | ||
end | ||
end | ||
|
||
it 'returns true if verification passes' do | ||
JSON::JWT.stub(:decode, {}) do | ||
certificate.stub(:public_key, :foo) do | ||
certificate.must_be :valid?, :token | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#certificate' do | ||
it 'calls fetch if unprimed' do | ||
def certificate.fetch | ||
:sigil | ||
end | ||
|
||
certificate.send(:certificate).must_equal :sigil | ||
end | ||
end | ||
|
||
describe '#public_key' do | ||
it 'pulls from the certificate' do | ||
certificate.stub(:certificate, Struct.new(:public_key).new(:key)) do | ||
certificate.send(:public_key).must_equal :key | ||
end | ||
end | ||
end | ||
|
||
describe '#fetch' do | ||
it 'pulls from `#cert_location`' do | ||
Net::HTTP.stub(:get, ->(x) { "{\"certificates\":{\"asdf\":\"#{x}\"}}" }) do | ||
OpenSSL::X509::Certificate.stub(:new, ->(x) { x }) do | ||
certificate.stub(:cert_location, "a://fake.url/here") do | ||
certificate.send(:fetch).must_equal "a://fake.url/here" | ||
end | ||
end | ||
end | ||
end | ||
|
||
it 'sets the expiration value' do | ||
Net::HTTP.stub(:get, ->(x) { "{\"certificates\":{\"asdf\":\"#{x}\"}}" }) do | ||
OpenSSL::X509::Certificate.stub(:new, ->(_) { Struct.new(:not_after).new(Time.now + 10000) }) do | ||
certificate.send :certificate | ||
certificate.wont_be :needs_refresh? | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#expired?' do | ||
let(:stub_cert) { Struct.new(:not_after).new(Time.now + 10000) } | ||
before(:each) do | ||
certificate.instance_variable_set :'@certificate', stub_cert | ||
end | ||
|
||
it 'is false when the certificate is not expired' do | ||
certificate.wont_be :expired? | ||
end | ||
|
||
it 'is true when the certificate is expired' do | ||
stub_cert.not_after = Time.now - 500 | ||
certificate.must_be :expired? | ||
end | ||
end | ||
|
||
describe '#needs_refresh?' do | ||
def refresh_at=(time) | ||
certificate.instance_variable_set :'@refresh_at', time | ||
end | ||
|
||
it 'is true if certificate is expired' do | ||
certificate.stub(:expired?, true) do | ||
certificate.must_be :needs_refresh? | ||
end | ||
end | ||
|
||
it 'is true if we are past refresh value' do | ||
self.refresh_at = Time.now.to_i - 1000 | ||
certificate.stub(:expired?, false) do | ||
certificate.must_be :needs_refresh? | ||
end | ||
end | ||
|
||
it 'is false if certificate is not expired and refresh is in the future' do | ||
self.refresh_at = Time.now.to_i + 10000 | ||
certificate.stub(:expired?, false) do | ||
certificate.wont_be :needs_refresh? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters