From 64da5721ee1c29db1835b2870bba41ca9852262a Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Thu, 19 Feb 2015 11:16:24 -0200 Subject: [PATCH] api: improve status codes for the newRepository handler Related to #183. --- api/handler.go | 13 ++++++++++--- api/handler_test.go | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/handler.go b/api/handler.go index e457619..29633ba 100644 --- a/api/handler.go +++ b/api/handler.go @@ -206,12 +206,19 @@ func newRepository(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - rep, err := repository.New(repo.Name, repo.Users, repo.ReadOnlyUsers, repo.IsPublic) + _, err := repository.New(repo.Name, repo.Users, repo.ReadOnlyUsers, repo.IsPublic) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + status := http.StatusInternalServerError + if err == repository.ErrRepositoryAlreadyExists { + status = http.StatusConflict + } + if _, ok := err.(*repository.InvalidRepositoryError); ok { + status = http.StatusBadRequest + } + http.Error(w, err.Error(), status) return } - fmt.Fprintf(w, "Repository \"%s\" successfully created\n", rep.Name) + fmt.Fprintf(w, "Repository \"%s\" successfully created\n", repo.Name) } func getRepository(w http.ResponseWriter, r *http.Request) { diff --git a/api/handler_test.go b/api/handler_test.go index 2880d59..1120d16 100644 --- a/api/handler_test.go +++ b/api/handler_test.go @@ -278,13 +278,29 @@ func (s *S) TestNewRepositoryShouldSaveUserIdInRepository(c *check.C) { c.Assert(len(p.Users), check.Not(check.Equals), 0) } +func (s *S) TestNewRepositoryDuplicate(c *check.C) { + b := strings.NewReader(`{"name": "myRepository", "users": ["r2d2"]}`) + recorder, request := post("/repository", b, c) + s.router.ServeHTTP(recorder, request) + conn, err := db.Conn() + c.Assert(err, check.IsNil) + defer conn.Close() + collection := conn.Repository() + defer collection.Remove(bson.M{"_id": "myRepository"}) + b = strings.NewReader(`{"name": "myRepository", "users": ["r2d2"]}`) + recorder, request = post("/repository", b, c) + s.router.ServeHTTP(recorder, request) + c.Assert(recorder.Code, check.Equals, http.StatusConflict) + c.Assert(recorder.Body.String(), check.Equals, "repository already exists\n") +} + func (s *S) TestNewRepositoryShouldReturnErrorWhenNoUserIsPassed(c *check.C) { b := strings.NewReader(`{"name": "myRepository"}`) recorder, request := post("/repository", b, c) s.router.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, 400) body := readBody(recorder.Body, c) - expected := "Validation Error: repository should have at least one user" + expected := "repository should have at least one user" got := strings.Replace(body, "\n", "", -1) c.Assert(got, check.Equals, expected) } @@ -295,7 +311,7 @@ func (s *S) TestNewRepositoryShouldReturnErrorWhenNoParametersArePassed(c *check s.router.ServeHTTP(recorder, request) c.Assert(recorder.Code, check.Equals, 400) body := readBody(recorder.Body, c) - expected := "Validation Error: repository name is not valid" + expected := "repository name is not valid" got := strings.Replace(body, "\n", "", -1) c.Assert(got, check.Equals, expected) }