-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-endpoints.sh
executable file
·67 lines (52 loc) · 2.1 KB
/
test-endpoints.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
set -euo pipefail
if [ "$#" -gt 1 ]; then
echo "Illegal number of parameters, expected usage: ./test-endpoints.sh [service-url]"
echo "Parameter 'service-url' will default to 'localhost:3000' if missing."
exit 1
fi
_fail_if_command_unavailable() {
if ! command -v $1 &>/dev/null; then
echo "Error: this script requires the following command: $1"
exit 1
fi
}
function _ensure_service_up() {
local url="${SERVICE_URL}/ping"
local timeout_s=30
local end_at_s=$(($(date +%s) + $timeout_s))
while [ $(date +%s) -lt $end_at_s ]; do
echo "Trying to contact the authorization server..."
local status_code="$(curl -s -o /dev/null -w '%{http_code}' "$url")"
if [ "$status_code" == '200' ]; then
return 0
fi
sleep 1
done
echo "Failed to receive HTTP 200 OK"
exit 1
}
_fail_if_command_unavailable "jq"
_fail_if_command_unavailable "curl"
SERVICE_URL=${1:-localhost:3000}
_ensure_service_up
echo "Starting test, will try to contact the authorization server for max 30s..."
CLIENT_ID=$(jq -r '.client_credentials.client_id' placeholders/config.json)
CLIENT_SECRET=$(jq -r '.client_credentials.client_secret' placeholders/config.json)
FCM_PROJECT_NUMBER=$(jq -r '.fcm_config.expected_fcm_project_number' placeholders/config.json)
echo "Fetching access token..."
ACCESS_TOKEN=$(curl -f --request POST \
--url "${SERVICE_URL}/oauth/token" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data "client_id=$CLIENT_ID" \
--data "client_secret=$CLIENT_SECRET" \
--data "scope=https://www.googleapis.com/auth/firebase.messaging" | jq -r '.access_token')
echo "Fetching FCM token..."
FCM_TOKEN=$(curl -f --request POST \
--url "${SERVICE_URL}/oauth/fcm-token" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data "fcm_project_number=$FCM_PROJECT_NUMBER" \
--data grant_type=client_credentials | jq -r '.access_token')
echo "Successfully fetched FCM token ($FCM_TOKEN)"