spec: use real signature to avoid mocking

This commit is contained in:
simon lehericey 2020-12-14 11:12:50 +01:00
parent 5713b843f3
commit bc4dc22aea

View file

@ -72,48 +72,41 @@ RSpec.describe BillSignature, type: :model do
end
describe 'check_signature_contents' do
let(:signature) { File.open('spec/fixtures/files/bill_signature/signature.der') }
let(:signature_date) { DateTime.parse('2019-04-30 15:30:20') }
let(:signature_digest) { Digest::SHA256.hexdigest('CECI EST UN BLOB') }
let(:current_date) { Time.zone.now }
before do
bill_signature.signature.attach(io: StringIO.new(signature), filename: 'file') if signature.present?
allow(ASN1::Timestamp).to receive(:signature_time).and_return(signature_time)
allow(ASN1::Timestamp).to receive(:signed_digest).and_return(signed_digest)
bill_signature.digest = digest
Timecop.freeze(current_date)
bill_signature.signature.attach(io: signature, filename: 'file') if signature.present?
bill_signature.digest = signature_digest
bill_signature.valid?
Timecop.return
end
context 'when the signature is correct' do
let(:signature) { 'signature' }
let(:signature_time) { 1.day.ago }
let(:digest) { 'abcd' }
let(:signed_digest) { 'abcd' }
subject { bill_signature.errors.details[:signature] }
it { expect(bill_signature.errors.details[:signature]).to be_empty }
context 'when the signature is correct' do
it { is_expected.to be_empty }
end
context 'when the signature isnt set' do
let(:signature) { nil }
let(:signature_time) { 1.day.ago }
let(:digest) { 'abcd' }
let(:signed_digest) { 'abcd' }
it { expect(bill_signature.errors.details[:signature]).to eq [error: :blank] }
it { is_expected.to eq [error: :blank] }
end
context 'when the signature time is in the future' do
let(:signature) { 'signature' }
let(:signature_time) { 1.day.from_now }
let(:digest) { 'abcd' }
let(:signed_digest) { 'abcd' }
let(:current_date) { signature_date - 1.day }
it { expect(bill_signature.errors.details[:signature]).to eq [error: :invalid_date] }
it { is_expected.to eq [error: :invalid_date] }
end
context 'when the signature doesnt match the digest' do
let(:signature) { 'signature' }
let(:signature_time) { 1.day.ago }
let(:digest) { 'abcd' }
let(:signed_digest) { 'dcba' }
let(:signature_digest) { 'dcba' }
it { expect(bill_signature.errors.details[:signature]).to eq [error: :invalid] }
it { is_expected.to eq [error: :invalid] }
end
end
end