2020-04-06 21:58:15 +00:00
|
|
|
package tenant_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
influxdb "github.com/influxdata/influxdb/v2"
|
2020-09-02 16:15:57 +00:00
|
|
|
icontext "github.com/influxdata/influxdb/v2/context"
|
2020-04-06 21:58:15 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
|
|
|
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltOnboardingService(t *testing.T) {
|
|
|
|
influxdbtesting.OnboardInitialUser(initBoltOnboardingService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBoltOnboardingService(f influxdbtesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, func()) {
|
|
|
|
s, closeStore, err := NewTestInmemStore(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new bolt kv store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
svc := initOnboardingService(s, f, t)
|
|
|
|
return svc, func() {
|
|
|
|
closeStore()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *testing.T) influxdb.OnboardingService {
|
2020-07-01 11:08:20 +00:00
|
|
|
storage := tenant.NewStore(s)
|
2020-04-06 21:58:15 +00:00
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
|
|
|
// we will need an auth service as well
|
2020-08-03 15:33:46 +00:00
|
|
|
svc := tenant.NewOnboardService(ten, kv.NewService(zaptest.NewLogger(t), s))
|
2020-04-06 21:58:15 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Logf("Onboarding: %v", f.IsOnboarding)
|
|
|
|
if !f.IsOnboarding {
|
|
|
|
// create a dummy so so we can no longer onboard
|
|
|
|
err := ten.CreateUser(ctx, &influxdb.User{Name: "dummy", Status: influxdb.Active})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc
|
|
|
|
}
|
2020-09-02 16:15:57 +00:00
|
|
|
|
|
|
|
func TestOnboardURM(t *testing.T) {
|
|
|
|
s, _, _ := NewTestInmemStore(t)
|
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
|
|
|
// we will need an auth service as well
|
|
|
|
svc := tenant.NewOnboardService(ten, kv.NewService(zaptest.NewLogger(t), s))
|
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
|
|
|
onboard, err := svc.OnboardUser(ctx, &influxdb.OnboardingRequest{
|
|
|
|
User: "name",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
urms, _, err := ten.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{ResourceID: onboard.Org.ID})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(urms) > 1 {
|
|
|
|
t.Fatal("additional URMs created")
|
|
|
|
}
|
|
|
|
if urms[0].UserID != onboard.User.ID {
|
|
|
|
t.Fatal("org assigned to the wrong user")
|
|
|
|
}
|
|
|
|
}
|