Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
user: improve error handling on New
Browse files Browse the repository at this point in the history
Related to #183.
  • Loading branch information
Francisco Souza committed Feb 19, 2015
1 parent d03ccdf commit ebd46af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
30 changes: 19 additions & 11 deletions user/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 gandalf authors. All rights reserved.
// Copyright 2015 gandalf authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand All @@ -20,7 +20,12 @@ func init() {
log.Init()
}

var ErrUserNotFound = errors.New("User not found")
var (
ErrUserAlreadyExists = errors.New("user already exists")
ErrUserNotFound = errors.New("user not found")

userNameRegexp = regexp.MustCompile(`\s|[^aA-zZ0-9-+.@]|(^$)`)
)

type User struct {
Name string `bson:"_id"`
Expand All @@ -43,22 +48,17 @@ func New(name string, keys map[string]string) (*User, error) {
defer conn.Close()
if err := conn.User().Insert(&u); err != nil {
if mgo.IsDup(err) {
log.Errorf("user.New: %q duplicate user", name)
return u, errors.New("Could not create user: user already exists")
return nil, ErrUserAlreadyExists
}
log.Errorf("user.New: %s", err)
return u, err
return nil, err
}
return u, addKeys(keys, u.Name)
}

func (u *User) isValid() (isValid bool, err error) {
m, err := regexp.Match(`\s|[^aA-zZ0-9-+.@]|(^$)`, []byte(u.Name))
if err != nil {
panic(err)
}
if m {
return false, errors.New("Validation Error: user name is not valid")
if userNameRegexp.MatchString(u.Name) {
return false, &InvalidUserError{message: "username is not valid"}
}
return true, nil
}
Expand Down Expand Up @@ -151,3 +151,11 @@ func RemoveKey(uName, kName string) error {
}
return removeKey(kName, uName)
}

type InvalidUserError struct {
message string
}

func (err *InvalidUserError) Error() string {
return err.message
}
14 changes: 7 additions & 7 deletions user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (s *S) TestNewDuplicateUser(c *check.C) {
c.Assert(err, check.IsNil)
defer conn.User().Remove(bson.M{"_id": u.Name})
defer conn.Key().Remove(bson.M{"name": "somekey"})
u, err = New("someuser", map[string]string{"somekey": rawKey})
c.Assert(err, check.ErrorMatches, "Could not create user: user already exists")
_, err = New("someuser", map[string]string{"somekey": rawKey})
c.Assert(err, check.Equals, ErrUserAlreadyExists)
}

func (s *S) TestNewDuplicateUserDifferentKey(c *check.C) {
Expand All @@ -95,8 +95,8 @@ func (s *S) TestNewDuplicateUserDifferentKey(c *check.C) {
c.Assert(err, check.IsNil)
defer conn.User().Remove(bson.M{"_id": u.Name})
defer conn.Key().Remove(bson.M{"name": "somekey"})
u, err = New("someuser", map[string]string{"somedifferentkey": rawKey + "fakeKey"})
c.Assert(err, check.ErrorMatches, "Could not create user: user already exists")
_, err = New("someuser", map[string]string{"somedifferentkey": rawKey + "fakeKey"})
c.Assert(err, check.Equals, ErrUserAlreadyExists)
}

func (s *S) TestNewUserShouldStoreUserInDatabase(c *check.C) {
Expand All @@ -117,9 +117,9 @@ func (s *S) TestNewUserShouldStoreUserInDatabase(c *check.C) {
func (s *S) TestNewChecksIfUserIsValidBeforeStoring(c *check.C) {
_, err := New("", map[string]string{})
c.Assert(err, check.NotNil)
got := err.Error()
expected := "Validation Error: user name is not valid"
c.Assert(got, check.Equals, expected)
e, ok := err.(*InvalidUserError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "username is not valid")
}

func (s *S) TestNewWritesKeyInAuthorizedKeys(c *check.C) {
Expand Down

0 comments on commit ebd46af

Please sign in to comment.