From ce70f3e3c35ea640733275a8e5f0e9c670c102b3 Mon Sep 17 00:00:00 2001 From: Darrell Warde Date: Wed, 14 Aug 2024 11:25:13 +0100 Subject: [PATCH 1/3] customer-managed-key commands to use test helper --- .../customermanagedkey/delete_test.go | 76 +------ .../customermanagedkey/get_test.go | 199 +++--------------- .../customermanagedkey/list_test.go | 197 +++-------------- .../internal/test/testutils/auratesthelper.go | 8 +- 4 files changed, 71 insertions(+), 409 deletions(-) diff --git a/neo4j/aura/internal/subcommands/customermanagedkey/delete_test.go b/neo4j/aura/internal/subcommands/customermanagedkey/delete_test.go index a709af1..19bdb3e 100644 --- a/neo4j/aura/internal/subcommands/customermanagedkey/delete_test.go +++ b/neo4j/aura/internal/subcommands/customermanagedkey/delete_test.go @@ -1,81 +1,27 @@ package customermanagedkey_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 TestDeleteCustomerManagedKey(t *testing.T) { - assert := assert.New(t) + for _, command := range []string{"customer-managed-key", "cmk"} { + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - cmkId := "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9" + cmkId := "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9" - mux := http.NewServeMux() + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), http.StatusNoContent, "") - var authCounter = 0 - mux.HandleFunc("/oauth/token", func(res http.ResponseWriter, req *http.Request) { - authCounter++ + helper.ExecuteCommand(fmt.Sprintf("%s delete %s", command, cmkId)) - res.WriteHeader(200) - res.Write([]byte(`{"access_token":"12345678","expires_in":3600,"token_type":"bearer"}`)) - }) + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodDelete) - var getCounter = 0 - mux.HandleFunc(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), func(res http.ResponseWriter, req *http.Request) { - getCounter++ - - assert.Equal(http.MethodDelete, req.Method) - assert.Equal(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), req.URL.Path) - - res.WriteHeader(204) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"customer-managed-key", "delete", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), cmkId}) - - 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, getCounter) - - assert.Equal(`Operation Successful -`, string(out)) + helper.AssertOut("Operation Successful\n") + } } diff --git a/neo4j/aura/internal/subcommands/customermanagedkey/get_test.go b/neo4j/aura/internal/subcommands/customermanagedkey/get_test.go index 2dff047..2119e8e 100644 --- a/neo4j/aura/internal/subcommands/customermanagedkey/get_test.go +++ b/neo4j/aura/internal/subcommands/customermanagedkey/get_test.go @@ -1,45 +1,40 @@ package customermanagedkey_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 TestGetCustomerManagedKey(t *testing.T) { - assert := assert.New(t) - - cmkId := "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9" - - 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/customer-managed-keys/%s", cmkId), func(res http.ResponseWriter, req *http.Request) { - getCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + for _, command := range []string{"customer-managed-key", "cmk"} { + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() + + cmkId := "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9" + + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), http.StatusOK, `{ + "data": { + "id": "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9", + "name": "Instance01", + "created": "2024-01-31T14:06:57Z", + "cloud_provider": "aws", + "key_id": "arn:aws:kms:us-east-1:123456789:key/11111-a222-1212-x789-1212f1212f", + "region": "us-east-1", + "type": "enterprise-db", + "tenant_id": "YOUR_TENANT_ID", + "status": "ready" + } + }`) + + helper.ExecuteCommand(fmt.Sprintf("%s get %s", command, cmkId)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) + + helper.AssertOutJson(`{ "data": { "id": "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9", "name": "Instance01", @@ -51,145 +46,7 @@ func TestGetCustomerManagedKey(t *testing.T) { "tenant_id": "YOUR_TENANT_ID", "status": "ready" } - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"customer-managed-key", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), cmkId}) - - 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, getCounter) - - assert.Equal(`{ - "data": { - "id": "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9", - "name": "Instance01", - "created": "2024-01-31T14:06:57Z", - "cloud_provider": "aws", - "key_id": "arn:aws:kms:us-east-1:123456789:key/11111-a222-1212-x789-1212f1212f", - "region": "us-east-1", - "type": "enterprise-db", - "tenant_id": "YOUR_TENANT_ID", - "status": "ready" + `) } } -`, string(out)) -} - -func TestGetCustomerManagedKeyAlias(t *testing.T) { - assert := assert.New(t) - - cmkId := "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9" - - 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/customer-managed-keys/%s", cmkId), func(res http.ResponseWriter, req *http.Request) { - getCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal(fmt.Sprintf("/v1/customer-managed-keys/%s", cmkId), req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ - "data": { - "id": "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9", - "name": "Instance01", - "created": "2024-01-31T14:06:57Z", - "cloud_provider": "aws", - "key_id": "arn:aws:kms:us-east-1:123456789:key/11111-a222-1212-x789-1212f1212f", - "region": "us-east-1", - "type": "enterprise-db", - "tenant_id": "YOUR_TENANT_ID", - "status": "ready" - } - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"cmk", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), cmkId}) - - 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, getCounter) - - assert.Equal(`{ - "data": { - "id": "8c764aed-8eb3-4a1c-92f6-e4ef0c7a6ed9", - "name": "Instance01", - "created": "2024-01-31T14:06:57Z", - "cloud_provider": "aws", - "key_id": "arn:aws:kms:us-east-1:123456789:key/11111-a222-1212-x789-1212f1212f", - "region": "us-east-1", - "type": "enterprise-db", - "tenant_id": "YOUR_TENANT_ID", - "status": "ready" - } -} -`, string(out)) -} diff --git a/neo4j/aura/internal/subcommands/customermanagedkey/list_test.go b/neo4j/aura/internal/subcommands/customermanagedkey/list_test.go index 647e1ca..a465c23 100644 --- a/neo4j/aura/internal/subcommands/customermanagedkey/list_test.go +++ b/neo4j/aura/internal/subcommands/customermanagedkey/list_test.go @@ -1,132 +1,19 @@ package customermanagedkey_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 TestListCustomerManagedKeys(t *testing.T) { - assert := assert.New(t) + for _, command := range []string{"customer-managed-key", "cmk"} { + 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/customer-managed-keys", func(res http.ResponseWriter, req *http.Request) { - listCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal("/v1/customer-managed-keys", req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ - "data": [ - { - "id": "f15cc45b-1c29-44e8-911f-3ba719f70ed7", - "name": "Production Key", - "tenant_id": "YOUR_TENANT_ID" - }, - { - "id": "0d971cc4-f703-40fd-8c5c-f5ec134f6c84", - "name": "Dev Key", - "tenant_id": "YOUR_TENANT_ID" - } - ] - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"customer-managed-key", "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": "f15cc45b-1c29-44e8-911f-3ba719f70ed7", - "name": "Production Key", - "tenant_id": "YOUR_TENANT_ID" - }, - { - "id": "0d971cc4-f703-40fd-8c5c-f5ec134f6c84", - "name": "Dev Key", - "tenant_id": "YOUR_TENANT_ID" - } - ] -} -`, string(out)) -} - -func TestListCustomerManagedKeysAlias(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 listCounter = 0 - mux.HandleFunc("/v1/customer-managed-keys", func(res http.ResponseWriter, req *http.Request) { - listCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal("/v1/customer-managed-keys", req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock("/v1/customer-managed-keys", http.StatusOK, `{ "data": [ { "id": "f15cc45b-1c29-44e8-911f-3ba719f70ed7", @@ -139,57 +26,27 @@ func TestListCustomerManagedKeysAlias(t *testing.T) { "tenant_id": "YOUR_TENANT_ID" } ] - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"cmk", "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": "f15cc45b-1c29-44e8-911f-3ba719f70ed7", - "name": "Production Key", - "tenant_id": "YOUR_TENANT_ID" - }, - { - "id": "0d971cc4-f703-40fd-8c5c-f5ec134f6c84", - "name": "Dev Key", - "tenant_id": "YOUR_TENANT_ID" + }`) + + helper.ExecuteCommand(fmt.Sprintf("%s list", command)) + + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) + + helper.AssertOutJson(`{ + "data": [ + { + "id": "f15cc45b-1c29-44e8-911f-3ba719f70ed7", + "name": "Production Key", + "tenant_id": "YOUR_TENANT_ID" + }, + { + "id": "0d971cc4-f703-40fd-8c5c-f5ec134f6c84", + "name": "Dev Key", + "tenant_id": "YOUR_TENANT_ID" + } + ] } - ] -} -`, string(out)) + `) + } } diff --git a/neo4j/aura/internal/test/testutils/auratesthelper.go b/neo4j/aura/internal/test/testutils/auratesthelper.go index ccb44aa..0c925f1 100644 --- a/neo4j/aura/internal/test/testutils/auratesthelper.go +++ b/neo4j/aura/internal/test/testutils/auratesthelper.go @@ -132,12 +132,14 @@ func (helper *AuraTestHelper) NewRequestHandlerMock(path string, status int, bod mock := requestHandlerMock{Calls: []call{}, t: helper.t} helper.mux.HandleFunc(path, func(res http.ResponseWriter, req *http.Request) { - requestBody, err := io.ReadAll(req.Body) assert.Nil(helper.t, err) - unmarshalledBody, err := UmarshalJson(requestBody) - assert.Nil(helper.t, err) + var unmarshalledBody map[string]interface{} + if len(requestBody) > 0 { + unmarshalledBody, err = UmarshalJson(requestBody) + assert.Nil(helper.t, err) + } mock.Calls = append(mock.Calls, call{Method: req.Method, Path: req.URL.Path, Body: unmarshalledBody}) From e560daac1de9c16955fd4eea9ec27dea32dbe5de Mon Sep 17 00:00:00 2001 From: Darrell Warde Date: Wed, 14 Aug 2024 11:29:43 +0100 Subject: [PATCH 2/3] tenant command tests to use test helper --- .../internal/subcommands/tenant/get_test.go | 85 +++----------- .../internal/subcommands/tenant/list_test.go | 107 ++++-------------- 2 files changed, 40 insertions(+), 152 deletions(-) diff --git a/neo4j/aura/internal/subcommands/tenant/get_test.go b/neo4j/aura/internal/subcommands/tenant/get_test.go index b63413d..092bb8c 100644 --- a/neo4j/aura/internal/subcommands/tenant/get_test.go +++ b/neo4j/aura/internal/subcommands/tenant/get_test.go @@ -1,93 +1,38 @@ package tenant_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 TestGetTenant(t *testing.T) { - assert := assert.New(t) + helper := testutils.NewAuraTestHelper(t) + defer helper.Close() - var tenantId = "6981ace7-efe8-4f5c-b7c5-267b5162ce91" + tenantId := "6981ace7-efe8-4f5c-b7c5-267b5162ce91" - 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/tenants/%s", tenantId), func(res http.ResponseWriter, req *http.Request) { - getCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal(fmt.Sprintf("/v1/tenants/%s", tenantId), req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock(fmt.Sprintf("/v1/tenants/%s", tenantId), http.StatusOK, `{ "data": { "id": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", "name": "Production", "instance_configurations": [] } - }`)) - }) + }`) - server := httptest.NewServer(mux) - defer server.Close() + helper.ExecuteCommand(fmt.Sprintf("tenant get %s", tenantId)) - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"tenant", "get", "--auth-url", fmt.Sprintf("%s/oauth/token", server.URL), "--base-url", fmt.Sprintf("%s/v1", server.URL), tenantId}) + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) - fs, err := testfs.GetTestFs(`{ - "aura": { - "credentials": [{ - "name": "test-cred", - "access-token": "dsa", - "token-expiry": 123 - }], - "default-credential": "test-cred" + helper.AssertOutJson(`{ + "data": { + "id": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", + "name": "Production", + "instance_configurations": [] } - }`) - 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": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", - "name": "Production", - "instance_configurations": [] } -} -`, string(out)) + `) } diff --git a/neo4j/aura/internal/subcommands/tenant/list_test.go b/neo4j/aura/internal/subcommands/tenant/list_test.go index aa26287..2a3ecd1 100644 --- a/neo4j/aura/internal/subcommands/tenant/list_test.go +++ b/neo4j/aura/internal/subcommands/tenant/list_test.go @@ -1,43 +1,17 @@ package tenant_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 TestListTenants(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/tenants", func(res http.ResponseWriter, req *http.Request) { - listCounter++ - - assert.Equal(http.MethodGet, req.Method) - assert.Equal("/v1/tenants", req.URL.Path) - - res.WriteHeader(200) - res.Write([]byte(`{ + mockHandler := helper.NewRequestHandlerMock("/v1/tenants", http.StatusOK, `{ "data": [ { "id": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", @@ -52,59 +26,28 @@ func TestListTenants(t *testing.T) { "name": "Development" } ] - }`)) - }) - - server := httptest.NewServer(mux) - defer server.Close() - - cmd := aura.NewCmd() - b := bytes.NewBufferString("") - cmd.SetOut(b) - cmd.SetArgs([]string{"tenant", "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) + helper.ExecuteCommand("tenant list") - err = cmd.ExecuteContext(ctx) - assert.Nil(err) + mockHandler.AssertCalledTimes(1) + mockHandler.AssertCalledWithMethod(http.MethodGet) - out, err := io.ReadAll(b) - assert.Nil(err) - - assert.Equal(1, authCounter) - assert.Equal(1, listCounter) - - assert.Equal(`{ - "data": [ - { - "id": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", - "name": "Production" - }, - { - "id": "YOUR_TENANT_ID", - "name": "Staging" - }, - { - "id": "da045ab3-3b89-4f45-8b96-528f2e47cd13", - "name": "Development" - } - ] -} -`, string(out)) + helper.AssertOutJson(`{ + "data": [ + { + "id": "6981ace7-efe8-4f5c-b7c5-267b5162ce91", + "name": "Production" + }, + { + "id": "YOUR_TENANT_ID", + "name": "Staging" + }, + { + "id": "da045ab3-3b89-4f45-8b96-528f2e47cd13", + "name": "Development" + } + ] + } + `) } From de638114193b549c9355cf4136b8e5e88613b435 Mon Sep 17 00:00:00 2001 From: Darrell Warde Date: Wed, 14 Aug 2024 13:18:56 +0100 Subject: [PATCH 3/3] 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)