本文整理汇总了Golang中github.com/tsuru/gandalf/user.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAddKeyShouldNotAcceptRepeatedKeysForDifferentUsers
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: TestGrantAccessUpdatesReposDocument
func (s *S) TestGrantAccessUpdatesReposDocument(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"}
err = conn.Repository().Insert(&r)
c.Assert(err, gocheck.IsNil)
defer conn.Repository().Remove(bson.M{"_id": r.Name})
r2 := repository.Repository{Name: "otherepo"}
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 := del("/repository/grant", b, c)
GrantAccess(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})
}
}
示例3: TestRemoveKeyShouldRemoveKeyFromAuthorizedKeysFile
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, "")
}
示例4: TestRemoveKeyGivesExpectedSuccessResponse
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`)
}
示例5: TestHasReadPermissionShouldReturnTrueWhenRepositoryIsNotPublicAndUserHasPermissionToRead
func (s *S) TestHasReadPermissionShouldReturnTrueWhenRepositoryIsNotPublicAndUserHasPermissionToRead(c *check.C) {
user, err := user.New("readonlyuser", map[string]string{})
c.Check(err, check.IsNil)
repo := &repository.Repository{
Name: "otherapp",
Users: []string{s.user.Name},
ReadOnlyUsers: []string{user.Name},
}
allowed := hasReadPermission(user, repo)
c.Assert(allowed, check.Equals, true)
}
示例6: NewUser
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: "+err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "User \"%s\" successfully created\n", u.Name)
}
示例7: TestRemoveUser
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")
}
示例8: TestListKeysWithoutKeysGivesEmptyJSON
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, "{}")
}
示例9: TestRemoveKeyRemovesKeyFromDatabase
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)
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
count, err := conn.Key().Find(bson.M{"name": "keyname", "username": "Gandalf"}).Count()
c.Assert(err, gocheck.IsNil)
c.Assert(count, gocheck.Equals, 0)
}
示例10: TestRemoveUserShouldRemoveFromDB
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)
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
collection := conn.User()
lenght, err := collection.Find(bson.M{"_id": u.Name}).Count()
c.Assert(err, gocheck.IsNil)
c.Assert(lenght, gocheck.Equals, 0)
}
示例11: SetUpSuite
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")
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}}
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
err = conn.Repository().Insert(s.repo)
c.Check(err, gocheck.IsNil)
}
示例12: TestListKeysGivesExpectedSuccessResponse
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)
}
示例13: newUser
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 {
status := http.StatusInternalServerError
if err == user.ErrUserAlreadyExists {
status = http.StatusConflict
}
if _, ok := err.(*user.InvalidUserError); ok {
status = http.StatusBadRequest
}
http.Error(w, err.Error(), status)
return
}
fmt.Fprintf(w, "User \"%s\" successfully created\n", u.Name)
}
示例14: TestAddKey
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
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
err = conn.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)
}