Remove redundant return nil statement in user.go (#921)

Removing some redundant statements in user.go.
Adding test cases for user models.
pull/920/head^2
Shuhei Kitagawa 2018-01-12 09:11:57 +09:00 committed by Jordan Wright
parent a5c6ffb572
commit db19f0ac2a
2 changed files with 27 additions and 9 deletions

View File

@ -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"

View File

@ -15,22 +15,16 @@ 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
}
// GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an
// error is thrown.
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
}
// GetUserByUsername returns the user that the given username corresponds to. If no user is found, an
// error is thrown.