grafana/pkg/services/serviceaccounts/database/stats_test.go
linoman 1c7f89c41b
Auth: Add empty role usage metrics for service and user accounts (#73108)
* Add tests for service accounts metrics usage

* Add service account store implementation

* Add service account service implementation

* Add tests for org metrics usage

* Add org implementation

* Add service implementation
2023-08-16 10:56:47 +02:00

52 lines
1.4 KiB
Go

package database
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/satokengen"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
)
func TestIntegrationStore_UsageStats(t *testing.T) {
saToCreate := tests.TestUser{Login: "servicetestwithTeam@admin", IsServiceAccount: true}
db, store := setupTestDatabase(t)
sa := tests.SetupUserServiceAccount(t, db, saToCreate)
db.Cfg.SATokenExpirationDayLimit = 4
keyName := t.Name()
key, err := satokengen.New(keyName)
require.NoError(t, err)
cmd := serviceaccounts.AddServiceAccountTokenCommand{
Name: keyName,
OrgId: sa.OrgID,
Key: key.HashedKey,
SecondsToLive: 0,
}
_, err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
require.NoError(t, err)
role := org.RoleNone
form := serviceaccounts.UpdateServiceAccountForm{
Role: &role,
}
_, err = store.UpdateServiceAccount(context.Background(), sa.OrgID, sa.ID, &form)
require.NoError(t, err)
stats, err := store.GetUsageMetrics(context.Background())
require.NoError(t, err)
assert.Equal(t, int64(1), stats.ServiceAccounts)
assert.Equal(t, int64(1), stats.ServiceAccountsWithNoRole)
assert.Equal(t, int64(1), stats.Tokens)
assert.Equal(t, true, stats.ForcedExpiryEnabled)
}