Skip to content

Add Github test workflow #5

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

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Run Tests

on:
push:
branches:
- main
- dev
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
container:
image: python:3.11-slim
env:
DATABASE_URL: postgresql://postgres:password@postgres:5432/postgres
ENVIRONMENT: test
JWT_SECRET: test_secret
API_KEY: test_key
AZURE_STORAGE_CONNECTION_STRING: test_connection_string
AZURE_STORAGE_CONTAINER: test_container
PYTHONUNBUFFERED: "1"
ENV_MODE: local
SAS_URL: https://test.blob.core.windows.net/test
API_JWT: test-admin-token

services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Display Python version and environment info
run: |
python -V
pip -V
env | sort

- name: Install system dependencies
run: |
apt-get update
apt-get install -y postgresql-client libpq-dev gcc python3-dev

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements_dev.txt
pip list

- name: Check PostgreSQL connection
run: |
echo "Waiting for PostgreSQL to be ready..."
timeout 20s bash -c 'until pg_isready -h postgres -p 5432 -U postgres; do sleep 1; done'
env:
PGPASSWORD: password

- name: Initialize database
run: |
set -e
echo "Initializing database with schema..."
PGPASSWORD=password psql -h postgres -U postgres -d postgres -f sql/create_tables.sql -v ON_ERROR_STOP=1
echo "Verifying tables..."
PGPASSWORD=password psql -h postgres -U postgres -d postgres -c "\dt" -t | wc -l | xargs -I {} test {} -gt 0
echo "Database initialization completed successfully"

- name: Initialize test data
run: |
echo "Initializing test data..."
# Create test admin user
PGPASSWORD=password psql -h postgres -U postgres -d postgres -c "INSERT INTO users (email, role, name) VALUES ('[email protected]', 'Admin', 'Test User');"
# Create test regular user
PGPASSWORD=password psql -h postgres -U postgres -d postgres -c "INSERT INTO users (email, role, name) VALUES ('[email protected]', 'User', 'Regular User');"
# Initialize test signals
PGPASSWORD=password psql -h postgres -U postgres -d postgres -f sql/init_test_data.sql
PGPASSWORD=password psql -h postgres -U postgres -d postgres -c "SELECT insert_test_signals();"
echo "Test data initialization completed successfully"

- name: Run tests with coverage
run: |
python -m pytest tests/ -v --cov=src --cov-report=xml --cov-report=term-missing
env:
DB_CONNECTION: postgresql://postgres:password@postgres:5432/postgres
10 changes: 7 additions & 3 deletions src/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ async def authenticate_user(
user : User
Pydantic model for a User object (if authentication succeeded).
"""
logging.debug(f"Authenticating user with token")
logging.debug("Authenticating user with token")
if os.environ.get("TEST_USER_TOKEN"):
token = os.environ.get("TEST_USER_TOKEN")

if os.environ.get("ENV_MODE") == "local":
# defaul user data
# default user data
user_data = {
"email": "[email protected]",
"name": "Test User",
Expand All @@ -149,18 +150,21 @@ async def authenticate_user(
return User(**user_data)

if token == os.environ.get("API_KEY"):
if os.environ.get("ENV") == "dev":
if os.environ.get("ENV_MODE") == "local":
return User(email="[email protected]", role=Role.ADMIN)
else:
# dummy user object for anonymous access
return User(email="[email protected]", role=Role.VISITOR)

try:
payload = await decode_token(token)
except jwt.exceptions.PyJWTError as e:
raise exceptions.not_authenticated from e

email, name = payload.get("unique_name"), payload.get("name")
if email is None or name is None:
raise exceptions.not_authenticated

if (user := await db.read_user_by_email(cursor, email)) is None:
user = User(email=email, role=Role.USER, name=name)
await db.create_user(cursor, user)
Expand Down
Loading