Back to Repositories

Testing Plaid Integration Controller Operations in Maybe Finance

This test suite validates the Plaid integration functionality in the Maybe Finance application, focusing on core operations like item creation, deletion, and synchronization. The tests ensure proper handling of Plaid public tokens, access management, and account synchronization processes.

Test Coverage Overview

The test suite provides comprehensive coverage of Plaid item management operations:

  • Public token exchange and item creation
  • Account deletion and cleanup processes
  • Synchronization of Plaid items
  • Flash message validations
  • Proper redirection flows

Implementation Analysis

The testing approach utilizes Minitest’s integration testing capabilities with mock objects for Plaid API interactions. The implementation employs stubbing and expectations to isolate the controller tests from external Plaid API dependencies.

Key patterns include using OpenStruct for response simulation and mock expectations for verifying method calls.

Technical Details

Testing tools and configuration:

  • Minitest framework with ActionDispatch::IntegrationTest
  • Mock objects for Plaid provider isolation
  • Fixtures for user and plaid item data
  • Background job testing with assert_enqueued_with
  • Authentication setup via sign_in helper

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper setup and teardown management
  • Isolation of external dependencies
  • Verification of side effects (job scheduling)
  • Clear assertion patterns
  • Consistent test structure and organization

maybe-finance/maybe

test/controllers/plaid_items_controller_test.rb

            
require "test_helper"
require "ostruct"

class PlaidItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)

    @plaid_provider = mock

    PlaidItem.stubs(:plaid_provider).returns(@plaid_provider)
  end

  test "create" do
    public_token = "public-sandbox-1234"

    @plaid_provider.expects(:exchange_public_token).with(public_token).returns(
      OpenStruct.new(access_token: "access-sandbox-1234", item_id: "item-sandbox-1234")
    )

    assert_difference "PlaidItem.count", 1 do
      post plaid_items_url, params: {
        plaid_item: {
          public_token: public_token,
          metadata: { institution: { name: "Plaid Item Name" } }
        }
      }
    end

    assert_equal "Account linked successfully.  Please wait for accounts to sync.", flash[:notice]
    assert_redirected_to accounts_path
  end

  test "destroy" do
    delete plaid_item_url(plaid_items(:one))

    assert_equal "Accounts scheduled for deletion.", flash[:notice]
    assert_enqueued_with job: DestroyJob
    assert_redirected_to accounts_path
  end

  test "sync" do
    plaid_item = plaid_items(:one)
    PlaidItem.any_instance.expects(:sync_later).once

    post sync_plaid_item_url(plaid_item)

    assert_redirected_to accounts_path
  end
end