Skip to content

Commit

Permalink
Import script can create a user if no API key given #2670
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed May 5, 2022
1 parent b7fa16a commit 4dc7fb1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
4 changes: 4 additions & 0 deletions data-serving/scripts/setup-db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ To create a new migration (i.e. to change the expected state of the database):
migrate-mongo create name-of-migration

That creates a file called `migrations/datetime-name-of-migration.js`. Edit the `up()` function to apply your change, and the `down()` function to disapply it. Now `npm run migrate` (or `dev/setup_db.sh` at the root level, if you're testing locally in Docker, will also work).

## Importing sample data

This folder also contains `import-sample-data.py`, a script for importing the test data for running cypress tests (in `verification/curator-service/ui/cypress` in this repo). You can run it with no arguments, or via `npm run import-sample-data`, and it will create a curator user in the locally-running G.h instance and use that users' API key to import the data. You can choose a different user by setting the `GH_API_KEY` environment variable, and you can choose a different instance by setting `GH_BASE_URL`.
38 changes: 30 additions & 8 deletions data-serving/scripts/setup-db/import-sample-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import os
import requests
import sys
import time


def convert_dict_to_float(aDict):
return float(aDict['$numberDouble'])
Expand Down Expand Up @@ -54,17 +56,37 @@ def convert_case(case):

return converted


def api_key_for_generated_curator(base_url: str) -> str:
"""Create a user and retrieve their API key."""
register_user_endpoint = f'{base_url}/auth/register'
user = {
'name': 'G.h test data importer',
'email': f'robot_{time.time()}@global.health',
'roles': ['curator'],
}
response = requests.post(register_user_endpoint, json=user)
if response.ok:
return response.json()['apiKey']
else:
print(f'Failure registering test user: {response.text}')
sys.exit(1)


def main():
"""Import sample data from ../../samples/cases.json into the G.h instance at $GH_BASE_URL
using the curator API key at $GH_API_KEY. This is done through the curator-service
rather than loading directly into the database via mongoimport because the links to
other collections, particularly the age buckets, need setting up in the application."""
"""Import sample data from ../../samples/cases.json into a G.h instance at $GH_BASE_URL
(by default, the local-testing instance on localhost) using the curator API.
This is done through the curator-service rather than loading directly into the database
via mongoimport because the links to other collections, particularly the age buckets, need
setting up in the application.
You can set an API key using the environment variable $GH_API_KEY. If you do not, then
this script will register a new curator user and use their API key; this only works in
local testing."""
if not (base_url := os.getenv('GH_BASE_URL')):
print("Define the Global.health API location in $GH_BASE_URL.")
sys.exit(1)
base_url = 'http://localhost:3001'
if not (api_key := os.getenv('GH_API_KEY')):
print("Supply a Global.health API key in $GH_API_KEY.")
sys.exit(1)
api_key = api_key_for_generated_curator(base_url)
batch_upsert_endpoint = f"{base_url}/api/cases/batchUpsert"
with open("../../samples/cases.json") as f:
sample_cases = json.load(f)
Expand Down

0 comments on commit 4dc7fb1

Please sign in to comment.