2014-01-04 20:12:01 +01:00
|
|
|
require_relative '../spec_helper'
|
|
|
|
|
|
|
|
describe TokenVerifier do
|
2022-02-20 16:15:22 +01:00
|
|
|
let(:prefix) { 'xyz' }
|
|
|
|
let(:v) { TokenVerifier.new(prefix) }
|
|
|
|
let(:msg) { v.generate }
|
2014-01-04 20:12:01 +01:00
|
|
|
|
|
|
|
it 'validates' do
|
2023-05-12 13:01:12 +02:00
|
|
|
expect { v.verify(msg) }.not_to raise_error
|
2014-01-04 20:12:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates when recreated' do
|
|
|
|
v2 = TokenVerifier.new(prefix)
|
2023-05-12 13:01:12 +02:00
|
|
|
expect { v2.verify(msg) }.not_to 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)
|
2021-03-01 15:27:26 +01:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
expect do
|
|
|
|
v.verify(Faker::Lorem.characters(number: 100))
|
|
|
|
end.to raise_error(ActiveSupport::MessageVerifier::InvalidSignature)
|
2014-01-04 20:12:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the message' do
|
2021-03-01 15:27:26 +01:00
|
|
|
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
|