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

Commit

Permalink
gandalftest: add listKeys to the testing server
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Feb 10, 2015
1 parent 826f7a2 commit 252b68a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gandalftest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s *GandalfServer) buildMuxer() {
s.muxer = pat.New()
s.muxer.Post("/user/{name}/key", http.HandlerFunc(s.addKeys))
s.muxer.Delete("/user/{name}/key/{keyname}", http.HandlerFunc(s.removeKey))
s.muxer.Get("/user/{name}/keys", http.HandlerFunc(s.listKeys))
s.muxer.Post("/user", http.HandlerFunc(s.createUser))
s.muxer.Delete("/user/{name}", http.HandlerFunc(s.removeUser))
s.muxer.Post("/repository", http.HandlerFunc(s.createRepository))
Expand Down Expand Up @@ -260,6 +261,25 @@ func (s *GandalfServer) removeKey(w http.ResponseWriter, r *http.Request) {
s.keys[userName] = userKeys[:last]
}

func (s *GandalfServer) listKeys(w http.ResponseWriter, r *http.Request) {
userName := r.URL.Query().Get(":name")
s.usersLock.RLock()
defer s.usersLock.RUnlock()
keys, ok := s.keys[userName]
if !ok {
http.Error(w, "user not found", http.StatusNotFound)
return
}
keysMap := make(map[string]string, len(keys))
for _, k := range keys {
keysMap[k.Name] = k.Body
}
err := json.NewEncoder(w).Encode(keysMap)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func (s *GandalfServer) findUser(name string) (userName string, index int) {
s.usersLock.RLock()
defer s.usersLock.RUnlock()
Expand Down
28 changes: 28 additions & 0 deletions gandalftest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,34 @@ func (s *S) TestRemoveKeyKeyNotFound(c *check.C) {
c.Assert(recorder.Body.String(), check.Equals, "key not found\n")
}

func (s *S) TestListKeys(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.users = []string{"myuser"}
server.keys["myuser"] = []key{{Name: "theirkey", Body: "irrelevant"}, {Name: "mykey", Body: "not irrelevant"}}
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/myuser/keys", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
var result map[string]string
expected := map[string]string{"theirkey": "irrelevant", "mykey": "not irrelevant"}
err = json.NewDecoder(recorder.Body).Decode(&result)
c.Assert(err, check.IsNil)
c.Assert(result, check.DeepEquals, expected)
}

func (s *S) TestListKeysUserNotFound(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/user/myuser/keys", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, "user not found\n")
}

func (s *S) TestPrepareFailure(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
Expand Down

0 comments on commit 252b68a

Please sign in to comment.