2019-06-17 11:01:41 +02:00
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe BillSignature, type: :model do
|
|
|
|
|
describe 'validations' do
|
2019-06-17 14:38:17 +02:00
|
|
|
|
subject(:bill_signature) { BillSignature.new }
|
|
|
|
|
|
2019-06-17 11:01:41 +02:00
|
|
|
|
describe 'check_bill_digest' do
|
|
|
|
|
before do
|
2019-06-17 14:38:17 +02:00
|
|
|
|
bill_signature.dossier_operation_logs = dossier_operation_logs
|
|
|
|
|
bill_signature.digest = digest
|
|
|
|
|
bill_signature.valid?
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when there are no operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [] }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the digest is correct' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:digest) { Digest::SHA256.hexdigest('{}') }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:digest]).to be_empty }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the digest is incorrect' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:digest) { 'baadf00d' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:digest]).to eq [error: :invalid] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature has operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [build(:dossier_operation_log, id: '1234', digest: 'abcd')] }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the digest is correct' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:digest) { Digest::SHA256.hexdigest('{"1234":"abcd"}') }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:digest]).to be_empty }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the digest is incorrect' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:digest) { 'baadf00d' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:digest]).to eq [error: :invalid] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'check_serialized_bill_contents' do
|
|
|
|
|
before do
|
2019-06-17 14:38:17 +02:00
|
|
|
|
bill_signature.dossier_operation_logs = dossier_operation_logs
|
|
|
|
|
bill_signature.serialized.attach(io: StringIO.new(serialized), filename: 'file') if serialized.present?
|
|
|
|
|
bill_signature.valid?
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when there are no operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [] }
|
|
|
|
|
let(:serialized) { '{}' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:serialized]).to be_empty }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature has operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [build(:dossier_operation_log, id: '1234', digest: 'abcd')] }
|
|
|
|
|
let(:serialized) { '{"1234":"abcd"}' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:serialized]).to be_empty }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when serialized isn’t set' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [] }
|
|
|
|
|
let(:serialized) { nil }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:serialized]).to eq [error: :blank] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'check_signature_contents' do
|
|
|
|
|
before do
|
2019-06-17 14:38:17 +02:00
|
|
|
|
bill_signature.signature.attach(io: StringIO.new(signature), filename: 'file') if signature.present?
|
2019-06-17 11:01:41 +02:00
|
|
|
|
allow(ASN1::Timestamp).to receive(:signature_time).and_return(signature_time)
|
|
|
|
|
allow(ASN1::Timestamp).to receive(:signed_digest).and_return(signed_digest)
|
2019-06-17 14:38:17 +02:00
|
|
|
|
bill_signature.digest = digest
|
|
|
|
|
bill_signature.valid?
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature is correct' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:signature) { 'signature' }
|
|
|
|
|
let(:signature_time) { 1.day.ago }
|
|
|
|
|
let(:digest) { 'abcd' }
|
|
|
|
|
let(:signed_digest) { 'abcd' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:signature]).to be_empty }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature isn’t set' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:signature) { nil }
|
|
|
|
|
let(:signature_time) { 1.day.ago }
|
|
|
|
|
let(:digest) { 'abcd' }
|
|
|
|
|
let(:signed_digest) { 'abcd' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:signature]).to eq [error: :blank] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature time is in the future' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:signature) { 'signature' }
|
|
|
|
|
let(:signature_time) { 1.day.from_now }
|
|
|
|
|
let(:digest) { 'abcd' }
|
|
|
|
|
let(:signed_digest) { 'abcd' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:signature]).to eq [error: :invalid_date] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when the signature doesn’t match the digest' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:signature) { 'signature' }
|
|
|
|
|
let(:signature_time) { 1.day.ago }
|
|
|
|
|
let(:digest) { 'abcd' }
|
|
|
|
|
let(:signed_digest) { 'dcba' }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.errors.details[:signature]).to eq [error: :invalid] }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '.build_with_operations' do
|
2019-06-17 14:38:17 +02:00
|
|
|
|
subject(:bill_signature) { described_class.build_with_operations(dossier_operation_logs, Date.new(1871, 03, 18)) }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when there are no operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) { [] }
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
it { expect(bill_signature.operations_bill).to eq({}) }
|
|
|
|
|
it { expect(bill_signature.digest).to eq(Digest::SHA256.hexdigest('{}')) }
|
|
|
|
|
it { expect(bill_signature.serialized.download).to eq('{}') }
|
|
|
|
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when there is one operation' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) do
|
|
|
|
|
[build(:dossier_operation_log, id: '1234', digest: 'abcd')]
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
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.serialized.download).to eq('{"1234":"abcd"}') }
|
|
|
|
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
context 'when there are several operations' do
|
2019-06-17 11:01:41 +02:00
|
|
|
|
let(:dossier_operation_logs) do
|
|
|
|
|
[
|
|
|
|
|
build(:dossier_operation_log, id: '1234', digest: 'abcd'),
|
|
|
|
|
build(:dossier_operation_log, id: '5678', digest: 'dcba')
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-17 14:38:17 +02:00
|
|
|
|
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.serialized.download).to eq('{"1234":"abcd","5678":"dcba"}') }
|
|
|
|
|
it { expect(bill_signature.serialized.filename).to eq('demarches-simplifiees-operations-1871-03-18.json') }
|
2019-06-17 11:01:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|