From 29a1eb47147c5eb92a7b669f4f61f04e9d6c190a Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Fri, 14 Sep 2018 10:06:30 -0500 Subject: [PATCH] test(http): add client tests for auth and bucket --- http/auth_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++ http/bucket_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/http/auth_test.go b/http/auth_test.go index 5e4606ad5e..ada4b10849 100644 --- a/http/auth_test.go +++ b/http/auth_test.go @@ -5,6 +5,8 @@ import ( "context" "encoding/json" "fmt" + "github.com/influxdata/platform/inmem" + platformtesting "github.com/influxdata/platform/testing" "io/ioutil" "net/http" "net/http/httptest" @@ -443,3 +445,55 @@ func TestService_handleDeleteAuthorization(t *testing.T) { }) } } + +func initAuthorizationService(f platformtesting.AuthorizationFields, t *testing.T) (platform.AuthorizationService, func()) { + t.Helper() + + svc := inmem.NewService() + svc.IDGenerator = f.IDGenerator + svc.TokenGenerator = f.TokenGenerator + + ctx := context.TODO() + for _, u := range f.Users { + if err := svc.PutUser(ctx, u); err != nil { + t.Fatalf("failed to populate users") + } + } + for _, u := range f.Authorizations { + if err := svc.PutAuthorization(ctx, u); err != nil { + t.Fatalf("failed to populate authorizations") + } + } + + handler := NewAuthorizationHandler() + handler.AuthorizationService = svc + server := httptest.NewServer(handler) + client := AuthorizationService{ + Addr: server.URL, + } + done := func() { + server.Close() + } + + return &client, done +} + +func TestAuthorizationService_CreateAuthorization(t *testing.T) { + platformtesting.CreateAuthorization(initAuthorizationService, t) +} + +func TestAuthorizationService_FindAuthorizationByID(t *testing.T) { + platformtesting.FindAuthorizationByID(initAuthorizationService, t) +} + +func TestAuthorizationService_FindAuthorizationByToken(t *testing.T) { + platformtesting.FindAuthorizationByToken(initAuthorizationService, t) +} + +func TestAuthorizationService_FindAuthorizations(t *testing.T) { + platformtesting.FindAuthorizations(initAuthorizationService, t) +} + +func TestAuthorizationService_DeleteAuthorization(t *testing.T) { + platformtesting.DeleteAuthorization(initAuthorizationService, t) +} diff --git a/http/bucket_test.go b/http/bucket_test.go index f3b4e78a35..09af372a53 100644 --- a/http/bucket_test.go +++ b/http/bucket_test.go @@ -12,7 +12,9 @@ import ( "time" "github.com/influxdata/platform" + "github.com/influxdata/platform/inmem" "github.com/influxdata/platform/mock" + platformtesting "github.com/influxdata/platform/testing" "github.com/julienschmidt/httprouter" ) @@ -585,3 +587,57 @@ func TestService_handlePatchBucket(t *testing.T) { }) } } + +func initBucketService(f platformtesting.BucketFields, t *testing.T) (platform.BucketService, func()) { + t.Helper() + + svc := inmem.NewService() + svc.IDGenerator = f.IDGenerator + + ctx := context.TODO() + for _, o := range f.Organizations { + if err := svc.PutOrganization(ctx, o); err != nil { + t.Fatalf("failed to populate organizations") + } + } + for _, b := range f.Buckets { + if err := svc.PutBucket(ctx, b); err != nil { + t.Fatalf("failed to populate buckets") + } + } + + handler := NewBucketHandler() + handler.BucketService = svc + server := httptest.NewServer(handler) + client := BucketService{ + Addr: server.URL, + } + done := func() { + server.Close() + } + + return &client, done +} +func TestBucketService_CreateBucket(t *testing.T) { + platformtesting.CreateBucket(initBucketService, t) +} + +func TestBucketService_FindBucketByID(t *testing.T) { + platformtesting.FindBucketByID(initBucketService, t) +} + +func TestBucketService_FindBuckets(t *testing.T) { + platformtesting.FindBuckets(initBucketService, t) +} + +func TestBucketService_DeleteBucket(t *testing.T) { + platformtesting.DeleteBucket(initBucketService, t) +} + +func TestBucketService_FindBucket(t *testing.T) { + platformtesting.FindBucket(initBucketService, t) +} + +func TestBucketService_UpdateBucket(t *testing.T) { + platformtesting.UpdateBucket(initBucketService, t) +}