Clean up some spots where "current account" is ambiguous

Primarily this is in tests (which were caught by temporarily
introducing acts_as_tenant and enabling safety checks), but notably
action cable connections were not working properly, and that's now
fixed.
This commit is contained in:
Mike Dalessio
2025-11-14 10:09:05 -05:00
parent 488c74d99e
commit e3d86bb21e
11 changed files with 117 additions and 76 deletions
@@ -10,6 +10,7 @@ module ApplicationCable
def set_current_user
if session = find_session_by_cookie
account = Account.find_by(external_account_id: request.env["fizzy.external_account_id"])
Current.account = account
self.current_user = session.identity.users.find_by!(account: account) if account
end
end
+13 -5
View File
@@ -12,11 +12,19 @@ class Current < ActiveSupport::CurrentAttributes
end
end
def account=(value)
super(value)
if value.present? && identity.present?
self.user = identity.users.find_by(account: value)
def with_account(value)
@old_account = self.account
self.account = value
yield
ensure
self.account = @old_account
end
def without_account
@old_account = self.account
self.account = nil
yield
ensure
self.account = @old_account
end
end
@@ -24,11 +24,19 @@ module AccountSlug
# Stash the account's Queenbee ID.
env["fizzy.external_account_id"] = AccountSlug.decode($2)
Current.account = Account.find_by(external_account_id: env["fizzy.external_account_id"])
end
if env["fizzy.external_account_id"]
account = Account.find_by(external_account_id: env["fizzy.external_account_id"])
Current.with_account(account) do
@app.call env
end
else
Current.without_account do
@app.call env
end
end
end
end
def self.decode(slug) slug.to_i end
@@ -24,6 +24,7 @@ class Signup::AccountNameGenerator
end
def existing_indices
Current.without_account do
identity.accounts.filter_map do |account|
if account.name.match?(first_account_name_regex)
1
@@ -32,6 +33,7 @@ class Signup::AccountNameGenerator
end
end
end
end
def first_account_name_regex
@first_account_name_regex ||= /\A#{prefix}\s+#{SUFFIX}\Z/i
@@ -12,29 +12,37 @@ class Signup::AccountNameGeneratorTest < ActiveSupport::TestCase
assert_equal "Newart's Fizzy", account_name, "The 1st account doesn't have 1st in the name"
first_account = Account.create!(external_account_id: "1st", name: account_name)
Current.without_account do
@identity.users.create!(account: first_account, name: @name)
@identity.reload
end
account_name = @generator.generate
assert_equal "Newart's 2nd Fizzy", account_name
second_account = Account.create!(external_account_id: "2nd", name: account_name)
Current.without_account do
@identity.users.create!(account: second_account, name: @name)
@identity.reload
end
account_name = @generator.generate
assert_equal "Newart's 3rd Fizzy", account_name
third_account = Account.create!(external_account_id: "3rd", name: account_name)
Current.without_account do
@identity.users.create!(account: third_account, name: @name)
@identity.reload
end
account_name = @generator.generate
assert_equal "Newart's 4th Fizzy", account_name
fourth_account = Account.create!(external_account_id: "4th", name: account_name)
Current.without_account do
@identity.users.create!(account: fourth_account, name: @name)
@identity.reload
end
account_name = @generator.generate
assert_equal "Newart's 5th Fizzy", account_name
@@ -42,8 +50,10 @@ class Signup::AccountNameGeneratorTest < ActiveSupport::TestCase
test "generate continues from the previous highest index" do
account = Account.create!(external_account_id: "12th", name: "Newart's 12th Fizzy")
Current.without_account do
@identity.users.create!(account: account, name: @name)
@identity.reload
end
account_name = @generator.generate
assert_equal "Newart's 13th Fizzy", account_name
+2 -1
View File
@@ -30,7 +30,7 @@ class SignupTest < ActiveSupport::TestCase
test "#complete" do
Account.any_instance.expects(:setup_customer_template).once
Current.without_account do
signup = Signup.new(
full_name: "Kevin",
identity: identities(:kevin)
@@ -49,4 +49,5 @@ class SignupTest < ActiveSupport::TestCase
assert_not signup_invalid.complete
assert_not_empty signup_invalid.errors[:full_name]
end
end
end
@@ -18,9 +18,12 @@ class Sessions::MenusControllerTest < ActionDispatch::IntegrationTest
test "show with exactly one account" do
sign_in_as @identity
Current.without_account do
@identity.users.delete_all
account = Account.create!(external_account_id: 9999991, name: "Test Account")
@identity.users.create!(account: account, name: "Kevin")
end
untenanted do
get session_menu_url
+3
View File
@@ -13,6 +13,7 @@ class AccountTest < ActiveSupport::TestCase
end
test ".create_with_admin_user creates a new local account" do
Current.without_account do
identity = identities(:david)
account = nil
@@ -30,6 +31,7 @@ class AccountTest < ActiveSupport::TestCase
)
end
end
assert_not_nil account
assert account.persisted?
assert_equal ActiveRecord::FixtureSet.identify("account-create-with-admin-user-test"), account.external_account_id
@@ -40,4 +42,5 @@ class AccountTest < ActiveSupport::TestCase
assert_equal "david@37signals.com", admin.identity.email_address
assert_equal "admin", admin.role
end
end
end
+2
View File
@@ -23,6 +23,7 @@ class IdentityTest < ActiveSupport::TestCase
identity = identities(:david)
account = accounts(:initech)
Current.without_account do
assert_difference "User.count", 1 do
identity.join(account)
end
@@ -33,4 +34,5 @@ class IdentityTest < ActiveSupport::TestCase
assert_equal identity, user.identity
assert_equal identity.email_address, user.name
end
end
end
+1 -1
View File
@@ -14,10 +14,10 @@ module SearchTestHelper
Identity.find_by(email_address: "test@example.com")&.destroy
@account = Account.create!(name: "Search Test", external_account_id: ActiveRecord::FixtureSet.identify("search_test"))
Current.account = @account
@identity = Identity.create!(email_address: "test@example.com")
@user = User.create!(name: "Test User", account: @account, identity: @identity)
@board = Board.create!(name: "Test Board", account: @account, creator: @user)
Current.account = @account
end
def teardown_search_test
+4 -1
View File
@@ -41,10 +41,13 @@ module SessionTestHelper
def with_current_user(user)
user = users(user) unless user.is_a? User
@old_session = Current.session
begin
Current.session = Session.new(identity: user.identity)
yield
ensure
Current.clear_all
Current.session = @old_session
end
end
def untenanted(&block)