foodsoft/spec/lib/token_verifier_spec.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

2014-01-04 20:12:01 +01:00
require_relative '../spec_helper'
describe TokenVerifier do
let (:prefix) { 'xyz' }
let (:v) { TokenVerifier.new(prefix) }
let (:msg) { v.generate }
it 'validates' do
expect { v.verify(msg) }.to_not raise_error
2014-01-04 20:12:01 +01:00
end
it 'validates when recreated' do
v2 = TokenVerifier.new(prefix)
expect { v2.verify(msg) }.to_not raise_error
2014-01-04 20:12:01 +01:00
end
it 'does not validate with a different prefix' do
v2 = TokenVerifier.new('abc')
expect { v2.verify(msg) }.to raise_error(TokenVerifier::InvalidPrefix)
end
it 'does not validate in a different foodcoop scope' do
msg
oldscope = FoodsoftConfig.scope
begin
2019-10-28 09:40:43 +01:00
FoodsoftConfig.scope = Faker::Lorem.words(number: 1)
2014-01-04 20:12:01 +01:00
v2 = TokenVerifier.new(prefix)
expect { v2.verify(msg) }.to raise_error(TokenVerifier::InvalidScope)
2014-01-04 20:12:01 +01:00
ensure
FoodsoftConfig.scope = oldscope
end
end
it 'does not validate a random string' do
expect { v.verify(Faker::Lorem.characters(number: 100)) }.to raise_error(ActiveSupport::MessageVerifier::InvalidSignature)
2014-01-04 20:12:01 +01:00
end
it 'returns the message' do
data = [5, { 'hi' => :there }, 'bye', []]
2014-01-04 20:12:01 +01:00
msg = v.generate(data)
expect(v.verify(msg)).to eq data
end
end