Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
8.0 KB
Raw Blame
require "application_system_test_case"

class ImportsTest < ApplicationSystemTestCase
  include ActiveJob::TestHelper

  setup do
    sign_in @user = users(:family_admin)

    # Trade securities will be imported as "offline" tickers
    Security.stubs(:provider).returns(nil)
  end

  test "transaction import" do
    visit new_import_path

    click_on "Raw Data"
    click_on "Import transactions"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/transactions.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    select "Date", from: "import[date_col_label]"
    select "YYYY-MM-DD", from: "import[date_format]"
    select "Amount", from: "import[amount_col_label]"
    select "Account", from: "import[account_col_label]"
    select "Name", from: "import[name_col_label]"
    select "Category", from: "import[category_col_label]"
    select "Tags", from: "import[tags_col_label]"
    select "Notes", from: "import[notes_col_label]"

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your categories"
    click_on "Next"

    assert_selector "h1", text: "Assign your tags"
    click_on "Next"

    assert_selector "h1", text: "Assign your accounts"
    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "trade import" do
    visit new_import_path

    click_on "Raw Data"
    click_on "Import investments"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/trades.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    select "date", from: "import[date_col_label]"
    select "YYYY-MM-DD", from: "import[date_format]"
    select "qty", from: "import[qty_col_label]"
    select "ticker", from: "import[ticker_col_label]"
    select "price", from: "import[price_col_label]"
    select "account", from: "import[account_col_label]"

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your accounts"
    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "account import" do
    visit new_import_path

    click_on "Raw Data"
    click_on "Import accounts"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/accounts.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    select "type", from: "import[entity_type_col_label]"
    select "name", from: "import[name_col_label]"
    select "amount", from: "import[amount_col_label]"

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your account types"

    all("form").each do |form|
      within(form) do
        select = form.find("select")
        select "Depository", from: select["id"]
        sleep 0.5
      end
    end

    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "mint import" do
    visit new_import_path

    # Pending CSV-style imports default the dialog to the Raw Data tab; Mint lives under Financial Tools.
    click_on "Financial Tools"
    click_on "Import from Mint"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/mint.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your categories"
    click_on "Next"

    assert_selector "h1", text: "Assign your tags"
    click_on "Next"

    assert_selector "h1", text: "Assign your accounts"
    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "actual import" do
    visit new_import_path

    # Pending CSV-style imports default the dialog to the Raw Data tab; Actual lives under Financial Tools.
    click_on "Financial Tools"
    click_on "Import from Actual Budget"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/actual.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your categories"
    click_on "Next"

    assert_selector "h1", text: "Assign your accounts"
    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "ynab import" do
    visit new_import_path

    # Pending CSV-style imports default the dialog to the Raw Data tab; YNAB lives under Financial Tools.
    click_on "Financial Tools"
    click_on "Import from YNAB"

    within_testid("import-tabs") do
      click_on "Copy & Paste"
    end

    fill_in "import[raw_file_str]", with: file_fixture("imports/ynab.csv").read

    find_field("import[raw_file_str]").find(:xpath, "./ancestor::form").click_button("Upload CSV")

    click_on "Apply configuration"

    click_on "Next step"

    assert_selector "h1", text: "Assign your categories"
    click_on "Next"

    assert_selector "h1", text: "Assign your accounts"
    click_on "Next"

    click_on "Publish import"

    assert_text "Import in progress"

    perform_enqueued_jobs

    click_on "Check status"

    assert_text "Import successful"

    click_on "Back to dashboard"
  end

  test "monarch import" do
    visit new_import_path

    click_on "Financial Tools"
    click_on "Import from Monarch"

    attach_file "import[balances_file]", file_fixture("imports/monarch_balances.csv")
    attach_file "import[transactions_file]", file_fixture("imports/monarch_transactions.csv")
    click_on "Upload exports"

    assert_selector "h1", text: "Check each Monarch account"
    find("select[aria-label='Type of Card']").select("Loan")
    click_toggle "Old Savings is open"
    assert_selector "td", text: "Closed"
    click_toggle "Transactions for every account"
    assert_no_selector "input[type=checkbox][data-checkbox-group=transactions]:checked", visible: :all
    click_on "Review import"

    # The account choices form opts out of Turbo, so this is a full page load.
    # Wait for the new URL before reading any text: Chrome can fail to read the
    # outgoing page's h1 with an error Capybara does not retry.
    assert_current_path import_path(MonarchImport.where(family: @user.family).sole)
    assert_selector "h1", text: "Confirm your import data"
    click_on "Publish import"

    assert_text "Import in progress"
    perform_enqueued_jobs
    click_on "Check status"
    assert_text "Import successful"

    accounts = @user.family.accounts
    assert_equal "Loan", accounts.find_by!(name: "Card").accountable_type
    assert accounts.find_by!(name: "Old Savings").disabled?
    assert_equal 0, accounts.where(name: %w[Checking Card]).joins(:entries).where(entries: { entryable_type: "Transaction" }).count
  end

  private
    # DS::Toggle hides its checkbox; people click the switch, its label.
    def click_toggle(aria_label)
      find("input[aria-label='#{aria_label}']", visible: :all).find(:xpath, "following-sibling::label").click
    end
end