mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Sync: Move ActiveTokenCount to a new service (#52991)
* Move ActiveTokenCount to a new service * Fixing tests * fix `RootSystem` Co-authored-by: Artur Wierzbicki <artur.wierzbicki@grafana.com>
This commit is contained in:
@@ -42,14 +42,26 @@ type UserAuthTokenService struct {
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) {
|
||||
type ActiveAuthTokenService struct {
|
||||
cfg *setting.Cfg
|
||||
sqlStore sqlstore.Store
|
||||
}
|
||||
|
||||
func ProvideActiveAuthTokenService(cfg *setting.Cfg, sqlStore sqlstore.Store) *ActiveAuthTokenService {
|
||||
return &ActiveAuthTokenService{
|
||||
cfg: cfg,
|
||||
sqlStore: sqlStore,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ActiveAuthTokenService) ActiveTokenCount(ctx context.Context) (int64, error) {
|
||||
var count int64
|
||||
var err error
|
||||
err = s.SQLStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
||||
err = a.sqlStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
||||
var model userAuthToken
|
||||
count, err = dbSession.Where(`created_at > ? AND rotated_at > ? AND revoked_at = 0`,
|
||||
s.createdAfterParam(),
|
||||
s.rotatedAfterParam()).
|
||||
getTime().Add(-a.cfg.LoginMaxLifetime).Unix(),
|
||||
getTime().Add(-a.cfg.LoginMaxInactiveLifetime).Unix()).
|
||||
Count(&model)
|
||||
|
||||
return err
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestUserAuthToken(t *testing.T) {
|
||||
userToken := createToken()
|
||||
|
||||
t.Run("Can count active tokens", func(t *testing.T) {
|
||||
count, err := ctx.tokenService.ActiveTokenCount(context.Background())
|
||||
count, err := ctx.activeTokenService.ActiveTokenCount(context.Background())
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(1), count)
|
||||
})
|
||||
@@ -209,7 +209,7 @@ func TestUserAuthToken(t *testing.T) {
|
||||
require.Nil(t, notGood)
|
||||
|
||||
t.Run("should not find active token when expired", func(t *testing.T) {
|
||||
count, err := ctx.tokenService.ActiveTokenCount(context.Background())
|
||||
count, err := ctx.activeTokenService.ActiveTokenCount(context.Background())
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), count)
|
||||
})
|
||||
@@ -534,25 +534,35 @@ func createTestContext(t *testing.T) *testContext {
|
||||
maxInactiveDurationVal, _ := time.ParseDuration("168h")
|
||||
maxLifetimeDurationVal, _ := time.ParseDuration("720h")
|
||||
sqlstore := sqlstore.InitTestDB(t)
|
||||
|
||||
cfg := &setting.Cfg{
|
||||
LoginMaxInactiveLifetime: maxInactiveDurationVal,
|
||||
LoginMaxLifetime: maxLifetimeDurationVal,
|
||||
TokenRotationIntervalMinutes: 10,
|
||||
}
|
||||
|
||||
tokenService := &UserAuthTokenService{
|
||||
SQLStore: sqlstore,
|
||||
Cfg: &setting.Cfg{
|
||||
LoginMaxInactiveLifetime: maxInactiveDurationVal,
|
||||
LoginMaxLifetime: maxLifetimeDurationVal,
|
||||
TokenRotationIntervalMinutes: 10,
|
||||
},
|
||||
log: log.New("test-logger"),
|
||||
Cfg: cfg,
|
||||
log: log.New("test-logger"),
|
||||
}
|
||||
|
||||
activeTokenService := &ActiveAuthTokenService{
|
||||
cfg: cfg,
|
||||
sqlStore: sqlstore,
|
||||
}
|
||||
|
||||
return &testContext{
|
||||
sqlstore: sqlstore,
|
||||
tokenService: tokenService,
|
||||
sqlstore: sqlstore,
|
||||
tokenService: tokenService,
|
||||
activeTokenService: activeTokenService,
|
||||
}
|
||||
}
|
||||
|
||||
type testContext struct {
|
||||
sqlstore *sqlstore.SQLStore
|
||||
tokenService *UserAuthTokenService
|
||||
sqlstore *sqlstore.SQLStore
|
||||
tokenService *UserAuthTokenService
|
||||
activeTokenService *ActiveAuthTokenService
|
||||
}
|
||||
|
||||
func (c *testContext) getAuthTokenByID(id int64) (*userAuthToken, error) {
|
||||
|
||||
Reference in New Issue
Block a user