Add sign and verify to etablissement

This commit is contained in:
Paul Chavard 2018-04-03 16:27:59 +02:00
parent 714ebda2fe
commit 3cd80af6cb
2 changed files with 36 additions and 0 deletions

View file

@ -2,6 +2,7 @@ class Etablissement < ApplicationRecord
belongs_to :dossier
belongs_to :entreprise, dependent: :destroy
has_one :champ, class_name: 'Champs::SiretChamp'
has_many :exercices, dependent: :destroy
accepts_nested_attributes_for :exercices
@ -17,4 +18,19 @@ class Etablissement < ApplicationRecord
# squeeze needed because of space in excess in the data
"#{numero_voie} #{type_voie} #{nom_voie}, #{complement_adresse}, #{code_postal} #{localite}".squeeze(' ')
end
def verify
SignatureService.verify(signature, message_for_signature)
end
def sign
SignatureService.sign(message_for_signature)
end
attr_accessor :signature
def message_for_signature
JSON.pretty_generate(as_json(include: {
exercices: { only: [:ca, :date_fin_exercice, :date_fin_exercice_timestamp] }
}).delete_if { |k,v| v.blank? })
end
end

View file

@ -14,4 +14,24 @@ describe Etablissement do
it { expect(etablissement.inline_adresse).to eq '6 RUE green moon, IMMEUBLE BORA, 92270 BOIS COLOMBES' }
end
describe '#verify' do
let(:etablissement) { create(:etablissement) }
let(:etablissement2) { create(:etablissement) }
it 'should verify signed etablissement' do
etablissement.signature = etablissement.sign
expect(etablissement.verify).to eq(true)
end
it 'should reject etablissement with other etablissement signature' do
etablissement.signature = etablissement2.sign
expect(etablissement.verify).to eq(false)
end
it 'should reject etablissement with wrong signature' do
etablissement.signature = "fd7687fdsgdf6gd7f8g"
expect(etablissement.verify).to eq(false)
end
end
end