add siret to user

This commit is contained in:
Tanguy PATTE 2015-10-07 10:29:44 +02:00
parent d655b2319a
commit 0a14fbd5dd
5 changed files with 48 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class AddSiretToUser < ActiveRecord::Migration
def change
add_column :users, :siret, :string
end
end

View file

@ -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

View file

@ -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