diff --git a/README.md b/README.md
index 03b39dec..f7b48ef7 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,7 @@ cp app/config.py.example app/config.py
# Edit the variables' values.
# Rendering JWT_KEY:
python -c "import secrets; from pathlib import Path; f = Path('app/config.py'); f.write_text(f.read_text().replace('JWT_KEY_PLACEHOLDER', secrets.token_hex(32), 1));"
+```
### Running tox
```shell
diff --git a/app/internal/global_variable.py b/app/internal/global_variable.py
new file mode 100644
index 00000000..b1c1755d
--- /dev/null
+++ b/app/internal/global_variable.py
@@ -0,0 +1,18 @@
+from fastapi import Request
+
+from app.dependencies import templates
+from app.internal.security.ouath2 import (
+ Session, get_jwt_token, get_authorization_cookie
+)
+
+
+async def get_user_for_global_var(db: Session, jwt: str) -> str:
+ jwt_payload = await get_jwt_token(db, jwt)
+ username = jwt_payload.get("sub")
+ return username
+
+
+async def set_global_user_var(request: Request, db: Session, temp: templates):
+ jwt = await get_authorization_cookie(request)
+ user = await get_user_for_global_var(db, jwt)
+ temp.env.globals['user'] = user
diff --git a/app/templates/global_var_test.html b/app/templates/global_var_test.html
new file mode 100644
index 00000000..16d23ada
--- /dev/null
+++ b/app/templates/global_var_test.html
@@ -0,0 +1,52 @@
+
+
+
+
+
+
+
+ PyLendar global var test
+
+
+
+
+
+
+
+ {% if user %}
+ username: {{ user }}
+ {% endif %}
+
+
+
\ No newline at end of file
diff --git a/tests/client_fixture.py b/tests/client_fixture.py
index c40b9fb8..a7dbe4e5 100644
--- a/tests/client_fixture.py
+++ b/tests/client_fixture.py
@@ -11,9 +11,10 @@
invitation, profile, weight,
)
from app.routers.salary import routes as salary
-from tests import security_testing_routes
+from tests import security_testing_routes, global_var_testing_routes
from tests.conftest import get_test_db, test_engine
+main.app.include_router(global_var_testing_routes.router)
main.app.include_router(security_testing_routes.router)
@@ -44,6 +45,11 @@ def create_test_client(get_db_function) -> Generator[Session, None, None]:
Base.metadata.drop_all(bind=test_engine)
+@pytest.fixture(scope="session")
+def global_var_test_client() -> Iterator[TestClient]:
+ yield from create_test_client(global_var_testing_routes.get_db)
+
+
@pytest.fixture(scope="session")
def agenda_test_client() -> Generator[TestClient, None, None]:
yield from create_test_client(agenda.get_db)
diff --git a/tests/global_var_testing_routes.py b/tests/global_var_testing_routes.py
new file mode 100644
index 00000000..52c9ac0d
--- /dev/null
+++ b/tests/global_var_testing_routes.py
@@ -0,0 +1,21 @@
+from sqlalchemy.orm import Session
+
+from app.dependencies import get_db, templates
+from app.internal.global_variable import set_global_user_var
+from fastapi import APIRouter, Depends, Request
+
+
+router = APIRouter(
+ prefix="/global-variable",
+ tags=["global-variable"],
+ responses={404: {"description": "Not found"}},
+)
+
+
+@router.get("/")
+async def global_var(request: Request, db: Session = Depends(get_db)):
+ await set_global_user_var(request, db, templates)
+
+ return templates.TemplateResponse("global_var_test.html", {
+ "request": request
+ })
diff --git a/tests/test_global_variable.py b/tests/test_global_variable.py
new file mode 100644
index 00000000..8ab75457
--- /dev/null
+++ b/tests/test_global_variable.py
@@ -0,0 +1,28 @@
+REGISTER_DETAIL = {
+ 'username': 'correct_user', 'full_name': 'full_name',
+ 'password': 'correct_password', 'confirm_password': 'correct_password',
+ 'email': 'example@email.com', 'description': ""}
+
+LOGIN_DATA = {'username': 'correct_user', 'password': 'correct_password'}
+
+
+def test_global_var(global_var_test_client):
+ response = global_var_test_client.get("/global-variable")
+
+ assert response.ok
+ assert b'correct_user' not in response.content
+ assert b'Sign In' in response.content
+ assert b'Sign Up' in response.content
+
+ global_var_test_client.post(
+ global_var_test_client.app.url_path_for('register'),
+ data=REGISTER_DETAIL)
+ global_var_test_client.post(
+ global_var_test_client.app.url_path_for('login'),
+ data=LOGIN_DATA)
+
+ response = global_var_test_client.get("/global-variable")
+ assert response.ok
+ assert b'correct_user' in response.content
+ assert b'Sign In' not in response.content
+ assert b'Profile' in response.content