grafana/pkg/services/sqlstore/user_test.go
Kristin Laemmert 3e272d2bda
Bug(sqlstore): fix issue with postgres unable to find existing main organization (#73789)
* SQLStore: fix issue where postgres would not find the existing org

Grafana using a postgres databases would fail to find the created org when the CreatedAt and UpdatedAt times are (inaccurately) populated. This issue only occurs in postgres, and only shows up when getOrCreateOrg run to create the admin user AND the organization already exists. See https://github.com/grafana/grafana/issues/71781 for more information and a reproduction.

* add an integration test
2023-08-30 11:45:20 -04:00

41 lines
1.1 KiB
Go

package sqlstore
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
// testing a regression which shows up when the main org is created, but not the
// admin user: getOrCreateOrg was unable to find the existing org.
// https://github.com/grafana/grafana/issues/71781
func TestIntegrationGetOrCreateOrg(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
ss := InitTestDB(t)
err := ss.WithNewDbSession(context.Background(), func(sess *DBSession) error {
// Create the org only:
ss.Cfg.AutoAssignOrg = true
ss.Cfg.DisableInitAdminCreation = true
ss.Cfg.AutoAssignOrgId = 1
createdOrgID, err := ss.getOrCreateOrg(sess, mainOrgName)
require.NoError(t, err)
require.Equal(t, int64(1), createdOrgID)
return nil
})
require.NoError(t, err)
err = ss.WithNewDbSession(context.Background(), func(sess *DBSession) error {
// Run it a second time and verify that it finds the org that was
// created above.
gotOrgId, err := ss.getOrCreateOrg(sess, mainOrgName)
require.NoError(t, err)
require.Equal(t, int64(1), gotOrgId)
return nil
})
require.NoError(t, err)
}