Merge pull request #5803 from betagouv/another_try_to_fix_timestamp
Another try to fix timestamp
This commit is contained in:
commit
1f1f38eef4
2 changed files with 30 additions and 35 deletions
|
@ -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
|
||||
|
|
|
@ -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 isn’t 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 doesn’t 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
|
||||
|
|
Loading…
Reference in a new issue