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:
@@ -10,6 +10,7 @@ module ApplicationCable
|
|||||||
def set_current_user
|
def set_current_user
|
||||||
if session = find_session_by_cookie
|
if session = find_session_by_cookie
|
||||||
account = Account.find_by(external_account_id: request.env["fizzy.external_account_id"])
|
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
|
self.current_user = session.identity.users.find_by!(account: account) if account
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+13
-5
@@ -12,11 +12,19 @@ class Current < ActiveSupport::CurrentAttributes
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def account=(value)
|
def with_account(value)
|
||||||
super(value)
|
@old_account = self.account
|
||||||
|
self.account = value
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
self.account = @old_account
|
||||||
|
end
|
||||||
|
|
||||||
if value.present? && identity.present?
|
def without_account
|
||||||
self.user = identity.users.find_by(account: value)
|
@old_account = self.account
|
||||||
end
|
self.account = nil
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
self.account = @old_account
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -24,10 +24,18 @@ module AccountSlug
|
|||||||
|
|
||||||
# Stash the account's Queenbee ID.
|
# Stash the account's Queenbee ID.
|
||||||
env["fizzy.external_account_id"] = AccountSlug.decode($2)
|
env["fizzy.external_account_id"] = AccountSlug.decode($2)
|
||||||
Current.account = Account.find_by(external_account_id: env["fizzy.external_account_id"])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@app.call env
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,13 @@ class Signup::AccountNameGenerator
|
|||||||
end
|
end
|
||||||
|
|
||||||
def existing_indices
|
def existing_indices
|
||||||
identity.accounts.filter_map do |account|
|
Current.without_account do
|
||||||
if account.name.match?(first_account_name_regex)
|
identity.accounts.filter_map do |account|
|
||||||
1
|
if account.name.match?(first_account_name_regex)
|
||||||
elsif match = account.name.match(nth_account_name_regex)
|
1
|
||||||
match[1].to_i
|
elsif match = account.name.match(nth_account_name_regex)
|
||||||
|
match[1].to_i
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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"
|
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)
|
first_account = Account.create!(external_account_id: "1st", name: account_name)
|
||||||
@identity.users.create!(account: first_account, name: @name)
|
Current.without_account do
|
||||||
@identity.reload
|
@identity.users.create!(account: first_account, name: @name)
|
||||||
|
@identity.reload
|
||||||
|
end
|
||||||
|
|
||||||
account_name = @generator.generate
|
account_name = @generator.generate
|
||||||
assert_equal "Newart's 2nd Fizzy", account_name
|
assert_equal "Newart's 2nd Fizzy", account_name
|
||||||
|
|
||||||
second_account = Account.create!(external_account_id: "2nd", name: account_name)
|
second_account = Account.create!(external_account_id: "2nd", name: account_name)
|
||||||
@identity.users.create!(account: second_account, name: @name)
|
Current.without_account do
|
||||||
@identity.reload
|
@identity.users.create!(account: second_account, name: @name)
|
||||||
|
@identity.reload
|
||||||
|
end
|
||||||
|
|
||||||
account_name = @generator.generate
|
account_name = @generator.generate
|
||||||
assert_equal "Newart's 3rd Fizzy", account_name
|
assert_equal "Newart's 3rd Fizzy", account_name
|
||||||
|
|
||||||
third_account = Account.create!(external_account_id: "3rd", name: account_name)
|
third_account = Account.create!(external_account_id: "3rd", name: account_name)
|
||||||
@identity.users.create!(account: third_account, name: @name)
|
Current.without_account do
|
||||||
@identity.reload
|
@identity.users.create!(account: third_account, name: @name)
|
||||||
|
@identity.reload
|
||||||
|
end
|
||||||
|
|
||||||
account_name = @generator.generate
|
account_name = @generator.generate
|
||||||
assert_equal "Newart's 4th Fizzy", account_name
|
assert_equal "Newart's 4th Fizzy", account_name
|
||||||
|
|
||||||
fourth_account = Account.create!(external_account_id: "4th", name: account_name)
|
fourth_account = Account.create!(external_account_id: "4th", name: account_name)
|
||||||
@identity.users.create!(account: fourth_account, name: @name)
|
Current.without_account do
|
||||||
@identity.reload
|
@identity.users.create!(account: fourth_account, name: @name)
|
||||||
|
@identity.reload
|
||||||
|
end
|
||||||
|
|
||||||
account_name = @generator.generate
|
account_name = @generator.generate
|
||||||
assert_equal "Newart's 5th Fizzy", account_name
|
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
|
test "generate continues from the previous highest index" do
|
||||||
account = Account.create!(external_account_id: "12th", name: "Newart's 12th Fizzy")
|
account = Account.create!(external_account_id: "12th", name: "Newart's 12th Fizzy")
|
||||||
@identity.users.create!(account: account, name: @name)
|
Current.without_account do
|
||||||
@identity.reload
|
@identity.users.create!(account: account, name: @name)
|
||||||
|
@identity.reload
|
||||||
|
end
|
||||||
|
|
||||||
account_name = @generator.generate
|
account_name = @generator.generate
|
||||||
assert_equal "Newart's 13th Fizzy", account_name
|
assert_equal "Newart's 13th Fizzy", account_name
|
||||||
|
|||||||
@@ -30,23 +30,24 @@ class SignupTest < ActiveSupport::TestCase
|
|||||||
|
|
||||||
test "#complete" do
|
test "#complete" do
|
||||||
Account.any_instance.expects(:setup_customer_template).once
|
Account.any_instance.expects(:setup_customer_template).once
|
||||||
|
Current.without_account do
|
||||||
|
signup = Signup.new(
|
||||||
|
full_name: "Kevin",
|
||||||
|
identity: identities(:kevin)
|
||||||
|
)
|
||||||
|
|
||||||
signup = Signup.new(
|
assert signup.complete
|
||||||
full_name: "Kevin",
|
|
||||||
identity: identities(:kevin)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert signup.complete
|
assert signup.account
|
||||||
|
assert signup.user
|
||||||
|
assert_equal "Kevin", signup.user.name
|
||||||
|
|
||||||
assert signup.account
|
signup_invalid = Signup.new(
|
||||||
assert signup.user
|
full_name: "",
|
||||||
assert_equal "Kevin", signup.user.name
|
identity: identities(:kevin)
|
||||||
|
)
|
||||||
signup_invalid = Signup.new(
|
assert_not signup_invalid.complete
|
||||||
full_name: "",
|
assert_not_empty signup_invalid.errors[:full_name]
|
||||||
identity: identities(:kevin)
|
end
|
||||||
)
|
|
||||||
assert_not signup_invalid.complete
|
|
||||||
assert_not_empty signup_invalid.errors[:full_name]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -18,9 +18,12 @@ class Sessions::MenusControllerTest < ActionDispatch::IntegrationTest
|
|||||||
|
|
||||||
test "show with exactly one account" do
|
test "show with exactly one account" do
|
||||||
sign_in_as @identity
|
sign_in_as @identity
|
||||||
@identity.users.delete_all
|
|
||||||
account = Account.create!(external_account_id: 9999991, name: "Test Account")
|
Current.without_account do
|
||||||
@identity.users.create!(account: account, name: "Kevin")
|
@identity.users.delete_all
|
||||||
|
account = Account.create!(external_account_id: 9999991, name: "Test Account")
|
||||||
|
@identity.users.create!(account: account, name: "Kevin")
|
||||||
|
end
|
||||||
|
|
||||||
untenanted do
|
untenanted do
|
||||||
get session_menu_url
|
get session_menu_url
|
||||||
|
|||||||
+26
-23
@@ -13,31 +13,34 @@ class AccountTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
test ".create_with_admin_user creates a new local account" do
|
test ".create_with_admin_user creates a new local account" do
|
||||||
identity = identities(:david)
|
Current.without_account do
|
||||||
account = nil
|
identity = identities(:david)
|
||||||
|
account = nil
|
||||||
|
|
||||||
assert_changes -> { Account.count }, +1 do
|
assert_changes -> { Account.count }, +1 do
|
||||||
assert_changes -> { User.count }, +2 do
|
assert_changes -> { User.count }, +2 do
|
||||||
account = Account.create_with_admin_user(
|
account = Account.create_with_admin_user(
|
||||||
account: {
|
account: {
|
||||||
external_account_id: ActiveRecord::FixtureSet.identify("account-create-with-admin-user-test"),
|
external_account_id: ActiveRecord::FixtureSet.identify("account-create-with-admin-user-test"),
|
||||||
name: "Account Create With Admin"
|
name: "Account Create With Admin"
|
||||||
},
|
},
|
||||||
owner: {
|
owner: {
|
||||||
name: "David",
|
name: "David",
|
||||||
identity: identity
|
identity: identity
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
assert_not_nil account
|
|
||||||
assert account.persisted?
|
|
||||||
assert_equal ActiveRecord::FixtureSet.identify("account-create-with-admin-user-test"), account.external_account_id
|
|
||||||
assert_equal "Account Create With Admin", account.name
|
|
||||||
|
|
||||||
admin = account.users.find_by(role: "admin")
|
assert_not_nil account
|
||||||
assert_equal "David", admin.name
|
assert account.persisted?
|
||||||
assert_equal "david@37signals.com", admin.identity.email_address
|
assert_equal ActiveRecord::FixtureSet.identify("account-create-with-admin-user-test"), account.external_account_id
|
||||||
assert_equal "admin", admin.role
|
assert_equal "Account Create With Admin", account.name
|
||||||
|
|
||||||
|
admin = account.users.find_by(role: "admin")
|
||||||
|
assert_equal "David", admin.name
|
||||||
|
assert_equal "david@37signals.com", admin.identity.email_address
|
||||||
|
assert_equal "admin", admin.role
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,14 +23,16 @@ class IdentityTest < ActiveSupport::TestCase
|
|||||||
identity = identities(:david)
|
identity = identities(:david)
|
||||||
account = accounts(:initech)
|
account = accounts(:initech)
|
||||||
|
|
||||||
assert_difference "User.count", 1 do
|
Current.without_account do
|
||||||
identity.join(account)
|
assert_difference "User.count", 1 do
|
||||||
|
identity.join(account)
|
||||||
|
end
|
||||||
|
|
||||||
|
user = account.users.find_by!(identity: identity)
|
||||||
|
|
||||||
|
assert_not_nil user
|
||||||
|
assert_equal identity, user.identity
|
||||||
|
assert_equal identity.email_address, user.name
|
||||||
end
|
end
|
||||||
|
|
||||||
user = account.users.find_by!(identity: identity)
|
|
||||||
|
|
||||||
assert_not_nil user
|
|
||||||
assert_equal identity, user.identity
|
|
||||||
assert_equal identity.email_address, user.name
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ module SearchTestHelper
|
|||||||
Identity.find_by(email_address: "test@example.com")&.destroy
|
Identity.find_by(email_address: "test@example.com")&.destroy
|
||||||
|
|
||||||
@account = Account.create!(name: "Search Test", external_account_id: ActiveRecord::FixtureSet.identify("search_test"))
|
@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")
|
@identity = Identity.create!(email_address: "test@example.com")
|
||||||
@user = User.create!(name: "Test User", account: @account, identity: @identity)
|
@user = User.create!(name: "Test User", account: @account, identity: @identity)
|
||||||
@board = Board.create!(name: "Test Board", account: @account, creator: @user)
|
@board = Board.create!(name: "Test Board", account: @account, creator: @user)
|
||||||
Current.account = @account
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown_search_test
|
def teardown_search_test
|
||||||
|
|||||||
@@ -41,10 +41,13 @@ module SessionTestHelper
|
|||||||
|
|
||||||
def with_current_user(user)
|
def with_current_user(user)
|
||||||
user = users(user) unless user.is_a? User
|
user = users(user) unless user.is_a? User
|
||||||
Current.session = Session.new(identity: user.identity)
|
@old_session = Current.session
|
||||||
yield
|
begin
|
||||||
ensure
|
Current.session = Session.new(identity: user.identity)
|
||||||
Current.clear_all
|
yield
|
||||||
|
ensure
|
||||||
|
Current.session = @old_session
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def untenanted(&block)
|
def untenanted(&block)
|
||||||
|
|||||||
Reference in New Issue
Block a user