func NewRepository(w http.ResponseWriter, r *http.Request) { var repo repository.Repository if err := parseBody(r.Body, &repo); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } rep, err := repository.New(repo.Name, repo.Users, repo.IsPublic) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Fprintf(w, "Repository \"%s\" successfully created\n", rep.Name) }
func (s *S) TestRemoveRepository(c *gocheck.C) { r, err := repository.New("myRepo", []string{"pippin"}, true) c.Assert(err, gocheck.IsNil) url := fmt.Sprintf("repository/%s/?:name=%s", r.Name, r.Name) request, err := http.NewRequest("DELETE", url, nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() RemoveRepository(recorder, request) c.Assert(recorder.Code, gocheck.Equals, 200) b, err := ioutil.ReadAll(recorder.Body) c.Assert(err, gocheck.IsNil) c.Assert(string(b), gocheck.Equals, "Repository \"myRepo\" successfully removed\n") }
func (s *S) TestRemoveRepositoryShouldRemoveFromDB(c *gocheck.C) { r, err := repository.New("myRepo", []string{"pippin"}, true) c.Assert(err, gocheck.IsNil) url := fmt.Sprintf("repository/%s/?:name=%s", r.Name, r.Name) request, err := http.NewRequest("DELETE", url, nil) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() RemoveRepository(recorder, request) conn, err := db.Conn() c.Assert(err, gocheck.IsNil) defer conn.Close() err = conn.Repository().Find(bson.M{"_id": r.Name}).One(&r) c.Assert(err, gocheck.ErrorMatches, "^not found$") }
func (s *S) TestRenameRepository(c *gocheck.C) { r, err := repository.New("raising", []string{"*****@*****.**"}, true) c.Assert(err, gocheck.IsNil) url := fmt.Sprintf("/repository/%s/?:name=%s", r.Name, r.Name) body := strings.NewReader(`{"name":"freedom"}`) request, err := http.NewRequest("PUT", url, body) c.Assert(err, gocheck.IsNil) recorder := httptest.NewRecorder() RenameRepository(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") c.Assert(err, gocheck.IsNil) c.Assert(repo, gocheck.DeepEquals, *r) }
func newRepository(w http.ResponseWriter, r *http.Request) { var repo repository.Repository if err := parseBody(r.Body, &repo); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } _, err := repository.New(repo.Name, repo.Users, repo.ReadOnlyUsers, repo.IsPublic) if err != nil { 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", repo.Name) }