#!/bin/bash
#
# NOTE: This is expected to be run against a production install, not
# a development system.  In this case the calamari user won't have
# CREATEDB permissions, which are necessary to create the test database,
# so the tests are wrapped in `sudo -u postgres` invocations which
# first grant then later deny this permission.  So, you need to be
# running this script as a user that is capable of sudoing as postgres.
#

export CALAMARI_CONFIG=/etc/calamari/calamari.conf
export DJANGO_SETTINGS_MODULE=calamari_web.settings

cd $(dirname $0)

sudo -u postgres psql -c 'ALTER USER calamari CREATEDB' >/dev/null
if [ $? -ne 0 ]; then
	echo "Unable to grant CREATEDB permissions to calamari user"
	exit 1
fi

CALAMARI_CONFIG=/etc/calamari/calamari.conf python /srv/www/calamari/manage.py test rest-api/tests cthulhu/tests --verbosity=2
RC=$?

sudo -u postgres psql -c 'ALTER USER calamari NOCREATEDB' >/dev/null

exit $RC

