# delphi-slz-admin

## Make commands
- `make venv` - create local virtual env(base.pip dependencies)
- `make devenv` - create local virtual env(dev.pip dependencies)
- `make test` - run tests
- `make lint` - run pylint check
- `make clean` - clean unnecessary files
- `make docker/login` - aws configuration need to be set first
- `make docker/integration` - run integration tests within docker
(when run `make test`, `make lint`  command, all dependencies from requirements/test will be installed.
When run `make venv` base.pip dependencies will be installed)

## Quickstart

### Overridding application config

The most of settings can be overridden with a help of env variables. There are two options for doing that.

First of all env variables can be overridden by placing `local.env` file to the `/env` folder.
Consider this file to be out of VCS control.

Moreover env variables can be overridden with `export` shell command, see examples below.

### Database

Consider having a restored DB dump from staging or an empty DB with migrations applied. Locally migrations
can be applied with a help of `liquibase` tool. Migrations are defined
[here](https://github.com/filtr/delphi-slz-db-schema/tree/master/liquibase/migrations)
```shell script
liquibase --url="jdbc:postgresql://localhost:5432/slz?currentSchema=public" \
--classpath=tmp/postgresql-42.2.14.jar --changeLogFile=db.changelog-master.xml update
```

### Authentication

Generally speaking, there is an integration with Auth0 service for authentication providing.
For using the full auth flow consider having an account at Auth0 service and override env variables
`AUTH0__CLIENT_ID`, `AUTH0__CLIENT_SECRET` and `AUTH0__BASE_URL` according to your account settings.

Another option to get in is using a fake login. This option skips the Auth0 flow.
```shell script
export FAKE_LOGIN_ENABLED=1
```

### Virtual environment

Create virtualenv and install dependencies with a command:
```shell script
make devenv
```

Activate virtualenv:
```shell script
source venv/bin/activate
```

Start an application with hot reload enabled:
```shell script
cd delphi_slz_admin
export DB_URL=postgresql+psycopg2://localhost/slz; inv runserver --port=9080
```

## Developers guide

Use `pre-commit` for git hooks managing (https://pre-commit.com/#installation). Hook configuration
can be found at .pre-commit-config.yaml. `isort` and `yapf` tools are included to run as a part
of precommit hook.

To register a new hook run:
```shell script
pre-commit install
```

To run hooks without code committing:
```shell script
pre-commit run
```

Sometimes these tools can have a conflict regarding import sorting. In this case consider to
enable `include_trailing_comma` option at `setup.cfg` file.

Don’t forget to run `pylint` and `mypy` before code committing manually. It will save you from
wasting time at reading CI logs for failed build.
```shell script
make lint && make typecheck
```

In order to check everything is fine (and have a dry run for formatting and import sorting)
consider following commands, e.g. for `delphi_slz_admin`:
```shell script
cd delphi_slz_admin
workon delphi_slz_admin # or './venv/bin/activate'
isort --check-only -rc . && yapf --recursive --verbose --parallel --diff . \
&& pylint --rcfile=pylintrc ./${PWD##*/} ./tests && mypy ${PWD##*/} && echo 'Exit code:' $?
```

It will run all checks inside a current directory.


## Running tests

There are two options for starting tests. First one is about starting tests with a local environment.
This option is quickest one but it requires a test database to be created and migrations to be applied.
```shell script
export DB_URL=postgresql+psycopg2://localhost/slz_test; make test
```

Another way is running tests within docker containers. First and foremost it's required to perform
login to the AWS console to be able to pull images.
```shell script
make docker/login
```

With a following command it will set up a database container, update it with migrations and start tests.
```shell script
make docker/integration
```
This command starts container with PostgreSQL database, applies migrations to it and invokes
tests. After everything is done temporary containers are removed automatically.
