mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-25 23:23:21 -05:00
Add initial OFX import test
So far, this only checks to ensure that the import doesn't return an error code.
This commit is contained in:
parent
7a23739a0b
commit
aa8924243e
@ -10,6 +10,7 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@ -158,6 +159,49 @@ func remove(client *http.Client, urlsuffix string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func uploadFile(client *http.Client, filename, urlsuffix string) error {
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
filewriter, err := mw.CreateFormFile("file", filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(filewriter, file); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mw.Close()
|
||||
|
||||
response, err := client.Post(server.URL+urlsuffix, mw.FormDataContentType(), &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var e handlers.Error
|
||||
err = (&e).Read(string(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
||||
return &e
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
|
||||
testdata, err := d.Initialize()
|
||||
if err != nil {
|
||||
|
@ -1,57 +1,13 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/aclindsa/moneygo/internal/handlers"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func importGnucash(client *http.Client, filename string) error {
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
filewriter, err := mw.CreateFormFile("gnucash", filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(filewriter, file); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mw.Close()
|
||||
|
||||
response, err := client.Post(server.URL+"/v1/imports/gnucash", mw.FormDataContentType(), &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var e handlers.Error
|
||||
err = (&e).Read(string(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
||||
return &e
|
||||
}
|
||||
|
||||
return nil
|
||||
return uploadFile(client, filename, "/v1/imports/gnucash")
|
||||
}
|
||||
|
||||
func gnucashAccountBalanceHelper(t *testing.T, client *http.Client, account *handlers.Account, balance string) {
|
||||
|
140
internal/handlers/handlers_testdata/checking_20171126.ofx
Normal file
140
internal/handlers/handlers_testdata/checking_20171126.ofx
Normal file
@ -0,0 +1,140 @@
|
||||
<?xml version="1.0" encoding="utf-16"?>
|
||||
<?OFX OFXHEADER="200" VERSION="203" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>
|
||||
<OFX>
|
||||
<SIGNONMSGSRSV1><SONRS>
|
||||
<STATUS>
|
||||
<CODE>0</CODE>
|
||||
<SEVERITY>INFO</SEVERITY>
|
||||
</STATUS>
|
||||
<DTSERVER>20171126184401.091[0:GMT]</DTSERVER>
|
||||
<LANGUAGE>ENG</LANGUAGE>
|
||||
<FI>
|
||||
<ORG>YCKVJ</ORG>
|
||||
<FID>0351</FID>
|
||||
</FI>
|
||||
</SONRS></SIGNONMSGSRSV1>
|
||||
<BANKMSGSRSV1>
|
||||
<STMTTRNRS>
|
||||
<TRNUID>0549c828-f02c-43c7-81a3-de0b3f23c393</TRNUID>
|
||||
<STATUS>
|
||||
<CODE>0</CODE>
|
||||
<SEVERITY>INFO</SEVERITY>
|
||||
</STATUS>
|
||||
<STMTRS>
|
||||
<CURDEF>USD</CURDEF>
|
||||
<BANKACCTFROM>
|
||||
<BANKID>115483849</BANKID>
|
||||
<ACCTID>14839128817</ACCTID>
|
||||
<ACCTTYPE>CHECKING</ACCTTYPE>
|
||||
</BANKACCTFROM>
|
||||
<BANKTRANLIST>
|
||||
<DTSTART>20170828174401.637[0:GMT]</DTSTART>
|
||||
<DTEND>20171126184401.637[0:GMT]</DTEND>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20170830120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>2843.08</TRNAMT>
|
||||
<FITID>ce2cf749-dd15-4dc7-b78d-2f9e88d8a702</FITID>
|
||||
<NAME>SALARY</NAME>
|
||||
<MEMO>ACH Deposit 18181818199</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20170830120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-2354.66</TRNAMT>
|
||||
<FITID>2bda11f4-9a9c-43fb-b67a-71f747dcf684</FITID>
|
||||
<NAME>BILLPAY TO CREDIT CARD</NAME>
|
||||
<MEMO>ACH Debit 8181819191919</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CHECK</TRNTYPE>
|
||||
<DTPOSTED>20170830120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-15.00</TRNAMT>
|
||||
<FITID>62dcc92d-ba0f-4fe6-8611-d6c1b86594fc</FITID>
|
||||
<CHECKNUM>3304</CHECKNUM>
|
||||
<NAME>Check</NAME>
|
||||
<MEMO>INCLEARING CHECK</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171107120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-282.68</TRNAMT>
|
||||
<FITID>29c74a94-f226-4980-b54c-da6fa2721d7e</FITID>
|
||||
<NAME>DAYCARE o SIGONFILE</NAME>
|
||||
<MEMO>ACH Debit 11818191919191</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171109120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1300.98</TRNAMT>
|
||||
<FITID>32e40e98-61c3-421c-acaa-55ae67a5f8fe</FITID>
|
||||
<NAME>DIRECT DEPOSIT</NAME>
|
||||
<MEMO>ACH Deposit 8282828282828</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171109120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-98.20</TRNAMT>
|
||||
<FITID>4b73dbbf-aa27-4f62-b54a-ee0a9a3486d8</FITID>
|
||||
<NAME>DUKEENGYPROGRESS DUKEENGYPR</NAME>
|
||||
<MEMO>ACH Debit 017313004099621</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171115120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1.01</TRNAMT>
|
||||
<FITID>51c47252-4cf0-442c-b619-8a31b17ac489</FITID>
|
||||
<NAME>Dividend Earned</NAME>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171116120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-51.75</TRNAMT>
|
||||
<FITID>51cb12bb-cdd9-4333-8d8d-c423f9e8f833</FITID>
|
||||
<NAME>TARGET DEBIT CRD ACH TRAN</NAME>
|
||||
<MEMO>ACH Debit</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171120120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-25.18</TRNAMT>
|
||||
<FITID>366a5b23-2f2e-4cf0-a714-6a306bd4e909</FITID>
|
||||
<NAME>TARGET DEBIT CRD ACH TRAN</NAME>
|
||||
<MEMO>ACH Debit</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171121120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-10.71</TRNAMT>
|
||||
<FITID>9a463f21-c6e1-4fe0-b37b-f9a8cc942cf0</FITID>
|
||||
<NAME>NETFLIX COM NETFLIX COM</NAME>
|
||||
<MEMO>Point of Sale Debit L999 DATE 11-20</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171122120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1300.98</TRNAMT>
|
||||
<FITID>31f165e5-569f-4530-8438-a6ceb2301335</FITID>
|
||||
<NAME>DIRECT DEPOSIT</NAME>
|
||||
<MEMO>ACH Deposit 838383838383838</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171122120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>12.50</TRNAMT>
|
||||
<FITID>215a10dd-f3a2-4336-ab8c-f22276cad552</FITID>
|
||||
<NAME>CIRCLE INTERNET CIRCLE</NAME>
|
||||
<MEMO>ACH Deposit 017326000283477</MEMO>
|
||||
</STMTTRN>
|
||||
</BANKTRANLIST>
|
||||
<LEDGERBAL>
|
||||
<BALAMT>2620.37</BALAMT>
|
||||
<DTASOF>20171126184401.637[0:GMT]</DTASOF>
|
||||
</LEDGERBAL>
|
||||
<AVAILBAL>
|
||||
<BALAMT>3620.37</BALAMT>
|
||||
<DTASOF>20171126184401.637[0:GMT]</DTASOF>
|
||||
</AVAILBAL>
|
||||
</STMTRS></STMTTRNRS>
|
||||
</BANKMSGSRSV1>
|
||||
</OFX>
|
123
internal/handlers/handlers_testdata/checking_20171129.ofx
Normal file
123
internal/handlers/handlers_testdata/checking_20171129.ofx
Normal file
@ -0,0 +1,123 @@
|
||||
<?xml version="1.0" encoding="utf-16"?>
|
||||
<?OFX OFXHEADER="200" VERSION="203" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>
|
||||
<OFX>
|
||||
<SIGNONMSGSRSV1><SONRS>
|
||||
<STATUS>
|
||||
<CODE>0</CODE>
|
||||
<SEVERITY>INFO</SEVERITY>
|
||||
</STATUS>
|
||||
<DTSERVER>20171129025346.132[0:GMT]</DTSERVER>
|
||||
<LANGUAGE>ENG</LANGUAGE>
|
||||
<FI>
|
||||
<ORG>YCKVJ</ORG>
|
||||
<FID>0351</FID>
|
||||
</FI>
|
||||
</SONRS></SIGNONMSGSRSV1>
|
||||
<BANKMSGSRSV1>
|
||||
<STMTTRNRS>
|
||||
<TRNUID>0549c828-f02c-43c7-81a3-de0b3f23c393</TRNUID>
|
||||
<STATUS>
|
||||
<CODE>0</CODE>
|
||||
<SEVERITY>INFO</SEVERITY>
|
||||
</STATUS>
|
||||
<STMTRS>
|
||||
<CURDEF>USD</CURDEF>
|
||||
<BANKACCTFROM>
|
||||
<BANKID>115483849</BANKID>
|
||||
<ACCTID>14839128817</ACCTID>
|
||||
<ACCTTYPE>CHECKING</ACCTTYPE>
|
||||
</BANKACCTFROM>
|
||||
<BANKTRANLIST>
|
||||
<DTSTART>20170831174401.637[0:GMT]</DTSTART>
|
||||
<DTEND>20171129184401.637[0:GMT]</DTEND>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171107120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-282.68</TRNAMT>
|
||||
<FITID>29c74a94-f226-4980-b54c-da6fa2721d7e</FITID>
|
||||
<NAME>DAYCARE o SIGONFILE</NAME>
|
||||
<MEMO>ACH Debit 11818191919191</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171109120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1300.98</TRNAMT>
|
||||
<FITID>32e40e98-61c3-421c-acaa-55ae67a5f8fe</FITID>
|
||||
<NAME>DIRECT DEPOSIT</NAME>
|
||||
<MEMO>ACH Deposit 8282828282828</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171109120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-98.20</TRNAMT>
|
||||
<FITID>4b73dbbf-aa27-4f62-b54a-ee0a9a3486d8</FITID>
|
||||
<NAME>DUKEENGYPROGRESS DUKEENGYPR</NAME>
|
||||
<MEMO>ACH Debit 017313004099621</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171115120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1.01</TRNAMT>
|
||||
<FITID>51c47252-4cf0-442c-b619-8a31b17ac489</FITID>
|
||||
<NAME>Dividend Earned</NAME>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171116120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-51.75</TRNAMT>
|
||||
<FITID>51cb12bb-cdd9-4333-8d8d-c423f9e8f833</FITID>
|
||||
<NAME>TARGET DEBIT CRD ACH TRAN</NAME>
|
||||
<MEMO>ACH Debit</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171120120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-25.18</TRNAMT>
|
||||
<FITID>366a5b23-2f2e-4cf0-a714-6a306bd4e909</FITID>
|
||||
<NAME>TARGET DEBIT CRD ACH TRAN</NAME>
|
||||
<MEMO>ACH Debit</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>DEBIT</TRNTYPE>
|
||||
<DTPOSTED>20171121120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>-10.71</TRNAMT>
|
||||
<FITID>9a463f21-c6e1-4fe0-b37b-f9a8cc942cf0</FITID>
|
||||
<NAME>NETFLIX COM NETFLIX COM</NAME>
|
||||
<MEMO>Point of Sale Debit L999 DATE 11-20</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171122120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>1300.98</TRNAMT>
|
||||
<FITID>31f165e5-569f-4530-8438-a6ceb2301335</FITID>
|
||||
<NAME>DIRECT DEPOSIT</NAME>
|
||||
<MEMO>ACH Deposit 838383838383838</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171122120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>12.50</TRNAMT>
|
||||
<FITID>215a10dd-f3a2-4336-ab8c-f22276cad552</FITID>
|
||||
<NAME>CIRCLE INTERNET CIRCLE</NAME>
|
||||
<MEMO>ACH Deposit 017326000283477</MEMO>
|
||||
</STMTTRN>
|
||||
<STMTTRN>
|
||||
<TRNTYPE>CREDIT</TRNTYPE>
|
||||
<DTPOSTED>20171129120000.000[0:GMT]</DTPOSTED>
|
||||
<TRNAMT>2843.08</TRNAMT>
|
||||
<FITID>9a52df4b-3a8d-41bb-9141-96e1e3f294cf</FITID>
|
||||
<NAME>SALARY</NAME>
|
||||
<MEMO>ACH Deposit 18181818199</MEMO>
|
||||
</STMTTRN>
|
||||
</BANKTRANLIST>
|
||||
<LEDGERBAL>
|
||||
<BALAMT>5463.45</BALAMT>
|
||||
<DTASOF>20171129025346.132[0:GMT]</DTASOF>
|
||||
</LEDGERBAL>
|
||||
<AVAILBAL>
|
||||
<BALAMT>6463.45</BALAMT>
|
||||
<DTASOF>20171129025346.132[0:GMT]</DTASOF>
|
||||
</AVAILBAL>
|
||||
</STMTRS></STMTTRNRS>
|
||||
</BANKMSGSRSV1>
|
||||
</OFX>
|
36
internal/handlers/ofx_test.go
Normal file
36
internal/handlers/ofx_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"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")
|
||||
}
|
||||
|
||||
func TestImportOFX(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[1].AccountId, "handlers_testdata/checking_20171126.ofx"); err != nil {
|
||||
t.Fatalf("Error importing OFX: %s\n", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
@ -257,7 +257,7 @@ var data = []TestData{
|
||||
UserId: 0,
|
||||
SecurityId: 0,
|
||||
ParentAccountId: 0,
|
||||
Type: handlers.Asset,
|
||||
Type: handlers.Bank,
|
||||
Name: "Credit Union Checking",
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user