diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index b4b20f9..15293ad 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -41,12 +41,12 @@ type TransactType interface { Read(string) error } -func create(c *http.Client, input TransactType, output TransactType, urlsuffix, key string) error { +func create(client *http.Client, input, 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)}}) + response, err := client.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}}) if err != nil { return err } @@ -63,7 +63,7 @@ func create(c *http.Client, input TransactType, output TransactType, urlsuffix, return err } if e.ErrorId != 0 || len(e.ErrorString) != 0 { - return fmt.Errorf("Error when creating %s: %+v", key, e) + return fmt.Errorf("Error when creating %s: %+v", urlsuffix, e) } err = output.Read(string(body)) @@ -74,6 +74,92 @@ func create(c *http.Client, input TransactType, output TransactType, urlsuffix, return nil } +func read(client *http.Client, output TransactType, urlsuffix, key string) error { + 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 { + return fmt.Errorf("Error when updating %s: %+v", urlsuffix, e) + } + + err = output.Read(string(body)) + if err != nil { + return err + } + + return nil +} + +func update(client *http.Client, input, output TransactType, urlsuffix, key string) error { + bytes, err := json.Marshal(input) + if err != nil { + return err + } + response, err := PutForm(client, 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 updating %s: %+v", urlsuffix, e) + } + + err = output.Read(string(body)) + if err != nil { + return err + } + + return nil +} + +func remove(client *http.Client, urlsuffix, key string) error { + 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 { + return fmt.Errorf("Error when removing %s: %+v", urlsuffix, e) + } + + return nil +} + func RunWith(t *testing.T, d *TestData, fn TestDataFunc) { testdata, err := d.Initialize() if err != nil { diff --git a/internal/handlers/sessions_test.go b/internal/handlers/sessions_test.go index 96e4c1c..0bc0b03 100644 --- a/internal/handlers/sessions_test.go +++ b/internal/handlers/sessions_test.go @@ -3,7 +3,6 @@ package handlers_test import ( "fmt" "github.com/aclindsa/moneygo/internal/handlers" - "io/ioutil" "net/http" "net/http/cookiejar" "net/url" @@ -28,22 +27,7 @@ func newSession(user *User) (*http.Client, error) { func getSession(client *http.Client) (*handlers.Session, error) { var s handlers.Session - response, err := client.Get(server.URL + "/session/") - if err != nil { - return nil, err - } - - body, err := ioutil.ReadAll(response.Body) - response.Body.Close() - if err != nil { - return nil, err - } - - err = (&s).Read(string(body)) - if err != nil { - return nil, err - } - + read(client, &s, "/session/", "session") return &s, nil } diff --git a/internal/handlers/users_test.go b/internal/handlers/users_test.go index b76840a..6ee4829 100644 --- a/internal/handlers/users_test.go +++ b/internal/handlers/users_test.go @@ -1,12 +1,7 @@ package handlers_test import ( - "encoding/json" - "fmt" - "github.com/aclindsa/moneygo/internal/handlers" - "io/ioutil" "net/http" - "net/url" "strconv" "testing" ) @@ -17,102 +12,32 @@ func createUser(user *User) (*User, error) { return &u, err } -func updateUser(client *http.Client, user *User) (*User, error) { - bytes, err := json.Marshal(user) - if err != nil { - return nil, err - } - response, err := PutForm(client, server.URL+"/user/"+strconv.FormatInt(user.UserId, 10), 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 updating user %+v", e) - } - +func getUser(client *http.Client, userid int64) (*User, error) { var u User - err = (&u).Read(string(body)) + err := read(client, &u, "/user/"+strconv.FormatInt(userid, 10), "user") if err != nil { return nil, err } + return &u, nil +} - if u.UserId == 0 || len(u.Username) == 0 { - return nil, fmt.Errorf("Unable to update user: %+v", user) +func updateUser(client *http.Client, user *User) (*User, error) { + var u User + err := update(client, user, &u, "/user/"+strconv.FormatInt(user.UserId, 10), "user") + if err != nil { + return nil, err } - return &u, nil } func deleteUser(client *http.Client, u *User) error { - response, err := Delete(client, server.URL+"/user/"+strconv.FormatInt(u.UserId, 10)) + err := remove(client, "/user/"+strconv.FormatInt(u.UserId, 10), "user") 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 deleting user %+v", e) - } - return nil } -func getUser(client *http.Client, userid int64) (*User, error) { - response, err := client.Get(server.URL + "/user/" + strconv.FormatInt(userid, 10)) - 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 get 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 get userid: %d", userid) - } - - return &u, nil -} - 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 { @@ -134,6 +59,9 @@ func TestGetUser(t *testing.T) { if u.UserId != d.users[0].UserId { t.Errorf("UserId doesn't match") } + if len(u.Username) == 0 { + t.Fatalf("Empty username for: %d", d.users[0].UserId) + } }) } @@ -150,11 +78,14 @@ func TestUpdateUser(t *testing.T) { if u.UserId != user.UserId { t.Errorf("UserId doesn't match") } + if u.Username != u.Username { + t.Errorf("Username doesn't match") + } if u.Name != user.Name { - t.Errorf("UserId doesn't match") + t.Errorf("Name doesn't match") } if u.Email != user.Email { - t.Errorf("UserId doesn't match") + t.Errorf("Email doesn't match") } }) }