2017-10-07 06:21:05 -04:00
|
|
|
package handlers_test
|
|
|
|
|
|
|
|
import (
|
2017-11-13 20:48:19 -05:00
|
|
|
"bytes"
|
2017-10-08 21:18:30 -04:00
|
|
|
"encoding/json"
|
2017-10-07 06:21:05 -04:00
|
|
|
"github.com/aclindsa/moneygo/internal/config"
|
|
|
|
"github.com/aclindsa/moneygo/internal/handlers"
|
2017-12-04 05:55:25 -05:00
|
|
|
"github.com/aclindsa/moneygo/internal/models"
|
2017-12-06 21:09:47 -05:00
|
|
|
"github.com/aclindsa/moneygo/internal/store/db"
|
2017-11-13 20:48:19 -05:00
|
|
|
"io"
|
2017-10-07 06:21:05 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2017-11-26 14:42:05 -05:00
|
|
|
"mime/multipart"
|
2017-10-07 06:21:05 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var server *httptest.Server
|
|
|
|
|
|
|
|
func Delete(client *http.Client, url string) (*http.Response, error) {
|
|
|
|
request, err := http.NewRequest(http.MethodDelete, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client.Do(request)
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:19 -05:00
|
|
|
func Put(client *http.Client, url string, contentType string, body io.Reader) (*http.Response, error) {
|
|
|
|
request, err := http.NewRequest(http.MethodPut, url, body)
|
2017-10-07 06:21:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-13 20:48:19 -05:00
|
|
|
request.Header.Set("Content-Type", contentType)
|
2017-10-07 06:21:05 -04:00
|
|
|
return client.Do(request)
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:18:30 -04:00
|
|
|
type TransactType interface {
|
|
|
|
Read(string) error
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:19 -05:00
|
|
|
func create(client *http.Client, input, output TransactType, urlsuffix string) error {
|
|
|
|
obj, err := json.MarshalIndent(input, "", " ")
|
2017-10-08 21:18:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-13 20:48:19 -05:00
|
|
|
response, err := client.Post(server.URL+urlsuffix, "application/json", bytes.NewReader(obj))
|
2017-10-08 21:18:30 -04:00
|
|
|
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 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-08 21:18:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:19 -05:00
|
|
|
func read(client *http.Client, output TransactType, urlsuffix string) error {
|
2017-10-09 06:00:51 -04:00
|
|
|
response, err := client.Get(server.URL + urlsuffix)
|
|
|
|
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 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:19 -05:00
|
|
|
func update(client *http.Client, input, output TransactType, urlsuffix string) error {
|
|
|
|
obj, err := json.MarshalIndent(input, "", " ")
|
2017-10-09 06:00:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-13 20:48:19 -05:00
|
|
|
response, err := Put(client, server.URL+urlsuffix, "application/json", bytes.NewReader(obj))
|
2017-10-09 06:00:51 -04:00
|
|
|
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 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:48:19 -05:00
|
|
|
func remove(client *http.Client, urlsuffix string) error {
|
2017-10-09 06:00:51 -04:00
|
|
|
response, err := Delete(client, server.URL+urlsuffix)
|
|
|
|
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 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-26 14:42:05 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:55:25 -05:00
|
|
|
func accountBalanceHelper(t *testing.T, client *http.Client, account *models.Account, balance string) {
|
2017-11-26 21:01:26 -05:00
|
|
|
t.Helper()
|
|
|
|
transactions, err := getAccountTransactions(client, account.AccountId, 0, 0, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't fetch account transactions for '%s': %s\n", account.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if transactions.EndingBalance != balance {
|
|
|
|
t.Errorf("Expected ending balance for '%s' to be '%s', but found %s\n", account.Name, balance, transactions.EndingBalance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 20:29:51 -04:00
|
|
|
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
|
|
|
|
testdata, err := d.Initialize()
|
|
|
|
if err != nil {
|
2017-10-14 20:38:40 -04:00
|
|
|
t.Fatalf("Failed to initialize test data: %s", err)
|
2017-10-08 20:29:51 -04:00
|
|
|
}
|
2017-10-11 05:48:31 -04:00
|
|
|
defer func() {
|
|
|
|
err := testdata.Teardown()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-10-08 20:29:51 -04:00
|
|
|
|
|
|
|
fn(t, testdata)
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:21:05 -04:00
|
|
|
func RunTests(m *testing.M) int {
|
2017-11-16 19:24:51 -05:00
|
|
|
envDbType := os.Getenv("MONEYGO_TEST_DB")
|
|
|
|
var dbType config.DbType
|
|
|
|
var dsn string
|
|
|
|
|
|
|
|
switch envDbType {
|
|
|
|
case "", "sqlite", "sqlite3":
|
|
|
|
dbType = config.SQLite
|
|
|
|
dsn = ":memory:"
|
|
|
|
case "mariadb", "mysql":
|
|
|
|
dbType = config.MySQL
|
|
|
|
dsn = "root@127.0.0.1/moneygo_test&parseTime=true"
|
|
|
|
case "postgres", "postgresql":
|
|
|
|
dbType = config.Postgres
|
|
|
|
dsn = "postgres://postgres@localhost/moneygo_test"
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid value for $MONEYGO_TEST_DB: %s\n", envDbType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if envDSN := os.Getenv("MONEYGO_TEST_DSN"); len(envDSN) > 0 {
|
|
|
|
dsn = envDSN
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:09:47 -05:00
|
|
|
db, err := db.GetStore(dbType, dsn)
|
2017-10-07 06:21:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-12-06 21:09:47 -05:00
|
|
|
defer db.Close()
|
2017-10-07 06:21:05 -04:00
|
|
|
|
2017-12-06 21:09:47 -05:00
|
|
|
err = db.DbMap.TruncateTables()
|
2017-11-16 19:24:51 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:09:47 -05:00
|
|
|
server = httptest.NewTLSServer(&handlers.APIHandler{Store: db})
|
2017-10-07 06:21:05 -04:00
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2017-10-31 21:04:03 -04:00
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
2017-10-07 06:21:05 -04:00
|
|
|
os.Exit(RunTests(m))
|
|
|
|
}
|