Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic unit tests and unit test running action #16

Merged
merged 9 commits into from
Oct 6, 2021
Merged
28 changes: 28 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Run pytests

on:
pull_request:
branches: [ "*" ]
paths:
- 'data/**'
- 'eligibility_server/**'
- 'keys/**'
- 'static/**'
machikoyasuda marked this conversation as resolved.
Show resolved Hide resolved
- 'tests/**'
- 'requirements.txt'

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Install package and dependencies
run: |
python -m pip install --upgrade pip
pip install -r tests/requirements.txt
pip install -r requirements.txt
- name: Test with pytest
run: |
coverage run -m pytest
coverage report -m
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ docker compose build server

- Build and Open in Container on VS Code

## Tests

### Run tests
```bash
coverage run -m pytest
```

### Check test coverage

```bash
coverage report -m
```

## License

[AGPL-3.0 License](./LICENSE)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import pytest

from eligibility_server.app import app as server
from eligibility_server.app import Database as Database


@pytest.fixture
def app():
machikoyasuda marked this conversation as resolved.
Show resolved Hide resolved
yield server


@pytest.fixture
def database():
db = Database()
return db


@pytest.fixture
def client(app):
return app.test_client()
2 changes: 2 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
pytest
9 changes: 7 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
def test_healthcheck(app, client):
response = client.get("/healthcheck")
"""
Test API
machikoyasuda marked this conversation as resolved.
Show resolved Hide resolved
"""


def test_healthcheck(client):
response = client.get("healthcheck")
assert response.status_code == 200
assert response.data == b"Healthy"
57 changes: 57 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Test database class and methods
"""

import json

with open("data/server.json", encoding="utf8") as file:
DATA = json.load(file)


def test_database_init(database):
assert database._config
assert database._merchants
assert database._users


def test_database_properties(database):
assert database.auth_header
assert database.auth_token
assert database.token_header
assert database.jwe_cek_enc
assert database.jwe_encryption_alg
assert database.jws_signing_alg
assert database.request_access


def test_database_check_merchant_in_database(database):
merchant_id = DATA["merchants"][1]
response = database.check_merchant(merchant_id)
assert response is True


def test_database_check_merchant_not_in_database(database):
merchant_id_not_in_database = "123"
response = database.check_merchant(merchant_id_not_in_database)
assert response is False


def test_database_check_user_in_database(database):
key = min(DATA["users"])
user = DATA["users"][key][0]
types = DATA["users"][key][1]
response = database.check_user(key, user, types)
assert response == types


def test_database_check_user_in_database_not_eligible(database):
key = min(DATA["users"])
user = DATA["users"][key][0]
types = ["type2"]
response = database.check_user(key, user, types)
assert response == []


def test_database_check_user_not_in_database(database):
response = database.check_user("G7778889", "Thomas", ["type1"])
assert response == []