From d60704744d63765abb550346ce84af3c48ca898a Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Fri, 17 Mar 2023 16:55:58 +0100 Subject: [PATCH] Expand test cases for api cmd --- internal/cli/api.go | 21 +++++-------- internal/cli/api_test.go | 2 +- test/integration/api-test-cases.yaml | 44 ++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/internal/cli/api.go b/internal/cli/api.go index cae7ac4db..761089cb5 100644 --- a/internal/cli/api.go +++ b/internal/cli/api.go @@ -62,7 +62,7 @@ type ( RawQueryParams map[string]string Method string URL *url.URL - Data []byte + Data interface{} } ) @@ -131,17 +131,10 @@ func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []s var response *http.Response if err := ansi.Waiting(func() error { - var data interface{} - if string(inputs.Data) != "" { - if err := json.Unmarshal(inputs.Data, &data); err != nil { - return fmt.Errorf("invalid json provided: %w", err) - } - } - request, err := cli.api.HTTPClient.NewRequest( inputs.Method, inputs.URL.String(), - data, + inputs.Data, management.Context(cmd.Context()), ) if err != nil { @@ -152,7 +145,7 @@ func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []s cli.renderer.Infof("Sending the following request: %+v", map[string]interface{}{ "method": request.Method, "url": request.URL.String(), - "payload": data, + "payload": inputs.Data, }) } @@ -238,12 +231,12 @@ func (i *apiCmdInputs) validateAndSetData() error { ) } - if len(data) > 0 && !json.Valid(data) { - return fmt.Errorf("invalid json data given: %s", data) + if len(data) > 0 { + if err := json.Unmarshal(data, &i.Data); err != nil { + return fmt.Errorf("invalid json data given: %w", err) + } } - i.Data = data - return nil } diff --git a/internal/cli/api_test.go b/internal/cli/api_test.go index d43824b76..8d63685cd 100644 --- a/internal/cli/api_test.go +++ b/internal/cli/api_test.go @@ -54,7 +54,7 @@ func TestAPICmdInputs_FromArgs(t *testing.T) { name: "it fails to parse input arguments when data is not a valid JSON", givenArgs: []string{"patch", "clients"}, givenDataFlag: "{", - expectedError: "invalid json data given: {", + expectedError: "invalid json data given: unexpected end of JSON input", }, { name: "it fails to parse input arguments when uri is invalid", diff --git a/test/integration/api-test-cases.yaml b/test/integration/api-test-cases.yaml index 6feb19855..0dfc911a0 100644 --- a/test/integration/api-test-cases.yaml +++ b/test/integration/api-test-cases.yaml @@ -9,23 +9,61 @@ tests: contains: - "idle_session_lifetime" - 002 - it successfully uses the api command to patch tenant settings with piped data: + 002 - it defaults to using a get method when method is missing: + command: auth0 api "tenants/settings" --query "include_fields=true" --query "fields=idle_session_lifetime" + exit-code: 0 + stdout: + contains: + - "idle_session_lifetime" + + 003 - it successfully uses the api command to patch tenant settings with piped data: command: cat ./test/integration/fixtures/update-tenant-settings.json | auth0 api patch "tenants/settings" && auth0 api get "tenants/settings" --query "include_fields=true" --query "fields=idle_session_lifetime" exit-code: 0 stdout: json: idle_session_lifetime: "73" - 003 - it successfully uses the api command to patch tenant settings: + 004 - it successfully uses the api command to patch tenant settings: command: auth0 api patch "tenants/settings" --data "{\"idle_session_lifetime\":72}" && auth0 api get "tenants/settings" --query "include_fields=true" --query "fields=idle_session_lifetime" exit-code: 0 stdout: json: idle_session_lifetime: "72" - 004 - it fails to use the api command to patch tenant settings with invalid json: + 005 - it defaults to using a post method when method is missing but data flag is present: + command: auth0 api "clients" --data '{"name":"integration-test-app-for-api-cmd"}' + exit-code: 0 + stdout: + json: + name: "integration-test-app-for-api-cmd" + + 006 - it fails to use the api command to patch tenant settings with invalid json: command: auth0 api patch "tenants/settings" --data "{\"idle_session_lifetime:72}" exit-code: 1 stderr: contains: - "failed to parse command inputs: invalid json data given" + + 007 - it fails to use the api command if an invalid method is given: + command: auth0 api conquer "tenants/settings" + exit-code: 1 + stderr: + contains: + - "failed to parse command inputs: invalid method given" + + 008 - it throws a warning when both piped data and the data flag are present: + command: cat ./test/integration/fixtures/update-tenant-settings.json | auth0 api patch "tenants/settings" --data "{\"idle_session_lifetime\":72}" + exit-code: 0 + stdout: + json: + idle_session_lifetime: "72" + stderr: + contains: + - "JSON data was passed using both the flag and as piped input. The Auth0 CLI will use only the data from the flag." + + 009 - it successfully prints out debug log messages: + command: auth0 api get "stats/daily" --query "from=20000317" --query "to=20000317" + exit-code: 0 + stdout: + contains: + - "Sending the following request"