mirror of
https://github.com/we-promise/sure.git
synced 2026-09-09 08:34:26 +00:00
* Support Wise Strong Customer Authentication for balance statements The balance-statement endpoint always 403s because it requires a signed one-time-token challenge (SCA) that Sure never implemented, so every sync silently fell back to /v1/transfers — an outgoing-only endpoint — meaning incoming payments into a Wise balance never synced. Adds a per-item RSA keypair (private key encrypted at rest) that signs the SCA challenge and retries the statement request once, plus a settings UI to generate the keypair and register its public key with Wise. Fixes #3384 * Backfill incoming statements past legacy transfers; fix review nits Backfill: once statements start succeeding for an account that already has legacy /v1/transfers rows, the fetch window was clamped to end the day before the oldest legacy transfer, so the window where incoming payments were actually missing (the recent window transfers already "covered" with outgoing-only data) was never re-fetched. Statement rows in that overlap are now kept when they're incoming and dropped when outgoing, since the legacy transfer rows already account for the outgoing side. Also: replace the inline onclick handler on the SCA public key display with the existing clipboard Stimulus controller (copy button, matching the API key reveal pattern), and correct the regenerate-keypair confirmation text, which implied local regeneration revokes the key with Wise -- it doesn't; the old public key stays valid there until removed manually. * Avoid double-booking internal cross-currency conversions on statement backfill The backfilled statement fetch's outgoing/incoming filter only looked at sign: a positive (credit) statement row was always kept in the legacy overlap window. But a legacy transfer row can itself be incoming for this account when it's the target side of a conversion between two of the profile's own balances -- Wise already fully captures both legs of those via /v1/transfers, unlike genuine external payments. Now an incoming statement row in the overlap window is dropped only when it matches a known incoming legacy transfer's date and amount, so internal conversions aren't duplicated while external incoming payments (no legacy counterpart) still backfill correctly. * Never drop an incoming statement row on a date/amount heuristic The previous fix dropped an incoming statement row in the legacy-overlap window when it matched a known incoming legacy transfer's date and amount, to avoid double-booking internal cross-currency conversions. But nothing short of an endpoint-proven correlation id can tell that apart from a genuine external payment that happens to share the same date and amount -- and silently losing a real transaction is worse than an occasional visible, user-correctable duplicate. Incoming rows are kept unconditionally again. Instead, bound the exposure at the source: the /v1/transfers fallback now stops running for an account as soon as it has a successful statement row, since statements alone cover both directions from then on. This leaves only a narrow, one-time window (the initial backfill of historical internal conversions) where a duplicate can occur, rather than an indefinite one. * Gate the transfer fallback per-account, not per-item legacy_transfer_import_needed? decides whether to fetch /v1/transfers at all, but that decision is profile-wide -- true as soon as any one account still needs the fallback. store_transfers_per_account then merged those transfers into every currency-matching account by currency alone, with no check for whether that specific account had already migrated to statements. A still-legacy account in one currency was enough to make an already-migrated account in the same currency re-absorb a movement its own statements already had, double-booked under a different key. account_transfers is now cleared for any account that already has statement rows, regardless of why the profile-wide fetch ran. * Handle SCA controller errors, corrupted keys, and adapter test coverage - generate_sca_keypair now rescues like every other mutating action in this controller, logging and re-rendering the panel with an error instead of a raw 500 if the update ever raises. - sca_configured? now depends on sca_public_key actually parsing, not just sca_private_key being present, so a corrupted/unparsable stored key (encryption misconfig, manual DB edit) falls back to the "generate a keypair" UI state instead of rendering a public key box around nothing. - Added test/models/provider/wise_adapter_test.rb, which had no coverage at all, to cover build_provider's family/wise_item_id resolution and that sca_private_key actually reaches the constructed Provider::Wise. * Add logging to Wise sync --------- Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
153 lines
6.1 KiB
Ruby
153 lines
6.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
|
|
class WiseItemsControllerTest < ActionDispatch::IntegrationTest
|
|
setup do
|
|
sign_in users(:family_admin)
|
|
SyncJob.stubs(:perform_later)
|
|
@family = families(:dylan_family)
|
|
@wise_item = wise_items(:one)
|
|
|
|
@valid_profiles = [
|
|
{ "id" => "99999999", "type" => "personal", "details" => { "firstName" => "Jane", "lastName" => "Doe" } }
|
|
]
|
|
end
|
|
|
|
# create redirects to select_profiles (Turbo requires a redirect from a standard
|
|
# form submission) — the encrypted token travels via the session, not the response body.
|
|
|
|
test "create redirects to select_profiles and keeps raw token out of the session" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
|
|
|
|
assert_redirected_to select_profiles_wise_items_path
|
|
assert_nil session[:wise_pending_token], "raw API token must not be stored in the session"
|
|
assert session[:wise_pending_encrypted_token].present?
|
|
|
|
follow_redirect!
|
|
assert_select "input[name='encrypted_pending_token']"
|
|
end
|
|
|
|
test "create stores an encrypted token that round-trips to the original value" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
|
|
follow_redirect!
|
|
|
|
encrypted = css_select("input[name='encrypted_pending_token']").first["value"]
|
|
assert encrypted.present?, "hidden encrypted_pending_token field must be present"
|
|
|
|
key = Rails.application.key_generator.generate_key("wise_pending_token", 32)
|
|
decrypted = ActiveSupport::MessageEncryptor.new(key).decrypt_and_verify(encrypted)
|
|
assert_equal "live_token_abc", decrypted
|
|
end
|
|
|
|
test "create redirects to providers on blank token" do
|
|
post wise_items_url, params: { wise_item: { token: "" } }
|
|
assert_redirected_to settings_providers_path
|
|
assert_nil session[:wise_pending_token]
|
|
end
|
|
|
|
test "create redirects to providers when Wise API rejects the token" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).raises(
|
|
Provider::Wise::WiseError.new("unauthorized", :unauthorized)
|
|
)
|
|
|
|
post wise_items_url, params: { wise_item: { token: "bad_token" } }
|
|
assert_redirected_to settings_providers_path
|
|
assert_nil session[:wise_pending_token]
|
|
end
|
|
|
|
# link_profiles reads the encrypted token from the session (set by create) —
|
|
# the client no longer needs to (and cannot) supply or tamper with it via params.
|
|
|
|
test "link_profiles creates WiseItems using the session-held encrypted token" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
|
|
|
|
assert_difference "WiseItem.count", 1 do
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
|
|
end
|
|
|
|
assert_redirected_to settings_providers_path
|
|
assert_equal "live_token_abc", @family.wise_items.find_by!(profile_id: "99999999").token
|
|
assert_nil session[:wise_pending_profiles]
|
|
assert_nil session[:wise_pending_encrypted_token]
|
|
end
|
|
|
|
test "link_profiles applies the pending import_all_history setting to created items" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc", import_all_history: "1" } }
|
|
|
|
assert_difference "WiseItem.count", 1 do
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
|
|
end
|
|
|
|
assert @family.wise_items.find_by!(profile_id: "99999999").import_all_history?
|
|
assert_nil session[:wise_pending_import_all_history]
|
|
end
|
|
|
|
test "link_profiles defaults import_all_history to false when not requested" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
|
|
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
|
|
|
|
assert_not @family.wise_items.find_by!(profile_id: "99999999").import_all_history?
|
|
end
|
|
|
|
test "link_profiles applies import_all_history to every created profile" do
|
|
profiles = [
|
|
{ "id" => "99999999", "type" => "personal", "details" => { "firstName" => "Jane", "lastName" => "Doe" } },
|
|
{ "id" => "88888888", "type" => "business", "details" => { "name" => "Acme" } }
|
|
]
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(profiles)
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc", import_all_history: "1" } }
|
|
|
|
assert_difference "WiseItem.count", 2 do
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999", "88888888" ] }
|
|
end
|
|
|
|
assert @family.wise_items.find_by!(profile_id: "99999999").import_all_history?
|
|
assert @family.wise_items.find_by!(profile_id: "88888888").import_all_history?
|
|
assert_nil session[:wise_pending_import_all_history]
|
|
end
|
|
|
|
test "link_profiles redirects to providers when there is no pending session" do
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
|
|
|
|
assert_redirected_to settings_providers_path
|
|
end
|
|
|
|
test "generate_sca_keypair stores a keypair on the item" do
|
|
assert_nil @wise_item.sca_private_key
|
|
|
|
post generate_sca_keypair_wise_item_url(@wise_item)
|
|
|
|
assert_redirected_to accounts_path
|
|
assert @wise_item.reload.sca_configured?
|
|
end
|
|
|
|
test "generate_sca_keypair replaces a previously generated keypair" do
|
|
@wise_item.generate_sca_keypair!
|
|
previous_key = @wise_item.sca_private_key
|
|
|
|
post generate_sca_keypair_wise_item_url(@wise_item)
|
|
|
|
assert_not_equal previous_key, @wise_item.reload.sca_private_key
|
|
end
|
|
|
|
test "link_profiles redirects to providers when the session token cannot be decrypted" do
|
|
Provider::Wise.any_instance.stubs(:get_profiles).returns(@valid_profiles)
|
|
post wise_items_url, params: { wise_item: { token: "live_token_abc" } }
|
|
|
|
session[:wise_pending_encrypted_token] = "corrupted_garbage_value"
|
|
|
|
post link_profiles_wise_items_url, params: { profile_ids: [ "99999999" ] }
|
|
|
|
assert_redirected_to settings_providers_path
|
|
end
|
|
end
|