Skip to content

Commit

Permalink
1. fix ci-tests, 2. added the tests for the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Hk669 committed Jul 11, 2024
1 parent 9aafdbd commit ad20f5a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -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}}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,5 @@ local_cache


# workflows
workflows/
update_db.yml
store/
File renamed without changes.
52 changes: 52 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ad20f5a

Please sign in to comment.