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

Commit

Permalink
api: improve status codes for the newRepository handler
Browse files Browse the repository at this point in the history
Related to #183.
  • Loading branch information
Francisco Souza committed Feb 19, 2015
1 parent ee6ea9e commit 64da572
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
13 changes: 10 additions & 3 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 64da572

Please sign in to comment.