mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Correlations: Add organization id (#72258)
* Add org_id to correlations * Add tests * Allow org_id to be null in case org_id=0 is used * Create organization to ensure stable id is generated * Fix linting * Ensure backwards compatibility * Add deprecation information * Migrate correlations indices * Default org_id when migrating * Remove redundant default * Make PK non-nullable
This commit is contained in:
@@ -134,6 +134,18 @@ func (c TestContext) getURL(url string, user User) string {
|
||||
)
|
||||
}
|
||||
|
||||
func (c TestContext) createOrg(name string) int64 {
|
||||
c.t.Helper()
|
||||
store := c.env.SQLStore
|
||||
store.Cfg.AutoAssignOrg = false
|
||||
quotaService := quotaimpl.ProvideService(store, store.Cfg)
|
||||
orgService, err := orgimpl.ProvideService(store, store.Cfg, quotaService)
|
||||
require.NoError(c.t, err)
|
||||
orgId, err := orgService.GetOrCreate(context.Background(), name)
|
||||
require.NoError(c.t, err)
|
||||
return orgId
|
||||
}
|
||||
|
||||
func (c TestContext) createUser(cmd user.CreateUserCommand) User {
|
||||
c.t.Helper()
|
||||
store := c.env.SQLStore
|
||||
|
||||
@@ -29,6 +29,14 @@ func TestIntegrationReadCorrelation(t *testing.T) {
|
||||
Login: "admin",
|
||||
})
|
||||
|
||||
otherOrgId := ctx.createOrg("New organization")
|
||||
otherOrgUser := ctx.createUser(user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
Password: "admin2",
|
||||
Login: "admin2",
|
||||
OrgID: otherOrgId,
|
||||
})
|
||||
|
||||
viewerUser := ctx.createUser(user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleViewer),
|
||||
Password: "viewer",
|
||||
@@ -86,6 +94,14 @@ func TestIntegrationReadCorrelation(t *testing.T) {
|
||||
}
|
||||
dsWithoutCorrelations := ctx.createDs(createDsCommand)
|
||||
|
||||
createDsCommand = &datasources.AddDataSourceCommand{
|
||||
Name: "with-correlations",
|
||||
UID: dsWithCorrelations.UID, // reuse UID
|
||||
Type: "loki",
|
||||
OrgID: otherOrgId,
|
||||
}
|
||||
ctx.createDs(createDsCommand)
|
||||
|
||||
// This creates 2 records in the correlation table that should never be returned by the API.
|
||||
// Given all tests in this file work on the assumption that only a single correlation exists,
|
||||
// this covers the case where bad data exists in the database.
|
||||
@@ -157,6 +173,25 @@ func TestIntegrationReadCorrelation(t *testing.T) {
|
||||
|
||||
require.NoError(t, res.Body.Close())
|
||||
})
|
||||
|
||||
t.Run("Should correctly return correlations for current organization", func(t *testing.T) {
|
||||
res := ctx.Get(GetParams{
|
||||
url: "/api/datasources/correlations",
|
||||
user: otherOrgUser,
|
||||
})
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
|
||||
responseBody, err := io.ReadAll(res.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
var response correlations.GetCorrelationsResponseBody
|
||||
err = json.Unmarshal(responseBody, &response)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, response.Correlations, 0)
|
||||
|
||||
require.NoError(t, res.Body.Close())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Get all correlations for a given data source", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user