diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml new file mode 100644 index 0000000..5ce3c22 --- /dev/null +++ b/.github/workflows/ci-tests.yml @@ -0,0 +1,31 @@ +name: CI Tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + Tests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.11 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest + + - name: Run tests + run: | + pytest tests + env: + JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY}} diff --git a/.gitignore b/.gitignore index 0c9ea1d..cbc0b15 100644 --- a/.gitignore +++ b/.gitignore @@ -187,5 +187,5 @@ local_cache # workflows -workflows/ +update_db.yml store/ \ No newline at end of file diff --git a/.github/workflows/ci.yml b/tests/__init__.py similarity index 100% rename from .github/workflows/ci.yml rename to tests/__init__.py diff --git a/tests/test_server.py b/tests/test_server.py new file mode 100644 index 0000000..07a3e61 --- /dev/null +++ b/tests/test_server.py @@ -0,0 +1,52 @@ +import jwt +import pytest +from datetime import datetime, timedelta +from fastapi.testclient import TestClient +from src.api import app, create_jwt, JWT_SECRET_KEY, JWT_ALGORITHM + +client = TestClient(app) + +@pytest.fixture() +def valid_token(): + return create_jwt("test_user") + +@pytest.fixture() +def expired_token(): + payload = { + "user_id": "test_user", + "exp": datetime.now() - timedelta(days=1) + } + + return jwt.encode(payload, JWT_SECRET_KEY, algorithm=JWT_ALGORITHM) + + +@pytest.mark.skip(reason="Skipping the test") +def test_github_login(): + response = client.get("/github-login") + assert response.status_code == 302 + assert "https://github.com/login/oauth/authorize" in response.headers["location"] + + +@pytest.mark.skip(reason="Skipping the test") +def test_verify_token(valid_token): + headers = { + "Authorization" : f"Bearer {valid_token}" + } + response = client.get("/verify-token", headers=headers) + assert response.status_code == 200 + assert response.json()["username"] == "test_user" + assert "email" in response.json() + + +def test_verify_token_invalid(expired_token): + headers = { + "Authorization": f"Bearer {expired_token}" + } + + response = client.get("/verify-token", headers=headers) + assert response.status_code == 401 + + +def test_get_recommendations_unauthorized(): + response = client.post("/api/recommendations/") + assert response.status_code == 401