diff --git a/models/models_test.go b/models/models_test.go index 02b92a91..5bcd7126 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -94,12 +94,36 @@ func (s *ModelsSuite) createCampaign(ch *check.C) Campaign { return c } -func (s *ModelsSuite) TestGetUser(c *check.C) { +func (s *ModelsSuite) TestGetUserExists(c *check.C) { u, err := GetUser(1) c.Assert(err, check.Equals, nil) c.Assert(u.Username, check.Equals, "admin") } +func (s *ModelsSuite) TestGetUserDoesNotExist(c *check.C) { + u, err := GetUser(100) + c.Assert(err, check.Equals, gorm.ErrRecordNotFound) + c.Assert(u.Username, check.Equals, "") +} + +func (s *ModelsSuite) TestGetUserByAPIKeyWithExistingAPIKey(c *check.C) { + u, err := GetUser(1) + c.Assert(err, check.Equals, nil) + + u, err = GetUserByAPIKey(u.ApiKey) + c.Assert(err, check.Equals, nil) + c.Assert(u.Username, check.Equals, "admin") +} + +func (s *ModelsSuite) TestGetUserByAPIKeyWithNotExistingAPIKey(c *check.C) { + u, err := GetUser(1) + c.Assert(err, check.Equals, nil) + + u, err = GetUserByAPIKey(u.ApiKey + "test") + c.Assert(err, check.Equals, gorm.ErrRecordNotFound) + c.Assert(u.Username, check.Equals, "") +} + func (s *ModelsSuite) TestPutUser(c *check.C) { u, err := GetUser(1) u.Username = "admin_changed" diff --git a/models/user.go b/models/user.go index bb72cb12..81f9081f 100644 --- a/models/user.go +++ b/models/user.go @@ -15,10 +15,7 @@ type User struct { func GetUser(id int64) (User, error) { u := User{} err := db.Where("id=?", id).First(&u).Error - if err != nil { - return u, err - } - return u, nil + return u, err } // GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an @@ -26,10 +23,7 @@ func GetUser(id int64) (User, error) { func GetUserByAPIKey(key string) (User, error) { u := User{} err := db.Where("api_key = ?", key).First(&u).Error - if err != nil { - return u, err - } - return u, nil + return u, err } // GetUserByUsername returns the user that the given username corresponds to. If no user is found, an