Files
grafana/pkg/services/anonymous/anonimpl/anonstore/database_test.go
Jo 40a1f8434d Anon: Scaffold anon service (#74744)
* remove API tagging method and authed tagging

* add anonstore

move debug to after cache

change test order

fix issue where mysql trims to second

* add old device cleanup

lint

utc-ize everything

trim whitespace

* remove dangling setting

* Add delete devices

* Move anonymous authnclient to anonimpl

* Add simple post login hook

* move registration of Background Service

cleanup

* add updated_at index

* do not untag device if login err

* add delete device integration test
2023-09-25 16:25:29 +02:00

80 lines
2.1 KiB
Go

package anonstore
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationAnonStore_DeleteDevicesOlderThan(t *testing.T) {
store := db.InitTestDB(t)
anonDBStore := ProvideAnonDBStore(store)
const keepFor = time.Hour * 24 * 61
anonDevice := &Device{
DeviceID: "32mdo31deeqwes",
ClientIP: "10.30.30.2",
UserAgent: "test",
UpdatedAt: time.Now().Add(-keepFor).Add(-time.Hour),
}
err := anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
require.NoError(t, err)
anonDevice.DeviceID = "keep"
anonDevice.UpdatedAt = time.Now().Add(-time.Hour)
err = anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
require.NoError(t, err)
from := time.Now().Add(-2 * keepFor)
to := time.Now()
count, err := anonDBStore.CountDevices(context.Background(), from, to)
require.NoError(t, err)
require.Equal(t, int64(2), count)
err = anonDBStore.DeleteDevicesOlderThan(context.Background(), time.Now().Add(-keepFor))
require.NoError(t, err)
devices, err := anonDBStore.ListDevices(context.Background(), &from, &to)
require.NoError(t, err)
require.Equal(t, 1, len(devices))
assert.Equal(t, "keep", devices[0].DeviceID)
}
func TestIntegrationAnonStore_DeleteDevice(t *testing.T) {
store := db.InitTestDB(t)
anonDBStore := ProvideAnonDBStore(store)
const keepFor = time.Hour * 24 * 61
anonDevice := &Device{
DeviceID: "32mdo31deeqwes",
ClientIP: "10.30.30.2",
UserAgent: "test",
UpdatedAt: time.Now().Add(-keepFor).Add(-time.Hour),
}
err := anonDBStore.CreateOrUpdateDevice(context.Background(), anonDevice)
require.NoError(t, err)
from := time.Now().Add(-2 * keepFor)
to := time.Now()
count, err := anonDBStore.CountDevices(context.Background(), from, to)
require.NoError(t, err)
require.Equal(t, int64(1), count)
err = anonDBStore.DeleteDevice(context.Background(), "32mdo31deeqwes")
require.NoError(t, err)
devices, err := anonDBStore.ListDevices(context.Background(), &from, &to)
require.NoError(t, err)
require.Equal(t, 0, len(devices))
}