Last active
November 10, 2015 14:55
-
-
Save jeremija/7c7f4c62bb7b55c26700 to your computer and use it in GitHub Desktop.
Functional tests using curl and jq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| PREFIX='http://localhost:8080/oauth-example' | |
| source 'tests-lib.sh' | |
| # non-authenticated | |
| test "curl $PREFIX/securedOAuth2Resources/my/clientRole" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/clientHasAnyRole" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/client" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/user" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/denyClient" 200 | |
| test "curl $PREFIX/securedOAuth2Resources/my/anyone" 200 | |
| test "curl $PREFIX/securedOAuth2Resources/my/nobody" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/trustedClient" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/trustedUser" 401 | |
| test "curl $PREFIX/securedOAuth2Resources/my/userRoleOrReadScope" 401 | |
| # resource owner password credentials grant (useful for mobile) | |
| ACCESS_TOKEN=$(curl -sX POST \ | |
| -d client_id=my-client -d client_secret=my-secret -d grant_type=password \ | |
| -d username=my-user -d password=my-password -d scope=read \ | |
| $PREFIX/oauth/token | jq .access_token -r) | |
| AUTH_HEADER="Authorization: Bearer $ACCESS_TOKEN" | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/clientRole" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/clientHasAnyRole" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/client" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/user" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/denyClient" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/anyone" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/nobody" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/trustedClient" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/trustedUser" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/userRoleOrReadScope" 200 | |
| # client credentials grant | |
| ACCESS_TOKEN=$(curl -sX POST \ | |
| -d client_id=my-client -d client_secret=my-secret \ | |
| -d grant_type=client_credentials -d scope=read \ | |
| $PREFIX/oauth/token | jq .access_token -r) | |
| AUTH_HEADER="Authorization: Bearer $ACCESS_TOKEN" | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/clientRole" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/clientHasAnyRole" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/client" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/user" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/denyClient" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/anyone" 200 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/nobody" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/trustedClient" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/trustedUser" 403 | |
| test "curl -H '$AUTH_HEADER' $PREFIX/securedOAuth2Resources/my/userRoleOrReadScope" 200 | |
| print_summary | |
| exit $ERRORS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ./test.sh | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientRole | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientHasAnyRole | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/client | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/user | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/denyClient | |
| expecting status 200... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/anyone | |
| expecting status 200... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/nobody | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedClient | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedUser | |
| expecting status 401... ok | |
| curl http://localhost:8080/oauth-example/securedOAuth2Resources/my/userRoleOrReadScope | |
| expecting status 401... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientRole | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientHasAnyRole | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/client | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/user | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/denyClient | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/anyone | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/nobody | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedClient | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedUser | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 2ad250d6-9823-4b79-9186-86e3479b84ad' http://localhost:8080/oauth-example/securedOAuth2Resources/my/userRoleOrReadScope | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientRole | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/clientHasAnyRole | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/client | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/user | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/denyClient | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/anyone | |
| expecting status 200... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/nobody | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedClient | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/trustedUser | |
| expecting status 403... ok | |
| curl -H 'Authorization: Bearer 8e28e329-5ccc-406d-9163-52752acc2aff' http://localhost:8080/oauth-example/securedOAuth2Resources/my/userRoleOrReadScope | |
| expecting status 200... ok | |
| Executed 30 tests. 0 errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PREFIX="http://localhost:8080/oauth-example" | |
| ERRORS=0 | |
| TESTS=0 | |
| if ! which jq 1>/dev/null || ! which curl 1>/dev/null; then | |
| echo 'These tests require jq and curl binaries' 1>&2 | |
| exit 1 | |
| fi | |
| function print_output { | |
| echo -e '--== RESPONSE ==--' | |
| # cat ./stderr | |
| cat ./stdout | |
| echo -e '\n------------------\n' | |
| } | |
| function test { | |
| ((TESTS++)) | |
| CURL=$1 | |
| EXPECTED_STATUS="$2" | |
| eval $CURL -v 1>./stdout 2>./stderr | |
| CURL_STATUS=$? | |
| echo $CURL | |
| echo -n ' expecting status '$EXPECTED_STATUS'... ' | |
| if [ "$CURL_STATUS" -gt 0 ]; then | |
| ((ERRORS++)) | |
| echo "error!" | |
| echo " status code: $CURL_STATUS" | |
| print_output | |
| return 1 | |
| fi | |
| STATUS=$(cat ./stderr | grep '^< HTTP/1.1' | cut -d ' ' -f 3) | |
| RESPONSE=$(cat ./stdout) | |
| if [ "$EXPECTED_STATUS" == "" ]; then | |
| EXPECTED_STATUS=200 | |
| fi | |
| if [ "$STATUS" == "" ]; then | |
| STATUS=-1 | |
| fi | |
| if [ "$STATUS" -ne "$EXPECTED_STATUS" ]; then | |
| ((ERRORS++)) | |
| echo error! | |
| echo " expected status code $EXPECTED_STATUS, got $STATUS" | |
| print_output | |
| return 1 | |
| fi | |
| echo "ok" | |
| return 0 | |
| } | |
| function print_summary { | |
| rm ./stdout | |
| rm ./stderr | |
| echo "Executed $TESTS tests. $ERRORS errors" 1>&2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment