From de638114193b549c9355cf4136b8e5e88613b435 Mon Sep 17 00:00:00 2001 From: Darrell Warde Date: Wed, 14 Aug 2024 13:18:56 +0100 Subject: [PATCH] instance command tests to use helper --- go.mod | 1 + go.sum | 2 + .../subcommands/instance/create_test.go | 164 ++----- .../internal/subcommands/instance/get_test.go | 107 ++--- .../subcommands/instance/list_test.go | 136 ++---- .../subcommands/instance/update_test.go | 430 +++++------------- .../internal/test/testutils/auratesthelper.go | 15 +- 7 files changed, 228 insertions(+), 627 deletions(-) diff --git a/go.mod b/go.mod index 0b05791..f83224c 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect diff --git a/go.sum b/go.sum index 21a6a9f..70f15a5 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= diff --git a/neo4j/aura/internal/subcommands/instance/create_test.go b/neo4j/aura/internal/subcommands/instance/create_test.go index 210879e..155ff45 100644 --- a/neo4j/aura/internal/subcommands/instance/create_test.go +++ b/neo4j/aura/internal/subcommands/instance/create_test.go @@ -1,20 +1,10 @@ package instance_test import ( - "bytes" - "context" - "fmt" - "io" "net/http" - "net/http/httptest" "testing" - "github.com/neo4j/cli/common/clicfg" - "github.com/neo4j/cli/common/clictx" - "github.com/neo4j/cli/neo4j/aura" "github.com/neo4j/cli/neo4j/aura/internal/test/testutils" - "github.com/neo4j/cli/test/utils/testfs" - "github.com/stretchr/testify/assert" ) func TestCreateFreeInstance(t *testing.T) { @@ -57,29 +47,10 @@ func TestCreateFreeInstance(t *testing.T) { } func TestCreateProfessionalInstance(t *testing.T) { - assert := assert.New(t) - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var postCounter = 0 - mux.HandleFunc("/v1/instances", func(res http.ResponseWriter, req *http.Request) { - postCounter++ - - assert.Equal(http.MethodPost, req.Method) - assert.Equal("/v1/instances", req.URL.Path) - body, err := io.ReadAll(req.Body) - assert.Nil(err) - assert.Equal(`{"cloud_provider":"gcp","memory":"4GB","name":"Instance01","region":"europe-west1","tenant_id":"YOUR_TENANT_ID","type":"professional-db","version":"5"}`, string(body)) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock("/v1/instances", http.StatusOK, `{ "data": { "id": "db1d1234", "connection_url": "YOUR_CONNECTION_URL", @@ -91,114 +62,43 @@ func TestCreateProfessionalInstance(t *testing.T) { "type": "professional-db", "name": "Instance01" } - }`)) - - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "create", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--region", "europe-west1", "--name", "Instance01", "--type", "professional-db", "--tenant-id", "YOUR_TENANT_ID", "--cloud-provider", "gcp", "--memory", "4GB"}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + }`) + + helper.ExecuteCommand("instance create --region europe-west1 --name Instance01 --type professional-db --tenant-id YOUR_TENANT_ID --cloud-provider gcp --memory 4GB") + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodPost) + mockHandler.AssertCalledWithBody(`{"cloud_provider":"gcp","memory":"4GB","name":"Instance01","region":"europe-west1","tenant_id":"YOUR_TENANT_ID","type":"professional-db","version":"5"}`) + + helper.AssertOutJson(`{ + "data": { + "id": "db1d1234", + "connection_url": "YOUR_CONNECTION_URL", + "username": "neo4j", + "password": "letMeIn123!", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "region": "europe-west1", + "type": "professional-db", + "name": "Instance01" } - }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, postCounter) - - assert.Equal(`{ - "data": { - "id": "db1d1234", - "connection_url": "YOUR_CONNECTION_URL", - "username": "neo4j", - "password": "letMeIn123!", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "region": "europe-west1", - "type": "professional-db", - "name": "Instance01" } -} -`, string(out)) + `) } func TestCreateProfessionalInstanceNoMemory(t *testing.T) { - assert := assert.New(t) - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - }) - - var postCounter = 0 - mux.HandleFunc("/v1/instances", func(res http.ResponseWriter, req *http.Request) { - postCounter++ - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"instance", "create", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--region", "europe-west1", "--name", "Instance01", "--type", "professional-db", "--tenant-id", "YOUR_TENANT_ID", "--cloud-provider", "gcp"}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" - } - }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - err = cmd.ExecuteContext(ctx) - assert.ErrorContains(err, `required flag(s) "memory" not set`) + mockHandler := helper.NewRequestHandlerMock("/v1/instances", http.StatusOK, "") - assert.Equal(0, authCounter) - assert.Equal(0, postCounter) + helper.ExecuteCommand("instance create --region europe-west1 --name Instance01 --type professional-db --tenant-id YOUR_TENANT_ID --cloud-provider gcp") - out, err := io.ReadAll(b) - assert.Nil(err) + mockHandler.AssertCalledTimes(0) - assert.Equal(`Error: required flag(s) "memory" not set -Usage: + helper.AssertErr(`Error: required flag(s) "memory" not set +`) + helper.AssertOut(`Usage: aura instance create [flags] Flags: @@ -217,5 +117,5 @@ Global Flags: --base-url string --output string -`, string(out)) +`) } diff --git a/neo4j/aura/internal/subcommands/instance/get_test.go b/neo4j/aura/internal/subcommands/instance/get_test.go index 0ffc4f7..1cd1ff4 100644 --- a/neo4j/aura/internal/subcommands/instance/get_test.go +++ b/neo4j/aura/internal/subcommands/instance/get_test.go @@ -1,44 +1,20 @@ package instance_test import ( - "bytes" - "context" "fmt" - "io" "net/http" - "net/http/httptest" "testing" - "github.com/neo4j/cli/common/clicfg" - "github.com/neo4j/cli/common/clictx" - "github.com/neo4j/cli/neo4j/aura" - "github.com/neo4j/cli/test/utils/testfs" - "github.com/stretchr/testify/assert" + "github.com/neo4j/cli/neo4j/aura/internal/test/testutils" ) func TestGetInstance(t *testing.T) { - assert := assert.New(t) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - var instanceId = "2f49c2b3" + instanceId := "2f49c2b3" - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var getCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) { - getCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal(fmt.Sprintf("/v1/instances/%s", instanceId), req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusOK, `{ "data": { "id": "2f49c2b3", "name": "Production", @@ -52,58 +28,27 @@ func TestGetInstance(t *testing.T) { "memory": "8GB", "storage": "16GB" } - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), instanceId}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + }`) + + helper.ExecuteCommand(fmt.Sprintf("instance get %s", instanceId)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) + + helper.AssertOutJson(`{ + "data": { + "id": "2f49c2b3", + "name": "Production", + "status": "running", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "connection_url": "YOUR_CONNECTION_URL", + "metrics_integration_url": "YOUR_METRICS_INTEGRATION_ENDPOINT", + "region": "europe-west1", + "type": "enterprise-db", + "memory": "8GB", + "storage": "16GB" } - }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, getCounter) - - assert.Equal(`{ - "data": { - "id": "2f49c2b3", - "name": "Production", - "status": "running", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "connection_url": "YOUR_CONNECTION_URL", - "metrics_integration_url": "YOUR_METRICS_INTEGRATION_ENDPOINT", - "region": "europe-west1", - "type": "enterprise-db", - "memory": "8GB", - "storage": "16GB" } -} -`, string(out)) + `) } diff --git a/neo4j/aura/internal/subcommands/instance/list_test.go b/neo4j/aura/internal/subcommands/instance/list_test.go index 033f2f5..eabdae7 100644 --- a/neo4j/aura/internal/subcommands/instance/list_test.go +++ b/neo4j/aura/internal/subcommands/instance/list_test.go @@ -1,42 +1,17 @@ package instance_test import ( - "bytes" - "context" - "fmt" - "io" "net/http" - "net/http/httptest" "testing" - "github.com/neo4j/cli/common/clicfg" - "github.com/neo4j/cli/common/clictx" - "github.com/neo4j/cli/neo4j/aura" - "github.com/neo4j/cli/test/utils/testfs" - "github.com/stretchr/testify/assert" + "github.com/neo4j/cli/neo4j/aura/internal/test/testutils" ) func TestListInstances(t *testing.T) { - assert := assert.New(t) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var listCounter = 0 - mux.HandleFunc("/v1/instances", func(res http.ResponseWriter, req *http.Request) { - listCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal("/v1/instances", req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock("/v1/instances", http.StatusOK, `{ "data": [ { "id": "2f49c2b3", @@ -63,71 +38,40 @@ func TestListInstances(t *testing.T) { "cloud_provider": "gcp" } ] - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "list", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" - } - }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, listCounter) - - assert.Equal(`{ - "data": [ - { - "id": "2f49c2b3", - "name": "Production", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp" - }, - { - "id": "b51dc964", - "name": "Instance01", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "aws" - }, - { - "id": "432392ae", - "name": "Recommendations", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "azure" - }, - { - "id": "524b7d8d", - "name": "Northwind", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp" - } - ] -} -`, string(out)) + }`) + + helper.ExecuteCommand("instance list") + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) + + helper.AssertOutJson(`{ + "data": [ + { + "id": "2f49c2b3", + "name": "Production", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp" + }, + { + "id": "b51dc964", + "name": "Instance01", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "aws" + }, + { + "id": "432392ae", + "name": "Recommendations", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "azure" + }, + { + "id": "524b7d8d", + "name": "Northwind", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp" + } + ] + } + `) } diff --git a/neo4j/aura/internal/subcommands/instance/update_test.go b/neo4j/aura/internal/subcommands/instance/update_test.go index 2723eac..d5d7efc 100644 --- a/neo4j/aura/internal/subcommands/instance/update_test.go +++ b/neo4j/aura/internal/subcommands/instance/update_test.go @@ -1,352 +1,154 @@ package instance_test import ( - "bytes" - "context" "fmt" - "io" "net/http" - "net/http/httptest" "testing" - "github.com/neo4j/cli/common/clicfg" - "github.com/neo4j/cli/common/clictx" - "github.com/neo4j/cli/neo4j/aura" - "github.com/neo4j/cli/test/utils/testfs" - "github.com/stretchr/testify/assert" + "github.com/neo4j/cli/neo4j/aura/internal/test/testutils" ) func TestUpdateMemory(t *testing.T) { - assert := assert.New(t) - - var instanceId = "2f49c2b3" - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var patchCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) { - patchCounter++ - - assert.Equal(http.MethodPatch, req.Method) - assert.Equal(fmt.Sprintf("/v1/instances/%s", instanceId), req.URL.Path) - body, err := io.ReadAll(req.Body) - assert.Nil(err) - assert.Equal(`{"memory":"8GB"}`, string(body)) - - res.WriteHeader(200) - res.Write([]byte(`{ - "data": { - "id": "2f49c2b3", - "name": "Production", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "8GB", - "region": "europe-west1", - "type": "enterprise-db" - } -}`)) - - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "update", instanceId, "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--memory", "8GB"}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() + + instanceId := "2f49c2b3" + + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusAccepted, `{ + "data": { + "id": "2f49c2b3", + "name": "Production", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "8GB", + "region": "europe-west1", + "type": "enterprise-db" } }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, patchCounter) - - assert.Equal(`{ - "data": { - "id": "2f49c2b3", - "name": "Production", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "8GB", - "region": "europe-west1", - "type": "enterprise-db" + helper.ExecuteCommand(fmt.Sprintf("instance update %s --memory 8GB", instanceId)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodPatch) + mockHandler.AssertCalledWithBody(`{"memory":"8GB"}`) + + helper.AssertOutJson(`{ + "data": { + "id": "2f49c2b3", + "name": "Production", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "8GB", + "region": "europe-west1", + "type": "enterprise-db" + } } -} -`, string(out)) + `) } func TestUpdateName(t *testing.T) { - assert := assert.New(t) - - var instanceId = "2f49c2b3" - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var patchCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) { - patchCounter++ - - assert.Equal(http.MethodPatch, req.Method) - assert.Equal(fmt.Sprintf("/v1/instances/%s", instanceId), req.URL.Path) - body, err := io.ReadAll(req.Body) - assert.Nil(err) - assert.Equal(`{"name":"New Name"}`, string(body)) - - res.WriteHeader(200) - res.Write([]byte(`{ - "data": { - "id": "2f49c2b3", - "name": "New Name", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "4GB", - "region": "europe-west1", - "type": "enterprise-db" - } -}`)) - - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "update", instanceId, "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--name", "New Name"}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() + + instanceId := "2f49c2b3" + + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusOK, `{ + "data": { + "id": "2f49c2b3", + "name": "New Name", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "4GB", + "region": "europe-west1", + "type": "enterprise-db" } }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - assert.Equal(1, authCounter) - assert.Equal(1, patchCounter) - - assert.Equal(`{ - "data": { - "id": "2f49c2b3", - "name": "New Name", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "4GB", - "region": "europe-west1", - "type": "enterprise-db" + helper.ExecuteCommand(fmt.Sprintf(`instance update %s --name "New Name"`, instanceId)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodPatch) + mockHandler.AssertCalledWithBody(`{"name":"New Name"}`) + + helper.AssertOutJson(`{ + "data": { + "id": "2f49c2b3", + "name": "New Name", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "4GB", + "region": "europe-west1", + "type": "enterprise-db" + } } -} -`, string(out)) + `) } func TestUpdateMemoryAndName(t *testing.T) { - assert := assert.New(t) - - var instanceId = "2f49c2b3" - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) - - var patchCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) { - patchCounter++ - - assert.Equal(http.MethodPatch, req.Method) - assert.Equal(fmt.Sprintf("/v1/instances/%s", instanceId), req.URL.Path) - body, err := io.ReadAll(req.Body) - assert.Nil(err) - assert.Equal(`{"memory":"8GB","name":"New Name"}`, string(body)) - - res.WriteHeader(200) - res.Write([]byte(`{ - "data": { - "id": "2f49c2b3", - "name": "New Name", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "8GB", - "region": "europe-west1", - "type": "enterprise-db" - } -}`)) - - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"instance", "update", instanceId, "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), "--name", "New Name", "--memory", "8GB"}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() + + instanceId := "2f49c2b3" + + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusAccepted, `{ + "data": { + "id": "2f49c2b3", + "name": "New Name", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "8GB", + "region": "europe-west1", + "type": "enterprise-db" } }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) - - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) - err = cmd.ExecuteContext(ctx) - assert.Nil(err) - - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, patchCounter) - - assert.Equal(`{ - "data": { - "id": "2f49c2b3", - "name": "New Name", - "status": "updating", - "connection_url": "YOUR_CONNECTION_URL", - "tenant_id": "YOUR_TENANT_ID", - "cloud_provider": "gcp", - "memory": "8GB", - "region": "europe-west1", - "type": "enterprise-db" + helper.ExecuteCommand(fmt.Sprintf(`instance update %s --name "New Name" --memory 8GB`, instanceId)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodPatch) + mockHandler.AssertCalledWithBody(`{"memory":"8GB","name":"New Name"}`) + + helper.AssertOutJson(`{ + "data": { + "id": "2f49c2b3", + "name": "New Name", + "status": "updating", + "connection_url": "YOUR_CONNECTION_URL", + "tenant_id": "YOUR_TENANT_ID", + "cloud_provider": "gcp", + "memory": "8GB", + "region": "europe-west1", + "type": "enterprise-db" + } } -} -`, string(out)) + `) } func TestUpdateErrorsWithNoFlags(t *testing.T) { - assert := assert.New(t) - - var instanceId = "2f49c2b3" - - mux := http.NewServeMux() - - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ - }) - - var patchCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/instances/%s", instanceId), func(res http.ResponseWriter, req *http.Request) { - patchCounter++ - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetErr(b) - cmd.SetArgs([]string{"instance", "update", instanceId, "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL)}) - - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" - } - }`) - assert.Nil(err) - - cfg, err := clicfg.NewConfig(fs) - assert.Nil(err) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - ctx, err := clictx.NewContext(context.Background(), cfg, "test") - assert.Nil(err) + instanceId := "2f49c2b3" - err = cmd.ExecuteContext(ctx) - assert.ErrorContains(err, `at least one of the flags in the group [memory name] is required`) + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/instances/%s", instanceId), http.StatusAccepted, "") - assert.Equal(0, authCounter) - assert.Equal(0, patchCounter) + helper.ExecuteCommand(fmt.Sprintf(`instance update %s`, instanceId)) - out, err := io.ReadAll(b) - assert.Nil(err) + mockHandler.AssertCalledTimes(0) - assert.Equal(`Error: at least one of the flags in the group [memory name] is required -Usage: + helper.AssertErr(`Error: at least one of the flags in the group [memory name] is required +`) + helper.AssertOut(`Usage: aura instance update [flags] Flags: @@ -359,5 +161,5 @@ Global Flags: --base-url string --output string -`, string(out)) +`) } diff --git a/neo4j/aura/internal/test/testutils/auratesthelper.go b/neo4j/aura/internal/test/testutils/auratesthelper.go index 0c925f1..8297a2f 100644 --- a/neo4j/aura/internal/test/testutils/auratesthelper.go +++ b/neo4j/aura/internal/test/testutils/auratesthelper.go @@ -7,10 +7,10 @@ import ( "io" "net/http" "net/http/httptest" - "strings" "testing" "github.com/google/go-cmp/cmp" + "github.com/google/shlex" "github.com/neo4j/cli/common/clicfg" "github.com/neo4j/cli/common/clictx" "github.com/neo4j/cli/neo4j/aura" @@ -88,7 +88,8 @@ func (helper *AuraTestHelper) Close() { } func (helper *AuraTestHelper) ExecuteCommand(command string) { - args := strings.Split(command, " ") + args, err := shlex.Split(command) + assert.Nil(helper.t, err) args = append(args, "--auth-url", fmt.Sprintf("%s/oauth/token", helper.Server.URL), "--base-url", fmt.Sprintf("%s/v1", helper.Server.URL)) @@ -103,14 +104,20 @@ func (helper *AuraTestHelper) ExecuteCommand(command string) { ctx, err := clictx.NewContext(context.Background(), cfg, "test") assert.Nil(helper.t, err) - err = helper.cmd.ExecuteContext(ctx) - assert.Nil(helper.t, err) + helper.cmd.ExecuteContext(ctx) } func (helper *AuraTestHelper) SetConfig(cfg string) { helper.cfg = cfg } +func (helper *AuraTestHelper) AssertErr(expected string) { + out, err := io.ReadAll(helper.err) + assert.Nil(helper.t, err) + + assert.Equal(helper.t, expected, string(out)) +} + func (helper *AuraTestHelper) AssertOut(expected string) { out, err := io.ReadAll(helper.out) assert.Nil(helper.t, err)