Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
5.4 KB
Raw Blame
require "test_helper"

class Trading212ItemsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)
    @item = trading212_items(:configured_item)
  end

  # === create ===

  test "create redirects to accounts on success" do
    assert_difference "Trading212Item.count", 1 do
      post trading212_items_url, params: {
        trading212_item: {
          api_key: "new_api_key",
          api_secret: "new_api_secret",
          environment: "live",
          currency: "USD"
        }
      }
    end

    assert_redirected_to accounts_path
  end

  test "create uses the settings panel default name when no name is submitted" do
    post trading212_items_url, params: {
      trading212_item: {
        api_key: "new_api_key",
        api_secret: "new_api_secret",
        environment: "live",
        currency: "USD"
      }
    }

    assert_redirected_to accounts_path
    assert_equal I18n.t("settings.providers.trading212_panel.default_account_name"), Trading212Item.order(:created_at).last.name
  end

  test "create renders error on invalid params" do
    assert_no_difference "Trading212Item.count" do
      post trading212_items_url, params: {
        trading212_item: {
          api_key: "",
          api_secret: "",
          environment: "live"
        }
      }, as: :turbo_stream
    end

    assert_turbo_stream status: :unprocessable_entity, action: "replace", target: "trading212-providers-panel"
  end

  # === update ===

  test "update redirects to accounts on success" do
    patch trading212_item_url(@item), params: {
      trading212_item: {
        api_key: "",
        api_secret: "",
        environment: "demo"
      }
    }

    assert_redirected_to accounts_path
    assert_equal "demo", @item.reload.environment
  end

  test "update from the page re-renders the panel in place instead of leaving for accounts" do
    patch trading212_item_url(@item),
          params: { trading212_item: { api_key: "", api_secret: "", environment: "demo" } },
          as: :turbo_stream

    assert_turbo_stream action: "replace", target: "trading212-providers-panel"
    assert_includes response.body, %(id="trading212-providers-panel")
    assert_equal "demo", @item.reload.environment
  end

  # === destroy ===

  test "destroy schedules item for deletion" do
    delete trading212_item_url(@item)

    assert_redirected_to settings_providers_path
    assert @item.reload.scheduled_for_deletion?
  end

  # === sync ===

  test "sync triggers sync job" do
    @item.update!(status: :good)
    assert_enqueued_with(job: SyncJob) do
      post sync_trading212_item_url(@item)
    end

    assert_response :redirect
  end

  # Redirecting back to Bank sync would collapse the open connection row.
  test "sync from the panel re-renders the panel in place" do
    post sync_trading212_item_url(@item, source: "panel"), as: :turbo_stream

    assert_turbo_stream action: "replace", target: "trading212-providers-panel"
    assert_includes response.body, I18n.t("settings.providers.sync_provider_in_progress")
    assert @item.reload.syncing?
  end

  # === complete_account_setup ===

  test "complete_account_setup creates investment account and provider link" do
    t212_account = trading212_accounts(:main_account)

    assert_difference "Account.count", 1 do
      assert_difference "AccountProvider.count", 1 do
        post complete_account_setup_trading212_item_url(@item), params: {
          account_ids: [ t212_account.id ]
        }
      end
    end

    created_account = Account.order(created_at: :desc).first
    assert_equal "Investment", created_account.accountable_type
    assert_redirected_to accounts_path

    t212_account.reload
    assert_equal created_account, t212_account.current_account
  end

  test "complete_account_setup returns alert when no accounts selected" do
    post complete_account_setup_trading212_item_url(@item), params: {
      account_ids: []
    }

    assert_redirected_to setup_accounts_trading212_item_path(@item)
    assert_not_nil flash[:alert]
  end

  # === link_existing_account ===

  test "link_existing_account links manual investment account" do
    account = accounts(:investment)

    assert_difference "AccountProvider.count", 1 do
      post link_existing_account_trading212_items_url, params: {
        account_id: account.id,
        trading212_account_id: trading212_accounts(:main_account).id
      }
    end

    assert_redirected_to account_path(account)
    trading212_accounts(:main_account).reload
    assert_equal account, trading212_accounts(:main_account).current_account
  end

  test "link_existing_account rejects already linked trading212 account" do
    original_account = accounts(:investment)
    t212_account = trading212_accounts(:main_account)
    AccountProvider.create!(account: original_account, provider: t212_account)

    assert_no_difference "AccountProvider.count" do
      post link_existing_account_trading212_items_url, params: {
        account_id: original_account.id,
        trading212_account_id: t212_account.id
      }
    end

    assert_redirected_to account_path(original_account)
  end

  # === select_existing_account ===

  test "select_existing_account renders available trading212 accounts" do
    get select_existing_account_trading212_items_url, params: { account_id: accounts(:investment).id }

    assert_response :success
    assert_includes response.body, trading212_accounts(:main_account).name
  end
end