Add support to unlink lunch flow accounts (#318)

* Add support to unlink lunch flow accounts

* add support to link and unlink to any provider

* Fix tests and query

* Let's keep Amr happy about his brand

* Wrap unlink operations in a transaction and add error handling.

* Fix tests

---------

Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
soky srm
2025-11-14 10:42:31 +01:00
committed by GitHub
parent 972648b66d
commit 606e4b1554
20 changed files with 546 additions and 17 deletions

View File

@@ -30,7 +30,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
delete account_url(@account)
assert_redirected_to accounts_path
assert_enqueued_with job: DestroyJob
assert_equal "Account scheduled for deletion", flash[:notice]
assert_equal "Depository account scheduled for deletion", flash[:notice]
end
test "syncing linked account triggers sync for all provider items" do
@@ -57,4 +57,90 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest
post sync_account_url(@account)
assert_redirected_to account_url(@account)
end
test "confirms unlink for linked account" do
plaid_account = plaid_accounts(:one)
AccountProvider.create!(account: @account, provider: plaid_account)
get confirm_unlink_account_url(@account)
assert_response :success
end
test "redirects when confirming unlink for unlinked account" do
get confirm_unlink_account_url(@account)
assert_redirected_to account_url(@account)
assert_equal "Account is not linked to a provider", flash[:alert]
end
test "unlinks linked account successfully with new system" do
plaid_account = plaid_accounts(:one)
AccountProvider.create!(account: @account, provider: plaid_account)
@account.reload
assert @account.linked?
delete unlink_account_url(@account)
@account.reload
assert_not @account.linked?
assert_redirected_to accounts_path
assert_equal "Account unlinked successfully. It is now a manual account.", flash[:notice]
end
test "unlinks linked account successfully with legacy system" do
plaid_account = plaid_accounts(:one)
@account.update!(plaid_account_id: plaid_account.id)
@account.reload
assert @account.linked?
delete unlink_account_url(@account)
@account.reload
assert_not @account.linked?
assert_nil @account.plaid_account_id
assert_redirected_to accounts_path
assert_equal "Account unlinked successfully. It is now a manual account.", flash[:notice]
end
test "redirects when unlinking unlinked account" do
delete unlink_account_url(@account)
assert_redirected_to account_url(@account)
assert_equal "Account is not linked to a provider", flash[:alert]
end
test "unlinked account can be deleted" do
plaid_account = plaid_accounts(:one)
AccountProvider.create!(account: @account, provider: plaid_account)
@account.reload
# Cannot delete while linked
delete account_url(@account)
assert_redirected_to account_url(@account)
assert_equal "Cannot delete a linked account. Please unlink it first.", flash[:alert]
# Unlink the account
delete unlink_account_url(@account)
@account.reload
# Now can delete
delete account_url(@account)
assert_redirected_to accounts_path
assert_enqueued_with job: DestroyJob
assert_equal "Depository account scheduled for deletion", flash[:notice]
end
test "select_provider shows available providers" do
get select_provider_account_url(@account)
assert_response :success
end
test "select_provider redirects for already linked account" do
plaid_account = plaid_accounts(:one)
AccountProvider.create!(account: @account, provider: plaid_account)
get select_provider_account_url(@account)
assert_redirected_to account_url(@account)
assert_equal "Account is already linked to a provider", flash[:alert]
end
end

View File

@@ -46,4 +46,45 @@ class PlaidItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to accounts_path
end
test "select_existing_account redirects when no available plaid accounts" do
account = accounts(:depository)
get select_existing_account_plaid_items_url(account_id: account.id, region: "us")
assert_redirected_to account_path(account)
assert_equal "No available Plaid accounts to link. Please connect a new Plaid account first.", flash[:alert]
end
test "link_existing_account links plaid account to existing account" do
account = accounts(:depository)
# Create a new unlinked plaid_account for testing
plaid_account = PlaidAccount.create!(
plaid_item: plaid_items(:one),
name: "Test Plaid Account",
plaid_id: "test_acc_123",
plaid_type: "depository",
plaid_subtype: "checking",
currency: "USD",
current_balance: 1000,
available_balance: 1000
)
assert_not account.linked?
assert_nil plaid_account.account
assert_nil plaid_account.account_provider
assert_difference "AccountProvider.count", 1 do
post link_existing_account_plaid_items_url, params: {
account_id: account.id,
plaid_account_id: plaid_account.id
}
end
account.reload
assert account.linked?, "Account should be linked after creating AccountProvider"
assert_equal 1, account.account_providers.count
assert_redirected_to accounts_path
assert_equal "Account successfully linked to Plaid", flash[:notice]
end
end

View File

@@ -235,4 +235,12 @@ class SimplefinItemsControllerTest < ActionDispatch::IntegrationTest
@simplefin_item.reload
assert @simplefin_item.scheduled_for_deletion?
end
test "select_existing_account redirects when no available simplefin accounts" do
account = accounts(:depository)
get select_existing_account_simplefin_items_url(account_id: account.id)
assert_redirected_to account_path(account)
assert_equal "No available SimpleFIN accounts to link. Please connect a new SimpleFIN account first.", flash[:alert]
end
end