WIP
This commit is contained in:
parent
13c21d89af
commit
c9ab80c880
4 changed files with 45 additions and 17 deletions
|
@ -17,7 +17,7 @@ class BillSignature < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_operations(day)
|
def serialize_operations(day)
|
||||||
self.serialized.attach(
|
serialized.attach(
|
||||||
io: StringIO.new(operations_bill_json),
|
io: StringIO.new(operations_bill_json),
|
||||||
filename: "demarches-simplifiees-operations-#{day.to_date.iso8601}.json",
|
filename: "demarches-simplifiees-operations-#{day.to_date.iso8601}.json",
|
||||||
content_type: 'application/json',
|
content_type: 'application/json',
|
||||||
|
@ -40,7 +40,7 @@ class BillSignature < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_signature(signature, day)
|
def set_signature(signature, day)
|
||||||
self.signature.attach(
|
signature.attach(
|
||||||
io: StringIO.new(signature),
|
io: StringIO.new(signature),
|
||||||
filename: "demarches-simplifiees-signature-#{day.to_date.iso8601}.der",
|
filename: "demarches-simplifiees-signature-#{day.to_date.iso8601}.der",
|
||||||
content_type: 'application/x-x509-ca-cert'
|
content_type: 'application/x-x509-ca-cert'
|
||||||
|
@ -49,36 +49,54 @@ class BillSignature < ApplicationRecord
|
||||||
|
|
||||||
# Validations
|
# Validations
|
||||||
def check_bill_digest
|
def check_bill_digest
|
||||||
if self.digest != self.operations_bill_digest
|
if digest != operations_bill_digest
|
||||||
errors.add(:digest)
|
errors.add(:digest)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_serialized_bill_contents
|
def check_serialized_bill_contents
|
||||||
if !self.serialized.attached?
|
if !serialized.attached?
|
||||||
errors.add(:serialized, :blank)
|
errors.add(:serialized, :blank)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if JSON.parse(self.serialized.download) != self.operations_bill
|
if JSON.parse(read_serialized) != operations_bill
|
||||||
errors.add(:serialized)
|
errors.add(:serialized)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_signature_contents
|
def check_signature_contents
|
||||||
if !self.signature.attached?
|
if !signature.attached?
|
||||||
errors.add(:signature, :blank)
|
errors.add(:signature, :blank)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
timestamp_signature_date = ASN1::Timestamp.signature_time(self.signature.download)
|
timestamp_signature_date = ASN1::Timestamp.signature_time(read_signature)
|
||||||
if timestamp_signature_date > Time.zone.now
|
if timestamp_signature_date > Time.zone.now
|
||||||
errors.add(:signature, :invalid_date)
|
errors.add(:signature, :invalid_date)
|
||||||
end
|
end
|
||||||
|
|
||||||
timestamp_signed_digest = ASN1::Timestamp.signed_digest(self.signature.download)
|
timestamp_signed_digest = ASN1::Timestamp.signed_digest(read_signature)
|
||||||
if timestamp_signed_digest != self.digest
|
if timestamp_signed_digest != digest
|
||||||
errors.add(:signature)
|
errors.add(:signature)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read_signature
|
||||||
|
if attachment_changes['signature']
|
||||||
|
io = attachment_changes['signature'].attachable[:io]
|
||||||
|
io.read if io.present?
|
||||||
|
elsif signature.attached?
|
||||||
|
signature.download
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_serialized
|
||||||
|
if attachment_changes['serialized']
|
||||||
|
io = attachment_changes['serialized'].attachable[:io]
|
||||||
|
io.read if io.present?
|
||||||
|
elsif serialized.attached?
|
||||||
|
serialized.download
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
factory :bill_signature do
|
factory :bill_signature do
|
||||||
|
trait :with_serialized do
|
||||||
serialized { Rack::Test::UploadedFile.new('spec/fixtures/files/bill_signature/serialized.json', 'application/json') }
|
serialized { Rack::Test::UploadedFile.new('spec/fixtures/files/bill_signature/serialized.json', 'application/json') }
|
||||||
|
end
|
||||||
|
trait :with_signature do
|
||||||
signature { Rack::Test::UploadedFile.new('spec/fixtures/files/bill_signature/signature.der', 'application/x-x509-ca-cert') }
|
signature { Rack::Test::UploadedFile.new('spec/fixtures/files/bill_signature/signature.der', 'application/x-x509-ca-cert') }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
RSpec.describe BillSignature, type: :model do
|
RSpec.describe BillSignature, type: :model do
|
||||||
describe 'validations' do
|
describe 'validations' do
|
||||||
subject(:bill_signature) { BillSignature.new }
|
subject(:bill_signature) { build(:bill_signature) }
|
||||||
|
|
||||||
describe 'check_bill_digest' do
|
describe 'check_bill_digest' do
|
||||||
before do
|
before do
|
||||||
|
@ -119,14 +119,20 @@ RSpec.describe BillSignature, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.build_with_operations' do
|
describe '.build_with_operations' do
|
||||||
subject(:bill_signature) { described_class.build_with_operations(dossier_operation_logs, Date.new(1871, 03, 18)) }
|
let(:day) { Date.new(1871, 03, 18) }
|
||||||
|
subject(:bill_signature) { build(:bill_signature, :with_signature) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
bill_signature.dossier_operation_logs = dossier_operation_logs
|
||||||
|
bill_signature.serialize_operations(day)
|
||||||
|
end
|
||||||
|
|
||||||
context 'when there are no operations' do
|
context 'when there are no operations' do
|
||||||
let(:dossier_operation_logs) { [] }
|
let(:dossier_operation_logs) { [] }
|
||||||
|
|
||||||
it { expect(bill_signature.operations_bill).to eq({}) }
|
it { expect(bill_signature.operations_bill).to eq({}) }
|
||||||
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{}')) }
|
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{}')) }
|
||||||
it { expect(bill_signature.serialized.download).to eq('{}') }
|
it { expect(bill_signature.read_serialized).to eq('{}') }
|
||||||
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -137,7 +143,7 @@ RSpec.describe BillSignature, type: :model do
|
||||||
|
|
||||||
it { expect(bill_signature.operations_bill).to eq({ '1234' => 'abcd' }) }
|
it { expect(bill_signature.operations_bill).to eq({ '1234' => 'abcd' }) }
|
||||||
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{"1234":"abcd"}')) }
|
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{"1234":"abcd"}')) }
|
||||||
it { expect(bill_signature.serialized.download).to eq('{"1234":"abcd"}') }
|
it { expect(bill_signature.read_serialized).to eq('{"1234":"abcd"}') }
|
||||||
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -151,7 +157,7 @@ RSpec.describe BillSignature, type: :model do
|
||||||
|
|
||||||
it { expect(bill_signature.operations_bill).to eq({ '1234' => 'abcd', '5678' => 'dcba' }) }
|
it { expect(bill_signature.operations_bill).to eq({ '1234' => 'abcd', '5678' => 'dcba' }) }
|
||||||
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{"1234":"abcd","5678":"dcba"}')) }
|
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{"1234":"abcd","5678":"dcba"}')) }
|
||||||
it { expect(bill_signature.serialized.download).to eq('{"1234":"abcd","5678":"dcba"}') }
|
it { expect(bill_signature.read_serialized).to eq('{"1234":"abcd","5678":"dcba"}') }
|
||||||
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,7 +25,7 @@ describe BillSignatureService do
|
||||||
|
|
||||||
context "when there are no operations to be signed" do
|
context "when there are no operations to be signed" do
|
||||||
before do
|
before do
|
||||||
create :dossier_operation_log, created_at: 1.day.ago, bill_signature: build(:bill_signature)
|
create :dossier_operation_log, created_at: 1.day.ago, bill_signature: build(:bill_signature, :with_signature, :with_serialized)
|
||||||
create :dossier_operation_log, created_at: 1.day.from_now
|
create :dossier_operation_log, created_at: 1.day.from_now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue