2019-02-06 10:02:57 -06:00
|
|
|
package auth
|
2019-01-21 10:05:42 -06:00
|
|
|
|
|
|
|
import (
|
2019-04-30 07:42:01 -05:00
|
|
|
"context"
|
2019-01-21 10:05:42 -06:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserAuthTokenCleanup(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("Test user auth token cleanup", t, func() {
|
|
|
|
ctx := createTestContext(t)
|
2019-02-05 14:20:11 -06:00
|
|
|
ctx.tokenService.Cfg.LoginMaxInactiveLifetimeDays = 7
|
|
|
|
ctx.tokenService.Cfg.LoginMaxLifetimeDays = 30
|
2019-01-21 10:05:42 -06:00
|
|
|
|
2019-02-05 14:20:11 -06:00
|
|
|
insertToken := func(token string, prev string, createdAt, rotatedAt int64) {
|
|
|
|
ut := userAuthToken{AuthToken: token, PrevAuthToken: prev, CreatedAt: createdAt, RotatedAt: rotatedAt, UserAgent: "", ClientIp: ""}
|
2019-01-21 10:05:42 -06:00
|
|
|
_, err := ctx.sqlstore.NewSession().Insert(&ut)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
}
|
|
|
|
|
2019-02-05 14:20:11 -06:00
|
|
|
t := time.Date(2018, 12, 13, 13, 45, 0, 0, time.UTC)
|
|
|
|
getTime = func() time.Time {
|
|
|
|
return t
|
2019-01-21 10:05:42 -06:00
|
|
|
}
|
|
|
|
|
2019-02-05 14:20:11 -06:00
|
|
|
Convey("should delete tokens where token rotation age is older than or equal 7 days", func() {
|
|
|
|
from := t.Add(-7 * 24 * time.Hour)
|
|
|
|
|
|
|
|
// insert three old tokens that should be deleted
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
insertToken(fmt.Sprintf("oldA%d", i), fmt.Sprintf("oldB%d", i), from.Unix(), from.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert three active tokens that should not be deleted
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
from = from.Add(time.Second)
|
|
|
|
insertToken(fmt.Sprintf("newA%d", i), fmt.Sprintf("newB%d", i), from.Unix(), from.Unix())
|
|
|
|
}
|
|
|
|
|
2019-04-30 07:42:01 -05:00
|
|
|
affected, err := ctx.tokenService.deleteExpiredTokens(context.Background(), 7*24*time.Hour, 30*24*time.Hour)
|
2019-02-05 14:20:11 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(affected, ShouldEqual, 3)
|
|
|
|
})
|
2019-01-21 10:05:42 -06:00
|
|
|
|
2019-02-05 14:20:11 -06:00
|
|
|
Convey("should delete tokens where token age is older than or equal 30 days", func() {
|
|
|
|
from := t.Add(-30 * 24 * time.Hour)
|
|
|
|
fromRotate := t.Add(-time.Second)
|
|
|
|
|
|
|
|
// insert three old tokens that should be deleted
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
insertToken(fmt.Sprintf("oldA%d", i), fmt.Sprintf("oldB%d", i), from.Unix(), fromRotate.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert three active tokens that should not be deleted
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
from = from.Add(time.Second)
|
|
|
|
insertToken(fmt.Sprintf("newA%d", i), fmt.Sprintf("newB%d", i), from.Unix(), fromRotate.Unix())
|
|
|
|
}
|
|
|
|
|
2019-04-30 07:42:01 -05:00
|
|
|
affected, err := ctx.tokenService.deleteExpiredTokens(context.Background(), 7*24*time.Hour, 30*24*time.Hour)
|
2019-02-05 14:20:11 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(affected, ShouldEqual, 3)
|
|
|
|
})
|
2019-01-21 10:05:42 -06:00
|
|
|
})
|
|
|
|
}
|