Skip to content

Commit

Permalink
Fix exceptions there isn't an organization with a host (#12861)
Browse files Browse the repository at this point in the history
* Fix exception in time zone when there isn't an organization

* Fix exception in force authentication when there isn't an organization

* Fix rubocop offenses
  • Loading branch information
andreslucena committed May 17, 2024
1 parent 981b087 commit 928efaa
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module ForceAuthentication
# Breaks the request lifecycle, if user is not authenticated.
# Otherwise returns.
def ensure_authenticated!
return true unless current_organization.force_users_to_authenticate_before_access_organization
return true unless current_organization&.force_users_to_authenticate_before_access_organization

# Next stop: Check whether auth is ok
unless user_signed_in?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def use_organization_time_zone(&)
#
# Returns a String.
def organization_time_zone
@organization_time_zone ||= current_organization.time_zone
@organization_time_zone ||= current_organization&.time_zone
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
describe "ForceAuthentication" do
let(:organization) { create(:organization, force_users_to_authenticate_before_access_organization:) }

controller do
include Decidim::ForceAuthentication

def current_organization
request.env["decidim.current_organization"]
end

def show
render plain: "Hello world"
end

def locale
render plain: "Locale changed"
end
end

before do
request.env["decidim.current_organization"] = organization
routes.draw do
get "show" => "anonymous#show"
get "locale" => "anonymous#locale"
end
end

context "when the organization is configured to force user authentication" do
let(:force_users_to_authenticate_before_access_organization) { true }

it "forces authentication" do
get :show
expect(response.location).to eq("http://test.host/users/sign_in")
expect(response.body).to have_text("You are being redirected")
expect(response).to have_http_status(:found)
end

it "allows accessing the locale page" do
get :locale
expect(request.path).to eq("/locale")
expect(response.body).to have_text("Locale changed")
expect(response).to have_http_status(:ok)
end
end

context "when the organization is configured to not force user authentication" do
let(:force_users_to_authenticate_before_access_organization) { false }

it "shows the page" do
get :show
expect(request.path).to eq("/show")
expect(response.body).to have_text("Hello world")
expect(response).to have_http_status(:ok)
end
end

context "when there is no organization" do
let(:organization) { nil }

it "shows the page" do
get :show
expect(request.path).to eq("/show")
expect(response.body).to have_text("Hello world")
expect(response).to have_http_status(:ok)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,17 @@ module Decidim
end
end
end

context "when there is no organization" do
let(:time_zone) { utc_time_zone }

before do
request.env["decidim.current_organization"] = nil
end

it "controller uses nil" do
expect(controller.organization_time_zone).to be_nil
end
end
end
end

0 comments on commit 928efaa

Please sign in to comment.