Hackaton: Add more unit tests, take 3 (#101525)
* serviceaccounts/secretscan: test Service more thoroughly * middleware/cookies: add tests for CookieOptions * anonymous/anonimpl: cover a couple more methods * components/imguploader: Implement WebDAV integration tests * components/apikeygen: also check IsValid method * bus: cover invalid callback signature cases * cloudmigration/objectstorage: add basic unit tests * login/social/connectors: add test case for GitHub OAuth fetch emails+orgs * expr/classic: cover more evaluator types in tests
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/anonymous"
|
||||
"github.com/grafana/grafana/pkg/services/anonymous/anonimpl/anonstore"
|
||||
"github.com/grafana/grafana/pkg/services/anonymous/validator"
|
||||
"github.com/grafana/grafana/pkg/services/authn"
|
||||
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@@ -41,6 +42,7 @@ func TestIntegrationDeviceService_tag(t *testing.T) {
|
||||
expectedAnonUICount int64
|
||||
expectedKey string
|
||||
expectedDevice *anonstore.Device
|
||||
disableService bool
|
||||
}{
|
||||
{
|
||||
name: "no requests",
|
||||
@@ -118,20 +120,49 @@ func TestIntegrationDeviceService_tag(t *testing.T) {
|
||||
},
|
||||
expectedAnonUICount: 2,
|
||||
},
|
||||
{
|
||||
name: "when the service is disabled, read operations return empty",
|
||||
req: []tagReq{
|
||||
{
|
||||
httpReq: &http.Request{
|
||||
Header: http.Header{
|
||||
"User-Agent": []string{"test"},
|
||||
"X-Forwarded-For": []string{"10.30.30.1"},
|
||||
http.CanonicalHeaderKey(deviceIDHeader): []string{"32mdo31deeqwes"},
|
||||
},
|
||||
},
|
||||
kind: anonymous.AnonDeviceUI,
|
||||
},
|
||||
},
|
||||
disableService: true,
|
||||
expectedAnonUICount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
store := db.InitTestDB(t)
|
||||
anonService := ProvideAnonymousDeviceService(&usagestats.UsageStatsMock{},
|
||||
&authntest.FakeService{}, store, setting.NewCfg(), orgtest.NewOrgServiceFake(), nil, actest.FakeAccessControl{}, &routing.RouteRegisterImpl{}, validator.FakeAnonUserLimitValidator{})
|
||||
|
||||
cfg := setting.NewCfg()
|
||||
cfg.Anonymous.Enabled = !tc.disableService
|
||||
|
||||
anonService := ProvideAnonymousDeviceService(
|
||||
&usagestats.UsageStatsMock{}, &authntest.FakeService{}, store, cfg, orgtest.NewOrgServiceFake(),
|
||||
nil, actest.FakeAccessControl{}, &routing.RouteRegisterImpl{}, validator.FakeAnonUserLimitValidator{},
|
||||
)
|
||||
|
||||
for _, req := range tc.req {
|
||||
err := anonService.TagDevice(context.Background(), req.httpReq, req.kind)
|
||||
err := anonService.TagDevice(ctx, req.httpReq, req.kind)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
anonService.untagDevice(ctx, nil, &authn.Request{HTTPRequest: req.httpReq}, nil)
|
||||
})
|
||||
}
|
||||
|
||||
devices, err := anonService.anonStore.ListDevices(context.Background(), nil, nil)
|
||||
devices, err := anonService.ListDevices(ctx, nil, nil)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, devices, int(tc.expectedAnonUICount))
|
||||
if tc.expectedDevice != nil {
|
||||
@@ -147,10 +178,29 @@ func TestIntegrationDeviceService_tag(t *testing.T) {
|
||||
assert.Equal(t, tc.expectedDevice, devices[0])
|
||||
}
|
||||
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -1)
|
||||
|
||||
devicesCount, err := anonService.CountDevices(ctx, from, to)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedAnonUICount, devicesCount)
|
||||
|
||||
devicesFound, err := anonService.SearchDevices(ctx, &anonstore.SearchDeviceQuery{
|
||||
From: from,
|
||||
To: to,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if tc.expectedAnonUICount > 0 {
|
||||
require.NotNil(t, devicesFound)
|
||||
require.Equal(t, tc.expectedAnonUICount, devicesFound.TotalCount)
|
||||
}
|
||||
|
||||
stats, err := anonService.usageStatFn(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tc.expectedAnonUICount, stats["stats.anonymous.device.ui.count"].(int64), stats)
|
||||
if !tc.disableService {
|
||||
assert.Equal(t, tc.expectedAnonUICount, stats["stats.anonymous.device.ui.count"].(int64), stats)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user