demarches-normaliennes/spec/services/bill_signature_service_spec.rb
2024-08-22 09:26:48 +02:00

50 lines
1.5 KiB
Ruby
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
describe BillSignatureService do
describe ".sign_operations" do
let(:date) { Date.today }
let(:operations_hash) { [['1', 'hash1'], ['2', 'hash2']] }
let(:operations) do
operations_hash
.map { |id, digest| DossierOperationLog.new(id:, digest:, operation: 'accepter') }
end
let(:timestamp) { File.read('spec/fixtures/files/bill_signature/signature.der') }
subject { BillSignatureService.sign_operations(operations, date) }
before do
DossierOperationLog.where(id: [1, 2]).destroy_all
expect(Certigna::API).to receive(:timestamp).and_return(timestamp)
end
context "when everything is fine" do
it do
expect { subject }.not_to raise_error
expect(BillSignature.count).to eq(1)
end
end
context "when the digest does not match with the pre recorded timestamp token" do
let(:operations_hash) { [['1', 'hash1'], ['2', 'hash3']] }
it do
expect { subject }.to raise_error(/La validation a échoué : Le champ « signature » ne correspond pas à lempreinte/)
expect(BillSignature.count).to eq(0)
end
end
context "when the timestamp token cannot be verified by openssl" do
let(:timestamp) do
File.read('spec/fixtures/files/bill_signature/signature.der').tap { |s| s[-1] = 'd' }
end
it do
expect { subject }.to raise_error(/openssl verification failed/)
expect(BillSignature.count).to eq(0)
end
end
end
end