Esempio n. 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)
}
Esempio n. 2
0
func RemoveUser(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get(":name")
	if err := user.Remove(name); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	fmt.Fprintf(w, "User \"%s\" successfully removed\n", name)
}
Esempio n. 3
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, "")
}
Esempio n. 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)
}
Esempio n. 5
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`)
}
Esempio n. 6
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, "{}")
}
Esempio n. 7
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)
}
Esempio n. 8
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)
}