1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 07:33:21 -05:00

testing: Add common way to POST objects

This commit is contained in:
Aaron Lindsay 2017-10-08 21:18:30 -04:00
parent bd52df65cd
commit 2decf765ac
4 changed files with 88 additions and 65 deletions

View File

@ -2,6 +2,8 @@ package handlers_test
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/db"
"github.com/aclindsa/moneygo/internal/handlers"
@ -35,6 +37,43 @@ func PutForm(client *http.Client, url string, data url.Values) (*http.Response,
return client.Do(request)
}
type TransactType interface {
Read(string) error
}
func create(c *http.Client, input TransactType, output TransactType, urlsuffix, key string) error {
bytes, err := json.Marshal(input)
if err != nil {
return err
}
response, err := c.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}})
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 fmt.Errorf("Error when creating %s: %+v", key, e)
}
err = output.Read(string(body))
if err != nil {
return err
}
return nil
}
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
testdata, err := d.Initialize()
if err != nil {

View File

@ -1,7 +1,6 @@
package handlers_test
import (
"encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/handlers"
"io/ioutil"
@ -13,7 +12,6 @@ import (
func newSession(user *User) (*http.Client, error) {
var u User
var e handlers.Error
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: nil})
if err != nil {
@ -23,34 +21,7 @@ func newSession(user *User) (*http.Client, error) {
client := server.Client()
client.Jar = jar
bytes, err := json.Marshal(user)
if err != nil {
return nil, err
}
response, err := client.PostForm(server.URL+"/session/", url.Values{"user": {string(bytes)}})
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
err = (&u).Read(string(body))
if err != nil {
return nil, err
}
err = (&e).Read(string(body))
if err != nil {
return nil, err
}
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return nil, fmt.Errorf("Unexpected error when creating session %+v", e)
}
create(client, user, &u, "/session/", "user")
return client, nil
}

View File

@ -35,8 +35,8 @@ type TestData struct {
initialized bool
users []User
clients []*http.Client
accounts []handlers.Account
securities []handlers.Security
accounts []handlers.Account // accounts must appear after their parents in this slice
transactions []handlers.Transaction
prices []handlers.Price
reports []handlers.Report
@ -107,5 +107,46 @@ var data = []TestData{
Email: "jsmith@example.com",
},
},
securities: []handlers.Security{
handlers.Security{
UserId: 0,
Name: "USD",
Description: "US Dollar",
Symbol: "$",
Precision: 2,
Type: handlers.Currency,
AlternateId: "840",
},
},
accounts: []handlers.Account{
handlers.Account{
UserId: 0,
SecurityId: 0,
ParentAccountId: -1,
Type: handlers.Asset,
Name: "Assets",
},
handlers.Account{
UserId: 0,
SecurityId: 0,
ParentAccountId: 0,
Type: handlers.Asset,
Name: "Credit Union Checking",
},
handlers.Account{
UserId: 0,
SecurityId: 0,
ParentAccountId: -1,
Type: handlers.Expense,
Name: "Expenses",
},
handlers.Account{
UserId: 0,
SecurityId: 0,
ParentAccountId: 2,
Type: handlers.Expense,
Name: "Groceries",
},
},
},
}

View File

@ -12,41 +12,9 @@ import (
)
func createUser(user *User) (*User, error) {
bytes, err := json.Marshal(user)
if err != nil {
return nil, err
}
response, err := server.Client().PostForm(server.URL+"/user/", url.Values{"user": {string(bytes)}})
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
var e handlers.Error
err = (&e).Read(string(body))
if err != nil {
return nil, err
}
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return nil, fmt.Errorf("Error when creating user %+v", e)
}
var u User
err = (&u).Read(string(body))
if err != nil {
return nil, err
}
if u.UserId == 0 || len(u.Username) == 0 {
return nil, fmt.Errorf("Unable to create user: %+v", user)
}
return &u, nil
err := create(server.Client(), user, &u, "/user/", "user")
return &u, err
}
func updateUser(client *http.Client, user *User) (*User, error) {
@ -147,6 +115,10 @@ func getUser(client *http.Client, userid int64) (*User, error) {
func TestCreateUser(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if d.users[0].UserId == 0 || len(d.users[0].Username) == 0 {
t.Errorf("Unable to create user: %+v", data[0].users[0])
}
if len(d.users[0].Password) != 0 || len(d.users[0].PasswordHash) != 0 {
t.Error("Never send password, only send password hash when necessary")
}