demarches-normaliennes/spec/models/bill_signature_spec.rb
Pierre de La Morinerie 4cb747fdb6 specs: always require rails_helper
Test helpers are separated between two files: spec_helper and
rails_helper. This separation is meant to allow tests that do not
require Rails (like testing standalone libs) to boot faster.

The spec_helper file is always loaded, through `--require spec_helper`
in the `.rspec` config file. When needed, the rails_helper file is
expected to be required manually.

This is fine, but:
- Many test files have a redundant `require 'spec_helper'` line;
- Many test files should require `rails_helper`, but don't.

Not requiring `rails_helper` will cause the Rails-concerned section of
the test environment not to be configured–which may cause subtle bugs
(like the test database not being properly initialized).

Moreover, Spring loads all the Rails files on preloading anyway. So the
gains from using only `spec_helper` are thin.

To streamline this process, this commit:
- Configures `.rspec` to require `rails_helper` by default;
- Remove all manual requires to spec_helper or rails_helper.

Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
2020-03-31 12:48:32 +02:00

158 lines
5.8 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

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.

RSpec.describe BillSignature, type: :model do
describe 'validations' do
subject(:bill_signature) { BillSignature.new }
describe 'check_bill_digest' do
before do
bill_signature.dossier_operation_logs = dossier_operation_logs
bill_signature.digest = digest
bill_signature.valid?
end
context 'when there are no operations' do
let(:dossier_operation_logs) { [] }
context 'when the digest is correct' do
let(:digest) { Digest::SHA256.hexdigest('{}') }
it { expect(bill_signature.errors.details[:digest]).to be_empty }
end
context 'when the digest is incorrect' do
let(:digest) { 'baadf00d' }
it { expect(bill_signature.errors.details[:digest]).to eq [error: :invalid] }
end
end
context 'when the signature has operations' do
let(:dossier_operation_logs) { [build(:dossier_operation_log, id: '1234', digest: 'abcd')] }
context 'when the digest is correct' do
let(:digest) { Digest::SHA256.hexdigest('{"1234":"abcd"}') }
it { expect(bill_signature.errors.details[:digest]).to be_empty }
end
context 'when the digest is incorrect' do
let(:digest) { 'baadf00d' }
it { expect(bill_signature.errors.details[:digest]).to eq [error: :invalid] }
end
end
end
describe 'check_serialized_bill_contents' do
before do
bill_signature.dossier_operation_logs = dossier_operation_logs
bill_signature.serialized.attach(io: StringIO.new(serialized), filename: 'file') if serialized.present?
bill_signature.valid?
end
context 'when there are no operations' do
let(:dossier_operation_logs) { [] }
let(:serialized) { '{}' }
it { expect(bill_signature.errors.details[:serialized]).to be_empty }
end
context 'when the signature has operations' do
let(:dossier_operation_logs) { [build(:dossier_operation_log, id: '1234', digest: 'abcd')] }
let(:serialized) { '{"1234":"abcd"}' }
it { expect(bill_signature.errors.details[:serialized]).to be_empty }
end
context 'when serialized isnt set' do
let(:dossier_operation_logs) { [] }
let(:serialized) { nil }
it { expect(bill_signature.errors.details[:serialized]).to eq [error: :blank] }
end
end
describe 'check_signature_contents' do
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
bill_signature.valid?
end
context 'when the signature is correct' do
let(:signature) { 'signature' }
let(:signature_time) { 1.day.ago }
let(:digest) { 'abcd' }
let(:signed_digest) { 'abcd' }
it { expect(bill_signature.errors.details[:signature]).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] }
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' }
it { expect(bill_signature.errors.details[:signature]).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' }
it { expect(bill_signature.errors.details[:signature]).to eq [error: :invalid] }
end
end
end
describe '.build_with_operations' do
subject(:bill_signature) { described_class.build_with_operations(dossier_operation_logs, Date.new(1871, 03, 18)) }
context 'when there are no operations' do
let(:dossier_operation_logs) { [] }
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') }
end
context 'when there is one operation' do
let(:dossier_operation_logs) do
[build(:dossier_operation_log, id: '1234', digest: 'abcd')]
end
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') }
end
context 'when there are several operations' do
let(:dossier_operation_logs) do
[
build(:dossier_operation_log, id: '1234', digest: 'abcd'),
build(:dossier_operation_log, id: '5678', digest: 'dcba')
]
end
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') }
end
end
end