Skip to content
Open
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
65 changes: 65 additions & 0 deletions pkg/cmd/connection_description_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hookdeck/hookdeck-cli/pkg/config"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
)

// `--description ""` is how a stored description is cleared, so it has to reach
// the API, while omitting the flag has to leave the stored one alone.
func TestConnectionUpsertDescription(t *testing.T) {
stored := storedUpsertConnection()
stored.Description = strPtr("old description")

t.Run("explicit empty value is sent", func(t *testing.T) {
body := captureConnectionUpsertBody(t, stored,
"gateway", "connection", "upsert", "my-conn", "--description", "")

require.Contains(t, body, "description")
assert.Equal(t, "", body["description"])
})

t.Run("omitted flag is not sent", func(t *testing.T) {
body := captureConnectionUpsertBody(t, stored,
"gateway", "connection", "upsert", "my-conn")

assert.NotContains(t, body, "description")
})
}

func TestConnectionUpdateEmptyDescriptionIsSent(t *testing.T) {
var body map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPut {
raw, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(raw, &body))
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(hookdeck.Connection{ID: "web_1"})
}))
t.Cleanup(server.Close)
config.ResetAPIClientForTesting()
t.Cleanup(config.ResetAPIClientForTesting)

old := Config
t.Cleanup(func() { Config = old })
Config = config.Config{APIBaseURL: server.URL, LogLevel: "info"}
Config.Profile.APIKey = "sk_test_123456789012"

cu := newConnectionUpdateCmd()
require.NoError(t, cu.cmd.ParseFlags([]string{"--description", ""}))
require.NoError(t, cu.runConnectionUpdateCmd(cu.cmd, []string{"web_1"}))

require.NotNil(t, body, "the command made no PUT request")
assert.Equal(t, map[string]interface{}{"description": ""}, body)
}
3 changes: 2 additions & 1 deletion pkg/cmd/connection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func (cu *connectionUpdateCmd) runConnectionUpdateCmd(cmd *cobra.Command, args [
hasChanges = true
}

if cu.description != "" {
// An explicit --description "" clears the stored description.
if cmd.Flags().Changed("description") {
req.Description = &cu.description
hasChanges = true
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/connection_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,9 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection,
Name: &cu.name,
}

if cu.description != "" {
// An explicit --description "" clears the stored description; only an
// omitted flag leaves it alone.
if cu.description != "" || (cu.cmd != nil && cu.cmd.Flags().Changed("description")) {
req.Description = &cu.description
}

Expand Down