Skip to content

Instantly share code, notes, and snippets.

@pldubouilh
Last active February 28, 2024 13:22
Show Gist options
  • Save pldubouilh/8bcb95f1c3abfe9b5ee1603aaa86e0b7 to your computer and use it in GitHub Desktop.
Save pldubouilh/8bcb95f1c3abfe9b5ee1603aaa86e0b7 to your computer and use it in GitHub Desktop.
API integration tests
#! /bin/bash
set -e
# set -x
# Integration tests made easy !
# Locally it assumes DB and server is running, and on a CI it automagically starts them and waits till they're up
# All tests are cURL based, using files on tmp to pass state and variables - this allows for very easy manual debugging
# if a test fails, as the state remains on disk, and you can just copy/past the call
function test() {
printf "+ test $1\n"
}
function echo_green() {
printf "\n\e[32m==> $1\e[0m\n"
}
function echo_red() {
printf "\n\e[31m==> $1\e[0m\n"
}
function db_is_up() {
pg_isready -q -h 127.0.0.1 -p ${PG_PORT_USERS}
}
function server_is_up() {
`which curl` -f -s http://127.0.0.1:3000/hc
}
# catch errors and print last curl call
function error_exit() {
echo_red "TESTS FAILED! \n"
echo $LASTCALL
cat /tmp/lg-log
cat /tmp/lg-payload
exit 1
}
trap error_exit ERR
# log last call, and write both output to disk
function curl() {
LASTCALL="curl -vvv $@"
`which curl` -vvv "$@" 2> /tmp/lg-log > /tmp/lg-payload || error_exit
}
# if on CI, start DB, init DB, and run server
if [ -n "$CI" ]; then
make db-start &
for i in {1..60}; do db_is_up && break || sleep 1; done
make run &
for i in {1..60}; do server_is_up && break || sleep 1; done
fi
# clean temp files & reset DB
rm -rf /tmp/lg-*
make db-reset > /dev/null
# login & registering
test "show login page"
curl -f http://localhost:3000/
cat /tmp/lg-payload | grep -q "login with your email"
test "create user0, extract cookie"
curl -f -XPOST http://localhost:3000/auth/[email protected]/password
cat /tmp/lg-log | grep "Set-Cookie" | awk '{print $3}' > /tmp/lg-user0-cookie
test "get user0's id"
curl -f -b `cat /tmp/lg-user0-cookie` http://localhost:3000/user
cat /tmp/lg-payload | jq -r '.id' > /tmp/lg-user0-id
# metrics
test "check no silent error is reported by metrics on prometheus endpoint"
curl http://localhost:3001/metrics
! cat /tmp/lg-payload | grep "err"
echo_green "all tests passed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment