Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-417: Tests for test commands #694

Merged
merged 7 commits into from
Mar 31, 2023
Merged
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
11 changes: 9 additions & 2 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -269,8 +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.IsRequired = true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requiring the audience precluded the ability to test authentication in non-interactive mode.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the initial change of toggling this to be required is in #221 otherwise it generated invalid tokens. Testing locally it still seems to be that dropping audience will generate an invalid token due to a missing payload.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's talk about this later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: Turns out that this marks the audience flag required for both the test token and test login commands but it should only be requiring for test token command. Will be fixing in a following commit.

testAudience.RegisterString(cmd, &inputs.Audience, "")
testAudienceRequired.RegisterString(cmd, &inputs.Audience, "")
testScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)

return cmd
Expand Down
6 changes: 6 additions & 0 deletions test/integration/scripts/create-client-grant.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash

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\": []}"
18 changes: 18 additions & 0 deletions test/integration/scripts/get-default-app-id.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/integration/scripts/get-m2m-app-id.sh
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/integration/scripts/get-manage-api-audience.sh
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions test/integration/test-commands-test-cases.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
# `timeout` command provided by GNU coreutils
command: timeout 2s auth0 test login $(./test/integration/scripts/get-default-app-id.sh) --no-input 2>&1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admittedly, looks odd. What's happening is running the auth0 test login command but enforcing a 2s timeout and piping stderr into stdout. Otherwise, it will wait indefinitely for the user to authorize in the broswer. By terminating early, this test only verifies the first stage, but its better than nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is timeout a default command on MacOS? I don't appear to have it, maybe I need to install coreutils or something (I think that's fine though, maybe just needs a contributing note?)

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 eyJhbGci"