Permalink
Browse files

auth: remove default value for salt and token key

Related to #280. I will close the issue after I fix the loadConfig
dependency and update the docs.
  • Loading branch information...
1 parent d3368d7 commit 7f377cb68747f4c755ee0e5932fd61ea85b2e127 @fsouza fsouza committed Jan 22, 2013
Showing with 29 additions and 34 deletions.
  1. +4 −8 auth/suite_test.go
  2. +5 −12 auth/user.go
  3. +20 −14 auth/user_test.go
View
12 auth/suite_test.go
@@ -58,25 +58,21 @@ type S struct {
var _ = Suite(&S{})
-var panicIfErr = func(err error) {
- if err != nil {
- panic(err)
- }
-}
-
func (s *S) SetUpSuite(c *C) {
s.hashed = hashPassword("123")
err := config.ReadConfigFile("../etc/tsuru.conf")
c.Assert(err, IsNil)
config.Set("database:url", "127.0.0.1:27017")
config.Set("database:name", "tsuru_auth_test")
+ err = loadConfig()
+ c.Assert(err, IsNil)
s.conn, _ = db.Conn()
s.user = &User{Email: "timeredbull@globo.com", Password: "123"}
s.user.Create()
s.token, _ = s.user.CreateToken()
team := &Team{Name: "cobrateam", Users: []string{s.user.Email}}
err = s.conn.Teams().Insert(team)
- panicIfErr(err)
+ c.Assert(err, IsNil)
s.team = team
s.gitHost, _ = config.GetString("git:host")
s.gitPort, _ = config.GetString("git:port")
@@ -91,7 +87,7 @@ func (s *S) TearDownTest(c *C) {
if s.user.Password != s.hashed {
s.user.Password = s.hashed
err := s.user.Update()
- panicIfErr(err)
+ c.Assert(err, IsNil)
}
config.Set("git:host", s.gitHost)
config.Set("git:port", s.gitPort)
View
17 auth/user.go
@@ -15,23 +15,15 @@ import (
"time"
)
-const (
- defaultSalt = "tsuru-salt"
- defaultExpiration = 7 * 24 * time.Hour
- defaultKey = "tsuru-key"
-)
+const defaultExpiration = 7 * 24 * time.Hour
var salt, tokenKey string
var tokenExpire time.Duration
-func init() {
- loadConfig()
-}
-
-func loadConfig() {
+func loadConfig() error {
var err error
if salt, err = config.GetString("auth:salt"); err != nil {
- salt = defaultSalt
+ return errors.New(`Setting "auth:salt" is undefined.`)
}
if iface, err := config.Get("auth:token-expire-days"); err == nil {
day := int64(iface.(int))
@@ -40,8 +32,9 @@ func loadConfig() {
tokenExpire = defaultExpiration
}
if tokenKey, err = config.GetString("auth:token-key"); err != nil {
- tokenKey = defaultKey
+ return errors.New(`Setting "auth:token-key" is undefined.`)
}
+ return nil
}
func hashPassword(password string) string {
View
34 auth/user_test.go
@@ -232,40 +232,44 @@ func (s *S) TestCheckTokenReturnTheUserIfTheTokenIsValid(c *C) {
c.Assert(u.Email, Equals, s.user.Email)
}
-func (s *S) TestLoadConfigSetsTheSaltThatIsInTheConfigFile(c *C) {
+func (s *S) TestLoadConfigSalt(c *C) {
configuredSalt, err := config.GetString("auth:salt")
c.Assert(err, IsNil)
loadConfig()
c.Assert(salt, Equals, configuredSalt)
}
-func (s *S) TestLoadConfigSetsTheSaltToDefaultIfItIsNotPresentInConfig(c *C) {
- key := "auth"
+func (s *S) TestLoadConfigUndefinedSalt(c *C) {
+ key := "auth:salt"
oldValue, err := config.Get(key)
c.Assert(err, IsNil)
err = config.Unset(key)
c.Assert(err, IsNil)
defer config.Set(key, oldValue)
- loadConfig()
- c.Assert(salt, Equals, defaultSalt)
+ err = loadConfig()
+ c.Assert(err, NotNil)
+ c.Assert(err.Error(), Equals, `Setting "auth:salt" is undefined.`)
+ c.Assert(salt, Equals, "")
}
-func (s *S) TestLoadConfigSetsTheTokenExpireToTheValueInTheConfig(c *C) {
+func (s *S) TestLoadConfigTokenExpire(c *C) {
configuredToken, err := config.Get("auth:token-expire-days")
c.Assert(err, IsNil)
expected := time.Duration(int64(configuredToken.(int)) * 24 * int64(time.Hour))
loadConfig()
c.Assert(tokenExpire, Equals, expected)
}
-func (s *S) TestLoadConfigSetTheTokenExpireToTheDefaultValueIfTheConfigIsNotPresent(c *C) {
- key := "auth"
+func (s *S) TestLoadConfigUndefinedTokenExpire(c *C) {
+ tokenExpire = 0
+ key := "auth:token-expire-days"
oldConfig, err := config.Get(key)
c.Assert(err, IsNil)
err = config.Unset(key)
c.Assert(err, IsNil)
defer config.Set(key, oldConfig)
- loadConfig()
+ err = loadConfig()
+ c.Assert(err, IsNil)
c.Assert(tokenExpire, Equals, defaultExpiration)
}
@@ -281,22 +285,24 @@ func (s *S) TestLoadConfigShouldPanicIfTheTokenExpireDaysIsNotInteger(c *C) {
loadConfig()
}
-func (s *S) TestLoadConfigShouldSetTheTokenKeyToTheValueInTheConfig(c *C) {
+func (s *S) TestLoadConfigTokenKey(c *C) {
configuredKey, err := config.Get("auth:token-key")
c.Assert(err, IsNil)
loadConfig()
c.Assert(tokenKey, Equals, configuredKey)
}
-func (s *S) TestLoadConfigShouldSetTheTokenKeyToTheDefaultValueIfItsIsNotInTheConfig(c *C) {
- key := "auth"
+func (s *S) TestLoadConfigUndefineTokenKey(c *C) {
+ key := "auth:token-key"
oldConfig, err := config.Get(key)
c.Assert(err, IsNil)
err = config.Unset(key)
c.Assert(err, IsNil)
defer config.Set(key, oldConfig)
- loadConfig()
- c.Assert(tokenKey, Equals, defaultKey)
+ err = loadConfig()
+ c.Assert(err, NotNil)
+ c.Assert(err.Error(), Equals, `Setting "auth:token-key" is undefined.`)
+ c.Assert(tokenKey, Equals, "")
}
func (s *S) TestTeams(c *C) {

0 comments on commit 7f377cb

Please sign in to comment.