Move dossier invites handler to invites controller

This commit is contained in:
Paul Chavard 2018-11-13 16:18:52 +01:00
parent f3a72482a5
commit 2eb61bd6ed
9 changed files with 119 additions and 114 deletions

View file

@ -1,5 +1,8 @@
class InvitesController < ApplicationController
before_action :ensure_user_signed_in
SESSION_USER_RETURN_LOCATION = 'user_return_to'
before_action :authenticate_user!, only: [:create]
before_action :store_user_location!, only: [:show]
def create
email = params[:invite_email].downcase
@ -30,11 +33,35 @@ class InvitesController < ApplicationController
end
end
def show
if user_signed_in?
erase_user_location!
dossier = Dossier.joins(:invites)
.find_by!(invites: { email: current_user.email, id: params[:id] })
if dossier.brouillon?
redirect_to brouillon_dossier_path(dossier)
else
redirect_to dossier_path(dossier)
end
elsif params[:email].present? && !User.find_by(email: params[:email])
redirect_to new_user_registration_path(user: { email: params[:email] })
else
authenticate_user!
end
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to dossiers_path
end
private
def ensure_user_signed_in
if !user_signed_in?
return redirect_to root_path
end
def store_user_location!
store_location_for(:user, request.fullpath)
end
def erase_user_location!
session.delete(SESSION_USER_RETURN_LOCATION)
end
end

View file

@ -1,26 +0,0 @@
class Users::Dossiers::InvitesController < UsersController
def authenticate_user!
session["user_return_to"] = request.fullpath
email = params[:email]
if email.present? && User.find_by(email: email).nil?
redirect_to new_user_registration_path(user: { email: email })
else
super
end
end
def show
invite = Invite.where(email: current_user.email, id: params[:id].to_i).first!
dossier = invite.dossier
if dossier.brouillon?
redirect_to brouillon_dossier_path(dossier)
else
redirect_to dossier_path(dossier)
end
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to url_for dossiers_path
end
end

View file

@ -13,7 +13,7 @@
Afin de répondre à cette invitation, merci de vous inscrire avec l'adresse email
= @invite.email
sur
= users_dossiers_invite_url(@invite.id, params: { email: @invite.email })
= invite_url(@invite, params: { email: @invite.email })
%p
Bonne journée,

View file

@ -8,7 +8,7 @@
%p
Pour le consulter, merci de suivre ce lien :
= users_dossiers_invite_url(@invite.id)
= invite_url(@invite)
%p
Bonne journée,

View file

@ -12,6 +12,6 @@
%p Vous pouvez inviter quelquun à remplir ce dossier avec vous.
%p Cette personne aura le droit de modifier votre dossier.
= form_tag invites_dossier_path(dossier_id: dossier.id), remote: true, method: :post, class: 'form' do
= form_tag dossier_invites_path(dossier), remote: true, method: :post, class: 'form' do
= email_field_tag :invite_email, '', class: 'small', placeholder: 'adresse email', required: true
= submit_tag 'Envoyer une invitation', class: 'button accepted'

View file

@ -144,10 +144,6 @@ Rails.application.routes.draw do
#
namespace :users do
namespace :dossiers do
resources :invites, only: [:index, :show]
end
resources :dossiers, only: [] do
post '/carte/zones' => 'carte#zones'
get '/carte' => 'carte#show'
@ -159,6 +155,7 @@ Rails.application.routes.draw do
# Redirection of legacy "/users/dossiers" route to "/dossiers"
get 'dossiers', to: redirect('/dossiers')
get 'dossiers/:id/recapitulatif', to: redirect('/dossiers/%{id}')
get 'dossiers/invites/:id', to: redirect(path: '/invites/%{id}')
end
namespace :gestionnaire do
@ -241,8 +238,10 @@ Rails.application.routes.draw do
get 'address/suggestions' => 'address#suggestions'
get 'address/geocode' => 'address#geocode'
namespace :invites do
post 'dossier/:dossier_id' => '/invites#create', as: 'dossier'
resources :invites, only: [:show] do
collection do
post 'dossier/:dossier_id', to: 'invites#create', as: :dossier
end
end
#

View file

@ -158,4 +158,81 @@ describe InvitesController, type: :controller do
end
end
end
describe '#GET show' do
let(:user) { create :user }
context "when invite without email exists" do
let(:invite) { create(:invite, dossier: dossier) }
subject { get :show, params: { id: invite.id, email: email } }
context 'when email is not set' do
let(:email) { nil }
context 'and user is not connected' do
it { is_expected.to redirect_to new_user_session_path }
end
context 'and user is connected' do
let(:invite) { create :invite, dossier: dossier, user: user }
before { sign_in user }
it { is_expected.to redirect_to(dossier_path(dossier)) }
end
end
context 'when email is blank' do
let(:email) { '' }
it { is_expected.to redirect_to new_user_session_path }
end
context 'when email is not blank' do
context 'when email is affected at an user' do
let(:email) { user.email }
it { is_expected.to redirect_to new_user_session_path }
end
context 'when email is not affected at an user' do
let(:email) { 'new_user@octo.com' }
it { is_expected.to redirect_to new_user_registration_path(user: { email: email }) }
end
end
end
context "when invite with email exists" do
let(:invite) { create :invite, email: email, dossier: dossier }
before do
sign_in user
end
subject! { get :show, params: { id: invite.id } }
context 'when invitation ID is attached at the user email account' do
let(:email) { user.email }
context 'and dossier is a brouillon' do
let(:dossier) { create :dossier, state: Dossier.states.fetch(:brouillon) }
it { is_expected.to redirect_to brouillon_dossier_path(dossier) }
end
context 'and dossier is not a brouillon' do
let(:dossier) { create :dossier, :en_construction }
it { is_expected.to redirect_to(dossier_path(dossier)) }
end
end
context 'when invitation ID is not attached at the user email account' do
let(:email) { 'fake@email.com' }
it { is_expected.to redirect_to dossiers_path }
it { expect(flash[:alert]).to be_present }
end
end
end
end

View file

@ -1,72 +0,0 @@
describe Users::Dossiers::InvitesController, type: :controller do
describe '#authenticate_user!' do
let(:user) { create :user }
let(:dossier) { create(:dossier, :en_construction) }
let(:invite) { create(:invite, dossier: dossier) }
subject { get :show, params: { id: invite.id, email: email } }
context 'when email is not set' do
let(:email) { nil }
context 'and user is not connected' do
it { is_expected.to redirect_to new_user_session_path }
end
context 'and user is connected' do
let(:invite) { create :invite, dossier: dossier, user: user }
before { sign_in invite.user }
it { is_expected.to redirect_to(dossier_path(dossier)) }
end
end
context 'when email is blank' do
let(:email) { '' }
it { is_expected.to redirect_to new_user_session_path }
end
context 'when email is not blank' do
context 'when email is affected at an user' do
let(:email) { user.email }
it { is_expected.to redirect_to new_user_session_path }
end
context 'when email is not affected at an user' do
let(:email) { 'new_user@octo.com' }
it { is_expected.to redirect_to new_user_registration_path(user: { email: email }) }
end
end
end
describe '#GET show' do
let(:user) { create :user }
let(:dossier) { create :dossier }
let(:invite) { create :invite, email: email, dossier: dossier }
before do
sign_in user
end
subject! { get :show, params: { id: invite.id } }
context 'when invitation ID is attached at the user email account' do
let(:email) { user.email }
context 'and dossier is a brouillon' do
let(:dossier) { create :dossier, state: Dossier.states.fetch(:brouillon) }
it { is_expected.to redirect_to brouillon_dossier_path(dossier) }
end
context 'and dossier is not a brouillon' do
let(:dossier) { create :dossier, :en_construction }
it { is_expected.to redirect_to(dossier_path(dossier)) }
end
end
context 'when invitation ID is not attached at the user email account' do
let(:email) { 'fake@email.com' }
it { is_expected.to redirect_to dossiers_path }
it { expect(flash[:alert]).to be_present }
end
end
end

View file

@ -31,7 +31,7 @@ feature 'Invitations' do
scenario 'an invited user can register using the registration link sent in the invitation email' do
# Click the invitation link
visit users_dossiers_invite_path(invite.id, params: { email: invite.email })
visit invite_path(invite, params: { email: invite.email })
expect(page).to have_current_path(new_user_registration_path, ignore_query: true)
expect(page).to have_field('user_email', with: invite.email)
@ -115,7 +115,7 @@ feature 'Invitations' do
end
def navigate_to_invited_dossier(invite)
visit users_dossiers_invite_path(invite)
visit invite_path(invite)
expect(page).to have_current_path(new_user_session_path)
sign_in_with(invited_user.email, invited_user.password)
end