示例#1
0
func (s *S) TestAddKeyShouldNotAcceptRepeatedKeysForDifferentUsers(c *gocheck.C) {
	usr, err := user.New("Frodo", map[string]string{"keyname": rawKey})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(usr.Name)
	usr2, err := user.New("tempo", nil)
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(usr2.Name)
	b := strings.NewReader(fmt.Sprintf(`{"keyname": %q}`, rawKey))
	recorder, request := post(fmt.Sprintf("/user/%s/key?:name=%s", usr2.Name, usr2.Name), b, c)
	AddKey(recorder, request)
	got := readBody(recorder.Body, c)
	expected := "Key already exists.\n"
	c.Assert(got, gocheck.Equals, expected)
	c.Assert(recorder.Code, gocheck.Equals, http.StatusConflict)
}
示例#2
0
func (s *S) TestRemoveKeyShouldRemoveKeyFromAuthorizedKeysFile(c *gocheck.C) {
	u, err := user.New("Gandalf", map[string]string{"keyname": rawKey})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	content := s.authKeysContent(c)
	c.Assert(content, gocheck.Equals, "")
}
示例#3
0
func (s *S) TestRemoveKeyGivesExpectedSuccessResponse(c *gocheck.C) {
	u, err := user.New("Gandalf", map[string]string{"keyname": rawKey})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	c.Assert(recorder.Code, gocheck.Equals, 200)
	b := readBody(recorder.Body, c)
	c.Assert(b, gocheck.Equals, `Key "keyname" successfully removed`)
}
示例#4
0
func (s *S) TestRemoveKeyRemovesKeyFromDatabase(c *gocheck.C) {
	u, err := user.New("Gandalf", map[string]string{"keyname": rawKey})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	count, err := db.Session.Key().Find(bson.M{"name": "keyname", "username": "******"}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(count, gocheck.Equals, 0)
}
示例#5
0
func (s *S) TestRemoveKeyShouldRemoveKeyFromAuthorizedKeysFile(c *C) {
	k := "ssh-key somekey gandalf@host"
	u, err := user.New("Gandalf", map[string]string{"keyname": k})
	c.Assert(err, IsNil)
	defer db.Session.User().RemoveId(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	content := s.authKeysContent(c)
	c.Assert(content, Not(Matches), ".* "+k)
}
示例#6
0
func (s *S) TestRemoveKeyGivesExpectedSuccessResponse(c *C) {
	u, err := user.New("Gandalf", map[string]string{"keyname": "ssh-key somekey gandalf@host"})
	c.Assert(err, IsNil)
	defer db.Session.User().RemoveId(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	c.Assert(recorder.Code, Equals, 200)
	b := readBody(recorder.Body, c)
	c.Assert(b, Equals, `Key "keyname" successfully removed`)
}
示例#7
0
func (s *S) TestRemoveKeyRemovesKeyFromUserDocument(c *C) {
	k := "ssh-key somekey gandalf@host"
	u, err := user.New("Gandalf", map[string]string{"keyname": k})
	c.Assert(err, IsNil)
	defer db.Session.User().RemoveId(u.Name)
	url := "/user/Gandalf/key/keyname?:keyname=keyname&:name=Gandalf"
	recorder, request := del(url, nil, c)
	RemoveKey(recorder, request)
	err = db.Session.User().FindId(u.Name).One(&u)
	c.Assert(err, IsNil)
	c.Assert(u.Keys, DeepEquals, map[string]string{})
}
示例#8
0
func (s *S) TestAddKey(c *C) {
	user, err := user.New("Frodo", map[string]string{})
	c.Assert(err, IsNil)
	defer db.Session.User().RemoveId("Frodo")
	b := strings.NewReader(`{"keyname": "keycontent"}`)
	recorder, request := post(fmt.Sprintf("/user/%s/key?:name=%s", user.Name, user.Name), b, c)
	AddKey(recorder, request)
	got := readBody(recorder.Body, c)
	expected := "Key(s) successfully created"
	c.Assert(got, Equals, expected)
	c.Assert(recorder.Code, Equals, 200)
}
示例#9
0
func (s *S) TestAddKeyShouldReturnProperStatusCodeWhenKeyAlreadyExists(c *C) {
	user, err := user.New("Frodo", map[string]string{"keyname": "keycontent"})
	c.Assert(err, IsNil)
	defer db.Session.User().RemoveId("Frodo")
	b := strings.NewReader(`{"keyname": "keycontent"}`)
	recorder, request := post(fmt.Sprintf("/user/%s/key?:name=%s", user.Name, user.Name), b, c)
	AddKey(recorder, request)
	got := readBody(recorder.Body, c)
	expected := "Key already exists.\n"
	c.Assert(got, Equals, expected)
	c.Assert(recorder.Code, Equals, http.StatusConflict)
}
示例#10
0
func NewUser(w http.ResponseWriter, r *http.Request) {
	var usr jsonUser
	if err := parseBody(r.Body, &usr); err != nil {
		http.Error(w, "Got error while parsing body: "+err.Error(), http.StatusBadRequest)
		return
	}
	u, err := user.New(usr.Name, usr.Keys)
	if err != nil {
		http.Error(w, "Got error while creating user: "******"User \"%s\" successfully created\n", u.Name)
}
示例#11
0
func (s *S) TestRemoveUser(c *gocheck.C) {
	u, err := user.New("username", map[string]string{})
	c.Assert(err, gocheck.IsNil)
	url := fmt.Sprintf("/user/%s/?:name=%s", u.Name, u.Name)
	request, err := http.NewRequest("DELETE", url, nil)
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	RemoveUser(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, "User \"username\" successfully removed\n")
}
示例#12
0
func (s *S) TestRemoveUserShouldRemoveFromDB(c *gocheck.C) {
	u, err := user.New("anuser", map[string]string{})
	c.Assert(err, gocheck.IsNil)
	url := fmt.Sprintf("/user/%s/?:name=%s", u.Name, u.Name)
	request, err := http.NewRequest("DELETE", url, nil)
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	RemoveUser(recorder, request)
	collection := db.Session.User()
	lenght, err := collection.Find(bson.M{"_id": u.Name}).Count()
	c.Assert(err, gocheck.IsNil)
	c.Assert(lenght, gocheck.Equals, 0)
}
示例#13
0
func (s *S) TestListKeysWithoutKeysGivesEmptyJSON(c *gocheck.C) {
	u, err := user.New("Gandalf", map[string]string{})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(u.Name)
	url := "/user/Gandalf/keys?:name=Gandalf"
	request, err := http.NewRequest("GET", url, nil)
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	ListKeys(recorder, request)
	c.Assert(recorder.Code, gocheck.Equals, 200)
	b := readBody(recorder.Body, c)
	c.Assert(b, gocheck.Equals, "{}")
}
func (s *S) SetUpSuite(c *gocheck.C) {
	var err error
	log, err = syslog.New(syslog.LOG_INFO, "gandalf-listener")
	c.Check(err, gocheck.IsNil)
	err = config.ReadConfigFile("../etc/gandalf.conf")
	c.Check(err, gocheck.IsNil)
	config.Set("database:name", "gandalf_bin_tests")
	db.Connect()
	s.user, err = user.New("testuser", map[string]string{})
	c.Check(err, gocheck.IsNil)
	// does not uses repository.New to avoid creation of bare git repo
	s.repo = &repository.Repository{Name: "myapp", Users: []string{s.user.Name}}
	err = db.Session.Repository().Insert(s.repo)
	c.Check(err, gocheck.IsNil)
}
示例#15
0
func (s *S) TestAddKey(c *gocheck.C) {
	usr, err := user.New("Frodo", map[string]string{})
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(usr.Name)
	b := strings.NewReader(fmt.Sprintf(`{"keyname": %q}`, rawKey))
	recorder, request := post(fmt.Sprintf("/user/%s/key?:name=%s", usr.Name, usr.Name), b, c)
	AddKey(recorder, request)
	got := readBody(recorder.Body, c)
	expected := "Key(s) successfully created"
	c.Assert(got, gocheck.Equals, expected)
	c.Assert(recorder.Code, gocheck.Equals, 200)
	var k user.Key
	err = db.Session.Key().Find(bson.M{"name": "keyname", "username": usr.Name}).One(&k)
	c.Assert(err, gocheck.IsNil)
	c.Assert(k.Body, gocheck.Equals, keyBody)
	c.Assert(k.Comment, gocheck.Equals, keyComment)
}
示例#16
0
func (s *S) TestListKeysGivesExpectedSuccessResponse(c *gocheck.C) {
	keys := map[string]string{"key1": rawKey, "key2": otherKey}
	u, err := user.New("Gandalf", keys)
	c.Assert(err, gocheck.IsNil)
	defer user.Remove(u.Name)
	url := "/user/Gandalf/keys?:name=Gandalf"
	request, err := http.NewRequest("GET", url, nil)
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	ListKeys(recorder, request)
	c.Assert(recorder.Code, gocheck.Equals, 200)
	body, err := ioutil.ReadAll(recorder.Body)
	c.Assert(err, gocheck.IsNil)
	var data map[string]string
	err = json.Unmarshal(body, &data)
	c.Assert(err, gocheck.IsNil)
	c.Assert(data, gocheck.DeepEquals, keys)
}
示例#17
0
func (s *S) TestGrantAccessUpdatesReposDocument(c *gocheck.C) {
	u, err := user.New("pippin", map[string]string{})
	defer db.Session.User().Remove(bson.M{"_id": "pippin"})
	c.Assert(err, gocheck.IsNil)
	r := repository.Repository{Name: "onerepo"}
	err = db.Session.Repository().Insert(&r)
	c.Assert(err, gocheck.IsNil)
	defer db.Session.Repository().Remove(bson.M{"_id": r.Name})
	r2 := repository.Repository{Name: "otherepo"}
	err = db.Session.Repository().Insert(&r2)
	c.Assert(err, gocheck.IsNil)
	defer db.Session.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 := del("/repository/grant", b, c)
	GrantAccess(rec, req)
	var repos []repository.Repository
	err = db.Session.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})
	}
}