Skip to content
Snippets Groups Projects
Commit 1582a142 authored by Colton Leekley-Winslow's avatar Colton Leekley-Winslow Committed by GitHub
Browse files

Merge pull request #506 from scitran/test_inject_api_key

Add test/bin/inject_api_key.py as replacement for bootstrap-dev.py
parents 156d124b f46bacd3
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
"""This script helps bootstrap users and data"""
import os
import os.path
import sys
import json
import logging
import argparse
import datetime
import jsonschema
from api import config, validators
def bootstrap_users_and_groups(bootstrap_json_file_path):
"""Loads users and groups directly into the database.
Args:
bootstrap_json_file_path (str): Path to json file with users and groups
"""
log = logging.getLogger('scitran.bootstrap')
with open(bootstrap_json_file_path, 'r') as bootstrap_data_file:
bootstrap_data = json.load(bootstrap_data_file)
user_schema_path = validators.schema_uri('mongo', 'user.json')
user_schema, user_resolver = validators._resolve_schema(user_schema_path)
for user in bootstrap_data.get('users', []):
config.log.info('Bootstrapping user: {0}'.format(user.get('email', user['_id'])))
user['created'] = user['modified'] = datetime.datetime.utcnow()
if user.get('api_key'):
user['api_key']['created'] = datetime.datetime.utcnow()
validators._validate_json(user, user_schema, user_resolver)
config.db.users.insert_one(user)
group_schema_path = validators.schema_uri('mongo', 'group.json')
group_schema, group_resolver = validators._resolve_schema(group_schema_path)
for group in bootstrap_data.get('groups', []):
config.log.info('Bootstrapping group: {0}'.format(group['name']))
group['created'] = group['modified'] = datetime.datetime.utcnow()
validators._validate_json(group, group_schema, group_resolver)
config.db.groups.insert_one(group)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.description = 'Bootstrap SciTran users and groups'
ap.add_argument('json', help='JSON file containing users and groups')
args = ap.parse_args()
bootstrap_users_and_groups(args.json)
......@@ -154,7 +154,11 @@ if [ $BOOTSTRAP_USERS -eq 1 ]; then
echo "Users previously bootstrapped. Remove $SCITRAN_PERSISTENT_DB_PATH to re-bootstrap."
else
echo "Bootstrapping users"
PYTHONPATH=. bin/bootstrap-dev.py "$SCITRAN_RUNTIME_BOOTSTRAP"
PYTHONPATH=. bin/load_users_drone_secret.py \
--insecure --secret "$SCITRAN_CORE_DRONE_SECRET" \
"$SCITRAN_SITE_API_URL" \
"$SCITRAN_RUNTIME_BOOTSTRAP"
echo "Bootstrapped users"
touch "$SCITRAN_PERSISTENT_DB_PATH/.bootstrapped"
fi
......
#!/usr/bin/env python
"""This script helps bootstrap users and data"""
import os
import os.path
import sys
import json
import logging
import argparse
import datetime
import jsonschema
from api import config, validators
from api.handlers.userhandler import UserHandler
from api.dao.containerstorage import ContainerStorage
def set_api_key(user_id, api_key):
"""Sets an API key for a user in the database
Args:
user_id (str): ID of the user to reset the api key for
api_key (str): api_key to set for given user
"""
users_storage = ContainerStorage('users', use_object_id=False)
api_key_doc = {
"api_key":{
"key":api_key
}
}
result = users_storage.exec_op('PUT', _id=user_id, payload=api_key_doc)
if result.modified_count != 1:
raise RuntimeError("Unable to set API key for user")
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.description = 'Bootstrap SciTran users and groups'
ap.add_argument('user_id', help='ID of User to set API key for')
ap.add_argument("api_key", help="API key to set for given user")
args = ap.parse_args()
set_api_key(args.user_id, args.api_key)
......@@ -8,13 +8,14 @@ rm -f .coverage.integration-tests
USAGE="
Usage:\n
$0 <api-base-url> <mongodb-uri>\n
$0 <api-base-url> <mongodb-uri> <drone-secret>\n
\n
"
if [ "$#" -eq 2 ]; then
if [ "$#" -eq 3 ]; then
SCITRAN_SITE_API_URL=$1
MONGODB_URI=$2
SCITRAN_CORE_DRONE_SECRET=$3
else
echo "Wrong number of positional arguments"
echo $USAGE >&2
......@@ -32,7 +33,9 @@ echo "Bootstrapping test data..."
PYTHONPATH="$( pwd )" \
SCITRAN_PERSISTENT_DB_URI="$MONGODB_URI" \
python "bin/bootstrap-dev.py" \
python "bin/load_users_drone_secret.py" \
--secret "$SCITRAN_CORE_DRONE_SECRET" \
"$SCITRAN_SITE_API_URL" \
"test/integration_tests/bootstrap-test-accounts.json"
# Remove __pycache__ directory for issue with __file__ attribute
......@@ -40,6 +43,11 @@ SCITRAN_PERSISTENT_DB_URI="$MONGODB_URI" \
# Which have a mismatched __file__ attribute when loaded in docker container
rm -rf test/integration_tests/python/__pycache__
PYTHONPATH="$( pwd )" \
SCITRAN_PERSISTENT_DB_URI="$MONGODB_URI" \
python test/bin/inject_api_key.py admin@user.com \
"XZpXI40Uk85eozjQkU1zHJ6yZHpix+j0mo1TMeGZ4dPzIqVPVGPmyfeK"
BASE_URL="$SCITRAN_SITE_API_URL" \
MONGO_PATH="$MONGODB_URI" \
py.test test/integration_tests/python
......@@ -51,5 +59,3 @@ newman run test/integration_tests/postman/integration_tests.postman_collection -
pushd raml/schemas/definitions
abao ../../api.raml "--server=$SCITRAN_SITE_API_URL" "--hookfiles=../../../test/integration_tests/abao/abao_test_hooks.js"
popd
......@@ -10,6 +10,7 @@ SCITRAN_RUNTIME_PATH=${SCITRAN_RUNTIME_PATH:-"$( pwd )/runtime"}
SCITRAN_PERSISTENT_DB_PORT=9003
SCITRAN_PERSISTENT_DB_URI="mongodb://localhost:$SCITRAN_PERSISTENT_DB_PORT/integration-tests"
SCITRAN_PERSISTENT_PATH="$( mktemp -d )"
SCITRAN_CORE_DRONE_SECRET=${SCITRAN_CORE_DRONE_SECRET:-$( openssl rand -base64 32 )}
clean_up () {
kill $API_PID || true
......@@ -34,11 +35,12 @@ trap clean_up EXIT
./test/bin/run-unit-tests.sh
SCITRAN_RUNTIME_PORT=8081 \
SCITRAN_CORE_DRONE_SECRET=integration-tests \
SCITRAN_CORE_DRONE_SECRET="$SCITRAN_CORE_DRONE_SECRET" \
SCITRAN_RUNTIME_COVERAGE="true" \
./bin/run-dev-osx.sh -T -U -I &
API_PID=$!
./test/bin/run-integration-tests.sh \
"http://localhost:8081/api" \
"$SCITRAN_PERSISTENT_DB_URI"
"$SCITRAN_PERSISTENT_DB_URI" \
"$SCITRAN_CORE_DRONE_SECRET"
......@@ -26,6 +26,7 @@ SCITRAN_PERSISTENT_DB_PORT=${SCITRAN_PERSISTENT_DB_PORT:-"9001"}
SCITRAN_PERSISTENT_DB_URI=${SCITRAN_PERSISTENT_DB_URI:-"mongodb://localhost:$SCITRAN_PERSISTENT_DB_PORT/scitran"}
SCITRAN_PERSISTENT_PATH=`mktemp -d`
SCITRAN_PERSISTENT_DATA_PATH="$SCITRAN_PERSISTENT_PATH/data"
SCITRAN_CORE_DRONE_SECRET=${SCITRAN_CORE_DRONE_SECRET:-$( openssl rand -base64 32 )}
uwsgi --http "localhost:8081" --master --http-keepalive \
--so-keepalive --add-header "Connection: Keep-Alive" \
......@@ -37,9 +38,11 @@ uwsgi --http "localhost:8081" --master --http-keepalive \
--env "SCITRAN_PERSISTENT_DB_URI=$SCITRAN_PERSISTENT_DB_URI" \
--env "SCITRAN_PERSISTENT_PATH=$SCITRAN_PERSISTENT_PATH" \
--env "SCITRAN_PERSISTENT_DATA_PATH=$SCITRAN_PERSISTENT_DATA_PATH" \
--env "SCITRAN_CORE_DRONE_SECRET=$SCITRAN_CORE_DRONE_SECRET" \
--env 'SCITRAN_RUNTIME_COVERAGE=true' &
API_PID=$!
./test/bin/run-integration-tests.sh \
"$API_BASE_URL" \
"$SCITRAN_PERSISTENT_DB_URI"
"$SCITRAN_PERSISTENT_DB_URI" \
"$SCITRAN_CORE_DRONE_SECRET"
......@@ -17,10 +17,7 @@
"email": "admin@user.com",
"firstname": "Admin",
"lastname": "User",
"root": true,
"api_key":{
"key":"XZpXI40Uk85eozjQkU1zHJ6yZHpix+j0mo1TMeGZ4dPzIqVPVGPmyfeK"
}
"root": true
},
{
"_id": "test@user.com",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment