diff --git a/test/controllers/concerns/current_timezone_test.rb b/test/controllers/concerns/current_timezone_test.rb new file mode 100644 index 000000000..4f0d583fe --- /dev/null +++ b/test/controllers/concerns/current_timezone_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +class CurrentTimezoneTest < ActionDispatch::IntegrationTest + test "includes the timezone cookie in the ETag" do + cookies[:timezone] = "America/New_York" + get user_avatar_path(users(:kevin)) + etag = response.headers.fetch("ETag") + + get user_avatar_path(users(:kevin)), headers: { "If-None-Match" => etag } + assert_equal 304, response.status + + cookies[:timezone] = "America/Los_Angeles" + get user_avatar_path(users(:kevin)), headers: { "If-None-Match" => etag } + assert_response :success + assert_not_equal etag, response.headers.fetch("ETag") + end +end diff --git a/test/middleware/account_slug_extractor_test.rb b/test/middleware/account_slug_extractor_test.rb new file mode 100644 index 000000000..79a1c6ab9 --- /dev/null +++ b/test/middleware/account_slug_extractor_test.rb @@ -0,0 +1,65 @@ +require "test_helper" +require "rack/mock" + +class AccountSlugExtractorTest < ActiveSupport::TestCase + test "moves account prefix from PATH_INFO to SCRIPT_NAME" do + account = accounts(:initech) + slug = AccountSlug.encode(account.external_account_id) + + captured = call_with_env "/#{slug}/boards" + + assert_equal "/#{slug}", captured.fetch(:script_name) + assert_equal "/boards", captured.fetch(:path_info) + assert_equal account.external_account_id, captured.fetch(:external_account_id) + assert_equal account, captured.fetch(:current_account) + end + + test "treats a bare account prefix as the root path" do + account = accounts(:initech) + slug = AccountSlug.encode(account.external_account_id) + + captured = call_with_env "/#{slug}" + + assert_equal "/#{slug}", captured.fetch(:script_name) + assert_equal "/", captured.fetch(:path_info) + end + + test "detects the account prefix when already in SCRIPT_NAME" do + account = accounts(:initech) + slug = AccountSlug.encode(account.external_account_id) + + captured = call_with_env "/boards", "SCRIPT_NAME" => "/#{slug}" + + assert_equal "/#{slug}", captured.fetch(:script_name) + assert_equal "/boards", captured.fetch(:path_info) + assert_equal account, captured.fetch(:current_account) + end + + test "clears Current.account when no account prefix is present" do + captured = call_with_env "/boards" + + assert_equal "", captured.fetch(:script_name) + assert_equal "/boards", captured.fetch(:path_info) + assert_nil captured.fetch(:external_account_id) + assert_nil captured.fetch(:current_account) + end + + private + def call_with_env(path, extra_env = {}) + captured = {} + extra_env = { "action_dispatch.routes" => Rails.application.routes }.merge(extra_env) + + app = ->(env) do + captured[:script_name] = env["SCRIPT_NAME"] + captured[:path_info] = env["PATH_INFO"] + captured[:external_account_id] = env["fizzy.external_account_id"] + captured[:current_account] = Current.account + [ 200, {}, [ "ok" ] ] + end + + middleware = AccountSlug::Extractor.new(app) + middleware.call Rack::MockRequest.env_for(path, extra_env.merge(method: "GET")) + + captured + end +end