mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-26 23:42:29 -05:00
Create framework to automate creation/teardown of test data
This commit is contained in:
parent
3c6b5528f9
commit
bd52df65cd
@ -35,6 +35,16 @@ func PutForm(client *http.Client, url string, data url.Values) (*http.Response,
|
|||||||
return client.Do(request)
|
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 {
|
func RunTests(m *testing.M) int {
|
||||||
tmpdir, err := ioutil.TempDir("./", "handlertest")
|
tmpdir, err := ioutil.TempDir("./", "handlertest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -97,48 +97,30 @@ func sessionExistsOrError(c *http.Client) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateSession(t *testing.T) {
|
func TestCreateSession(t *testing.T) {
|
||||||
u, err := createUser(&users[0])
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||||
if err != nil {
|
if err := sessionExistsOrError(d.clients[0]); err != nil {
|
||||||
t.Fatal(err)
|
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 {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetSession(t *testing.T) {
|
func TestGetSession(t *testing.T) {
|
||||||
u, err := createUser(&users[0])
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||||
if err != nil {
|
session, err := getSession(d.clients[0])
|
||||||
t.Fatal(err)
|
if err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
u.Password = users[0].Password
|
}
|
||||||
|
|
||||||
client, err := newSession(u)
|
if len(session.SessionSecret) != 0 {
|
||||||
if err != nil {
|
t.Error("Session.SessionSecret should not be passed back in JSON")
|
||||||
t.Fatal(err)
|
}
|
||||||
}
|
|
||||||
defer deleteUser(client, u)
|
|
||||||
session, err := getSession(client)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(session.SessionSecret) != 0 {
|
if session.UserId != d.users[0].UserId {
|
||||||
t.Error("Session.SessionSecret should not be passed back in JSON")
|
t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, d.users[0].UserId)
|
||||||
}
|
}
|
||||||
|
|
||||||
if session.UserId != u.UserId {
|
if session.SessionId == 0 {
|
||||||
t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, u.UserId)
|
t.Error("session's SessionId should not be 0")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
if session.SessionId == 0 {
|
|
||||||
t.Error("session's SessionId should not be 0")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,14 @@ package handlers_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/aclindsa/moneygo/internal/handlers"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Needed because handlers.User doesn't allow Password to be written to JSON
|
// Needed because handlers.User doesn't allow Password to be written to JSON
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
UserId int64
|
UserId int64
|
||||||
DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user
|
DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user
|
||||||
@ -28,12 +30,82 @@ func (u *User) Read(json_str string) error {
|
|||||||
return dec.Decode(u)
|
return dec.Decode(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
var users = []User{
|
// TestData
|
||||||
User{
|
type TestData struct {
|
||||||
DefaultCurrency: 840, // USD
|
initialized bool
|
||||||
Name: "John Smith",
|
users []User
|
||||||
Username: "jsmith",
|
clients []*http.Client
|
||||||
Password: "hunter2",
|
accounts []handlers.Account
|
||||||
Email: "jsmith@example.com",
|
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",
|
||||||
|
Username: "jsmith",
|
||||||
|
Password: "hunter2",
|
||||||
|
Email: "jsmith@example.com",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -146,73 +146,43 @@ func getUser(client *http.Client, userid int64) (*User, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateUser(t *testing.T) {
|
func TestCreateUser(t *testing.T) {
|
||||||
u, err := createUser(&users[0])
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||||
if err != nil {
|
if len(d.users[0].Password) != 0 || len(d.users[0].PasswordHash) != 0 {
|
||||||
t.Fatal(err)
|
t.Error("Never send password, only send password hash when necessary")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
if len(u.Password) != 0 || len(u.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) {
|
func TestGetUser(t *testing.T) {
|
||||||
origu, err := createUser(&users[0])
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||||
if err != nil {
|
u, err := getUser(d.clients[0], d.users[0].UserId)
|
||||||
t.Fatal(err)
|
if err != nil {
|
||||||
}
|
t.Fatalf("Error fetching user: %s\n", err)
|
||||||
origu.Password = users[0].Password
|
}
|
||||||
|
if u.UserId != d.users[0].UserId {
|
||||||
client, err := newSession(origu)
|
t.Errorf("UserId doesn't match")
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error fetching user: %s\n", err)
|
|
||||||
}
|
|
||||||
if u.UserId != origu.UserId {
|
|
||||||
t.Errorf("UserId doesn't match")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateUser(t *testing.T) {
|
func TestUpdateUser(t *testing.T) {
|
||||||
origu, err := createUser(&users[0])
|
RunWith(t, &data[0], func(t *testing.T, d *TestData) {
|
||||||
if err != nil {
|
user := &d.users[0]
|
||||||
t.Fatal(err)
|
user.Name = "Bob"
|
||||||
}
|
user.Email = "bob@example.com"
|
||||||
origu.Password = users[0].Password
|
|
||||||
|
|
||||||
client, err := newSession(origu)
|
u, err := updateUser(d.clients[0], user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
|
t.Fatalf("Error updating user: %s\n", err)
|
||||||
}
|
}
|
||||||
defer deleteUser(client, origu)
|
if u.UserId != user.UserId {
|
||||||
|
t.Errorf("UserId doesn't match")
|
||||||
origu.Name = "Bob"
|
}
|
||||||
origu.Email = "bob@example.com"
|
if u.Name != user.Name {
|
||||||
|
t.Errorf("UserId doesn't match")
|
||||||
u, err := updateUser(client, origu)
|
}
|
||||||
if err != nil {
|
if u.Email != user.Email {
|
||||||
t.Fatalf("Error updating user: %s\n", err)
|
t.Errorf("UserId doesn't match")
|
||||||
}
|
}
|
||||||
if u.UserId != origu.UserId {
|
})
|
||||||
t.Errorf("UserId doesn't match")
|
|
||||||
}
|
|
||||||
if u.Name != origu.Name {
|
|
||||||
t.Errorf("UserId doesn't match")
|
|
||||||
}
|
|
||||||
if u.Email != origu.Email {
|
|
||||||
t.Errorf("UserId doesn't match")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user