From db19f0ac2a46cb37f6f277d071ffb2fb54c469e6 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Fri, 12 Jan 2018 09:11:57 +0900 Subject: [PATCH] Remove redundant return nil statement in user.go (#921) Removing some redundant statements in user.go. Adding test cases for user models. --- models/models_test.go | 26 +++++++++++++++++++++++++- models/user.go | 10 ++-------- 2 files changed, 27 insertions(+), 9 deletions(-) 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