From ad6d62448b7c0660ccb7390beda9783620534e04 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Thu, 30 Mar 2023 17:44:04 -0400 Subject: [PATCH 1/5] Adding tests for test commands --- internal/cli/test.go | 2 +- .../scripts/create-client-grant.sh | 4 ++ .../integration/scripts/get-default-app-id.sh | 18 +++++++ test/integration/scripts/get-m2m-app-id.sh | 13 +++++ .../scripts/get-manage-api-audience.sh | 18 +++++++ .../integration/test-commands-test-cases.yaml | 49 +++++++++++++++++++ 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100755 test/integration/scripts/create-client-grant.sh create mode 100755 test/integration/scripts/get-default-app-id.sh create mode 100755 test/integration/scripts/get-m2m-app-id.sh create mode 100755 test/integration/scripts/get-manage-api-audience.sh create mode 100644 test/integration/test-commands-test-cases.yaml diff --git a/internal/cli/test.go b/internal/cli/test.go index bdf1206fd..3fadb641c 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -269,7 +269,7 @@ func testTokenCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.") cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") - testAudience.IsRequired = true + // testAudience.IsRequired = true testAudience.RegisterString(cmd, &inputs.Audience, "") testScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil) diff --git a/test/integration/scripts/create-client-grant.sh b/test/integration/scripts/create-client-grant.sh new file mode 100755 index 000000000..b2a8d5ffa --- /dev/null +++ b/test/integration/scripts/create-client-grant.sh @@ -0,0 +1,4 @@ +management_api_audience=$(./test/integration/scripts/get-manage-api-audience.sh) +m2m_client_id=$(./test/integration/scripts/get-m2m-app-id.sh) + +auth0 api POST "client-grants" --data "{\"client_id\":\"$m2m_client_id\",\"audience\": \"$management_api_audience\",\"scope\": []}" \ No newline at end of file diff --git a/test/integration/scripts/get-default-app-id.sh b/test/integration/scripts/get-default-app-id.sh new file mode 100755 index 000000000..e16fed1d6 --- /dev/null +++ b/test/integration/scripts/get-default-app-id.sh @@ -0,0 +1,18 @@ +#! /bin/bash + +apps=$( auth0 apps list --json --no-input ) +for app in $( printf "%s" "$apps" | jq -r '.[] | @base64' ); do + _jq() { + echo "${app}" | base64 --decode | jq -r "${1}" + } + + id=$(_jq '.client_id') + name=$(_jq '.name') + + if [[ $name = "Default App" ]] + then + echo $id + exit 0 + fi + exit 1 +done \ No newline at end of file diff --git a/test/integration/scripts/get-m2m-app-id.sh b/test/integration/scripts/get-m2m-app-id.sh new file mode 100755 index 000000000..067c99adf --- /dev/null +++ b/test/integration/scripts/get-m2m-app-id.sh @@ -0,0 +1,13 @@ +#! /bin/bash + +FILE=./test/integration/identifiers/m2m-app-id +if [ -f "$FILE" ]; then + cat $FILE + exit 0 +fi + +m2m_app=$( auth0 apps create -n integration-test-app-m2m -t m2m --description "M2M test app" --json --no-input ) + +mkdir -p ./test/integration/identifiers +echo "$m2m_app" | jq -r '.["client_id"]' > $FILE +cat $FILE diff --git a/test/integration/scripts/get-manage-api-audience.sh b/test/integration/scripts/get-manage-api-audience.sh new file mode 100755 index 000000000..2e1052507 --- /dev/null +++ b/test/integration/scripts/get-manage-api-audience.sh @@ -0,0 +1,18 @@ +#! /bin/bash + +apis=$( auth0 apis list --json --no-input ) +for api in $( printf "%s" "$apis" | jq -r '.[] | @base64' ); do + _jq() { + echo "${api}" | base64 --decode | jq -r "${1}" + } + + audience=$(_jq '.identifier') + name=$(_jq '.name') + + if [[ $name = "Auth0 Management API" ]] + then + echo $audience + exit 0 + fi + exit 1 +done \ No newline at end of file diff --git a/test/integration/test-commands-test-cases.yaml b/test/integration/test-commands-test-cases.yaml new file mode 100644 index 000000000..d014a611b --- /dev/null +++ b/test/integration/test-commands-test-cases.yaml @@ -0,0 +1,49 @@ +config: + inherit-env: true + +tests: + 001 - test login of default app (partially successful): + #Terminate early because command will otherwise wait for in-browser authorization stage + command: timeout 2s auth0 test login $(./test/integration/scripts/get-default-app-id.sh) --no-input 2>&1 + exit-code: 124 #Timeout exit code + stdout: + contains: + - "Open the following URL in a browser: https://" + - ".auth0.com/authorize?client_id=" + - "&response_type=code&scope=openid+profile&state=" + + 002 - unsuccessful test login of machine-to-machine app: + command: auth0 test login $(./test/integration/scripts/get-m2m-app-id.sh) --no-input + exit-code: 1 + stderr: + contains: + - "cannot test the Universal Login with a Machine to Machine application." + + 003 - test token without client grant: + command: auth0 test token $(./test/integration/scripts/get-m2m-app-id.sh) --audience "$(./test/integration/scripts/get-manage-api-audience.sh)" --no-input + exit-code: 1 + stderr: + contains: + - "failed to log in with client credentials for client with ID " + - "the integration-test-app-m2m application is not authorized to request access tokens for this API " + - ".auth0.com/api/v2/." + + 004 - test token after creating client grant: + command: ./test/integration/scripts/create-client-grant.sh; auth0 test token $(./test/integration/scripts/get-m2m-app-id.sh) --audience "$(./test/integration/scripts/get-manage-api-audience.sh)" --no-input + exit-code: 0 + stdout: + contains: + - "TOKEN TYPE Bearer" + - "EXPIRES IN 1440 minute(s)" + - "ACCESS TOKEN ey" + + # 002 - it successfully shows universal login branding: + # command: auth0 ul show + # exit-code: 0 + # stdout: + # contains: + # - "ACCENT COLOR #2A2E35" + # - "BACKGROUND COLOR #FF4F40" + # - LOGO URL https://example.com/logo.png + # - FAVICON URL https://example.com/favicon.png + # - CUSTOM FONT URL https://example.com/font.woff From 1fe68aff427db5ea7c190445491f8d49e0c0a1cf Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Thu, 30 Mar 2023 18:10:48 -0400 Subject: [PATCH 2/5] Removing commented-out code --- internal/cli/test.go | 1 - test/integration/test-commands-test-cases.yaml | 11 ----------- 2 files changed, 12 deletions(-) diff --git a/internal/cli/test.go b/internal/cli/test.go index 3fadb641c..49778535c 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -269,7 +269,6 @@ func testTokenCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.") cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") - // testAudience.IsRequired = true testAudience.RegisterString(cmd, &inputs.Audience, "") testScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil) diff --git a/test/integration/test-commands-test-cases.yaml b/test/integration/test-commands-test-cases.yaml index d014a611b..0d1621f9e 100644 --- a/test/integration/test-commands-test-cases.yaml +++ b/test/integration/test-commands-test-cases.yaml @@ -36,14 +36,3 @@ tests: - "TOKEN TYPE Bearer" - "EXPIRES IN 1440 minute(s)" - "ACCESS TOKEN ey" - - # 002 - it successfully shows universal login branding: - # command: auth0 ul show - # exit-code: 0 - # stdout: - # contains: - # - "ACCENT COLOR #2A2E35" - # - "BACKGROUND COLOR #FF4F40" - # - LOGO URL https://example.com/logo.png - # - FAVICON URL https://example.com/favicon.png - # - CUSTOM FONT URL https://example.com/font.woff From dc14b9fc6be220c9a3794e0431856b42129d9949 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Thu, 30 Mar 2023 18:15:32 -0400 Subject: [PATCH 3/5] Adding hashbang --- test/integration/scripts/create-client-grant.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/integration/scripts/create-client-grant.sh b/test/integration/scripts/create-client-grant.sh index b2a8d5ffa..a007d5f95 100755 --- a/test/integration/scripts/create-client-grant.sh +++ b/test/integration/scripts/create-client-grant.sh @@ -1,3 +1,5 @@ +#! /bin/bash + management_api_audience=$(./test/integration/scripts/get-manage-api-audience.sh) m2m_client_id=$(./test/integration/scripts/get-m2m-app-id.sh) From a0369b779a8f68074a3ff1608a31f69ea7f883e2 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Fri, 31 Mar 2023 09:14:41 -0400 Subject: [PATCH 4/5] Adding required flag, contrib note --- internal/cli/test.go | 10 +++++++++- test/integration/test-commands-test-cases.yaml | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/cli/test.go b/internal/cli/test.go index 49778535c..44e7664a1 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -37,6 +37,14 @@ var ( Help: "The unique identifier of the target API you want to access.", } + testAudienceRequired = Flag{ + Name: testAudience.Name, + LongForm: testAudience.LongForm, + ShortForm: testAudience.ShortForm, + Help: testAudience.Help, + IsRequired: true, + } + testScopes = Flag{ Name: "Scopes", LongForm: "scopes", @@ -269,7 +277,7 @@ func testTokenCmd(cli *cli) *cobra.Command { cmd.SetUsageTemplate(resourceUsageTemplate()) cmd.Flags().BoolVar(&cli.force, "force", false, "Skip confirmation.") cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") - testAudience.RegisterString(cmd, &inputs.Audience, "") + testAudienceRequired.RegisterString(cmd, &inputs.Audience, "") testScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil) return cmd diff --git a/test/integration/test-commands-test-cases.yaml b/test/integration/test-commands-test-cases.yaml index 0d1621f9e..9d5f63a9b 100644 --- a/test/integration/test-commands-test-cases.yaml +++ b/test/integration/test-commands-test-cases.yaml @@ -4,6 +4,7 @@ config: tests: 001 - test login of default app (partially successful): #Terminate early because command will otherwise wait for in-browser authorization stage + # `timeout` command provided by GNU coreutils command: timeout 2s auth0 test login $(./test/integration/scripts/get-default-app-id.sh) --no-input 2>&1 exit-code: 124 #Timeout exit code stdout: From 8b07b0ba09493fa1a8402d77f2791a61f9ef944a Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Fri, 31 Mar 2023 09:22:14 -0400 Subject: [PATCH 5/5] Slightly stronger assertion --- test/integration/test-commands-test-cases.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/test-commands-test-cases.yaml b/test/integration/test-commands-test-cases.yaml index 9d5f63a9b..e21bf20e0 100644 --- a/test/integration/test-commands-test-cases.yaml +++ b/test/integration/test-commands-test-cases.yaml @@ -36,4 +36,4 @@ tests: contains: - "TOKEN TYPE Bearer" - "EXPIRES IN 1440 minute(s)" - - "ACCESS TOKEN ey" + - "ACCESS TOKEN eyJhbGci"