From cfd1950e17e98f82ae4669206538605f7bade205 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 1 Nov 2018 13:00:35 +0100 Subject: [PATCH] Migrate commencer to new_user --- .../new_user/commencer_controller.rb | 25 +++++ .../new_user/dossiers_controller.rb | 36 ++++++- app/controllers/users/dossiers_controller.rb | 81 --------------- config/routes.rb | 14 +-- .../new_user/commencer_controller_spec.rb | 40 ++++++++ .../new_user/dossiers_controller_spec.rb | 61 +++++++++++- .../users/dossiers_controller_spec.rb | 99 ------------------- .../user_routes_authorization_service_spec.rb | 19 ---- 8 files changed, 166 insertions(+), 209 deletions(-) create mode 100644 app/controllers/new_user/commencer_controller.rb delete mode 100644 app/controllers/users/dossiers_controller.rb create mode 100644 spec/controllers/new_user/commencer_controller_spec.rb delete mode 100644 spec/controllers/users/dossiers_controller_spec.rb diff --git a/app/controllers/new_user/commencer_controller.rb b/app/controllers/new_user/commencer_controller.rb new file mode 100644 index 000000000..b514544d1 --- /dev/null +++ b/app/controllers/new_user/commencer_controller.rb @@ -0,0 +1,25 @@ +module NewUser + class CommencerController < ApplicationController + def commencer_test + procedure = Procedure.brouillons.find_by(path: params[:path]) + + if procedure.present? + redirect_to new_dossier_path(procedure_id: procedure.id, brouillon: true) + else + flash.alert = "La démarche est inconnue." + redirect_to root_path + end + end + + def commencer + procedure = Procedure.publiees.find_by(path: params[:path]) + + if procedure.present? + redirect_to new_dossier_path(procedure_id: procedure.id) + else + flash.alert = "La démarche est inconnue, ou la création de nouveaux dossiers pour cette démarche est terminée." + redirect_to root_path + end + end + end +end diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index a5ae2dbf7..cfd0ee208 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -2,7 +2,8 @@ module NewUser class DossiersController < UserController include DossierHelper - ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche] + SESSION_USER_RETURN_LOCATION = 'user_return_to' + ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new] ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :modifier, :update, :create_commentaire] before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE @@ -11,6 +12,7 @@ module NewUser before_action :forbid_invite_submission!, only: [:update_brouillon] before_action :forbid_closed_submission!, only: [:update_brouillon] before_action :show_demarche_en_test_banner + before_action :store_user_location!, only: :new def index @user_dossiers = current_user.dossiers.includes(:procedure).order_by_updated_at.page(page) @@ -194,7 +196,7 @@ module NewUser redirect_to dossiers_path else flash.notice = "L'instruction de votre dossier a commencé, il n'est plus possible de supprimer votre dossier. Si vous souhaitez annuler l'instruction contactez votre administration par la messagerie de votre dossier." - redirect_to users_dossier_path(dossier) + redirect_to dossier_path(dossier) end end @@ -210,8 +212,38 @@ module NewUser end end + def new + erase_user_location! + + if params[:brouillon] + procedure = Procedure.brouillon.find(params[:procedure_id]) + else + procedure = Procedure.publiees.find(params[:procedure_id]) + end + + dossier = Dossier.create!(procedure: procedure, user: current_user, state: Dossier.states.fetch(:brouillon)) + + if dossier.procedure.for_individual + redirect_to identite_dossier_path(dossier) + else + redirect_to siret_dossier_path(id: dossier.id) + end + rescue ActiveRecord::RecordNotFound + flash.alert = t('errors.messages.procedure_not_found') + + redirect_to url_for dossiers_path + end + private + def store_user_location! + store_location_for(:user, request.fullpath) + end + + def erase_user_location! + session.delete(SESSION_USER_RETURN_LOCATION) + end + def show_demarche_en_test_banner if @dossier.present? && @dossier.procedure.brouillon? flash.now.notice = "Ce dossier est déposé sur une démarche en test. Il sera supprimé lors de la publication de la démarche." diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb deleted file mode 100644 index 914c41146..000000000 --- a/app/controllers/users/dossiers_controller.rb +++ /dev/null @@ -1,81 +0,0 @@ -class Users::DossiersController < UsersController - include SmartListing::Helper::ControllerExtensions - helper SmartListing::Helper - - SESSION_USER_RETURN_LOCATION = 'user_return_to' - - before_action :store_user_location!, only: :new - before_action :authenticate_user!, except: [:commencer, :commencer_test] - - before_action only: [:show] do - authorized_routes? self.class - end - - def commencer_test - procedure = Procedure.brouillons.find_by(path: params[:path]) - - if procedure.present? - redirect_to new_users_dossier_path(procedure_id: procedure.id, brouillon: true) - else - flash.alert = "La démarche est inconnue." - redirect_to root_path - end - end - - def commencer - procedure = Procedure.publiees.find_by(path: params[:path]) - - if procedure.present? - redirect_to new_users_dossier_path(procedure_id: procedure.id) - else - flash.alert = "La démarche est inconnue, ou la création de nouveaux dossiers pour cette démarche est terminée." - redirect_to root_path - end - end - - def new - erase_user_location! - - if params[:brouillon] - procedure = Procedure.brouillon.find(params[:procedure_id]) - else - procedure = Procedure.publiees.find(params[:procedure_id]) - end - - dossier = Dossier.create!(procedure: procedure, user: current_user, state: Dossier.states.fetch(:brouillon)) - - if dossier.procedure.for_individual - redirect_to identite_dossier_path(dossier) - else - redirect_to siret_dossier_path(id: dossier.id) - end - rescue ActiveRecord::RecordNotFound - error_procedure - end - - def self.route_authorization - { - states: [Dossier.states.fetch(:brouillon)] - } - end - - private - - def error_procedure - flash.alert = t('errors.messages.procedure_not_found') - - redirect_to url_for dossiers_path - end - - def facade(id = params[:id]) - DossierFacades.new id, current_user.email - end - - def store_user_location! - store_location_for(:user, request.fullpath) - end - - def erase_user_location! - session.delete(SESSION_USER_RETURN_LOCATION) - end -end diff --git a/config/routes.rb b/config/routes.rb index 3bddb4def..bb0ebce98 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -127,11 +127,6 @@ Rails.application.routes.draw do post ':position/carte', to: 'carte#show', as: :carte end - namespace :commencer do - get '/test/:path' => '/users/dossiers#commencer_test', as: :test - get '/:path' => '/users/dossiers#commencer' - end - get "patron" => "root#patron" get "contact", to: "support#index" @@ -149,7 +144,7 @@ Rails.application.routes.draw do resources :invites, only: [:index, :show] end - resources :dossiers do + resources :dossiers, only: [] do post '/carte/zones' => 'carte#zones' get '/carte' => 'carte#show' post '/carte' => 'carte#save' @@ -267,7 +262,12 @@ Rails.application.routes.draw do # scope module: 'new_user' do - resources :dossiers, only: [:index, :show] do + namespace :commencer do + get '/test/:path', action: 'commencer_test', as: :test + get '/:path', action: 'commencer' + end + + resources :dossiers, only: [:index, :show, :new] do member do get 'identite' patch 'update_identite' diff --git a/spec/controllers/new_user/commencer_controller_spec.rb b/spec/controllers/new_user/commencer_controller_spec.rb new file mode 100644 index 000000000..3bede7f9b --- /dev/null +++ b/spec/controllers/new_user/commencer_controller_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe NewUser::CommencerController, type: :controller do + let(:user) { create(:user) } + let(:procedure) { create(:procedure, :published) } + let(:procedure_id) { procedure.id } + + describe 'GET #commencer' do + subject { get :commencer, params: { path: path } } + let(:path) { procedure.path } + + it { expect(subject.status).to eq 302 } + it { expect(subject).to redirect_to new_dossier_path(procedure_id: procedure.id) } + + context 'when procedure path does not exist' do + let(:path) { 'hello' } + + it { expect(subject).to redirect_to(root_path) } + end + end + + describe 'GET #commencer_test' do + before do + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) + end + + subject { get :commencer_test, params: { path: path } } + let(:procedure) { create(:procedure, :with_path) } + let(:path) { procedure.path } + + it { expect(subject.status).to eq 302 } + it { expect(subject).to redirect_to new_dossier_path(procedure_id: procedure.id, brouillon: true) } + + context 'when procedure path does not exist' do + let(:path) { 'hello' } + + it { expect(subject).to redirect_to(root_path) } + end + end +end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 3e944ee59..b66a5f448 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -843,7 +843,7 @@ describe NewUser::DossiersController, type: :controller do let(:dossier) { create(:dossier, :en_instruction, user: user, autorisation_donnees: true) } it_behaves_like "the dossier can not be deleted" - it { is_expected.to redirect_to(users_dossier_path(dossier)) } + it { is_expected.to redirect_to(dossier_path(dossier)) } end end @@ -855,4 +855,63 @@ describe NewUser::DossiersController, type: :controller do it { is_expected.to redirect_to(root_path) } end end + + describe '#new' do + let(:procedure) { create(:procedure, :published) } + let(:procedure_id) { procedure.id } + + subject { get :new, params: { procedure_id: procedure_id } } + + context 'when params procedure_id is present' do + context 'when procedure_id is valid' do + context 'when user is logged in' do + before do + sign_in user + end + + it { is_expected.to have_http_status(302) } + it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) } + + it { expect { subject }.to change(Dossier, :count).by 1 } + + context 'when procedure is archived' do + let(:procedure) { create(:procedure, :archived) } + + it { is_expected.to redirect_to dossiers_path } + end + end + context 'when user is not logged' do + it { is_expected.to have_http_status(302) } + it { is_expected.to redirect_to new_user_session_path } + end + end + + context 'when procedure_id is not valid' do + let(:procedure_id) { 0 } + + before do + sign_in user + end + + it { is_expected.to redirect_to dossiers_path } + end + + context 'when procedure is not published' do + let(:procedure) { create(:procedure) } + + before do + sign_in user + end + + it { is_expected.to redirect_to dossiers_path } + + context 'and brouillon param is passed' do + subject { get :new, params: { procedure_id: procedure_id, brouillon: true } } + + it { is_expected.to have_http_status(302) } + it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) } + end + end + end + end end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb deleted file mode 100644 index f323d0f3f..000000000 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ /dev/null @@ -1,99 +0,0 @@ -require 'spec_helper' - -describe Users::DossiersController, type: :controller do - let(:user) { create(:user) } - - let(:procedure) { create(:procedure, :published) } - let(:procedure_id) { procedure.id } - let(:dossier) { create(:dossier, user: user, procedure: procedure) } - let(:dossier_id) { dossier.id } - - describe 'GET #new' do - subject { get :new, params: { procedure_id: procedure_id } } - - context 'when params procedure_id is present' do - context 'when procedure_id is valid' do - context 'when user is logged in' do - before do - sign_in user - end - - it { is_expected.to have_http_status(302) } - it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) } - - it { expect { subject }.to change(Dossier, :count).by 1 } - - context 'when procedure is archived' do - let(:procedure) { create(:procedure, :archived) } - - it { is_expected.to redirect_to dossiers_path } - end - end - context 'when user is not logged' do - it { is_expected.to have_http_status(302) } - it { is_expected.to redirect_to new_user_session_path } - end - end - - context 'when procedure_id is not valid' do - let(:procedure_id) { 0 } - - before do - sign_in user - end - - it { is_expected.to redirect_to dossiers_path } - end - - context 'when procedure is not published' do - let(:procedure) { create(:procedure) } - - before do - sign_in user - end - - it { is_expected.to redirect_to dossiers_path } - - context 'and brouillon param is passed' do - subject { get :new, params: { procedure_id: procedure_id, brouillon: true } } - - it { is_expected.to have_http_status(302) } - it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) } - end - end - end - end - - describe 'GET #commencer' do - subject { get :commencer, params: { path: path } } - let(:path) { procedure.path } - - it { expect(subject.status).to eq 302 } - it { expect(subject).to redirect_to new_users_dossier_path(procedure_id: procedure.id) } - - context 'when procedure path does not exist' do - let(:path) { 'hello' } - - it { expect(subject).to redirect_to(root_path) } - end - end - - describe 'GET #commencer_test' do - before do - Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) - end - - subject { get :commencer_test, params: { path: path } } - let(:procedure) { create(:procedure, :with_path) } - let(:path) { procedure.path } - - it { expect(subject.status).to eq 302 } - it { expect(subject).to redirect_to new_users_dossier_path(procedure_id: procedure.id, brouillon: true) } - - context 'when procedure path does not exist' do - let(:path) { 'hello' } - - it { expect(subject).to redirect_to(root_path) } - end - end -end diff --git a/spec/services/user_routes_authorization_service_spec.rb b/spec/services/user_routes_authorization_service_spec.rb index 53a1f948b..a3e0eee5e 100644 --- a/spec/services/user_routes_authorization_service_spec.rb +++ b/spec/services/user_routes_authorization_service_spec.rb @@ -10,25 +10,6 @@ describe UserRoutesAuthorizationService do subject { described_class.authorized_route? controller, dossier } - describe 'Users::DossiersController' do - let(:controller) { Users::DossiersController } - - describe 'brouillon' do - let(:state) { Dossier.states.fetch(:brouillon) } - it { is_expected.to be_truthy } - end - - describe 'en_construction' do - let(:state) { Dossier.states.fetch(:en_construction) } - it { is_expected.to be_falsey } - end - - describe 'accepte' do - let(:state) { Dossier.states.fetch(:accepte) } - it { is_expected.to be_falsey } - end - end - describe 'carte' do let(:controller) { Users::CarteController }