Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
api: add new handler to update key
Browse files Browse the repository at this point in the history
Closes #193.
  • Loading branch information
Francisco Souza committed Jul 28, 2015
1 parent c7f103c commit 274d80b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
25 changes: 25 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func SetupRouter() *pat.Router {
router := pat.New()
router.Post("/user/{name}/key", http.HandlerFunc(addKey))
router.Delete("/user/{name}/key/{keyname}", http.HandlerFunc(removeKey))
router.Put("/user/{name}/key/{keyname}", http.HandlerFunc(updateKey))
router.Get("/user/{name}/keys", http.HandlerFunc(listKeys))
router.Post("/user", http.HandlerFunc(newUser))
router.Delete("/user/{name}", http.HandlerFunc(removeUser))
Expand Down Expand Up @@ -147,6 +148,30 @@ func addKey(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Key(s) successfully created")
}

func updateKey(w http.ResponseWriter, r *http.Request) {
uName := r.URL.Query().Get(":name")
kName := r.URL.Query().Get(":keyname")
defer r.Body.Close()
content, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
key := user.Key{Name: kName, Body: string(content)}
if err := user.UpdateKey(uName, key); err != nil {
switch err {
case user.ErrInvalidKey:
http.Error(w, err.Error(), http.StatusBadRequest)
case user.ErrUserNotFound, user.ErrKeyNotFound:
http.Error(w, err.Error(), http.StatusNotFound)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
fmt.Fprintf(w, "Key %q successfully updated!", kName)
}

func removeKey(w http.ResponseWriter, r *http.Request) {
uName := r.URL.Query().Get(":name")
kName := r.URL.Query().Get(":keyname")
Expand Down
54 changes: 54 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,60 @@ func (s *S) TestAddKey(c *check.C) {
c.Assert(k.Comment, check.Equals, keyComment)
}

func (s *S) TestUpdateKey(c *check.C) {
usr, err := user.New("Frodo", map[string]string{})
c.Assert(err, check.IsNil)
defer user.Remove(usr.Name)
err = user.AddKey(usr.Name, map[string]string{"keyname": rawKey})
c.Assert(err, check.IsNil)
defer user.RemoveKey(usr.Name, "keyname")
b := strings.NewReader(otherKey)
recorder, request := put(fmt.Sprintf("/user/%s/key/keyname", usr.Name), b, c)
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, 200)
c.Assert(recorder.Body.String(), check.Equals, `Key "keyname" successfully updated!`)
var k user.Key
conn, err := db.Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
err = conn.Key().Find(bson.M{"name": "keyname", "username": usr.Name}).One(&k)
c.Assert(err, check.IsNil)
c.Assert(k.Body, check.Equals, otherKey+"\n")
}

func (s *S) TestUpdateKeyUserNotFound(c *check.C) {
b := strings.NewReader(rawKey)
recorder, request := put("/user/frodo/key/keyname", b, c)
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, user.ErrUserNotFound.Error()+"\n")
}

func (s *S) TestUpdateKeyNotFound(c *check.C) {
usr, err := user.New("Frodo", map[string]string{})
c.Assert(err, check.IsNil)
defer user.Remove(usr.Name)
b := strings.NewReader(rawKey)
recorder, request := put(fmt.Sprintf("/user/%s/key/keyname", usr.Name), b, c)
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, user.ErrKeyNotFound.Error()+"\n")
}

func (s *S) TestUpdateKeyInvalidKey(c *check.C) {
usr, err := user.New("Frodo", map[string]string{})
c.Assert(err, check.IsNil)
defer user.Remove(usr.Name)
err = user.AddKey(usr.Name, map[string]string{"keyname": rawKey})
c.Assert(err, check.IsNil)
defer user.RemoveKey(usr.Name, "keyname")
b := strings.NewReader("invalid-key")
recorder, request := put(fmt.Sprintf("/user/%s/key/keyname", usr.Name), b, c)
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusBadRequest)
c.Assert(recorder.Body.String(), check.Equals, user.ErrInvalidKey.Error()+"\n")
}

func (s *S) TestAddPostReceiveHookRepository(c *check.C) {
b := strings.NewReader(`{"repositories": ["some-repo"], "content": "some content"}`)
recorder, request := post("/hook/post-receive", b, c)
Expand Down

0 comments on commit 274d80b

Please sign in to comment.