2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2022-01-25 22:11:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*Suite) TestCreateAPIKey(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
apiKeyStr, apiKey, err := db.CreateAPIKey(nil)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
|
|
|
// Did we get a valid key?
|
|
|
|
c.Assert(apiKey.Prefix, check.NotNil)
|
|
|
|
c.Assert(apiKey.Hash, check.NotNil)
|
|
|
|
c.Assert(apiKeyStr, check.Not(check.Equals), "")
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
_, err = db.ListAPIKeys()
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
keys, err := db.ListAPIKeys()
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(keys), check.Equals, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestAPIKeyDoesNotExist(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
key, err := db.GetAPIKey("does-not-exist")
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(key, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestValidateAPIKeyOk(c *check.C) {
|
|
|
|
nowPlus2 := time.Now().Add(2 * time.Hour)
|
2023-05-21 16:37:59 +00:00
|
|
|
apiKeyStr, apiKey, err := db.CreateAPIKey(&nowPlus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
valid, err := db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(valid, check.Equals, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestValidateAPIKeyNotOk(c *check.C) {
|
|
|
|
nowMinus2 := time.Now().Add(time.Duration(-2) * time.Hour)
|
2023-05-21 16:37:59 +00:00
|
|
|
apiKeyStr, apiKey, err := db.CreateAPIKey(&nowMinus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
valid, err := db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(valid, check.Equals, false)
|
|
|
|
|
|
|
|
now := time.Now()
|
2023-05-21 16:37:59 +00:00
|
|
|
apiKeyStrNow, apiKey, err := db.CreateAPIKey(&now)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
validNow, err := db.ValidateAPIKey(apiKeyStrNow)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(validNow, check.Equals, false)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
validSilly, err := db.ValidateAPIKey("nota.validkey")
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(validSilly, check.Equals, false)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
validWithErr, err := db.ValidateAPIKey("produceerrorkey")
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
c.Assert(validWithErr, check.Equals, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Suite) TestExpireAPIKey(c *check.C) {
|
|
|
|
nowPlus2 := time.Now().Add(2 * time.Hour)
|
2023-05-21 16:37:59 +00:00
|
|
|
apiKeyStr, apiKey, err := db.CreateAPIKey(&nowPlus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
valid, err := db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(valid, check.Equals, true)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
err = db.ExpireAPIKey(apiKey)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey.Expiration, check.NotNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
notValid, err := db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(notValid, check.Equals, false)
|
|
|
|
}
|