Skip to content

Commit

Permalink
Read session store domain from config
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjay-w committed Jul 16, 2024
1 parent e3d3499 commit cdac9ce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
3 changes: 3 additions & 0 deletions config/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ development: &default
devise_unlock_accounts_after: 1
login_rate_limit: 10
api_rate_limit: 100

session_store_key: '_checkdesk_session_dev'
session_store_domain: 'localhost'
test:
<<: *default
checkdesk_base_url_private: http://api:3000
Expand Down
20 changes: 4 additions & 16 deletions config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
# Be sure to restart your server when you modify this file.
# config/initializers/session_store.rb

# Retrieve the session key name based on the environment using CheckConfig.
# Provide default values specific to each environment.

# Default keys by environment
default_keys = {
production: '_checkdesk_session',
development: '_checkdesk_session_dev',
test: '_checkdesk_session_test'
}

# Get the environment specific session key or default to a predefined value.
cookie_key = CheckConfig.get('session_key', default_keys[Rails.env.to_sym])

# Set the domain for the session cookies based on the environment.
domain_setting = Rails.env.development? ? 'localhost' : '.checkmedia.org'
# Retrieve the session key and domain based on the environment using CheckConfig.
cookie_key = CheckConfig.get('session_store_key', '_checkdesk_session')
domain_setting = CheckConfig.get('session_store_domain', Rails.env.development? ? 'localhost' : '.checkmedia.org')

# Configure the session store with the dynamically obtained session key and domain.
Rails.application.config.session_store :cookie_store, key: cookie_key, domain: domain_setting
37 changes: 10 additions & 27 deletions test/lib/check_session_store_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'test_helper'

class SessionStoreTest < ActiveSupport::TestCase
# Helper to temporarily override Rails.env
def with_environment(env)
original_env = Rails.env
Rails.singleton_class.class_eval do
Expand All @@ -14,40 +13,24 @@ def with_environment(env)
end
end

test "session store configuration in development" do
with_environment('development') do
load Rails.root.join('config/initializers/session_store.rb')
assert_equal ActionDispatch::Session::CookieStore, Rails.application.config.session_store
assert_equal '_checkdesk_session_dev', Rails.application.config.session_options[:key]
assert_equal 'localhost', Rails.application.config.session_options[:domain]
end
end

test "session store configuration in test" do
with_environment('test') do
load Rails.root.join('config/initializers/session_store.rb')
assert_equal ActionDispatch::Session::CookieStore, Rails.application.config.session_store
assert_equal '_checkdesk_session_test', Rails.application.config.session_options[:key]
assert_equal '.checkmedia.org', Rails.application.config.session_options[:domain]
end
end

test "session store configuration in production with default key" do
test "session store configuration with default key and domain when config values are not set" do
with_environment('production') do
load Rails.root.join('config/initializers/session_store.rb')
assert_equal ActionDispatch::Session::CookieStore, Rails.application.config.session_store
assert_equal '_checkdesk_session', Rails.application.config.session_options[:key]
assert_equal '.checkmedia.org', Rails.application.config.session_options[:domain]
stub_configs({ 'session_store_key' => nil, 'session_store_domain' => nil }) do
load Rails.root.join('config/initializers/session_store.rb')
assert_equal ActionDispatch::Session::CookieStore, Rails.application.config.session_store
assert_equal '_checkdesk_session', Rails.application.config.session_options[:key]
assert_equal '.checkmedia.org', Rails.application.config.session_options[:domain]
end
end
end

test "session store configuration in production with overriding key in config" do
test "session store configuration with overriding key and domain in config" do
with_environment('production') do
stub_configs({ 'session_key' => '_checkdesk_session_qa' }) do
stub_configs({ 'session_store_key' => '_checkdesk_session_qa', 'session_store_domain' => 'qa.checkmedia.org' }) do
load Rails.root.join('config/initializers/session_store.rb')
assert_equal ActionDispatch::Session::CookieStore, Rails.application.config.session_store
assert_equal '_checkdesk_session_qa', Rails.application.config.session_options[:key]
assert_equal '.checkmedia.org', Rails.application.config.session_options[:domain]
assert_equal 'qa.checkmedia.org', Rails.application.config.session_options[:domain]
end
end
end
Expand Down

0 comments on commit cdac9ce

Please sign in to comment.