From 252b68a84fd029ec3861287115d198c1793592f3 Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Tue, 10 Feb 2015 16:07:55 -0200 Subject: [PATCH] gandalftest: add listKeys to the testing server --- gandalftest/server.go | 20 ++++++++++++++++++++ gandalftest/server_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/gandalftest/server.go b/gandalftest/server.go index a38ea25..cf68d86 100644 --- a/gandalftest/server.go +++ b/gandalftest/server.go @@ -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)) @@ -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() diff --git a/gandalftest/server_test.go b/gandalftest/server_test.go index 308f2e2..bec4c11 100644 --- a/gandalftest/server_test.go +++ b/gandalftest/server_test.go @@ -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)