diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index 8966a18..3bbed81 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -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 { diff --git a/internal/handlers/sessions_test.go b/internal/handlers/sessions_test.go index 5e0702e..60e05fa 100644 --- a/internal/handlers/sessions_test.go +++ b/internal/handlers/sessions_test.go @@ -97,48 +97,30 @@ 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 { - t.Fatal(err) - } + 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 + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + session, err := getSession(d.clients[0]) + if err != nil { + t.Fatal(err) + } - client, err := newSession(u) - if err != nil { - t.Fatal(err) - } - defer deleteUser(client, u) - session, err := getSession(client) - if err != nil { - t.Fatal(err) - } + if len(session.SessionSecret) != 0 { + t.Error("Session.SessionSecret should not be passed back in JSON") + } - if len(session.SessionSecret) != 0 { - t.Error("Session.SessionSecret should not be passed back in JSON") - } + 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.UserId != u.UserId { - t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, u.UserId) - } - - if session.SessionId == 0 { - t.Error("session's SessionId should not be 0") - } + if session.SessionId == 0 { + t.Error("session's SessionId should not be 0") + } + }) } diff --git a/internal/handlers/testdata_test.go b/internal/handlers/testdata_test.go index 1cacd61..f2cea58 100644 --- a/internal/handlers/testdata_test.go +++ b/internal/handlers/testdata_test.go @@ -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,12 +30,82 @@ func (u *User) Read(json_str string) error { return dec.Decode(u) } -var users = []User{ - User{ - DefaultCurrency: 840, // USD - Name: "John Smith", - Username: "jsmith", - Password: "hunter2", - Email: "jsmith@example.com", +// 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", + Username: "jsmith", + Password: "hunter2", + Email: "jsmith@example.com", + }, + }, }, } diff --git a/internal/handlers/users_test.go b/internal/handlers/users_test.go index 31ba708..9a7bc9d 100644 --- a/internal/handlers/users_test.go +++ b/internal/handlers/users_test.go @@ -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 { - 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) + 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") + } + }) } 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) - if err != nil { - t.Fatalf("Error fetching user: %s\n", err) - } - if u.UserId != origu.UserId { - t.Errorf("UserId doesn't match") - } + 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 != 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) - if err != nil { - t.Fatalf("Error updating user: %s\n", err) - } - 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") - } + u, err := updateUser(d.clients[0], user) + if err != nil { + t.Fatalf("Error updating user: %s\n", err) + } + if u.UserId != user.UserId { + t.Errorf("UserId doesn't match") + } + if u.Name != user.Name { + t.Errorf("UserId doesn't match") + } + if u.Email != user.Email { + t.Errorf("UserId doesn't match") + } + }) }