Skip to content

Commit

Permalink
Add APIs integration tests [CLI-96] (#286)
Browse files Browse the repository at this point in the history
* Add APIs integration tests

* Add missing newline

* Add comments

* Correct comment

* Add default value via a function

* Add mkdir -p

* Update ci.md

* Fix typos
  • Loading branch information
Widcket authored May 10, 2021
1 parent 77c96d3 commit 318326a
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 55 deletions.
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 entities created during integration tests will be cleaned afterwards by the script `integration/test-cleanup.sh`. All the entities prefixed `integration-test-` will be deleted.

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
6 changes: 6 additions & 0 deletions integration/get-api-id.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash

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

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

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

mkdir -p ./integration/identifiers
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
26 changes: 21 additions & 5 deletions internal/cli/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ 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 {
defaultTokenLifetime := strconv.Itoa(apiDefaultTokenLifetime())
if err := apiTokenLifetime.Ask(cmd, &inputs.TokenLifetime, &defaultTokenLifetime); err != nil {
return err
}

Expand All @@ -208,6 +211,13 @@ auth0 apis create -n myapi -e 6100 --offline-access=true`,
api.Scopes = apiScopesFor(inputs.Scopes)
}

// Set token lifetime
if inputs.TokenLifetime <= 0 {
api.TokenLifetime = auth0.Int(apiDefaultTokenLifetime())
} else {
api.TokenLifetime = auth0.Int(inputs.TokenLifetime)
}

if err := ansi.Waiting(func() error {
return cli.api.ResourceServer.Create(api)
}); err != nil {
Expand Down Expand Up @@ -271,8 +281,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 Expand Up @@ -480,6 +492,10 @@ func apiScopesFor(scopes []string) []*management.ResourceServerScope {
return models
}

func apiDefaultTokenLifetime() int {
return 86400
}

func (c *cli) apiPickerOptions() (pickerOptions, error) {
list, err := c.api.ResourceServer.List()
if err != nil {
Expand Down

0 comments on commit 318326a

Please sign in to comment.