diff --git a/app/controllers/france_connect_controller.rb b/app/controllers/france_connect_controller.rb index 85b6c073e..e9591010a 100644 --- a/app/controllers/france_connect_controller.rb +++ b/app/controllers/france_connect_controller.rb @@ -19,7 +19,7 @@ class FranceConnectController < ApplicationController user_infos = FranceConnectService.retrieve_user_informations(params[:code]) unless user_infos.nil? - @user = User.find_for_france_connect(user_infos.email) + @user = User.find_for_france_connect(user_infos.email, user_infos.siret) sign_in @user diff --git a/app/models/user.rb b/app/models/user.rb index 621f42199..58094ec93 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,11 +6,13 @@ class User < ActiveRecord::Base has_many :dossiers - def self.find_for_france_connect email + def self.find_for_france_connect email, siret user = User.find_by_email(email) - - return user unless user.nil? - - User.create(email: email, password: Devise.friendly_token[0,20]) + if user.nil? + return User.create(email: email, password: Devise.friendly_token[0,20], siret: siret) + else + user.update_attributes(siret: siret) + user + end end end diff --git a/db/migrate/20151006155256_add_siret_to_user.rb b/db/migrate/20151006155256_add_siret_to_user.rb new file mode 100644 index 000000000..2e55c7a95 --- /dev/null +++ b/db/migrate/20151006155256_add_siret_to_user.rb @@ -0,0 +1,5 @@ +class AddSiretToUser < ActiveRecord::Migration + def change + add_column :users, :siret, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 7930f66c9..3fe332ad7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150923101000) do +ActiveRecord::Schema.define(version: 20151006155256) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -44,8 +44,8 @@ ActiveRecord::Schema.define(version: 20150923101000) do t.string "montant_aide_demande" t.integer "procedure_id" t.date "date_previsionnelle" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", default: '2015-09-22 09:25:29' + t.datetime "updated_at", default: '2015-09-22 09:25:29' t.string "state" t.integer "user_id" end @@ -110,7 +110,7 @@ ActiveRecord::Schema.define(version: 20150923101000) do t.integer "type_de_piece_justificative_id" end - add_index "pieces_justificatives", ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_piece_jointe_id", using: :btree + add_index "pieces_justificatives", ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree create_table "procedures", force: :cascade do |t| t.string "libelle" @@ -145,6 +145,7 @@ ActiveRecord::Schema.define(version: 20150923101000) do t.inet "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" + t.string "siret" end add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index ca38b1986..d27505573 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -14,8 +14,38 @@ describe User, type: :model do it { is_expected.to have_db_column(:last_sign_in_ip) } it { is_expected.to have_db_column(:created_at) } it { is_expected.to have_db_column(:updated_at) } + it { is_expected.to have_db_column(:siret) } end describe 'associations' do it { is_expected.to have_many(:dossiers) } end + describe '#find_for_france_connect' do + let(:siret) { '00000000000000' } + context 'when user exist' do + let!(:user) { create(:user) } + subject { described_class.find_for_france_connect(user.email, siret) } + it 'retrieves user' do + expect(subject).to eq(user) + end + it 'saves siret in user' do + expect(subject.siret).to eq(siret) + end + it 'does not create new user' do + expect{ subject }.not_to change(User, :count) + end + end + context 'when user does not exist' do + let(:email) { 'super-m@n.com' } + subject { described_class.find_for_france_connect(email, siret) } + it 'returns user' do + expect(subject).to be_an_instance_of(User) + end + it 'creates new user' do + expect{ subject }.to change(User, :count).by(1) + end + it 'saves siret' do + expect(subject.siret).to eq(siret) + end + end + end end