mirror of
				https://github.com/aclindsa/moneygo.git
				synced 2025-11-03 18:13:27 -05:00 
			
		
		
		
	Create framework to automate creation/teardown of test data
This commit is contained in:
		@@ -35,6 +35,16 @@ func PutForm(client *http.Client, url string, data url.Values) (*http.Response,
 | 
			
		||||
	return client.Do(request)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
 | 
			
		||||
	testdata, err := d.Initialize()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal("Failed to initialize test data: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer testdata.Teardown()
 | 
			
		||||
 | 
			
		||||
	fn(t, testdata)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RunTests(m *testing.M) int {
 | 
			
		||||
	tmpdir, err := ioutil.TempDir("./", "handlertest")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -97,35 +97,16 @@ func sessionExistsOrError(c *http.Client) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCreateSession(t *testing.T) {
 | 
			
		||||
	u, err := createUser(&users[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	u.Password = users[0].Password
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(u)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer deleteUser(client, u)
 | 
			
		||||
	if err := sessionExistsOrError(client); err != nil {
 | 
			
		||||
	RunWith(t, &data[0], func(t *testing.T, d *TestData) {
 | 
			
		||||
		if err := sessionExistsOrError(d.clients[0]); err != nil {
 | 
			
		||||
			t.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGetSession(t *testing.T) {
 | 
			
		||||
	u, err := createUser(&users[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	u.Password = users[0].Password
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(u)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer deleteUser(client, u)
 | 
			
		||||
	session, err := getSession(client)
 | 
			
		||||
	RunWith(t, &data[0], func(t *testing.T, d *TestData) {
 | 
			
		||||
		session, err := getSession(d.clients[0])
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
@@ -134,11 +115,12 @@ func TestGetSession(t *testing.T) {
 | 
			
		||||
			t.Error("Session.SessionSecret should not be passed back in JSON")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	if session.UserId != u.UserId {
 | 
			
		||||
		t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, u.UserId)
 | 
			
		||||
		if session.UserId != d.users[0].UserId {
 | 
			
		||||
			t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, d.users[0].UserId)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if session.SessionId == 0 {
 | 
			
		||||
			t.Error("session's SessionId should not be 0")
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,12 +2,14 @@ package handlers_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/aclindsa/moneygo/internal/handlers"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Needed because handlers.User doesn't allow Password to be written to JSON
 | 
			
		||||
 | 
			
		||||
type User struct {
 | 
			
		||||
	UserId          int64
 | 
			
		||||
	DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user
 | 
			
		||||
@@ -28,7 +30,75 @@ func (u *User) Read(json_str string) error {
 | 
			
		||||
	return dec.Decode(u)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var users = []User{
 | 
			
		||||
// TestData
 | 
			
		||||
type TestData struct {
 | 
			
		||||
	initialized  bool
 | 
			
		||||
	users        []User
 | 
			
		||||
	clients      []*http.Client
 | 
			
		||||
	accounts     []handlers.Account
 | 
			
		||||
	securities   []handlers.Security
 | 
			
		||||
	transactions []handlers.Transaction
 | 
			
		||||
	prices       []handlers.Price
 | 
			
		||||
	reports      []handlers.Report
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type TestDataFunc func(*testing.T, *TestData)
 | 
			
		||||
 | 
			
		||||
func (t *TestData) initUser(user *User, userid int) error {
 | 
			
		||||
	newuser, err := createUser(user)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	t.users = append(t.users, *newuser)
 | 
			
		||||
 | 
			
		||||
	// make a copy of the user so we can set the password for creating the
 | 
			
		||||
	// session without disturbing the original
 | 
			
		||||
	userWithPassword := *newuser
 | 
			
		||||
	userWithPassword.Password = user.Password
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(&userWithPassword)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	t.clients = append(t.clients, client)
 | 
			
		||||
 | 
			
		||||
	// TODO initialize everything else owned by this user in the TestData struct
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Initialize makes requests to the server to create all of the objects
 | 
			
		||||
// represented in it before returning a copy of the data, with all of the *Id
 | 
			
		||||
// fields updated to their actual values
 | 
			
		||||
func (t *TestData) Initialize() (*TestData, error) {
 | 
			
		||||
	var t2 TestData
 | 
			
		||||
	for userid, user := range t.users {
 | 
			
		||||
		err := t2.initUser(&user, userid)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t2.initialized = true
 | 
			
		||||
	return &t2, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (t *TestData) Teardown() error {
 | 
			
		||||
	if !t.initialized {
 | 
			
		||||
		return fmt.Errorf("Cannot teardown uninitialized TestData")
 | 
			
		||||
	}
 | 
			
		||||
	for userid, user := range t.users {
 | 
			
		||||
		err := deleteUser(t.clients[userid], &user)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var data = []TestData{
 | 
			
		||||
	{
 | 
			
		||||
		users: []User{
 | 
			
		||||
			User{
 | 
			
		||||
				DefaultCurrency: 840, // USD
 | 
			
		||||
				Name:            "John Smith",
 | 
			
		||||
@@ -36,4 +106,6 @@ var users = []User{
 | 
			
		||||
				Password:        "hunter2",
 | 
			
		||||
				Email:           "jsmith@example.com",
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -146,73 +146,43 @@ func getUser(client *http.Client, userid int64) (*User, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCreateUser(t *testing.T) {
 | 
			
		||||
	u, err := createUser(&users[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(u.Password) != 0 || len(u.PasswordHash) != 0 {
 | 
			
		||||
	RunWith(t, &data[0], func(t *testing.T, d *TestData) {
 | 
			
		||||
		if len(d.users[0].Password) != 0 || len(d.users[0].PasswordHash) != 0 {
 | 
			
		||||
			t.Error("Never send password, only send password hash when necessary")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	u.Password = users[0].Password
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(u)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer deleteUser(client, u)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestGetUser(t *testing.T) {
 | 
			
		||||
	origu, err := createUser(&users[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	origu.Password = users[0].Password
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(origu)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer deleteUser(client, origu)
 | 
			
		||||
 | 
			
		||||
	u, err := getUser(client, origu.UserId)
 | 
			
		||||
	RunWith(t, &data[0], func(t *testing.T, d *TestData) {
 | 
			
		||||
		u, err := getUser(d.clients[0], d.users[0].UserId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatalf("Error fetching user: %s\n", err)
 | 
			
		||||
		}
 | 
			
		||||
	if u.UserId != origu.UserId {
 | 
			
		||||
		if u.UserId != d.users[0].UserId {
 | 
			
		||||
			t.Errorf("UserId doesn't match")
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestUpdateUser(t *testing.T) {
 | 
			
		||||
	origu, err := createUser(&users[0])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	origu.Password = users[0].Password
 | 
			
		||||
	RunWith(t, &data[0], func(t *testing.T, d *TestData) {
 | 
			
		||||
		user := &d.users[0]
 | 
			
		||||
		user.Name = "Bob"
 | 
			
		||||
		user.Email = "bob@example.com"
 | 
			
		||||
 | 
			
		||||
	client, err := newSession(origu)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer deleteUser(client, origu)
 | 
			
		||||
 | 
			
		||||
	origu.Name = "Bob"
 | 
			
		||||
	origu.Email = "bob@example.com"
 | 
			
		||||
 | 
			
		||||
	u, err := updateUser(client, origu)
 | 
			
		||||
		u, err := updateUser(d.clients[0], user)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			t.Fatalf("Error updating user: %s\n", err)
 | 
			
		||||
		}
 | 
			
		||||
	if u.UserId != origu.UserId {
 | 
			
		||||
		if u.UserId != user.UserId {
 | 
			
		||||
			t.Errorf("UserId doesn't match")
 | 
			
		||||
		}
 | 
			
		||||
	if u.Name != origu.Name {
 | 
			
		||||
		if u.Name != user.Name {
 | 
			
		||||
			t.Errorf("UserId doesn't match")
 | 
			
		||||
		}
 | 
			
		||||
	if u.Email != origu.Email {
 | 
			
		||||
		if u.Email != user.Email {
 | 
			
		||||
			t.Errorf("UserId doesn't match")
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user