mirror of
https://github.com/we-promise/sure.git
synced 2026-05-12 23:25:00 +00:00
fix(mercury): support named multiple API connections (#1627)
* fix(mercury): support named multiple connections * fix(mercury): address multi-connection review feedback * fix(mercury): localize connection labels * fix(mercury): strip API tokens before provider calls * test(mercury): localize provider config assertions * fix(mercury): address multi-connection review * refactor(mercury): simplify connection selection failure
This commit is contained in:
@@ -44,6 +44,34 @@ class MercuryAccountTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "same account_id can be linked under different mercury_items in the same family" do
|
||||
item_a_2 = MercuryItem.create!(
|
||||
family: @family_a,
|
||||
name: "Family A Second Mercury",
|
||||
token: "token_a_2",
|
||||
base_url: "https://api-sandbox.mercury.com/api/v1",
|
||||
status: "good"
|
||||
)
|
||||
|
||||
MercuryAccount.create!(
|
||||
mercury_item: @item_a,
|
||||
account_id: "shared_merc_acc_1",
|
||||
name: "Checking",
|
||||
currency: "USD",
|
||||
current_balance: 5000
|
||||
)
|
||||
|
||||
assert_difference "MercuryAccount.count", 1 do
|
||||
MercuryAccount.create!(
|
||||
mercury_item: item_a_2,
|
||||
account_id: "shared_merc_acc_1",
|
||||
name: "Checking",
|
||||
currency: "USD",
|
||||
current_balance: 5000
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "same account_id cannot appear twice under the same mercury_item" do
|
||||
MercuryAccount.create!(
|
||||
mercury_item: @item_a,
|
||||
|
||||
@@ -22,6 +22,11 @@ class MercuryItemTest < ActiveSupport::TestCase
|
||||
assert_not @mercury_item.credentials_configured?
|
||||
end
|
||||
|
||||
test "credentials_configured returns false when token is whitespace" do
|
||||
@mercury_item.token = " "
|
||||
assert_not @mercury_item.credentials_configured?
|
||||
end
|
||||
|
||||
test "effective_base_url returns custom url when set" do
|
||||
assert_equal "https://api-sandbox.mercury.com/api/v1", @mercury_item.effective_base_url
|
||||
end
|
||||
@@ -42,6 +47,42 @@ class MercuryItemTest < ActiveSupport::TestCase
|
||||
assert_nil @mercury_item.mercury_provider
|
||||
end
|
||||
|
||||
test "family credential check ignores blank and scheduled for deletion items" do
|
||||
family = families(:empty)
|
||||
blank_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Blank Mercury",
|
||||
token: "temporary_token",
|
||||
base_url: "https://api-sandbox.mercury.com/api/v1"
|
||||
)
|
||||
blank_item.update_column(:token, "")
|
||||
|
||||
whitespace_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Whitespace Mercury",
|
||||
token: "temporary_token",
|
||||
base_url: "https://api-sandbox.mercury.com/api/v1"
|
||||
)
|
||||
whitespace_item.update_column(:token, " ")
|
||||
|
||||
deleted_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Deleted Mercury",
|
||||
token: "deleted_token",
|
||||
base_url: "https://api-sandbox.mercury.com/api/v1",
|
||||
scheduled_for_deletion: true
|
||||
)
|
||||
|
||||
refute family.has_mercury_credentials?
|
||||
|
||||
whitespace_item.update_column(:token, "configured_token")
|
||||
assert family.has_mercury_credentials?
|
||||
|
||||
whitespace_item.update_column(:token, " ")
|
||||
deleted_item.update!(scheduled_for_deletion: false)
|
||||
assert family.has_mercury_credentials?
|
||||
end
|
||||
|
||||
test "syncer returns MercuryItem::Syncer instance" do
|
||||
syncer = @mercury_item.send(:syncer)
|
||||
assert_instance_of MercuryItem::Syncer, syncer
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require "uri"
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class Provider::MercuryAdapterTest < ActiveSupport::TestCase
|
||||
@@ -9,17 +11,59 @@ class Provider::MercuryAdapterTest < ActiveSupport::TestCase
|
||||
assert_not_includes Provider::MercuryAdapter.supported_account_types, "Investment"
|
||||
end
|
||||
|
||||
test "returns connection configs for any family" do
|
||||
test "returns fallback connection config when no credentials exist yet" do
|
||||
# Mercury is a per-family provider - any family can connect
|
||||
family = families(:dylan_family)
|
||||
family = families(:empty)
|
||||
configs = Provider::MercuryAdapter.connection_configs(family: family)
|
||||
|
||||
assert_equal 1, configs.length
|
||||
assert_equal "mercury", configs.first[:key]
|
||||
assert_equal "Mercury", configs.first[:name]
|
||||
assert_equal I18n.t("mercury_items.provider_connection.default_name"), configs.first[:name]
|
||||
assert configs.first[:can_connect]
|
||||
end
|
||||
|
||||
test "returns one connection config per credentialed mercury item" do
|
||||
family = families(:dylan_family)
|
||||
first_item = mercury_items(:one)
|
||||
second_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Business Mercury",
|
||||
token: "second_mercury_token",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
)
|
||||
|
||||
configs = Provider::MercuryAdapter.connection_configs(family: family)
|
||||
|
||||
assert_equal 2, configs.length
|
||||
assert_equal [ "mercury_#{second_item.id}", "mercury_#{first_item.id}" ], configs.map { |config| config[:key] }
|
||||
assert_equal [
|
||||
I18n.t("mercury_items.provider_connection.name", name: second_item.name),
|
||||
I18n.t("mercury_items.provider_connection.name", name: first_item.name)
|
||||
], configs.map { |config| config[:name] }
|
||||
|
||||
new_account_uri = URI.parse(configs.first[:new_account_path].call("Depository", "/accounts"))
|
||||
assert_equal "/mercury_items/select_accounts", new_account_uri.path
|
||||
assert_includes new_account_uri.query, "mercury_item_id=#{second_item.id}"
|
||||
|
||||
existing_account_uri = URI.parse(configs.first[:existing_account_path].call(accounts(:depository).id))
|
||||
assert_equal "/mercury_items/select_existing_account", existing_account_uri.path
|
||||
assert_includes existing_account_uri.query, "mercury_item_id=#{second_item.id}"
|
||||
end
|
||||
|
||||
test "connection configs ignore items with whitespace-only tokens" do
|
||||
family = families(:dylan_family)
|
||||
MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Blank Mercury",
|
||||
token: "temporary_token",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
).update_column(:token, " ")
|
||||
|
||||
configs = Provider::MercuryAdapter.connection_configs(family: family)
|
||||
|
||||
assert_equal [ "mercury_#{mercury_items(:one).id}" ], configs.map { |config| config[:key] }
|
||||
end
|
||||
|
||||
test "build_provider returns nil when family is nil" do
|
||||
assert_nil Provider::MercuryAdapter.build_provider(family: nil)
|
||||
end
|
||||
@@ -35,4 +79,59 @@ class Provider::MercuryAdapterTest < ActiveSupport::TestCase
|
||||
|
||||
assert_instance_of Provider::Mercury, provider
|
||||
end
|
||||
|
||||
test "build_provider uses explicit mercury item credentials" do
|
||||
family = families(:dylan_family)
|
||||
second_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Business Mercury",
|
||||
token: "second_mercury_token",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
)
|
||||
|
||||
provider = Provider::MercuryAdapter.build_provider(family: family, mercury_item_id: second_item.id)
|
||||
|
||||
assert_instance_of Provider::Mercury, provider
|
||||
assert_equal "second_mercury_token", provider.token
|
||||
assert_equal "https://api.mercury.com/api/v1", provider.base_url
|
||||
end
|
||||
|
||||
test "build_provider strips surrounding token whitespace" do
|
||||
family = families(:dylan_family)
|
||||
second_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Business Mercury",
|
||||
token: " second_mercury_token \n",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
)
|
||||
|
||||
provider = Provider::MercuryAdapter.build_provider(family: family, mercury_item_id: second_item.id)
|
||||
|
||||
assert_equal "second_mercury_token", provider.token
|
||||
end
|
||||
|
||||
test "build_provider refuses mercury items outside the family" do
|
||||
family = families(:dylan_family)
|
||||
other_item = MercuryItem.create!(
|
||||
family: families(:empty),
|
||||
name: "Other Mercury",
|
||||
token: "other_mercury_token",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
)
|
||||
|
||||
assert_nil Provider::MercuryAdapter.build_provider(family: family, mercury_item_id: other_item.id)
|
||||
end
|
||||
|
||||
test "build_provider refuses explicit mercury item without usable credentials" do
|
||||
family = families(:dylan_family)
|
||||
blank_item = MercuryItem.create!(
|
||||
family: family,
|
||||
name: "Blank Mercury",
|
||||
token: "temporary_token",
|
||||
base_url: "https://api.mercury.com/api/v1"
|
||||
)
|
||||
blank_item.update_column(:token, " ")
|
||||
|
||||
assert_nil Provider::MercuryAdapter.build_provider(family: family, mercury_item_id: blank_item.id)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user