Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Updated README for Docker, including how to run tests - refs #603
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 25, 2021
1 parent 8ad3d8f commit a1bc33d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 54 deletions.
76 changes: 23 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,73 +47,35 @@ This documentation is also included in VIAL deployments:

## Setting up a development environment

If you run into issues during environment setup, start with the [FAQ][1] for troubleshooting.

_If you encounter any issues in setup, please document them and add them to the [FAQ][1]._
The recommended development environment for VIAL uses [Docker Compose](https://docs.docker.com/compose/). It has been tested using [Docker for Mac](https://docs.docker.com/docker-for-mac/install/).

Check out the repository. Create a new Python virtual environment for it (I use `pipenv shell` to do this); we depend on Python 3.7 or later, and only test and deploy on Python 3.9. Install the dependencies with `pip install -r requirements.txt`.
Check out the `vial` repository:

Set your environment variables, see _Configuration_ section below.
git clone [email protected]:CAVaccineInventory/vial

You'll need a PostgreSQL database called "vaccinate". On macOS I've used https://postgresapp.com/ for that.
Then `cd vial` and start Docker Compose:

Alternatively, you can use [Docker](docs/docker.md).
docker-compose up

We use GeoDjango, so you'll need GDAL installed. If you run into trouble with this check out the [GeoDjango installation instructions](https://docs.djangoproject.com/en/3.2/ref/contrib/gis/install/).
This should run three containers: `database_1`, running PostgreSQL (and PostGIS), `web_1` running a Django development server and `migrations_1` which will run any database migrations and then stop.

You'll need to run `./manage.py` commands from the `vaccinate` directory, so `cd vaccinate`.
The `web_1` container will start a server on port 3000 - you can then access the web application at `http://0.0.0.0:3000/`

Then run the database migrations with `./manage.py migrate` - you'll need to run this command any time we release new migrations.

Run the development server using `./manage.py runserver 0.0.0.0:3000`

To enable the Django debug toolbar, run this instead:
If you run into issues during environment setup, start with the [FAQ][1] for troubleshooting.

DEBUG=1 ./manage.py runserver 0.0.0.0:3000
_If you encounter any issues in setup, please document them and add them to the [FAQ][1]._

Visit it at `http://localhost:3000/` - it's important to use `localhost:3000` as that is the URL that is allow-listed for logins by the Auth0 configuration. Click "sign in" and sign in with an Auth0 account.
Visit it at `http://0.0.0.0:3000/` - it's important to use `0.0.0.0:3000` as that is the URL that is allow-listed for logins by the Auth0 configuration. Click "sign in" and sign in with an Auth0 account.

Once you have signed in and created an account you should grant yourself super-user access so you can use every feature of the admin site. You can do that by running the following:

cd vaccinate
./manage.py shell
>>> from django.contrib.auth.models import User
docker exec -it vial_web ./manage.py shell
>>> User.objects.all().update(is_superuser=True, is_staff=True)
>>> <Ctrl+D> to exit

You'll also neet to run this command once or your static assets will 404:

./manage.py collectstatic

To get the `/dashboard/` interface working in your local development environment you can run this:

DASHBOARD_DATABASE_URL=postgres://localhost/vaccinate \
./manage.py runserver 0.0.0.0:3000

## Configuration

Running this requires some secrets in environment variables:

- `SOCIAL_AUTH_AUTH0_SECRET` should be set to
`iqSZmWfTZeXMfmFCBp8Pmyb9ZaFOXHsJ69BxwslUYo4whUjVLwtthNUaMT1TWUDx`.
(This secret is OK to include in this README because it is only used
for local development)
- `DJANGO_SECRET_KEY` can be any random string. One way to generate
one is via `python -c "import secrets; print(secrets.token_urlsafe())"`

Create a file like this named `.env`, which is loaded by Django:

SOCIAL_AUTH_AUTH0_SECRET="iqSZmWfTZeXMfmFCBp8Pmyb9ZaFOXHsJ69BxwslUYo4whUjVLwtthNUaMT1TWUDx"
DJANGO_SECRET_KEY="just a big random string"
DJANGO_DEBUG=1

In development you will need to have a local PostgreSQL server running - I use PostgreSQL.app on my Mac for this. Alternatively, you can use [Docker][<docs/docker.md>]

Then create a database called `vaccinate` by running this in the terminal:

createdb vaccinate

If your database has alternative connection details you can specify them using a `DATABASE_URL` environment variable of the format `postgres://USER:PASSWORD@HOST:PORT/NAME`. You can place this in the `.env` file.
docker exec -it vial_web ./manage.py collectstatic

## Importing sample data from live or staging

Expand All @@ -123,7 +85,7 @@ To use these scripts, you will need two API keys: one for your development envir

To import locations:
```bash
python scripts/dev_copy_locations.py \
docker exec -it vial_web python /app/scripts/dev_copy_locations.py \
--source-token '17:ce619e0...' \
--destination-token '4:09b190...' \
--source-url 'https://vial.calltheshots.us/api/searchLocations?size=100'
Expand All @@ -141,7 +103,7 @@ Source locations are raw, unprocessed location data gathered by our [vaccine-fee

You can import these in a similar way to locations, using this script:
```bash
python dev_copy_source_locations.py
docker exec -it vial_web python /app/scripts/dev_copy_source_locations.py
--source-token '17:ce619e0...' \
--destination-token '4:09b19...' \
--destination-url 'https://vial.calltheshots.us/api/searchSourceLocations?state=RI&source_name=vaccinefinder_org'
Expand All @@ -150,7 +112,15 @@ This will import all of the source locations in Rhode Island that were originall

## Running the tests

To run the tests, change directory to the `vaccinate` folder and run `pytest`.
Run the tests like this:

docker exec -it vial_web pytest

You can pass extra arguments to run specific tests - for example:

- `docker exec -it vial_web pytest -k test_admin_location_actions_for_queue` - run specific test
- `docker exec -it vial_web pytest api/test_export_mapbox.py` - run the tests in the `vaccinate/api/test_export_mapbox.py` module
- `docker exec -it vial_web pytest --lf` - run tests that failed during the last test run

## Code formatting and linting

Expand Down
2 changes: 1 addition & 1 deletion vaccinate/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
)

# 'dashboard' should be a read-only connection
if "DASHBOARD_DATABASE_URL" in os.environ:
if os.environ.get("DASHBOARD_DATABASE_URL"):
DATABASES["dashboard"] = dj_database_url.parse(os.environ["DASHBOARD_DATABASE_URL"])
DATABASES["dashboard"]["OPTIONS"] = {
"options": "-c default_transaction_read_only=on -c statement_timeout=5000"
Expand Down
4 changes: 4 additions & 0 deletions vaccinate/config/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from .settings import *

MIN_CALL_REQUEST_QUEUE_ITEMS = 0

# Tests fail if read-only connection is present:
if "dashboard" in DATABASES:
DATABASES.pop("dashboard")

0 comments on commit a1bc33d

Please sign in to comment.