diff --git a/Gemfile b/Gemfile index 4b90958f8..89e8a2ee0 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,8 @@ gem 'fog-openstack' gem 'pg' +gem 'rbnacl-libsodium' + gem 'rgeo-geojson' gem 'leaflet-rails' gem 'leaflet-markercluster-rails', '~> 0.7.0' diff --git a/Gemfile.lock b/Gemfile.lock index 54fc7664c..065e64f97 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -591,6 +591,10 @@ GEM rb-fsevent (0.10.2) rb-inotify (0.9.10) ffi (>= 0.5.0, < 2) + rbnacl (5.0.0) + ffi + rbnacl-libsodium (1.0.16) + rbnacl (>= 3.0.1) rbovirt (0.1.5) nokogiri rest-client (> 1.7.0) @@ -836,6 +840,7 @@ DEPENDENCIES rack-mini-profiler rails (~> 5.2.0.rc1) rails-controller-testing + rbnacl-libsodium rest-client rgeo-geojson rspec-rails diff --git a/app/assets/javascripts/new_design/champs/siret.js b/app/assets/javascripts/new_design/champs/siret.js new file mode 100644 index 000000000..8904b05a6 --- /dev/null +++ b/app/assets/javascripts/new_design/champs/siret.js @@ -0,0 +1,27 @@ +document.addEventListener('turbolinks:load', function() { + $('[data-siret]').on('input', function(evt) { + var input = $(evt.target); + var value = input.val(); + var url = input.attr('data-siret'); + switch (value.length) { + case 0: + $.get(url+'?siret=blank'); + break; + case 14: + input.attr('disabled', 'disabled'); + $.get(url+'?siret='+value).then(function() { + input.removeAttr('data-invalid'); + input.removeAttr('disabled'); + }, function() { + input.removeAttr('disabled'); + input.attr('data-invalid', true); + }); + break; + default: + if (!input.attr('data-invalid')) { + input.attr('data-invalid', true); + $.get(url+'?siret=invalid'); + } + } + }); +}); diff --git a/app/assets/stylesheets/new_design/dossier-edit.scss b/app/assets/stylesheets/new_design/dossier-edit.scss index e906c529b..a2bd0a6fb 100644 --- a/app/assets/stylesheets/new_design/dossier-edit.scss +++ b/app/assets/stylesheets/new_design/dossier-edit.scss @@ -4,6 +4,7 @@ .dossier-edit { .dossier-header { background-color: $light-grey; + margin-bottom: $default-padding; .container { padding: $default-padding; diff --git a/app/assets/stylesheets/new_design/forms.scss b/app/assets/stylesheets/new_design/forms.scss index 17ef315f6..8c5191313 100644 --- a/app/assets/stylesheets/new_design/forms.scss +++ b/app/assets/stylesheets/new_design/forms.scss @@ -48,6 +48,10 @@ .updated-at.highlighted { visibility: visible; } + + .etablissement-titre { + margin-bottom: 2 * $default-padding; + } } .radios { @@ -82,6 +86,10 @@ &.small { padding: $default-padding / 2; } + + &.small-margin { + margin-bottom: $default-padding / 2; + } } input[type=text], diff --git a/app/controllers/champs/siret_controller.rb b/app/controllers/champs/siret_controller.rb new file mode 100644 index 000000000..b1fb41b55 --- /dev/null +++ b/app/controllers/champs/siret_controller.rb @@ -0,0 +1,29 @@ +class Champs::SiretController < ApplicationController + def index + siret, champ_id = params.required([:siret, :champ_id]) + @champ = Champs::SiretChamp.find(champ_id) + @etablissement = @champ.etablissement + if siret == 'blank' + if @etablissement + @etablissement.mark_for_destruction + end + @blank = true + elsif siret == 'invalid' + if @etablissement + @etablissement.mark_for_destruction + end + @error = "SIRET invalide" + else + etablissement_attributes = SIRETService.fetch(siret, @champ.dossier.procedure_id) + if etablissement_attributes.present? + @etablissement = @champ.build_etablissement(etablissement_attributes) + @etablissement.champ = @champ + else + @error = "SIRET invalide" + end + end + respond_to do |format| + format.js + end + end +end diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index e978a348c..2cfb23257 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -198,7 +198,10 @@ module NewGestionnaire end def champs_private_params - params.require(:dossier).permit(champs_private_attributes: [:id, :piece_justificative_file, :value, value: []]) + params.require(:dossier).permit(champs_private_attributes: [ + :id, :piece_justificative_file, :value, value: [], + etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES + ]) end def check_attestation_emailable diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index b4501c8e6..b0a63fb8a 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -116,7 +116,12 @@ module NewUser # FIXME: require(:dossier) when all the champs are united def champs_params - params.permit(dossier: { champs_attributes: [:id, :value, :piece_justificative_file, value: []] }) + params.permit(dossier: { + champs_attributes: [ + :id, :value, :piece_justificative_file, value: [], + etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES + ] + }) end def dossier diff --git a/app/helpers/type_de_champ_helper.rb b/app/helpers/type_de_champ_helper.rb index f91158bf6..12bb3674a 100644 --- a/app/helpers/type_de_champ_helper.rb +++ b/app/helpers/type_de_champ_helper.rb @@ -6,6 +6,10 @@ module TypeDeChampHelper tdcs.reject! { |tdc| tdc.last == "piece_justificative" } end + if !current_administrateur.feature_enabled?(:champ_siret_allowed) + tdcs.reject! { |tdc| tdc.last == "siret" } + end + tdcs end end diff --git a/app/models/champs/siret_champ.rb b/app/models/champs/siret_champ.rb new file mode 100644 index 000000000..0a55edb32 --- /dev/null +++ b/app/models/champs/siret_champ.rb @@ -0,0 +1,42 @@ +class Champs::SiretChamp < Champ + ETABLISSEMENT_ATTRIBUTES = [ + :id, + :_destroy, + :signature, + :siret, + :siege_social, + :naf, + :libelle_naf, + :adresse, + :numero_voie, + :type_voie, + :nom_voie, + :code_postal, + :localite, + :code_insee_localite, + :entreprise_siren, + :entreprise_capital_social, + :entreprise_numero_tva_intracommunautaire, + :entreprise_forme_juridique, + :entreprise_forme_juridique_code, + :entreprise_nom_commercial, + :entreprise_raison_sociale, + :entreprise_siret_siege_social, + :entreprise_code_effectif_entreprise, + :entreprise_date_creation, + :entreprise_nom, + :entreprise_prenom, + :association_rna, + :association_titre, + :association_objet, + :association_date_creation, + :association_date_declaration, + :association_date_publication, + exercices_attributes: [ + [:id, :ca, :date_fin_exercice, :date_fin_exercice_timestamp] + ] + ] + + belongs_to :etablissement, dependent: :destroy + accepts_nested_attributes_for :etablissement, allow_destroy: true, update_only: true +end diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 1f59bbb18..840e1aed5 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -2,12 +2,16 @@ 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 accepts_nested_attributes_for :entreprise, update_only: true - validates :dossier_id, uniqueness: true + validates :siret, presence: true + validates :dossier_id, uniqueness: { allow_nil: true } + + validate :validate_signature def geo_adresse [numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ') @@ -17,4 +21,32 @@ 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 titre + entreprise_raison_sociale || association_titre + end + + def verify + SignatureService.verify(signature, message_for_signature) + end + + def sign + SignatureService.sign(message_for_signature) + end + + attr_accessor :signature + + private + + def validate_signature + if champ && !verify + errors.add(:base, 'Numéro SIRET introuvable.') + end + end + + 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 diff --git a/app/models/individual.rb b/app/models/individual.rb index 5e3fd2ea6..8ab792ab4 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -5,12 +5,4 @@ class Individual < ApplicationRecord validates :gender, presence: true, allow_nil: false, on: :update validates :nom, presence: true, allow_blank: false, allow_nil: false, on: :update validates :prenom, presence: true, allow_blank: false, allow_nil: false, on: :update - - def birthdate - second_birthdate - end - - def birthdate=(date) - self.second_birthdate = date - end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index a10e445a7..ca90df71c 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -20,7 +20,8 @@ class TypeDeChamp < ApplicationRecord header_section: 'header_section', explication: 'explication', dossier_link: 'dossier_link', - piece_justificative: 'piece_justificative' + piece_justificative: 'piece_justificative', + siret: 'siret' } belongs_to :procedure diff --git a/app/models/types_de_champ/siret_type_de_champ.rb b/app/models/types_de_champ/siret_type_de_champ.rb new file mode 100644 index 000000000..47be81287 --- /dev/null +++ b/app/models/types_de_champ/siret_type_de_champ.rb @@ -0,0 +1,2 @@ +class TypesDeChamp::SiretTypeDeChamp < TypeDeChamp +end diff --git a/app/services/signature_service.rb b/app/services/signature_service.rb new file mode 100644 index 000000000..6896da62a --- /dev/null +++ b/app/services/signature_service.rb @@ -0,0 +1,38 @@ +class SignatureService + CONFIG_PATH = Rails.root.join("config", "signing_key.yml") + + class << self + def generate + RbNaCl::Util.bin2hex(RbNaCl::SigningKey.generate) + end + + def verify(signature, message) + message = Base64.urlsafe_encode64(message) + begin + signing_key.verify_key + .verify(RbNaCl::Util.hex2bin(signature), message) + rescue RbNaCl::BadSignatureError, RbNaCl::LengthError + return false + end + end + + def sign(message) + message = Base64.urlsafe_encode64(message) + RbNaCl::Util.bin2hex(signing_key.sign(message)) + end + + private + + def signing_key + @@signing_key ||= RbNaCl::SigningKey.new(RbNaCl::Util.hex2bin(config[:key])) + end + + def config + if File.exist?(CONFIG_PATH) + YAML.safe_load(File.read(CONFIG_PATH)).symbolize_keys + else + {} + end + end + end +end diff --git a/app/views/champs/siret/index.js.erb b/app/views/champs/siret/index.js.erb new file mode 100644 index 000000000..899f52440 --- /dev/null +++ b/app/views/champs/siret/index.js.erb @@ -0,0 +1,8 @@ +(function() { + <% if @blank || @error %> + var html = "<%= escape_javascript(render partial: 'shared/champs/siret/delete_etablissement', locals: { message: @error, position: @champ.order_place, etablissement: @etablissement }) %>"; + <% else %> + var html = "<%= escape_javascript(render partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>"; + <% end %> + $("#etablissement-for-<%= @champ.id %>").html(html); +})(); diff --git a/app/views/new_gestionnaire/dossiers/_map.html.haml b/app/views/new_gestionnaire/dossiers/_map.html.haml index 85ecbafc4..262d2f68f 100644 --- a/app/views/new_gestionnaire/dossiers/_map.html.haml +++ b/app/views/new_gestionnaire/dossiers/_map.html.haml @@ -1,20 +1,23 @@ -#map +- if dossier.json_latlngs.nil? + %h2.empty-text Non renseigné +- else + #map -- if dossier.quartier_prioritaires.any? - .card-title Quartiers prioritaires - %ul - - dossier.quartier_prioritaires.each do |q| - %li= q.nom + - if dossier.quartier_prioritaires.any? + .card-title Quartiers prioritaires + %ul + - dossier.quartier_prioritaires.each do |q| + %li= q.nom -- if dossier.cadastres.any? - .card-title Parcelles cadastrales - %ul - - dossier.cadastres.each do |p| - %li - = "Parcelle n° #{p.numero} - Feuille #{p.code_arr} #{p.section} #{p.feuille}" + - if dossier.cadastres.any? + .card-title Parcelles cadastrales + %ul + - dossier.cadastres.each do |p| + %li + = "Parcelle n° #{p.numero} - Feuille #{p.code_arr} #{p.section} #{p.feuille}" -:javascript - var getPositionUrl = "#{position_gestionnaire_dossier_path(dossier.procedure, dossier)}"; - var dossierJsonLatLngs = #{raw(ensure_safe_json(dossier.json_latlngs))}; - var dossierCadastres = #{raw(ensure_safe_json(dossier.cadastres.to_json))}; - var dossierQuartiersPrioritaires = #{raw(ensure_safe_json(dossier.quartier_prioritaires.to_json))}; + :javascript + var getPositionUrl = "#{position_gestionnaire_dossier_path(dossier.procedure, dossier)}"; + var dossierJsonLatLngs = #{raw(ensure_safe_json(dossier.json_latlngs))}; + var dossierCadastres = #{raw(ensure_safe_json(dossier.cadastres.to_json))}; + var dossierQuartiersPrioritaires = #{raw(ensure_safe_json(dossier.quartier_prioritaires.to_json))}; diff --git a/app/views/new_user/dossiers/modifier.html.haml b/app/views/new_user/dossiers/modifier.html.haml index 9e0d437ca..18979f145 100644 --- a/app/views/new_user/dossiers/modifier.html.haml +++ b/app/views/new_user/dossiers/modifier.html.haml @@ -4,6 +4,10 @@ %h1= @dossier.procedure.libelle .container + %p + Pour vous aider à remplir votre dossier, vous pouvez consulter + = link_to 'le guide de cette démarche', @dossier.procedure.lien_notice, { target: '_blank' } + %p.thanks Les champs avec une asterisque (*) sont obligatoires. = form_for @dossier, html: { class: 'form', multipart: true } do |f| diff --git a/app/views/shared/champs/siret/_delete_etablissement.html.haml b/app/views/shared/champs/siret/_delete_etablissement.html.haml new file mode 100644 index 000000000..279c69c2c --- /dev/null +++ b/app/views/shared/champs/siret/_delete_etablissement.html.haml @@ -0,0 +1,6 @@ += message +- if etablissement.present? + - champ_attributes = etablissement.champ.private? ? 'champs_private_attributes' : 'champs_attributes' + = fields_for "dossier[#{champ_attributes}][#{position}][etablissement_attributes]", etablissement do |form| + = form.hidden_field :_destroy + diff --git a/app/views/shared/champs/siret/_etablissement.html.haml b/app/views/shared/champs/siret/_etablissement.html.haml new file mode 100644 index 000000000..893a5ef2a --- /dev/null +++ b/app/views/shared/champs/siret/_etablissement.html.haml @@ -0,0 +1,4 @@ += render partial: 'shared/dossiers/editable_champs/etablissement_titre', locals: { etablissement: etablissement } +- champ_attributes = etablissement.champ.private? ? 'champs_private_attributes' : 'champs_attributes' += fields_for "dossier[#{champ_attributes}][#{position}][etablissement_attributes]", etablissement do |form| + = render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: etablissement.sign } diff --git a/app/views/shared/dossiers/editable_champs/_etablissement.html.haml b/app/views/shared/dossiers/editable_champs/_etablissement.html.haml new file mode 100644 index 000000000..81d5baac9 --- /dev/null +++ b/app/views/shared/dossiers/editable_champs/_etablissement.html.haml @@ -0,0 +1,34 @@ += form.hidden_field :signature, value: signature += form.hidden_field :siret += form.hidden_field :siege_social += form.hidden_field :naf += form.hidden_field :libelle_naf += form.hidden_field :adresse += form.hidden_field :numero_voie += form.hidden_field :type_voie += form.hidden_field :nom_voie += form.hidden_field :code_postal += form.hidden_field :localite += form.hidden_field :code_insee_localite += form.hidden_field :entreprise_siren += form.hidden_field :entreprise_capital_social += form.hidden_field :entreprise_numero_tva_intracommunautaire += form.hidden_field :entreprise_forme_juridique += form.hidden_field :entreprise_forme_juridique_code += form.hidden_field :entreprise_nom_commercial += form.hidden_field :entreprise_raison_sociale += form.hidden_field :entreprise_siret_siege_social += form.hidden_field :entreprise_code_effectif_entreprise += form.hidden_field :entreprise_date_creation += form.hidden_field :entreprise_nom += form.hidden_field :entreprise_prenom += form.hidden_field :association_rna += form.hidden_field :association_titre += form.hidden_field :association_objet += form.hidden_field :association_date_creation += form.hidden_field :association_date_declaration += form.hidden_field :association_date_publication += form.fields_for :exercices do |form| + = form.hidden_field :ca + = form.hidden_field :date_fin_exercice + = form.hidden_field :date_fin_exercice_timestamp diff --git a/app/views/shared/dossiers/editable_champs/_etablissement_titre.html.haml b/app/views/shared/dossiers/editable_champs/_etablissement_titre.html.haml new file mode 100644 index 000000000..b63734321 --- /dev/null +++ b/app/views/shared/dossiers/editable_champs/_etablissement_titre.html.haml @@ -0,0 +1,6 @@ +.etablissement-titre + = etablissement.titre + = etablissement.entreprise_forme_juridique + - if etablissement.entreprise_capital_social.present? + au capital social de + = number_to_currency(etablissement.entreprise_capital_social) diff --git a/app/views/shared/dossiers/editable_champs/_siret.html.haml b/app/views/shared/dossiers/editable_champs/_siret.html.haml new file mode 100644 index 000000000..2135b274f --- /dev/null +++ b/app/views/shared/dossiers/editable_champs/_siret.html.haml @@ -0,0 +1,10 @@ += form.text_field :value, + placeholder: champ.libelle, + class: 'small-margin', + data: { siret: champs_siret_path(format: :js, champ_id: champ) }, + required: champ.mandatory? +%div{ id: "etablissement-for-#{champ.id}" } + - if champ.etablissement.present? + = render partial: 'shared/dossiers/editable_champs/etablissement_titre', locals: { etablissement: champ.etablissement } + = form.fields_for :etablissement do |form| + = render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: champ.etablissement.sign } diff --git a/config/initializers/features.yml b/config/initializers/features.yml index b0210bbe9..a7b150256 100644 --- a/config/initializers/features.yml +++ b/config/initializers/features.yml @@ -2,3 +2,5 @@ remote_storage: false weekly_overview: false champ_pj_allowed_for_admin_ids: - 0 +champ_siret_allowed_for_admin_ids: + - 0 diff --git a/config/locales/models/type_de_champ/fr.yml b/config/locales/models/type_de_champ/fr.yml index 7d0d1e66c..e740448d3 100644 --- a/config/locales/models/type_de_champ/fr.yml +++ b/config/locales/models/type_de_champ/fr.yml @@ -26,3 +26,4 @@ fr: multiple_drop_down_list: 'Menu déroulant à choix multiples' dossier_link: 'Lien vers un autre dossier' piece_justificative: 'Pièce justificative' + siret: 'SIRET' diff --git a/config/routes.rb b/config/routes.rb index b2cb6bea7..901dd75cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,6 +72,10 @@ Rails.application.routes.draw do get 'particulier/callback' => 'particulier#callback' end + namespace :champs do + get ':champ_id/siret' => 'siret#index', as: 'siret' + end + namespace :users do namespace :dossiers do resources :invites, only: [:index, :show] diff --git a/config/signing_key.yml b/config/signing_key.yml new file mode 100644 index 000000000..6940da6a9 --- /dev/null +++ b/config/signing_key.yml @@ -0,0 +1,2 @@ +# This is a signing key used in dev and test environments +key: 'aef3153a9829fa4ba10acb02927ac855df6b92795b1ad265d654443c4b14a017' diff --git a/db/migrate/20180404113409_change_birthdate_type_from_individual.rb b/db/migrate/20180404113409_change_birthdate_type_from_individual.rb new file mode 100644 index 000000000..0b0e750ec --- /dev/null +++ b/db/migrate/20180404113409_change_birthdate_type_from_individual.rb @@ -0,0 +1,6 @@ +class ChangeBirthdateTypeFromIndividual < ActiveRecord::Migration[5.2] + def up + remove_column :individuals, :birthdate, :string + rename_column :individuals, :second_birthdate, :birthdate + end +end diff --git a/db/schema.rb b/db/schema.rb index 303a660fd..51e42562f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_04_03_094135) do +ActiveRecord::Schema.define(version: 2018_04_04_113409) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -361,12 +361,11 @@ ActiveRecord::Schema.define(version: 2018_04_03_094135) do create_table "individuals", id: :serial, force: :cascade do |t| t.string "nom" t.string "prenom" - t.string "birthdate" t.integer "dossier_id" t.string "gender" t.datetime "created_at" t.datetime "updated_at" - t.date "second_birthdate" + t.date "birthdate" t.index ["dossier_id"], name: "index_individuals_on_dossier_id" end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 0b4645124..af8dfd898 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -235,7 +235,7 @@ describe NewUser::DossiersController, type: :controller do context 'when the update fails' do before do - expect_any_instance_of(Dossier).to receive(:update).and_return(false) + expect_any_instance_of(Dossier).to receive(:save).and_return(false) expect_any_instance_of(Dossier).to receive(:errors) .and_return(double(full_messages: ['nop'])) diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index db149fa9f..b42acc7a1 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -75,6 +75,9 @@ FactoryBot.define do factory :type_de_champ_piece_justificative, class: 'TypesDeChamp::PieceJustificativeTypeDeChamp' do type_champ 'piece_justificative' end + factory :type_de_champ_siret, class: 'TypesDeChamp::SiretTypeDeChamp' do + type_champ 'siret' + end trait :private do private true diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index abc982ad4..31988c14d 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -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 diff --git a/spec/models/individual_spec.rb b/spec/models/individual_spec.rb index 6b11324f1..41c7b5a2f 100644 --- a/spec/models/individual_spec.rb +++ b/spec/models/individual_spec.rb @@ -21,21 +21,18 @@ describe Individual do let(:birthdate_from_user) { "12/11/1980" } it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } - it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is ISO" do let(:birthdate_from_user) { "1980-11-12" } it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } - it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } end context "and the format is WTF" do let(:birthdate_from_user) { "1980 1 12" } it { expect(individual.birthdate).to be_nil } - it { expect(individual.second_birthdate).to be_nil } end end end diff --git a/spec/services/signature_service_spec.rb b/spec/services/signature_service_spec.rb new file mode 100644 index 000000000..c10f38cf9 --- /dev/null +++ b/spec/services/signature_service_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe SignatureService do + let(:service) { SignatureService } + let(:message) { { hello: 'World!' }.to_json } + let(:message2) { { hello: 'World' }.to_json } + + it "sign and verify" do + signature = service.sign(message) + signature2 = service.sign(message2) + + expect(service.verify(signature, message)).to eq(true) + expect(service.verify(signature2, message)).to eq(false) + expect(service.verify(signature, message2)).to eq(false) + end +end