session_cleanup_test.go 951 B

123456789101112131415161718192021222324252627282930313233343536
  1. package auth
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestUserAuthTokenCleanup(t *testing.T) {
  9. Convey("Test user auth token cleanup", t, func() {
  10. ctx := createTestContext(t)
  11. insertToken := func(token string, prev string, rotatedAt int64) {
  12. ut := userAuthToken{AuthToken: token, PrevAuthToken: prev, RotatedAt: rotatedAt, UserAgent: "", ClientIp: ""}
  13. _, err := ctx.sqlstore.NewSession().Insert(&ut)
  14. So(err, ShouldBeNil)
  15. }
  16. // insert three old tokens that should be deleted
  17. for i := 0; i < 3; i++ {
  18. insertToken(fmt.Sprintf("oldA%d", i), fmt.Sprintf("oldB%d", i), int64(i))
  19. }
  20. // insert three active tokens that should not be deleted
  21. for i := 0; i < 3; i++ {
  22. insertToken(fmt.Sprintf("newA%d", i), fmt.Sprintf("newB%d", i), getTime().Unix())
  23. }
  24. affected, err := ctx.tokenService.deleteOldSession(time.Hour)
  25. So(err, ShouldBeNil)
  26. So(affected, ShouldEqual, 3)
  27. })
  28. }