also fix broken text env After the last couple of commits the tests ran correctly only on a fresh db, in addition, the roles within the db were not created/dropped on each request and we need that since their privileges differ and we need to to have an absolute clean db on each execution
57 lines
2.2 KiB
Bash
Executable File
57 lines
2.2 KiB
Bash
Executable File
#! /bin/bash
|
|
if [ -z "$1" ]
|
|
then
|
|
echo "Please supply the connection uri for the user with create database privileges"
|
|
exit -1
|
|
fi
|
|
|
|
if [ -z "$2" ]
|
|
then
|
|
echo "Please supply the test database name"
|
|
exit -1
|
|
fi
|
|
if [[ $1 != postgres://* ]]
|
|
then
|
|
echo "Please use a valid connection URI (https://www.postgresql.org/docs/current/static/libpq-connect.html#AEN45347)"
|
|
exit -1
|
|
fi
|
|
|
|
BASEPATH=$( cd $(dirname $0) ; pwd -P )
|
|
#Remove database path from the connection uri--prevents setting up the new database name with PGDATABASE
|
|
URI=$(echo $1 | cut -d'/' -f1-3)
|
|
#Extract host and port--we need this to form the new connection string
|
|
HOST_PORT=$(echo $URI | cut -d'/' -f3 | cut -d'@' -f2 )
|
|
DB=$2
|
|
# Specify the username of choice, or let the script create a random unique user by appending the database name
|
|
TEST_USER_NAME=postgrest_test_authenticator
|
|
# New password will get assigned only if the user does not already exist
|
|
# Otherwise make sure to provide the correct password for the existing user
|
|
TEST_USER_PASS=$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
|
|
|
|
PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" -Xq >/dev/null -c 'select rolcreatedb from pg_authid where rolname = current_user;' 2>/dev/null
|
|
if [ $? -ne 0 ]; then
|
|
echo "ERROR: Please specify the user with 'Create DB' permissions, and ensure that the default database for the username exists."
|
|
exit 1
|
|
fi
|
|
|
|
# plpgsql does not like psql variables, easier to pull this part off with bash variables
|
|
PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" -Xq >/dev/null <<EOF
|
|
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
|
FROM pg_stat_activity
|
|
WHERE pg_stat_activity.datname = '$DB'
|
|
AND pid <> pg_backend_pid();
|
|
|
|
DROP DATABASE IF EXISTS $DB;
|
|
DROP ROLE IF EXISTS $TEST_USER_NAME;
|
|
CREATE USER $TEST_USER_NAME WITH LOGIN NOINHERIT PASSWORD '$TEST_USER_PASS' CREATEROLE;
|
|
CREATE DATABASE $DB OWNER $TEST_USER_NAME;
|
|
EOF
|
|
|
|
PGDATABASE=$DB PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" --set=db=$DB -Xq <<EOF
|
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
ALTER DATABASE ${DB} SET request.jwt.claim.id = '-1';
|
|
EOF
|
|
|
|
# Create a new connection string to use with the test runner
|
|
echo 'postgres://'${TEST_USER_NAME}':'$TEST_USER_PASS'@'$HOST_PORT'/'$DB
|