diff --git a/app/controllers/users/dossiers/invites_controller.rb b/app/controllers/users/dossiers/invites_controller.rb index 1d451a8ca..f4c4d9dc6 100644 --- a/app/controllers/users/dossiers/invites_controller.rb +++ b/app/controllers/users/dossiers/invites_controller.rb @@ -1,7 +1,10 @@ class Users::Dossiers::InvitesController < UsersController def authenticate_user! session["user_return_to"] = request.fullpath - return redirect_to new_user_registration_path(user_email: params[:email]) if params[:email].present? && User.find_by(email: params[:email]).nil? + + if params[:email].present? && User.find_by(email: params[:email]).nil? + return redirect_to new_user_registration_path(user: { email: params[:email] }) + end super end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 6bbcbdf4a..89af9412d 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -9,9 +9,13 @@ class Users::RegistrationsController < Devise::RegistrationsController # end # GET /resource/sign_up - # def new - # super - # end + def new + # Allow pre-filling the user email from a query parameter + build_resource({ email: sign_up_params[:email] }) + + yield resource if block_given? + respond_with resource + end # POST /resource def create diff --git a/app/views/invite_mailer/invite_guest.html.haml b/app/views/invite_mailer/invite_guest.html.haml index 2bd76f10d..45ef1cd1b 100644 --- a/app/views/invite_mailer/invite_guest.html.haml +++ b/app/views/invite_mailer/invite_guest.html.haml @@ -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)}?email=#{@invite.email}" + = users_dossiers_invite_url(@invite.id, params: { email: @invite.email }) %p Bonne journée, diff --git a/app/views/users/registrations/new.html.haml b/app/views/users/registrations/new.html.haml index 3775c572f..2a11db8ea 100644 --- a/app/views/users/registrations/new.html.haml +++ b/app/views/users/registrations/new.html.haml @@ -11,7 +11,7 @@ .column.auth-form = devise_error_messages! - = form_for User.new, url: user_registration_path, html: { class: "form" } do |f| + = form_for resource, url: user_registration_path, html: { class: "form" } do |f| %h1 Créez-vous un compte = f.label :email, "Email" diff --git a/spec/controllers/users/dossiers/invites_controller_spec.rb b/spec/controllers/users/dossiers/invites_controller_spec.rb index c9b0f5e27..ba23279b7 100644 --- a/spec/controllers/users/dossiers/invites_controller_spec.rb +++ b/spec/controllers/users/dossiers/invites_controller_spec.rb @@ -33,7 +33,7 @@ describe Users::Dossiers::InvitesController, type: :controller do 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) } + it { is_expected.to redirect_to new_user_registration_path(user: { email: email }) } end end end diff --git a/spec/controllers/users/registrations_controller_spec.rb b/spec/controllers/users/registrations_controller_spec.rb index 1c9a5793b..58faecedd 100644 --- a/spec/controllers/users/registrations_controller_spec.rb +++ b/spec/controllers/users/registrations_controller_spec.rb @@ -8,6 +8,22 @@ describe Users::RegistrationsController, type: :controller do @request.env["devise.mapping"] = Devise.mappings[:user] end + describe '#new' do + subject! { get :new } + + it { expect(response).to have_http_status(:ok) } + it { expect(response).to render_template(:new) } + + context 'when an email address is provided' do + render_views true + subject! { get :new, params: { user: { email: 'test@exemple.fr' } } } + + it 'prefills the form with the email address' do + expect(response.body).to include('test@exemple.fr') + end + end + end + describe '#create' do subject do post :create, params: { user: user } diff --git a/spec/features/new_user/dossier_details_spec.rb b/spec/features/new_user/dossier_details_spec.rb index fb62483bf..ae3ec8248 100644 --- a/spec/features/new_user/dossier_details_spec.rb +++ b/spec/features/new_user/dossier_details_spec.rb @@ -10,6 +10,10 @@ describe 'Dossier details:' do Flipflop::FeatureSet.current.test!.switch!(:new_dossier_details, true) end + after do + Flipflop::FeatureSet.current.test!.switch!(:new_dossier_details, false) + end + scenario 'the user can see the summary of the dossier status' do visit_dossier dossier diff --git a/spec/features/new_user/invite_spec.rb b/spec/features/new_user/invite_spec.rb index 41d5f0631..ae9de45d6 100644 --- a/spec/features/new_user/invite_spec.rb +++ b/spec/features/new_user/invite_spec.rb @@ -24,11 +24,37 @@ feature 'Invitations' do expect(page).to have_field('Libelle du champ', with: 'Some edited value') end + context 'when inviting someone without an existing account' do + let(:invite) { create(:invite_user, dossier: dossier, user: nil) } + let(:user_password) { 'l33tus3r' } + + 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 }) + + # Create the account + expect(page).to have_current_path(new_user_registration_path, ignore_query: true) + expect(page).to have_field('user_email', with: invite.email) + fill_in 'user_password', with: user_password + click_on 'Créer un compte' + + expect(page).to have_content('lien de confirmation') + + # Confirm the email + user = User.find_by(email: invite.email) + visit Rails.application.routes.url_helpers.user_confirmation_path(confirmation_token: user.confirmation_token) + submit_login_form(user.email, user_password) + + # The user should be redirected to the dossier they was invited on + expect(page).to have_current_path(brouillon_dossier_path(dossier)) + end + end + scenario 'an invited user can see and edit the draft', js: true do visit users_dossiers_invite_path(invite) expect(page).to have_current_path(new_user_session_path) - submit_login_form(invited_user) + submit_login_form(invited_user.email, invited_user.password) expect(page).to have_current_path(brouillon_dossier_path(dossier)) expect(page).to have_no_selector('.button.invite-user-action') @@ -42,7 +68,7 @@ feature 'Invitations' do visit users_dossiers_invite_path(invite) expect(page).to have_current_path(new_user_session_path) - submit_login_form(invited_user) + submit_login_form(invited_user.email, invited_user.password) expect(page).to have_current_path(brouillon_dossier_path(dossier)) expect(page).to have_button('Soumettre le dossier', disabled: true) @@ -68,7 +94,7 @@ feature 'Invitations' do visit users_dossiers_invite_path(invite) expect(page).to have_current_path(new_user_session_path) - submit_login_form(invited_user) + submit_login_form(invited_user.email, invited_user.password) expect(page).to have_current_path(users_dossiers_invite_path(invite)) expect(page).to have_no_selector('.button.invite-user-action') expect(page).to have_text("Dossier nº #{dossier.id}") @@ -92,13 +118,13 @@ feature 'Invitations' do def log_in(user) visit '/' click_on 'Connexion' - submit_login_form(user) + submit_login_form(user.email, user.password) expect(page).to have_current_path(dossiers_path) end - def submit_login_form(user) - fill_in 'user_email', with: user.email - fill_in 'user_password', with: user.password + def submit_login_form(email, password) + fill_in 'user_email', with: email + fill_in 'user_password', with: password click_on 'Se connecter' end