Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 'Fix exceptions there isn't an organization with a host' to v0.28 #12861

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading