session_cleanup_test.go 999 B

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