func TestGetUser(t *testing.T) { resp := httptest.NewRecorder() uri := "/users/whatever" req, err := http.NewRequest("GET", uri, nil) if err != nil { t.Fatal(err) } http.DefaultServeMux.ServeHTTP(resp, req) if resp.Code != 500 { t.Fatal("Expected 500 error requesting invalid UUID but got", resp.Code) } if p, err := ioutil.ReadAll(resp.Body); err != nil { t.Fatal(err) } else { if !strings.Contains(string(p), "Error") { t.Error("User controller error response does not contain word 'Error'") } } coolUser := datastore.User{PublicKey: "something"} if err := coolUser.Create(); err != nil { t.Fatal("Error creating user:"******"/users/%s", coolUser.Id) req, err = http.NewRequest("GET", uri, nil) if err != nil { t.Fatal(err) } http.DefaultServeMux.ServeHTTP(resp, req) if resp.Code != 200 { t.Fatal("Expected 200 but got", resp.Code) } p, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } dbUser := datastore.User{} if err := json.Unmarshal(p, &dbUser); err != nil { t.Fatal("Error decoding response json", err) } expectedContentType := "application/json" if ct := resp.Header().Get("Content-Type"); ct != expectedContentType { t.Error("Expected content type", expectedContentType, "but got", ct) } if dbUser.Id != coolUser.Id || dbUser.PublicKey != coolUser.PublicKey { t.Error("Error: expected user", coolUser, "but got", dbUser) } }
func postUser(w http.ResponseWriter, r *http.Request) { dec := json.NewDecoder(r.Body) var user datastore.User if err := dec.Decode(&user); err != nil { log.Println("Error decoding user:"******"Error decoding user", 400) return } if err := user.Create(); err != nil { log.Println("Error creating user:"******"Error creating user", 500) return } w.Header().Add("Content-Type", "application/json") enc := json.NewEncoder(w) if err := enc.Encode(user); err != nil { log.Println("Error marshaling saved user:"******"Error marshaling user data", 500) return } return }
func TestPutUser(t *testing.T) { key, err := auth.GeneratePrivateKey(1024) if err != nil { t.Fatal("Error generating private key:", err) } pubKeyString, err := auth.StringForPublicKey(&key.PublicKey) if err != nil { t.Fatal("Error serializing public key:", err) } user := datastore.User{ PublicKey: pubKeyString, DeviceId: "some-id", Latitude: 5.0, Longitude: 6.0, } if err := user.Create(); err != nil { t.Fatal("Error creating user:"******"updated-key", } jsonBytes, err := json.Marshal(updatedUser) if err != nil { t.Fatal("Error marshaling user:"******"Error generating API token:", err) } resp := httptest.NewRecorder() uri := fmt.Sprintf("/users/%s", user.Id) req, err := http.NewRequest("PUT", uri, bytes.NewBuffer(jsonBytes)) req.Header.Add("X-API-Token", token) http.DefaultServeMux.ServeHTTP(resp, req) if resp.Code != 200 { t.Fatal("Expected 200 on user create but got", resp.Code) } p, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } var parsedUser datastore.User if err := json.Unmarshal(p, &parsedUser); err != nil { t.Fatal(err) } if parsedUser.Id == "" { t.Error("Error: parsed user has no ID") } if parsedUser.PublicKey != updatedUser.PublicKey { t.Error("Error: expected public key", updatedUser.PublicKey, "but got", parsedUser.PublicKey) } if err := user.Delete(); err != nil { t.Error("Error deleting user", err) } }