Merge pull request #5803 from betagouv/another_try_to_fix_timestamp

Another try to fix timestamp
This commit is contained in:
LeSim 2020-12-14 14:18:34 +01:00 committed by GitHub
commit 1f1f38eef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 35 deletions

View file

@ -92,25 +92,27 @@ class BillSignature < ApplicationRecord
end
def read_signature
if attachment_changes['signature']
io = io_for_changes(attachment_changes['signature'])
io.read if io.present?
elsif signature.attached?
signature.download
end
read_attachment('signature')
end
def read_serialized
if attachment_changes['serialized']
io = io_for_changes(attachment_changes['serialized'])
io.read if io.present?
read_attachment('serialized')
end
private
def read_attachment(attachment)
if attachment_changes[attachment]
io = io_for_changes(attachment_changes[attachment])
if io.present?
io.rewind
io.read
end
elsif serialized.attached?
serialized.download
end
end
private
def io_for_changes(attachment_changes)
attachable = attachment_changes.attachable
case attachable

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