spec/fernet_spec.rb in legacy-fernet-1.6.1 vs spec/fernet_spec.rb in legacy-fernet-1.6.2

- old
+ new

@@ -1,61 +1,61 @@ require 'spec_helper' require 'fernet' -describe Legacy::Fernet do - after { Legacy::Fernet::Configuration.run } +describe Fernet::Legacy do + after { Fernet::Legacy::Configuration.run } let(:token_data) do { :email => 'harold@heroku.com', :id => '123', :arbitrary => 'data' } end let(:secret) { 'JrdICDH6x3M7duQeM8dJEMK4Y5TkBIsYDw1lPy35RiY=' } let(:bad_secret) { 'badICDH6x3M7duQeM8dJEMK4Y5TkBIsYDw1lPy35RiY=' } it 'can verify tokens it generates' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end expect( - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| verifier.data['email'] == 'harold@heroku.com' end ).to be_true end it 'fails with a bad secret' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end expect( - Legacy::Fernet.verify(bad_secret, token) do |verifier| + Fernet::Legacy.verify(bad_secret, token) do |verifier| verifier.data['email'] == 'harold@heroku.com' end ).to be_false end it 'fails with a bad custom verification' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = { :email => 'harold@heroku.com' } end expect( - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| verifier.data['email'] == 'lol@heroku.com' end ).to be_false end it 'fails if the token is too old' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end expect( - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| verifier.ttl = 1 def verifier.now now = DateTime.now DateTime.new(now.year, now.month, now.day, now.hour, @@ -65,100 +65,100 @@ end ).to be_false end it 'verifies without a custom verification' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end - expect(Legacy::Fernet.verify(secret, token)).to be_true + expect(Fernet::Legacy.verify(secret, token)).to be_true end it 'can ignore TTL enforcement' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end expect( - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| def verifier.now Time.now + 99999999999 end verifier.enforce_ttl = false true end ).to be_true end it 'can ignore TTL enforcement via global config' do - Legacy::Fernet::Configuration.run do |config| + Fernet::Legacy::Configuration.run do |config| config.enforce_ttl = false end - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data = token_data end expect( - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| def verifier.now Time.now + 99999999999 end true end ).to be_true end it 'generates without custom data' do - token = Legacy::Fernet.generate(secret) + token = Fernet::Legacy.generate(secret) - expect(Legacy::Fernet.verify(secret, token)).to be_true + expect(Fernet::Legacy.verify(secret, token)).to be_true end it 'can encrypt the payload' do - token = Legacy::Fernet.generate(secret, true) do |generator| + token = Fernet::Legacy.generate(secret, true) do |generator| generator.data['password'] = 'password1' end expect(Base64.decode64(token)).not_to match /password1/ - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| expect(verifier.data['password']).to eq('password1') end end it 'does not encrypt when asked nicely' do - token = Legacy::Fernet.generate(secret, false) do |generator| + token = Fernet::Legacy.generate(secret, false) do |generator| generator.data['password'] = 'password1' end expect(Base64.decode64(token)).to match /password1/ - Legacy::Fernet.verify(secret, token, false) do |verifier| + Fernet::Legacy.verify(secret, token, false) do |verifier| expect(verifier.data['password']).to eq('password1') end end it 'can disable encryption via global configuration' do - Legacy::Fernet::Configuration.run { |c| c.encrypt = false } - token = Legacy::Fernet.generate(secret) do |generator| + Fernet::Legacy::Configuration.run { |c| c.encrypt = false } + token = Fernet::Legacy.generate(secret) do |generator| generator.data['password'] = 'password1' end expect(Base64.decode64(token)).to match /password1/ - Legacy::Fernet.verify(secret, token) do |verifier| + Fernet::Legacy.verify(secret, token) do |verifier| expect(verifier.data['password']).to eq('password1') end end it 'returns the unencrypted message upon verify' do - token = Legacy::Fernet.generate(secret) do |generator| + token = Fernet::Legacy.generate(secret) do |generator| generator.data['password'] = 'password1' end - verifier = Legacy::Fernet.verifier(secret, token) + verifier = Fernet::Legacy.verifier(secret, token) expect(verifier.valid?).to be_true expect(verifier.data['password']).to eq('password1') end end