This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from ricardodani/master
Added support to update repository data
- Loading branch information
Showing
4 changed files
with
219 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,6 +309,37 @@ func (s *S) TestParseBodyShouldMapBodyJsonToGivenStruct(c *gocheck.C) { | |
c.Assert(p.Name, gocheck.Equals, expected) | ||
} | ||
|
||
func (s *S) TestParseBodyShouldMapBodyEmptyJsonToADict(c *gocheck.C) { | ||
dict := make(map[string]interface{}) | ||
b := bufferCloser{bytes.NewBufferString(`{"name": "Test", "isPublic": false, "users": []}`)} | ||
err := parseBody(b, &dict) | ||
c.Assert(err, gocheck.IsNil) | ||
expected := map[string]interface{}{ | ||
"name": "Test", | ||
"isPublic": false, | ||
"users": []interface{}{}, | ||
} | ||
c.Assert(dict, gocheck.DeepEquals, expected) | ||
} | ||
|
||
func (s *S) TestParseBodyShouldMapBodyJsonAndUpdateMap(c *gocheck.C) { | ||
dict := map[string]interface{}{ | ||
"isPublic": false, | ||
"users": []string{"merry"}, | ||
"readonlyusers": []string{"pippin"}, | ||
} | ||
b := bufferCloser{bytes.NewBufferString(`{"name": "Test", "users": []}`)} | ||
err := parseBody(b, &dict) | ||
c.Assert(err, gocheck.IsNil) | ||
expected := map[string]interface{}{ | ||
"name": "Test", | ||
"isPublic": false, | ||
"users": []interface{}{}, | ||
"readonlyusers": []string{"pippin"}, | ||
} | ||
c.Assert(dict, gocheck.DeepEquals, expected) | ||
} | ||
|
||
func (s *S) TestParseBodyShouldReturnErrorWhenJsonIsInvalid(c *gocheck.C) { | ||
var p repository.Repository | ||
b := bufferCloser{bytes.NewBufferString("{]ja9aW}")} | ||
|
@@ -342,67 +373,6 @@ func (s *S) TestNewRepositoryShouldReturnErrorWhenBodyIsEmpty(c *gocheck.C) { | |
c.Assert(recorder.Code, gocheck.Equals, 400) | ||
} | ||
|
||
func (s *S) TestSetAccessShouldReturnErrorWhenBodyIsEmpty(c *gocheck.C) { | ||
b := strings.NewReader("") | ||
recorder, request := put("/repository/set", b, c) | ||
s.router.ServeHTTP(recorder, request) | ||
c.Assert(recorder.Code, gocheck.Equals, 400) | ||
} | ||
|
||
func (s *S) TestSetAccessUpdatesReposDocument(c *gocheck.C) { | ||
u, err := user.New("pippin", map[string]string{}) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.User().Remove(bson.M{"_id": "pippin"}) | ||
c.Assert(err, gocheck.IsNil) | ||
r := repository.Repository{Name: "onerepo", Users: []string{"oneuser"}} | ||
err = conn.Repository().Insert(&r) | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Repository().Remove(bson.M{"_id": r.Name}) | ||
r2 := repository.Repository{Name: "otherepo", Users: []string{"otheruser"}} | ||
err = conn.Repository().Insert(&r2) | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Repository().Remove(bson.M{"_id": r2.Name}) | ||
b := bytes.NewBufferString(fmt.Sprintf(`{"repositories": ["%s", "%s"], "users": ["%s"]}`, r.Name, r2.Name, u.Name)) | ||
rec, req := put("/repository/set", b, c) | ||
s.router.ServeHTTP(rec, req) | ||
var repos []repository.Repository | ||
err = conn.Repository().Find(bson.M{"_id": bson.M{"$in": []string{r.Name, r2.Name}}}).All(&repos) | ||
c.Assert(err, gocheck.IsNil) | ||
c.Assert(rec.Code, gocheck.Equals, 200) | ||
for _, repo := range repos { | ||
c.Assert(repo.Users, gocheck.DeepEquals, []string{u.Name}) | ||
} | ||
} | ||
|
||
func (s *S) TestSetReadonlyAccessUpdatesReposDocument(c *gocheck.C) { | ||
u, err := user.New("pippin", map[string]string{}) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.User().Remove(bson.M{"_id": "pippin"}) | ||
c.Assert(err, gocheck.IsNil) | ||
r := repository.Repository{Name: "onerepo", ReadOnlyUsers: []string{"oneuser"}} | ||
err = conn.Repository().Insert(&r) | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Repository().Remove(bson.M{"_id": r.Name}) | ||
r2 := repository.Repository{Name: "otherepo", ReadOnlyUsers: []string{"otheruser"}} | ||
err = conn.Repository().Insert(&r2) | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Repository().Remove(bson.M{"_id": r2.Name}) | ||
b := bytes.NewBufferString(fmt.Sprintf(`{"repositories": ["%s", "%s"], "users": ["%s"]}`, r.Name, r2.Name, u.Name)) | ||
rec, req := put("/repository/set?readonly=yes", b, c) | ||
s.router.ServeHTTP(rec, req) | ||
var repos []repository.Repository | ||
err = conn.Repository().Find(bson.M{"_id": bson.M{"$in": []string{r.Name, r2.Name}}}).All(&repos) | ||
c.Assert(err, gocheck.IsNil) | ||
c.Assert(rec.Code, gocheck.Equals, 200) | ||
for _, repo := range repos { | ||
c.Assert(repo.ReadOnlyUsers, gocheck.DeepEquals, []string{u.Name}) | ||
} | ||
} | ||
|
||
func (s *S) TestGrantAccessUpdatesReposDocument(c *gocheck.C) { | ||
u, err := user.New("pippin", map[string]string{}) | ||
conn, err := db.Conn() | ||
|
@@ -949,27 +919,96 @@ func (s *S) TestRemoveRepositoryShouldReturnErrorMsgWhenRepoDoesNotExist(c *goch | |
c.Assert(string(b), gocheck.Equals, "Could not remove repository: not found\n") | ||
} | ||
|
||
func (s *S) TestRenameRepository(c *gocheck.C) { | ||
r, err := repository.New("raising", []string{"[email protected]"}, []string{""}, true) | ||
func (s *S) TestUpdateRespositoryShouldReturnErrorWhenBodyIsEmpty(c *gocheck.C) { | ||
r, err := repository.New("something", []string{"[email protected]"}, []string{""}, true) | ||
c.Assert(err, gocheck.IsNil) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
b := strings.NewReader("") | ||
recorder, request := put("/repository/something", b, c) | ||
s.router.ServeHTTP(recorder, request) | ||
c.Assert(recorder.Code, gocheck.Equals, 400) | ||
} | ||
|
||
func (s *S) TestUpdateRepositoryData(c *gocheck.C) { | ||
r, err := repository.New("something", []string{"[email protected]"}, []string{""}, true) | ||
c.Assert(err, gocheck.IsNil) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
url := fmt.Sprintf("/repository/%s", r.Name) | ||
body := strings.NewReader(`{"name":"freedom"}`) | ||
body := strings.NewReader(`{"users": ["b"], "readonlyusers": ["a"], "ispublic": false}`) | ||
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 = "freedom" | ||
repo, err := repository.Get("freedom") | ||
r.Users = []string{"b"} | ||
r.ReadOnlyUsers = []string{"a"} | ||
r.IsPublic = false | ||
repo, err := repository.Get("something") | ||
c.Assert(err, gocheck.IsNil) | ||
c.Assert(repo, gocheck.DeepEquals, *r) | ||
} | ||
|
||
func (s *S) TestUpdateRepositoryDataPartial(c *gocheck.C) { | ||
r, err := repository.New("something", []string{"pippin"}, []string{"merry"}, true) | ||
c.Assert(err, gocheck.IsNil) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
url := fmt.Sprintf("/repository/%s", r.Name) | ||
body := strings.NewReader(`{"readonlyusers": ["a", "b"]}`) | ||
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) | ||
r.Users = []string{"pippin"} | ||
r.ReadOnlyUsers = []string{"a", "b"} | ||
r.IsPublic = true | ||
repo, err := repository.Get("something") | ||
c.Assert(err, gocheck.IsNil) | ||
c.Assert(repo, gocheck.DeepEquals, *r) | ||
} | ||
|
||
func (s *S) TestUpdateRepositoryNotFound(c *gocheck.C) { | ||
url := "/repository/foo" | ||
body := strings.NewReader(`{"ispublic":true}`) | ||
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.StatusNotFound) | ||
} | ||
|
||
func (s *S) TestUpdateRepositoryInvalidJSON(c *gocheck.C) { | ||
r, err := repository.New("bar", []string{"[email protected]"}, []string{""}, true) | ||
c.Assert(err, gocheck.IsNil) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
url := "/repository/bar" | ||
body := strings.NewReader(`{"name""`) | ||
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.StatusBadRequest) | ||
} | ||
|
||
func (s *S) TestRenameRepositoryWithNamespace(c *gocheck.C) { | ||
r, err := repository.New("lift/raising", []string{"[email protected]"}, []string{}, true) | ||
c.Assert(err, gocheck.IsNil) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
url := fmt.Sprintf("/repository/%s/", r.Name) | ||
body := strings.NewReader(`{"name":"norestraint/freedom"}`) | ||
request, err := http.NewRequest("PUT", url, body) | ||
|
@@ -986,6 +1025,12 @@ func (s *S) TestRenameRepositoryWithNamespace(c *gocheck.C) { | |
} | ||
|
||
func (s *S) TestRenameRepositoryInvalidJSON(c *gocheck.C) { | ||
r, err := repository.New("foo", []string{"[email protected]"}, []string{}, true) | ||
conn, err := db.Conn() | ||
c.Assert(err, gocheck.IsNil) | ||
defer conn.Close() | ||
defer conn.Repository().RemoveId(r.Name) | ||
c.Assert(err, gocheck.IsNil) | ||
url := "/repository/foo" | ||
body := strings.NewReader(`{"name""`) | ||
request, err := http.NewRequest("PUT", url, body) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.