From eee3e8b63c8523f916c64b6d494381bd5c3fb2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 22 Nov 2019 19:47:00 +0100 Subject: [PATCH] Draft: added script to setup and tear down tests --- package.json | 4 +- test/index.js | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 test/index.js diff --git a/package.json b/package.json index d3643a4d..c39f2c8d 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,9 @@ "lint:fix": "eslint --fix app.js \"lib/**/*.js\" \"test/**/*.js\"", "lint": "eslint app.js \"lib/**/*.js\" \"test/**/*.js\"", "preinstall": "make pre-install", - "test": "make test-all", + "pretest": "NODE_ENV=test node test setup", + "test": "mocha -t 5000 --exit --recursive test/acceptance test/integration test/unit", + "posttest": "NODE_ENV=test node test teardown", "update-internal-deps": "rm -rf node_modules && npm install", "docker-test": "./docker-test.sh", "docker-bash": "./docker-bash.sh" diff --git a/test/index.js b/test/index.js new file mode 100644 index 00000000..17a5dca1 --- /dev/null +++ b/test/index.js @@ -0,0 +1,138 @@ +'use strict'; + +const util = require('util'); +const path = require('path'); +const exec = util.promisify(require('child_process').exec); + +if (!process.env.NODE_ENV) { + console.error('Please set "NODE_ENV" variable, e.g.: "NODE_ENV=test"'); + process.exit(1); +} + +const environment = require(`../config/environments/${process.env.NODE_ENV}.js`); +const REDIS_PORT = environment.redis.port; +const REDIS_CELL_PATH = path.resolve( + process.platform === 'darwin' + ? './test/support/libredis_cell.dylib' + : './test/support/libredis_cell.so' +); + +const TEST_USER_ID = 1; +const TEST_USER = environment.postgres_auth_user.replace('<%= user_id %>', TEST_USER_ID); +const TEST_PASSWORD = environment.postgres_auth_pass.replace('<%= user_id %>', TEST_USER_ID); +const PUBLIC_USER = environment.postgres.user; +const PUBLIC_USER_PASSWORD = environment.postgres.password; +const TEST_DB = `${TEST_USER}_db`; + +async function startRedis () { + await exec(`redis-server --port ${REDIS_PORT} --loadmodule ${REDIS_CELL_PATH} --logfile ${__dirname}/redis-server.log --daemonize yes`); +} + +async function stopRedis () { + await exec(`redis-cli -p ${REDIS_PORT} shutdown`); +} + +async function dropDatabase () { + await exec(`dropdb --if-exists ${TEST_DB}`, { + env: { + PGUSER: 'postgres' + } + }); +} + +async function createDatabase () { + await exec(`createdb -T template_postgis -EUTF8 "${TEST_DB}"`, { + env: { + PGUSER: 'postgres' + } + }); +} + +async function createDatabaseExtension () { + await exec(`psql -c "CREATE EXTENSION IF NOT EXISTS cartodb CASCADE;" ${TEST_DB}`, { + env: { + PGUSER: 'postgres' + } + }); +} + +async function populateDatabase () { + const filenames = [ + 'analysis_catalog', + 'windshaft.test', + 'gadm4', + 'countries_null_values', + 'ported/populated_places_simple_reduced', + 'cdb_analysis_check', + 'cdb_invalidate_varnish' + ]; + + for (const filename of filenames) { + const populateDatabaseCmd = ` + cat ${__dirname}/support/sql/${filename}.sql | + sed -e "s/:PUBLICUSER/${PUBLIC_USER}/g" | + sed -e "s/:PUBLICPASS/${PUBLIC_USER_PASSWORD}/g" | + sed -e "s/:TESTUSER/${TEST_USER}/g" | + sed -e "s/:TESTPASS/${TEST_PASSWORD}/g" | + PGOPTIONS='--client-min-messages=WARNING' psql -q -v ON_ERROR_STOP=1 ${TEST_DB} + `; + + await exec(populateDatabaseCmd, { + env: { + PGUSER: 'postgres' + } + }); + } +} + +async function populateRedis () { + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET rails:users:localhost id ${TEST_USER_ID} database_name "${TEST_DB}" database_host localhost map_key 1234`); + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET rails:users:cartodb250user id ${TEST_USER_ID} database_name "${TEST_DB}" database_host "localhost" database_password "${TEST_PASSWORD}" map_key 4321`); + await exec(`redis-cli -p ${REDIS_PORT} -n 0 HSET rails:${TEST_DB}:my_table infowindow "this, that, the other"`); + await exec(`redis-cli -p ${REDIS_PORT} -n 0 HSET rails:${TEST_DB}:test_table_private_1 privacy "0"`); + // API keys + // User localhost + // API Key Master + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET api_keys:localhost:1234 user "localhost" type "master" grants_sql "true" grants_maps "true" database_role "${TEST_USER}" database_password "${TEST_PASSWORD}"`); + // API Key Default public + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET api_keys:localhost:default_public user "localhost" type "default" grants_sql "true" grants_maps "true" database_role "test_windshaft_publicuser" database_password "public"`); + // API Key Regular + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET api_keys:localhost:regular1 user "localhost" type "regular" grants_sql "true" grants_maps "true" database_role "test_windshaft_regular1" database_password "regular1"`); + // API Key Regular 2 no Maps API access, only to check grants permissions to the API + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET api_keys:localhost:regular2 user "localhost" type "regular" grants_sql "true" grants_maps "false" database_role "test_windshaft_publicuser" database_password "public"`); + + // User cartodb250user + // API Key Master + await exec(`redis-cli -p ${REDIS_PORT} -n 5 HMSET api_keys:cartodb250user:4321 user "localhost" type "master" grants_sql "true" grants_maps "true" database_role "${TEST_USER}" database_password "${TEST_PASSWORD}"`); + // API Key Default + await exec(`cat <