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

Add APIs integration tests [CLI-96] #286

Merged
merged 9 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# project artifacts
/auth0
integration/client-id
/integration/identifiers

# Swap
[._]*.s[a-v][a-z]
Expand All @@ -26,4 +26,3 @@ tags
# misc
.vscode
.DS_Store

203 changes: 163 additions & 40 deletions commander.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ make integration

`make integration` will then use [commander](https://github.com/commander-cli/commander) to run tests defined in [commander.yaml](./commander.yaml)

The apps created during integration tests will be cleaned afterwards by the script `integration/test-cleanup.sh`. Apps with the prefix `integration-test-` will be deleted.
The apps created during integration tests will be cleaned afterwards by the script `integration/test-cleanup.sh`. Apps with the prefix `integration-test-app-` will be deleted.
Widcket marked this conversation as resolved.
Show resolved Hide resolved

To run integration tests as part of a CI pipeline, several environment variables need to be exported first. When these variables are set, `auth0-cli-config-generator` will generate a valid auth0-cli config file being retrieving a token for the client, removing the need to run `auth0 login`:
```bash
Expand Down
5 changes: 5 additions & 0 deletions integration/get-api-id.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/bash

api=$( auth0 apis create --name integration-test-api-newapi --identifier http://integration-test-api-newapi --scopes read:todos --format json --no-input )

echo "$api" | jq -r '.["id"]' > ./integration/identifiers/api-id
5 changes: 5 additions & 0 deletions integration/get-app-id.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/bash

app=$( auth0 apps create -n integration-test-app-newapp -t native --description NewApp --format json --no-input )

echo "$app" | jq -r '.["client_id"]' > ./integration/identifiers/app-id
5 changes: 0 additions & 5 deletions integration/get-client-id.sh

This file was deleted.

22 changes: 20 additions & 2 deletions integration/test-cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/bash

apps=$( auth0 apps list --format json --no-input)
apps=$( auth0 apps list --format json --no-input )

for app in $( echo "${apps}" | jq -r '.[] | @base64' ); do
_jq() {
Expand All @@ -11,9 +11,27 @@ for app in $( echo "${apps}" | jq -r '.[] | @base64' ); do
name=$(_jq '.Name')
# TODO(jfatta): should remove only those
# created during the same test session
if [[ $name = integration-test-* ]]
if [[ $name = integration-test-app-* ]]
then
echo deleting "$name"
$( auth0 apps delete "$clientid")
fi
done

apis=$( auth0 apis list --format json --no-input )

for api in $( echo "${apis}" | jq -r '.[] | @base64' ); do
_jq() {
echo "${api}" | base64 --decode | jq -r "${1}"
}

id=$(_jq '.ID')
name=$(_jq '.Name')
# TODO(jfatta): should remove only those
# created during the same test session
if [[ $name = integration-test-api-* ]]
then
echo deleting "$name"
$( auth0 apis delete "$id")
fi
done
Copy link
Contributor Author

@Widcket Widcket May 8, 2021

Choose a reason for hiding this comment

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

This can be handled in batches of 5 with

auth0 apis list --format json --no-input \
    | jq -r '.[] | select(.Name | startswith("integration-test-api-")) | "\(.Name) \(.ID)\n"' \
    | xargs -P 5 -n 2 bash -c 'echo Deleting $0 && auth0 apis delete $1 --force' 

14 changes: 9 additions & 5 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ auth0 apis create -n myapi -e 6100 --offline-access=true`,
return err
}

if err := apiScopes.AskMany(cmd, &inputs.Scopes, nil); err != nil {
return err
if !apiScopes.IsSet(cmd) {
if err := apiScopes.AskMany(cmd, &inputs.Scopes, nil); err != nil {
return err
}
}

if err := apiTokenLifetime.Ask(cmd, &inputs.TokenLifetime, auth0.String("86400")); err != nil {
Expand Down Expand Up @@ -223,7 +225,7 @@ auth0 apis create -n myapi -e 6100 --offline-access=true`,
apiIdentifier.RegisterString(cmd, &inputs.Identifier, "")
apiScopes.RegisterStringSlice(cmd, &inputs.Scopes, nil)
apiOfflineAccess.RegisterBool(cmd, &inputs.AllowOfflineAccess, false)
apiTokenLifetime.RegisterInt(cmd, &inputs.TokenLifetime, 0)
apiTokenLifetime.RegisterInt(cmd, &inputs.TokenLifetime, 86400)

return cmd
}
Expand Down Expand Up @@ -271,8 +273,10 @@ auth0 apis update -n myapi -e 6100 --offline-access=true`,
return err
}

if err := apiScopes.AskManyU(cmd, &inputs.Scopes, nil); err != nil {
return err
if !apiScopes.IsSet(cmd) {
if err := apiScopes.AskManyU(cmd, &inputs.Scopes, nil); err != nil {
return err
}
}

currentTokenLifetime := strconv.Itoa(auth0.IntValue(current.TokenLifetime))
Expand Down