|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/spf13/pflag" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + |
| 11 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 12 | +) |
| 13 | + |
| 14 | +// A flag whose help says "comma-separated" is a promise that a list reaches the |
| 15 | +// API. Twice that promise was broken the same way: the value went out as one |
| 16 | +// query parameter, which the API matched against nothing, so the command |
| 17 | +// returned zero rows with exit 0. --id did it (#411), and --delivery-group did |
| 18 | +// it again after that fix because nothing tied the promise to the encoding. |
| 19 | +// |
| 20 | +// So every such flag must appear below, with how its value is actually sent, |
| 21 | +// and a query filter must be one the client expands into a list. A new |
| 22 | +// comma-separated flag fails here until someone checks it and says how. |
| 23 | + |
| 24 | +type commaFlagHandling int |
| 25 | + |
| 26 | +const ( |
| 27 | + // A filter on a list endpoint, sent through hookdeck.listQuery. The param |
| 28 | + // must be list-valued there, or a comma-joined value goes out as one. |
| 29 | + sentAsQueryList commaFlagHandling = iota |
| 30 | + // Split into an array in the JSON request body before sending. |
| 31 | + splitIntoBodyArray |
| 32 | + // Split into a slice the client sends as a list (metrics measures and |
| 33 | + // dimensions; every Outpost filter, via splitCommaList). |
| 34 | + splitIntoList |
| 35 | + // Sent as one string on purpose: the API turns the commas into spaces |
| 36 | + // (OAuth2 scopes, which the token endpoint expects space-separated). |
| 37 | + sentAsStringAPIConvertsCommas |
| 38 | +) |
| 39 | + |
| 40 | +type commaFlag struct { |
| 41 | + kind commaFlagHandling |
| 42 | + param string // for sentAsQueryList: the query parameter the flag sets |
| 43 | +} |
| 44 | + |
| 45 | +var commaSeparatedFlags = map[string]commaFlag{ |
| 46 | + "hookdeck gateway event list --delivery-group": {kind: sentAsQueryList, param: "delivery_group"}, |
| 47 | + "hookdeck gateway event list --id": {kind: sentAsQueryList, param: "id"}, |
| 48 | + "hookdeck gateway request events --delivery-group": {kind: sentAsQueryList, param: "delivery_group"}, |
| 49 | + "hookdeck gateway request list --id": {kind: sentAsQueryList, param: "id"}, |
| 50 | + "hookdeck connection create --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 51 | + "hookdeck connection create --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 52 | + "hookdeck connection create --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 53 | + "hookdeck connection create --source-allowed-http-methods": {kind: splitIntoBodyArray}, |
| 54 | + "hookdeck connection update --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 55 | + "hookdeck connection update --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 56 | + "hookdeck connection update --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 57 | + "hookdeck connection upsert --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 58 | + "hookdeck connection upsert --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 59 | + "hookdeck connection upsert --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 60 | + "hookdeck connection upsert --source-allowed-http-methods": {kind: splitIntoBodyArray}, |
| 61 | + "hookdeck gateway connection create --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 62 | + "hookdeck gateway connection create --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 63 | + "hookdeck gateway connection create --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 64 | + "hookdeck gateway connection create --source-allowed-http-methods": {kind: splitIntoBodyArray}, |
| 65 | + "hookdeck gateway connection update --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 66 | + "hookdeck gateway connection update --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 67 | + "hookdeck gateway connection update --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 68 | + "hookdeck gateway connection upsert --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray}, |
| 69 | + "hookdeck gateway connection upsert --rule-deduplicate-include-fields": {kind: splitIntoBodyArray}, |
| 70 | + "hookdeck gateway connection upsert --rule-retry-response-status-codes": {kind: splitIntoBodyArray}, |
| 71 | + "hookdeck gateway connection upsert --source-allowed-http-methods": {kind: splitIntoBodyArray}, |
| 72 | + "hookdeck gateway request retry --connection-ids": {kind: splitIntoBodyArray}, |
| 73 | + "hookdeck gateway source create --allowed-http-methods": {kind: splitIntoBodyArray}, |
| 74 | + "hookdeck gateway source update --allowed-http-methods": {kind: splitIntoBodyArray}, |
| 75 | + "hookdeck gateway source upsert --allowed-http-methods": {kind: splitIntoBodyArray}, |
| 76 | + "hookdeck connection create --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas}, |
| 77 | + "hookdeck connection upsert --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas}, |
| 78 | + "hookdeck gateway connection create --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas}, |
| 79 | + "hookdeck gateway connection upsert --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas}, |
| 80 | + "hookdeck gateway metrics attempts --dimensions": {kind: splitIntoList}, |
| 81 | + "hookdeck gateway metrics attempts --measures": {kind: splitIntoList}, |
| 82 | + "hookdeck gateway metrics events --dimensions": {kind: splitIntoList}, |
| 83 | + "hookdeck gateway metrics events --measures": {kind: splitIntoList}, |
| 84 | + "hookdeck gateway metrics requests --dimensions": {kind: splitIntoList}, |
| 85 | + "hookdeck gateway metrics requests --measures": {kind: splitIntoList}, |
| 86 | + "hookdeck gateway metrics transformations --dimensions": {kind: splitIntoList}, |
| 87 | + "hookdeck gateway metrics transformations --measures": {kind: splitIntoList}, |
| 88 | + "hookdeck outpost attempt get --include": {kind: splitIntoList}, |
| 89 | + "hookdeck outpost attempt list --destination-id": {kind: splitIntoList}, |
| 90 | + "hookdeck outpost attempt list --destination-type": {kind: splitIntoList}, |
| 91 | + "hookdeck outpost attempt list --event-id": {kind: splitIntoList}, |
| 92 | + "hookdeck outpost attempt list --include": {kind: splitIntoList}, |
| 93 | + "hookdeck outpost attempt list --tenant-id": {kind: splitIntoList}, |
| 94 | + "hookdeck outpost attempt list --topic": {kind: splitIntoList}, |
| 95 | + "hookdeck outpost destination create --topics": {kind: splitIntoList}, |
| 96 | + "hookdeck outpost destination list --topics": {kind: splitIntoList}, |
| 97 | + "hookdeck outpost destination list --type": {kind: splitIntoList}, |
| 98 | + "hookdeck outpost destination update --topics": {kind: splitIntoList}, |
| 99 | + "hookdeck outpost event list --destination-id": {kind: splitIntoList}, |
| 100 | + "hookdeck outpost event list --id": {kind: splitIntoList}, |
| 101 | + "hookdeck outpost event list --tenant-id": {kind: splitIntoList}, |
| 102 | + "hookdeck outpost event list --topic": {kind: splitIntoList}, |
| 103 | + "hookdeck outpost metrics attempts --dimensions": {kind: splitIntoList}, |
| 104 | + "hookdeck outpost metrics attempts --measures": {kind: splitIntoList}, |
| 105 | + "hookdeck outpost metrics events --dimensions": {kind: splitIntoList}, |
| 106 | + "hookdeck outpost metrics events --measures": {kind: splitIntoList}, |
| 107 | + "hookdeck outpost tenant list --id": {kind: splitIntoList}, |
| 108 | +} |
| 109 | + |
| 110 | +var commaSeparated = regexp.MustCompile(`(?i)comma[- ]separated`) |
| 111 | + |
| 112 | +func discoverCommaSeparatedFlags() map[string]bool { |
| 113 | + found := map[string]bool{} |
| 114 | + var walk func(c *cobra.Command) |
| 115 | + walk = func(c *cobra.Command) { |
| 116 | + // Flags() and PersistentFlags(), not LocalFlags(): LocalFlags merges |
| 117 | + // every parent's persistent flags into the command as a side effect, |
| 118 | + // which mutates the shared rootCmd for every later test in the package. |
| 119 | + // It put root's hidden --api-key onto outpost mcp and failed |
| 120 | + // TestOutpostMCPCommandIsRegistered. |
| 121 | + visit := func(f *pflag.Flag) { |
| 122 | + if commaSeparated.MatchString(f.Usage) { |
| 123 | + found[c.CommandPath()+" --"+f.Name] = true |
| 124 | + } |
| 125 | + } |
| 126 | + c.Flags().VisitAll(visit) |
| 127 | + c.PersistentFlags().VisitAll(visit) |
| 128 | + for _, sub := range c.Commands() { |
| 129 | + walk(sub) |
| 130 | + } |
| 131 | + } |
| 132 | + walk(rootCmd) |
| 133 | + return found |
| 134 | +} |
| 135 | + |
| 136 | +func TestEveryCommaSeparatedFlagSendsAList(t *testing.T) { |
| 137 | + found := discoverCommaSeparatedFlags() |
| 138 | + if len(found) < 40 { |
| 139 | + t.Fatalf("found only %d comma-separated flags; the walk is probably broken and the guard would pass vacuously", len(found)) |
| 140 | + } |
| 141 | + |
| 142 | + for flag := range found { |
| 143 | + handling, ok := commaSeparatedFlags[flag] |
| 144 | + if !assert.True(t, ok, |
| 145 | + "%s says comma-separated but is not in commaSeparatedFlags. Check how its value "+ |
| 146 | + "reaches the API -- a query filter sent as one value returns nothing (#411) -- "+ |
| 147 | + "and add it with its handling.", flag) { |
| 148 | + continue |
| 149 | + } |
| 150 | + if handling.kind == sentAsQueryList { |
| 151 | + assert.True(t, hookdeck.IsListValuedParam(handling.param), |
| 152 | + "%s is a comma-separated query filter, but %q is not list-valued in hookdeck.listQuery, "+ |
| 153 | + "so \"a,b\" is sent as one value and matches nothing", flag, handling.param) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // A stale entry would quietly cover a different flag added under the same |
| 158 | + // name later, so every entry must still exist. |
| 159 | + for flag := range commaSeparatedFlags { |
| 160 | + assert.True(t, found[flag], "%s is in commaSeparatedFlags but no longer exists or no longer says comma-separated", flag) |
| 161 | + } |
| 162 | +} |
0 commit comments