2017-11-26 14:42:05 -05:00
|
|
|
package handlers_test
|
|
|
|
|
|
|
|
import (
|
2017-11-29 19:59:02 -05:00
|
|
|
"fmt"
|
2017-12-03 06:38:22 -05:00
|
|
|
"github.com/aclindsa/moneygo/internal/models"
|
2017-11-26 14:42:05 -05:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func importOFX(client *http.Client, accountid int64, filename string) error {
|
|
|
|
return uploadFile(client, filename, "/v1/accounts/"+strconv.FormatInt(accountid, 10)+"/imports/ofxfile")
|
|
|
|
}
|
|
|
|
|
2017-11-28 20:14:38 -05:00
|
|
|
func TestImportOFXChecking(t *testing.T) {
|
2017-11-26 14:42:05 -05:00
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
// Ensure there's only one USD currency
|
|
|
|
oldDefault, err := getSecurity(d.clients[0], d.users[0].DefaultCurrency)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching default security: %s\n", err)
|
|
|
|
}
|
|
|
|
d.users[0].DefaultCurrency = d.securities[0].SecurityId
|
|
|
|
if _, err := updateUser(d.clients[0], &d.users[0]); err != nil {
|
|
|
|
t.Fatalf("Error updating user: %s\n", err)
|
|
|
|
}
|
|
|
|
if err := deleteSecurity(d.clients[0], oldDefault); err != nil {
|
|
|
|
t.Fatalf("Error removing default security: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import and ensure it didn't return a nasty error code
|
|
|
|
if err = importOFX(d.clients[0], d.accounts[1].AccountId, "handlers_testdata/checking_20171126.ofx"); err != nil {
|
|
|
|
t.Fatalf("Error importing OFX: %s\n", err)
|
|
|
|
}
|
2017-11-26 21:01:26 -05:00
|
|
|
accountBalanceHelper(t, d.clients[0], &d.accounts[1], "2493.19")
|
|
|
|
|
2017-11-26 14:42:05 -05:00
|
|
|
if err = importOFX(d.clients[0], d.accounts[1].AccountId, "handlers_testdata/checking_20171129.ofx"); err != nil {
|
|
|
|
t.Fatalf("Error importing OFX: %s\n", err)
|
|
|
|
}
|
2017-11-26 21:01:26 -05:00
|
|
|
accountBalanceHelper(t, d.clients[0], &d.accounts[1], "5336.27")
|
2017-11-26 14:42:05 -05:00
|
|
|
})
|
|
|
|
}
|
2017-11-28 20:14:38 -05:00
|
|
|
|
|
|
|
func TestImportOFXCreditCard(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
// Ensure there's only one USD currency
|
|
|
|
oldDefault, err := getSecurity(d.clients[0], d.users[0].DefaultCurrency)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching default security: %s\n", err)
|
|
|
|
}
|
|
|
|
d.users[0].DefaultCurrency = d.securities[0].SecurityId
|
|
|
|
if _, err := updateUser(d.clients[0], &d.users[0]); err != nil {
|
|
|
|
t.Fatalf("Error updating user: %s\n", err)
|
|
|
|
}
|
|
|
|
if err := deleteSecurity(d.clients[0], oldDefault); err != nil {
|
|
|
|
t.Fatalf("Error removing default security: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import and ensure it didn't return a nasty error code
|
|
|
|
if err = importOFX(d.clients[0], d.accounts[7].AccountId, "handlers_testdata/creditcard.ofx"); err != nil {
|
|
|
|
t.Fatalf("Error importing OFX: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], &d.accounts[7], "-4.49")
|
|
|
|
})
|
|
|
|
}
|
2017-11-28 21:25:50 -05:00
|
|
|
|
2017-12-03 06:38:22 -05:00
|
|
|
func findSecurity(client *http.Client, symbol string, tipe models.SecurityType) (*models.Security, error) {
|
2017-11-29 19:59:02 -05:00
|
|
|
securities, err := getSecurities(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, security := range *securities.Securities {
|
|
|
|
if security.Symbol == symbol && security.Type == tipe {
|
|
|
|
return security, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to find security: \"%s\"", symbol)
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:55:25 -05:00
|
|
|
func findAccount(client *http.Client, name string, tipe models.AccountType, securityid int64) (*models.Account, error) {
|
2017-11-29 19:59:02 -05:00
|
|
|
accounts, err := getAccounts(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, account := range *accounts.Accounts {
|
|
|
|
if account.Name == name && account.Type == tipe && account.SecurityId == securityid {
|
2017-12-07 20:47:55 -05:00
|
|
|
return account, nil
|
2017-11-29 19:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to find account: \"%s\"", name)
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:25:50 -05:00
|
|
|
func TestImportOFX401kMutualFunds(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
// Ensure there's only one USD currency
|
|
|
|
oldDefault, err := getSecurity(d.clients[0], d.users[0].DefaultCurrency)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching default security: %s\n", err)
|
|
|
|
}
|
|
|
|
d.users[0].DefaultCurrency = d.securities[0].SecurityId
|
|
|
|
if _, err := updateUser(d.clients[0], &d.users[0]); err != nil {
|
|
|
|
t.Fatalf("Error updating user: %s\n", err)
|
|
|
|
}
|
|
|
|
if err := deleteSecurity(d.clients[0], oldDefault); err != nil {
|
|
|
|
t.Fatalf("Error removing default security: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:55:25 -05:00
|
|
|
account := &models.Account{
|
2017-11-28 21:25:50 -05:00
|
|
|
SecurityId: d.securities[0].SecurityId,
|
|
|
|
UserId: d.users[0].UserId,
|
|
|
|
ParentAccountId: -1,
|
2017-12-04 05:55:25 -05:00
|
|
|
Type: models.Investment,
|
2017-11-28 21:25:50 -05:00
|
|
|
Name: "401k",
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err = createAccount(d.clients[0], account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating 401k account: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import and ensure it didn't return a nasty error code
|
|
|
|
if err = importOFX(d.clients[0], account.AccountId, "handlers_testdata/401k_mutualfunds.ofx"); err != nil {
|
|
|
|
t.Fatalf("Error importing OFX: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], account, "-192.10")
|
2017-11-29 19:59:02 -05:00
|
|
|
|
|
|
|
// Make sure the security was created and that the trading account has
|
|
|
|
// the right value
|
2017-12-03 06:38:22 -05:00
|
|
|
security, err := findSecurity(d.clients[0], "VANGUARD TARGET 2045", models.Stock)
|
2017-11-29 19:59:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding VANGUARD TARGET 2045 security: %s\n", err)
|
|
|
|
}
|
2017-12-04 05:55:25 -05:00
|
|
|
tradingaccount, err := findAccount(d.clients[0], "VANGUARD TARGET 2045", models.Trading, security.SecurityId)
|
2017-11-29 19:59:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding VANGUARD TARGET 2045 trading account: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], tradingaccount, "-3.35400")
|
|
|
|
|
|
|
|
// Ensure actual holding account was created and in the correct place
|
2017-12-04 05:55:25 -05:00
|
|
|
investmentaccount, err := findAccount(d.clients[0], "VANGUARD TARGET 2045", models.Investment, security.SecurityId)
|
2017-11-29 19:59:02 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding VANGUARD TARGET 2045 investment account: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], investmentaccount, "3.35400")
|
|
|
|
if investmentaccount.ParentAccountId != account.AccountId {
|
|
|
|
t.Errorf("Expected imported security account to be child of investment account it's imported into\n")
|
|
|
|
}
|
2017-11-28 21:25:50 -05:00
|
|
|
})
|
|
|
|
}
|
2017-12-01 21:21:55 -05:00
|
|
|
|
|
|
|
func TestImportOFXBrokerage(t *testing.T) {
|
|
|
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
|
|
|
// Ensure there's only one USD currency
|
|
|
|
oldDefault, err := getSecurity(d.clients[0], d.users[0].DefaultCurrency)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error fetching default security: %s\n", err)
|
|
|
|
}
|
|
|
|
d.users[0].DefaultCurrency = d.securities[0].SecurityId
|
|
|
|
if _, err := updateUser(d.clients[0], &d.users[0]); err != nil {
|
|
|
|
t.Fatalf("Error updating user: %s\n", err)
|
|
|
|
}
|
|
|
|
if err := deleteSecurity(d.clients[0], oldDefault); err != nil {
|
|
|
|
t.Fatalf("Error removing default security: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the brokerage account
|
2017-12-04 05:55:25 -05:00
|
|
|
account := &models.Account{
|
2017-12-01 21:21:55 -05:00
|
|
|
SecurityId: d.securities[0].SecurityId,
|
|
|
|
UserId: d.users[0].UserId,
|
|
|
|
ParentAccountId: -1,
|
2017-12-04 05:55:25 -05:00
|
|
|
Type: models.Investment,
|
2017-12-01 21:21:55 -05:00
|
|
|
Name: "Personal Brokerage",
|
|
|
|
}
|
|
|
|
|
|
|
|
account, err = createAccount(d.clients[0], account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating 'Personal Brokerage' account: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import and ensure it didn't return a nasty error code
|
|
|
|
if err = importOFX(d.clients[0], account.AccountId, "handlers_testdata/brokerage.ofx"); err != nil {
|
|
|
|
t.Fatalf("Error importing OFX: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], account, "387.48")
|
|
|
|
|
|
|
|
// Make sure the USD trading account was created and has the right
|
|
|
|
// value
|
2017-12-04 05:55:25 -05:00
|
|
|
usdtrading, err := findAccount(d.clients[0], "USD", models.Trading, d.users[0].DefaultCurrency)
|
2017-12-01 21:21:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding USD trading account: %s\n", err)
|
|
|
|
}
|
|
|
|
accountBalanceHelper(t, d.clients[0], usdtrading, "619.96")
|
|
|
|
|
|
|
|
// Check investment/trading balances for all securities traded
|
|
|
|
checks := []struct {
|
|
|
|
Ticker string
|
|
|
|
Name string
|
|
|
|
Balance string
|
|
|
|
TradingBalance string
|
|
|
|
}{
|
|
|
|
{"VBMFX", "Vanguard Total Bond Market Index Fund Investor Shares", "37.70000", "-37.70000"},
|
|
|
|
{"921909768", "VANGUARD TOTAL INTL STOCK INDE", "5.00000", "-5.00000"},
|
|
|
|
{"ATO", "ATMOS ENERGY CORP", "0.08600", "-0.08600"},
|
|
|
|
{"VMFXX", "Vanguard Federal Money Market Fund", "-21.57000", "21.57000"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range checks {
|
2017-12-03 06:38:22 -05:00
|
|
|
security, err := findSecurity(d.clients[0], check.Ticker, models.Stock)
|
2017-12-01 21:21:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding security: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:55:25 -05:00
|
|
|
account, err := findAccount(d.clients[0], check.Name, models.Investment, security.SecurityId)
|
2017-12-01 21:21:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding trading account: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountBalanceHelper(t, d.clients[0], account, check.Balance)
|
|
|
|
|
2017-12-04 05:55:25 -05:00
|
|
|
tradingaccount, err := findAccount(d.clients[0], check.Name, models.Trading, security.SecurityId)
|
2017-12-01 21:21:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error finding trading account: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountBalanceHelper(t, d.clients[0], tradingaccount, check.TradingBalance)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check reinvestment/income to make sure they're registered as income?
|
|
|
|
})
|
|
|
|
}
|