Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aura API commands to use new test helper #15

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
76 changes: 11 additions & 65 deletions neo4j/aura/internal/subcommands/customermanagedkey/delete_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
199 changes: 28 additions & 171 deletions neo4j/aura/internal/subcommands/customermanagedkey/get_test.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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))
}
Loading