Files
sure/test/models/snaptrade_item_oauth_test.rb
Max Barbare 51c93649da feat(snaptrade): replace device-flow OAuth with authorization-code + PKCE flow (#2747)
* feat(snaptrade): replace device-flow OAuth with authorization-code + PKCE flow

Squashed from 16 commits on snaptrade-oauth-apps for a clean rebase onto
current upstream/main ahead of opening a PR.

* fix(snaptrade): address PR #2747 review feedback on OAuth PKCE flow

- Remove unreachable dead-code guard in import_latest_snaptrade_data
- Guard apply_oauth_tokens! against a malformed payload missing access_token
- Wrap token endpoint network errors in ApiError and retry like data calls
- Remove unused Provider::Snaptrade#revoke_token! instance method
- Preserve return_to/accountable_type through the SnapTrade portal callback
  so the account-linking flow no longer drops users back to accounts_path
- Show the real absolute OAuth callback URL in self-hosted setup instructions
- Refresh brakeman.ignore fingerprint for the connect redirect after the
  return_to/accountable_type params were added

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y8SCCmKX6RphB5E73WSUQQ

* fix(snaptrade): don't retry non-idempotent OAuth/API requests

CodeRabbit flagged that Provider::Snaptrade retried OAuth token
exchanges/refreshes and all API POST/DELETE calls (get_connection_url,
delete_connection) after timeouts/connection failures. If the response
is lost after SnapTrade already consumed a single-use auth code,
rotated the refresh token, or applied a POST/DELETE, replaying the
request either fails with invalid_grant on a token that actually
succeeded, or risks duplicate side effects. Retries are now limited to
GET requests; OAuth token requests and non-GET API calls translate a
network failure straight into an ApiError without replay.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NrrGkgSBEqhjjBmmH1fcXL

* fix(snaptrade): stop querying non-deterministically encrypted token via empty-string compare

CodeRabbit flagged that the syncable scope's where.not(oauth_access_token:
[nil, ""]) re-encrypts "" with a random IV on every query, so the ""
comparison can never match a stored ciphertext and is a silent no-op.
No code path ever persists oauth_access_token as "" (only nil or a real
token via apply_oauth_tokens!), so the exclusion is unnecessary --
narrowed the scope to a plain NULL check, which encryption handles
transparently since nil is never encrypted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NrrGkgSBEqhjjBmmH1fcXL

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-24 22:45:44 +02:00

63 lines
2.4 KiB
Ruby

require "test_helper"
require "ostruct"
class SnaptradeItemOauthTest < ActiveSupport::TestCase
setup do
@item = snaptrade_items(:configured_item)
end
test "syncable scope includes only active items with an access token" do
assert_includes SnaptradeItem.syncable, snaptrade_items(:configured_item)
assert_not_includes SnaptradeItem.syncable, snaptrade_items(:unauthorized_item)
end
test "oauth_configured? and fully_configured? reflect token presence" do
assert @item.oauth_configured?
assert @item.fully_configured?
assert_not snaptrade_items(:unauthorized_item).oauth_configured?
end
test "apply_oauth_tokens! persists rotated tokens and keeps old refresh token when omitted" do
@item.apply_oauth_tokens!(
"access_token" => "new-at", "refresh_token" => "new-rt",
"token_type" => "Bearer", "scope" => "read", "expires_in" => 900
)
assert_equal "new-at", @item.oauth_access_token
assert_equal "new-rt", @item.oauth_refresh_token
assert_in_delta 900, @item.oauth_token_expires_at - Time.current, 10
@item.apply_oauth_tokens!("access_token" => "newer-at", "expires_in" => 900)
assert_equal "newer-at", @item.oauth_access_token
assert_equal "new-rt", @item.oauth_refresh_token, "refresh token must survive rotation that omits it"
end
test "complete_oauth_exchange! stores tokens and marks item good" do
@item.update!(status: :requires_update)
Provider::Snaptrade.expects(:exchange_code)
.with(code: "c0de", redirect_uri: "https://sure.test/cb", code_verifier: "v")
.returns({ "access_token" => "at", "refresh_token" => "rt", "expires_in" => 900 })
@item.complete_oauth_exchange!(code: "c0de", redirect_uri: "https://sure.test/cb", code_verifier: "v")
assert_equal "at", @item.oauth_access_token
assert @item.good?
end
test "snaptrade_provider returns provider only when token present" do
assert_instance_of Provider::Snaptrade, @item.snaptrade_provider
assert_nil snaptrade_items(:unauthorized_item).snaptrade_provider
end
test "destroy revokes tokens best-effort" do
Provider::Snaptrade.expects(:revoke_token).with(token: @item.oauth_refresh_token).returns(true)
@item.destroy!
end
test "destroy proceeds even when revocation raises" do
Provider::Snaptrade.expects(:revoke_token).raises(Provider::Snaptrade::ApiError.new("boom"))
assert_difference "SnaptradeItem.count", -1 do
@item.destroy!
end
end
end