From 94cab58a012d429d67bc0554c3dca52b2c0d464f Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 2 Oct 2017 14:47:20 +1100 Subject: [PATCH] test: add tests for basic auth --- script/foo-bar.json | 22 ++++++++++++++++++++++ script/publish.sh | 15 +++++++++++++++ script/test.sh | 32 +++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 script/foo-bar.json create mode 100755 script/publish.sh diff --git a/script/foo-bar.json b/script/foo-bar.json new file mode 100644 index 00000000..ba5048db --- /dev/null +++ b/script/foo-bar.json @@ -0,0 +1,22 @@ +{ + "consumer": { + "name": "Foo" + }, + "provider": { + "name": "Bar" + }, + "interactions": [ + { + "description" : "a request for something", + "providerState": null, + "request": { + "method": "get", + "path" : "/something" + }, + "response": { + "status": 200, + "body" : "something" + } + } + ] +} diff --git a/script/publish.sh b/script/publish.sh new file mode 100755 index 00000000..e20698a3 --- /dev/null +++ b/script/publish.sh @@ -0,0 +1,15 @@ +# set -x + +if [ -z "$1" ]; then + BASE_URL="http://localhost" +else + BASE_URL="$1" +fi + +BODY=$(ruby -e "require 'json'; j = JSON.parse(File.read('script/foo-bar.json')); j['interactions'][0]['providerState'] = 'it is ' + Time.now.to_s; puts j.to_json") +echo ${BODY} >> tmp.json +curl -v -XPUT -u foo:bar \-H "Content-Type: application/json" \ +-d@tmp.json \ +${BASE_URL}/pacts/provider/Bar/consumer/Foo/version/1.1.0 +rm tmp.json +echo "" diff --git a/script/test.sh b/script/test.sh index effc21b7..b36636b7 100755 --- a/script/test.sh +++ b/script/test.sh @@ -192,16 +192,34 @@ echo "Checking that server accepts and return HTML from outside" curl -H "Accept:text/html" --user ${PACT_BROKER_BASIC_AUTH_USERNAME}:${PACT_BROKER_BASIC_AUTH_PASSWORD} -s "http://${test_ip}:${EXTERN_BROKER_PORT}" echo "" -echo "Checking for specific HTML content from outside: '0 pacts'" -curl -H "Accept:text/html" --user ${PACT_BROKER_BASIC_AUTH_USERNAME}:${PACT_BROKER_BASIC_AUTH_PASSWORD} -s "http://${test_ip}:${EXTERN_BROKER_PORT}" | grep "0 pacts" +echo "Checking for specific HTML content from outside: 'Pacts'" +curl -H "Accept:text/html" --user ${PACT_BROKER_BASIC_AUTH_USERNAME}:${PACT_BROKER_BASIC_AUTH_PASSWORD} -s "http://${test_ip}:${EXTERN_BROKER_PORT}" | grep "Pacts" -echo "" echo "Checking that server accepts and responds with status 200" response_code=$(curl -s -o /dev/null -w "%{http_code}" --user ${PACT_BROKER_BASIC_AUTH_USERNAME}:${PACT_BROKER_BASIC_AUTH_PASSWORD} http://${test_ip}:${EXTERN_BROKER_PORT}) -if [[ "${response_code}" == '200' ]]; then +if [[ "${response_code}" -ne '200' ]]; then + die "Expected response code to be 200, but was ${response_code}" +fi + +if [[ ! -z "${PACT_BROKER_BASIC_AUTH_USERNAME}" ]]; then echo "" - echo "SUCCESS: All tests passed!" -else - die "While checking HTML response status 200" + echo "Checking that basic auth is configured" + response_code=$(curl -s -o /dev/null -w "%{http_code}" http://${test_ip}:${EXTERN_BROKER_PORT}) + + if [[ "${response_code}" -ne '401' ]]; then + die "Expected response code to be 401, but was ${response_code}" + fi fi + +script/publish.sh "http://${test_ip}:${EXTERN_BROKER_PORT}" + +echo "" +echo "Checking that badges can be accessed without basic auth" +response_code=$(curl -s -o /dev/null -w "%{http_code}" http://${test_ip}:${EXTERN_BROKER_PORT}/pacts/provider/Bar/consumer/Foo/latest/badge.svg) + +if [[ "${response_code}" -ne '200' ]]; then + die "Expected response code to be 200, but was ${response_code}" +fi + +echo "SUCCESS: All tests passed!" \ No newline at end of file