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

Commit

Permalink
Update routes to support namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Aug 28, 2014
1 parent 1a58fbc commit 71d712e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
22 changes: 11 additions & 11 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ func SetupRouter() *pat.Router {
router.Post("/repository/grant", http.HandlerFunc(GrantAccess))
router.Post("/repository", http.HandlerFunc(NewRepository))
router.Delete("/repository/revoke", http.HandlerFunc(RevokeAccess))
router.Delete("/repository/{name}", http.HandlerFunc(RemoveRepository))
router.Get("/repository/{name}", http.HandlerFunc(GetRepository))
router.Put("/repository/{name}", http.HandlerFunc(RenameRepository))
router.Get("/repository/{name}/archive", http.HandlerFunc(GetArchive))
router.Get("/repository/{name}/contents", http.HandlerFunc(GetFileContents))
router.Get("/repository/{name}/tree", http.HandlerFunc(GetTree))
router.Get("/repository/{name}/branches", http.HandlerFunc(GetBranches))
router.Get("/repository/{name}/tags", http.HandlerFunc(GetTags))
router.Get("/repository/{name}/diff/commits", http.HandlerFunc(GetDiff))
router.Post("/repository/{name}/commit", http.HandlerFunc(Commit))
router.Get("/repository/{name}/logs", http.HandlerFunc(GetLog))
router.Delete("/repository/{name:[^/]*/?[^/]+}", http.HandlerFunc(RemoveRepository))
router.Get("/repository/{name:[^/]*/?[^/]+}", http.HandlerFunc(GetRepository))
router.Put("/repository/{name:[^/]*/?[^/]+}", http.HandlerFunc(RenameRepository))
router.Get("/repository/{name:[^/]*/?[^/]+}/archive", http.HandlerFunc(GetArchive))
router.Get("/repository/{name:[^/]*/?[^/]+}/contents", http.HandlerFunc(GetFileContents))
router.Get("/repository/{name:[^/]*/?[^/]+}/tree", http.HandlerFunc(GetTree))
router.Get("/repository/{name:[^/]*/?[^/]+}/branches", http.HandlerFunc(GetBranches))
router.Get("/repository/{name:[^/]*/?[^/]+}/tags", http.HandlerFunc(GetTags))
router.Get("/repository/{name:[^/]*/?[^/]+}/diff/commits", http.HandlerFunc(GetDiff))
router.Post("/repository/{name:[^/]*/?[^/]+}/commit", http.HandlerFunc(Commit))
router.Get("/repository/{name:[^/]*/?[^/]+}/logs", http.HandlerFunc(GetLog))
router.Get("/healthcheck", http.HandlerFunc(HealthCheck))
router.Post("/hook/{name}", http.HandlerFunc(AddHook))
return router
Expand Down
42 changes: 42 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ func (s *S) TestGetRepository(c *gocheck.C) {
c.Assert(data, gocheck.DeepEquals, expected)
}

func (s *S) TestGetRepositoryWithNamespace(c *gocheck.C) {
r := repository.Repository{Name: "onenamespace/onerepo"}
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
err = conn.Repository().Insert(&r)
c.Assert(err, gocheck.IsNil)
defer conn.Repository().Remove(bson.M{"_id": r.Name})
recorder, request := get("/repository/onenamespace/onerepo", nil, c)
s.router.ServeHTTP(recorder, request)
body, err := ioutil.ReadAll(recorder.Body)
c.Assert(err, gocheck.IsNil)
var data map[string]interface{}
err = json.Unmarshal(body, &data)
c.Assert(err, gocheck.IsNil)
expected := map[string]interface{}{
"name": r.Name,
"public": r.IsPublic,
"ssh_url": r.ReadWriteURL(),
"git_url": r.ReadOnlyURL(),
}
c.Assert(data, gocheck.DeepEquals, expected)
}

func (s *S) TestGetRepositoryDoesNotExist(c *gocheck.C) {
recorder, request := get("/repository/doesnotexists", nil, c)
s.router.ServeHTTP(recorder, request)
Expand Down Expand Up @@ -827,6 +851,24 @@ func (s *S) TestRenameRepository(c *gocheck.C) {
c.Assert(repo, gocheck.DeepEquals, *r)
}

func (s *S) TestRenameRepositoryWithNamespace(c *gocheck.C) {
r, err := repository.New("lift/raising", []string{"guardian@what.com"}, true)
c.Assert(err, gocheck.IsNil)
url := fmt.Sprintf("/repository/%s/", r.Name)
body := strings.NewReader(`{"name":"norestraint/freedom"}`)
request, err := http.NewRequest("PUT", url, body)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
s.router.ServeHTTP(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
_, err = repository.Get("raising")
c.Assert(err, gocheck.NotNil)
r.Name = "norestraint/freedom"
repo, err := repository.Get("norestraint/freedom")
c.Assert(err, gocheck.IsNil)
c.Assert(repo, gocheck.DeepEquals, *r)
}

func (s *S) TestRenameRepositoryInvalidJSON(c *gocheck.C) {
url := "/repository/foo"
body := strings.NewReader(`{"name""`)
Expand Down

0 comments on commit 71d712e

Please sign in to comment.