Skip to content

Commit

Permalink
Expand test cases for api cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Mar 17, 2023
1 parent 0a5e89d commit 1d33d27
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
21 changes: 7 additions & 14 deletions internal/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type (
RawQueryParams map[string]string
Method string
URL *url.URL
Data []byte
Data interface{}
}
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 41 additions & 3 deletions test/integration/api-test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
stderr:
contains:
- "Sending the following request"

0 comments on commit 1d33d27

Please sign in to comment.