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

Commit

Permalink
gandalftest: implement getDiff in the testing server
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Feb 11, 2015
1 parent 4bf417e commit 3e6abf7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
44 changes: 38 additions & 6 deletions gandalftest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
)

type Repository struct {
Name string `json:"name"`
Users []string `json:"users"`
ReadOnlyUsers []string `json:",omit"`
ReadOnlyURL string `json:"git_url"`
ReadWriteURL string `json:"ssh_url"`
IsPublic bool `json:"ispublic"`
Name string `json:"name"`
Users []string `json:"users"`
ReadOnlyUsers []string `json:"readonlyusers"`
ReadOnlyURL string `json:"git_url"`
ReadWriteURL string `json:"ssh_url"`
IsPublic bool `json:"ispublic"`
Diffs chan string `json:"-"`
}

type user struct {
Expand Down Expand Up @@ -169,6 +170,17 @@ func (s *GandalfServer) Keys(user string) (map[string]string, error) {
return keyMap, nil
}

// PrepareDiff prepares a diff for the given repository and writes it to the
// next getDiff call in the server.
func (s *GandalfServer) PrepareDiff(repository, content string) {
if repo, index := s.findRepository(repository); index > -1 {
repo.Diffs <- content
s.repoLock.Lock()
s.repos[index] = repo
s.repoLock.Unlock()
}
}

// Reset resets all internal information of the server, like keys, repositories, users and prepared failures.
func (s *GandalfServer) Reset() {
s.usersLock.Lock()
Expand Down Expand Up @@ -197,6 +209,7 @@ func (s *GandalfServer) buildMuxer() {
s.muxer.Post("/repository/grant", http.HandlerFunc(s.grantAccess))
s.muxer.Delete("/repository/revoke", http.HandlerFunc(s.revokeAccess))
s.muxer.Post("/repository", http.HandlerFunc(s.createRepository))
s.muxer.Get("/repository/{name}/diff/commits", http.HandlerFunc(s.getDiff))
s.muxer.Delete("/repository/{name}", http.HandlerFunc(s.removeRepository))
s.muxer.Get("/repository/{name}", http.HandlerFunc(s.getRepository))
s.muxer.Get("/healthcheck", http.HandlerFunc(s.healthcheck))
Expand Down Expand Up @@ -247,6 +260,7 @@ func (s *GandalfServer) createRepository(w http.ResponseWriter, r *http.Request)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
repo.Diffs = make(chan string, 1)
users := append(repo.Users, repo.ReadOnlyUsers...)
for _, userName := range users {
_, index := s.findUser(userName)
Expand Down Expand Up @@ -295,6 +309,24 @@ func (s *GandalfServer) getRepository(w http.ResponseWriter, r *http.Request) {
}
}

func (s *GandalfServer) getDiff(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
repo, index := s.findRepository(name)
if index < 0 {
http.Error(w, "repository not found", http.StatusNotFound)
return
}
var content string
select {
case content = <-repo.Diffs:
default:
}
fmt.Fprint(w, content)
s.repoLock.Lock()
s.repos[index] = repo
s.repoLock.Unlock()
}

func (s *GandalfServer) grantAccess(w http.ResponseWriter, r *http.Request) {
readOnly := r.URL.Query().Get("readonly") == "yes"
repositories, users, err := s.validateAccessRequest(r)
Expand Down
38 changes: 38 additions & 0 deletions gandalftest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,44 @@ func (s *S) TestGetRepositoryNotFound(c *check.C) {
c.Assert(recorder.Body.String(), check.Equals, "repository not found\n")
}

func (s *S) TestGetDiff(c *check.C) {
repo := Repository{Name: "somerepo", Diffs: make(chan string, 1)}
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.repos = []Repository{repo}
server.PrepareDiff(repo.Name, "some diff")
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
c.Assert(recorder.Body.String(), check.Equals, "some diff")
}

func (s *S) TestGetDiffUnprepared(c *check.C) {
repo := Repository{Name: "somerepo", Diffs: make(chan string, 1)}
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.repos = []Repository{repo}
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
c.Assert(recorder.Body.String(), check.Equals, "")
}

func (s *S) TestGetDiffNotFound(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", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, "repository not found\n")
}

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

0 comments on commit 3e6abf7

Please sign in to comment.