diff --git a/lib/extends/controllers/decidim/initiatives/committee_requests_controller_extends.rb b/lib/extends/controllers/decidim/initiatives/committee_requests_controller_extends.rb index dc3706b08b..960295a967 100644 --- a/lib/extends/controllers/decidim/initiatives/committee_requests_controller_extends.rb +++ b/lib/extends/controllers/decidim/initiatives/committee_requests_controller_extends.rb @@ -4,9 +4,13 @@ module CommitteeRequestsControllerExtends def new return if authorized?(current_user) - authorization_method = Decidim::Verifications::Adapter.from_element(current_initiative.document_number_authorization_handler) - redirect_url = new_initiative_committee_request_path(current_initiative) - redirect_to authorization_method.root_path(redirect_url: redirect_url) + if current_user.nil? + redirect_to decidim.new_user_session_path + else + authorization_method = Decidim::Verifications::Adapter.from_element(current_initiative.document_number_authorization_handler) + redirect_url = new_initiative_committee_request_path(current_initiative) + redirect_to authorization_method.root_path(redirect_url: redirect_url) + end end private diff --git a/spec/controllers/decidim/initiatives/committee_requests_controller_spec.rb b/spec/controllers/decidim/initiatives/committee_requests_controller_spec.rb index 7fc83dafb5..15654f2f0e 100644 --- a/spec/controllers/decidim/initiatives/committee_requests_controller_spec.rb +++ b/spec/controllers/decidim/initiatives/committee_requests_controller_spec.rb @@ -40,6 +40,20 @@ module Initiatives end end + context "when not logged in" do + let(:current_user) { nil } + let(:authorized) { false } + + it "redirects to login page" do + allow(controller).to receive(:new_initiative_committee_request_path).with(initiative).and_return(committee_request_path) + + get :new, params: { initiative_slug: initiative.slug } + + expect(response).to have_http_status(:found) + expect(URI.parse(response.location).path).to eq("/users/sign_in") + end + end + context "when authorized" do let(:authorized) { true }