2023-05-10 07:24:05 +00:00
|
|
|
package hscontrol
|
2022-01-25 22:11:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*Suite) TestCreateAPIKey(c *check.C) {
|
2023-05-11 07:09:18 +00:00
|
|
|
apiKeyStr, apiKey, err := app.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-11 07:09:18 +00:00
|
|
|
_, err = app.db.ListAPIKeys()
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
keys, err := app.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-11 07:09:18 +00:00
|
|
|
key, err := app.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-11 07:09:18 +00:00
|
|
|
apiKeyStr, apiKey, err := app.db.CreateAPIKey(&nowPlus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
valid, err := app.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-11 07:09:18 +00:00
|
|
|
apiKeyStr, apiKey, err := app.db.CreateAPIKey(&nowMinus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
valid, err := app.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-11 07:09:18 +00:00
|
|
|
apiKeyStrNow, apiKey, err := app.db.CreateAPIKey(&now)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
validNow, err := app.db.ValidateAPIKey(apiKeyStrNow)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(validNow, check.Equals, false)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
validSilly, err := app.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-11 07:09:18 +00:00
|
|
|
validWithErr, err := app.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-11 07:09:18 +00:00
|
|
|
apiKeyStr, apiKey, err := app.db.CreateAPIKey(&nowPlus2)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey, check.NotNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
valid, err := app.db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(valid, check.Equals, true)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
err = app.db.ExpireAPIKey(apiKey)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(apiKey.Expiration, check.NotNil)
|
|
|
|
|
2023-05-11 07:09:18 +00:00
|
|
|
notValid, err := app.db.ValidateAPIKey(apiKeyStr)
|
2022-01-25 22:11:05 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(notValid, check.Equals, false)
|
|
|
|
}
|