From 65d7b742f0d8451ee1c4dcd6d5aea02200c0f12b Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Wed, 18 Oct 2023 10:02:50 -0400 Subject: [PATCH 01/10] Make warnings into errors during tests. Ignore two warnings (see comments) --- pyproject.toml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1b49ff2..4d64e7a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,8 +28,15 @@ httpx = "^0.25.0" [tool.pytest.ini_options] log_cli = true log_cli_level = "DEBUG" -# log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)" -# log_cli_date_format = "%Y-%m-%d %H:%M:%S" +addopts = "--tb=short" +filterwarnings = [ + "error", + # filed an issue: https://foss.heptapod.net/python-libs/passlib/-/issues/188 + "ignore:'crypt' is deprecated and slated for removal.*:DeprecationWarning:passlib", + # https://github.com/ReproNim/repromon/issues/9 + "ignore: passing settings to sha256_crypt.hash\\(\\) is deprecated, .*:DeprecationWarning:passlib", +] + [build-system] requires = ["poetry>=1.0"] From f09758606d806faac3a6cc453a7b1b7e5e1a9410 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 00:19:55 +0300 Subject: [PATCH 02/10] Fixed wraning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- repromon_app/security.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repromon_app/security.py b/repromon_app/security.py index bbd5fa2..4c444ee 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -3,7 +3,7 @@ import hashlib import logging import uuid -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from typing import Annotated from fastapi import Depends, HTTPException, Request, status @@ -166,7 +166,7 @@ def create_access_token(self, username: str, expire_sec: int = -1) -> str: expire_sec = app_settings().TOKEN_EXPIRE_SEC if username and len(username) > 0: expire: datetime.datetime = \ - datetime.utcnow() + timedelta(seconds=expire_sec) + datetime.now(timezone.utc) + timedelta(seconds=expire_sec) data = { "sub": username, "exp": expire From 1369a891eaa7901511c0e8e486e453770b62a945 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 00:27:49 +0300 Subject: [PATCH 03/10] Fixed wraning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- repromon_app/security.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repromon_app/security.py b/repromon_app/security.py index 4c444ee..49a01af 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -166,7 +166,7 @@ def create_access_token(self, username: str, expire_sec: int = -1) -> str: expire_sec = app_settings().TOKEN_EXPIRE_SEC if username and len(username) > 0: expire: datetime.datetime = \ - datetime.now(timezone.utc) + timedelta(seconds=expire_sec) + datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(seconds=expire_sec) data = { "sub": username, "exp": expire From 2cc3599321b9d7e948f18c92240e3a81abd58776 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 00:51:37 +0300 Subject: [PATCH 04/10] Fixed wraning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- repromon_app/security.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repromon_app/security.py b/repromon_app/security.py index 49a01af..7a9e104 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -166,7 +166,8 @@ def create_access_token(self, username: str, expire_sec: int = -1) -> str: expire_sec = app_settings().TOKEN_EXPIRE_SEC if username and len(username) > 0: expire: datetime.datetime = \ - datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(seconds=expire_sec) + datetime.now(timezone.utc) + timedelta(seconds=expire_sec) + expire = expire.timestamp() data = { "sub": username, "exp": expire From 278aa4e9da9a52f89d67f11f9ceecf65e17d1421 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 01:02:20 +0300 Subject: [PATCH 05/10] Fixed wraning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- repromon_app/security.py | 1 - repromon_app/tests/conftest.py | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/repromon_app/security.py b/repromon_app/security.py index 7a9e104..4c444ee 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -167,7 +167,6 @@ def create_access_token(self, username: str, expire_sec: int = -1) -> str: if username and len(username) > 0: expire: datetime.datetime = \ datetime.now(timezone.utc) + timedelta(seconds=expire_sec) - expire = expire.timestamp() data = { "sub": username, "exp": expire diff --git a/repromon_app/tests/conftest.py b/repromon_app/tests/conftest.py index f32a009..72699a9 100644 --- a/repromon_app/tests/conftest.py +++ b/repromon_app/tests/conftest.py @@ -65,10 +65,10 @@ def init_db(): _apikey_tester3 = svc.get_user_apikey("tester3").apikey global _token_admin, _token_tester1, _token_tester2, _token_tester3 - _token_admin = svc.create_access_token("admin", 60 * 60).access_token - _token_tester1 = svc.create_access_token("tester1", 60 * 60).access_token - _token_tester2 = svc.create_access_token("tester2", 60 * 60).access_token - _token_tester3 = svc.create_access_token("tester3", 60 * 60).access_token + _token_admin = svc.create_access_token("admin", 24 * 60 * 60).access_token + _token_tester1 = svc.create_access_token("tester1", 24 * 60 * 60).access_token + _token_tester2 = svc.create_access_token("tester2", 24 * 60 * 60).access_token + _token_tester3 = svc.create_access_token("tester3", 24 * 60 * 60).access_token yield From d0b9f6b7445d25fd6a471f9ffad3f413a9279dc7 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 01:12:28 +0300 Subject: [PATCH 06/10] Fixed wraning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- .github/workflows/pytest.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index b9f4a0f..a5ceead 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -28,6 +28,8 @@ jobs: git config --global user.name "repromon-test" git config --global user.email "repromon-test@example.com" uname -a + date -Is + date -u - name: Checkout source code uses: actions/checkout@v4 From bf8c90bb61007780e4d4cb6ae74d3cd1a11a60e6 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 02:15:09 +0300 Subject: [PATCH 07/10] Fixed warning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- .github/workflows/pytest.yml | 2 +- repromon_app/security.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index a5ceead..5f16739 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -57,7 +57,7 @@ jobs: - name: Run pytest run: | - poetry run pytest --cov=. --cov-report=xml + poetry run pytest --cov=. --cov-report=xml -s shell: bash - name: Upload coverage reports to Codecov diff --git a/repromon_app/security.py b/repromon_app/security.py index 4c444ee..c1bb3f7 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -169,7 +169,7 @@ def create_access_token(self, username: str, expire_sec: int = -1) -> str: datetime.now(timezone.utc) + timedelta(seconds=expire_sec) data = { "sub": username, - "exp": expire + "exp": int(expire.timestamp()) } logger.debug(f"data={str(data)}") token: str = jwt.encode(data, From 76f08d6ed2b5197b322b7a0edef337242c7a1b76 Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 02:28:25 +0300 Subject: [PATCH 08/10] Fixed warning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- repromon_app/security.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/repromon_app/security.py b/repromon_app/security.py index c1bb3f7..3f57c82 100644 --- a/repromon_app/security.py +++ b/repromon_app/security.py @@ -498,7 +498,7 @@ async def _web_oauth2_context( detail="Unauthorized: Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) - logger.debug(f"_web_oauth2_context, token: {token}, strict={strict}") + logger.debug(f"_web_oauth2_context, token: ***, strict={strict}") try: mgr: SecurityManager = SecurityManager.instance() username: str = mgr.get_username_by_token(token) @@ -515,6 +515,7 @@ async def _web_oauth2_context( return SecurityManager.instance().create_empty_context() except BaseException as be: if strict: + logging.error(f"Unauthorized exception occurred: {str(be)}", exc_info=True) credentials_exception.detail = f"Unauthorized: {str(be)}" raise credentials_exception return SecurityManager.instance().create_empty_context() From 3d597b326fb6c54ff54c14eeefe96e9980daa91e Mon Sep 17 00:00:00 2001 From: Vadim Melnik Date: Thu, 19 Oct 2023 02:37:23 +0300 Subject: [PATCH 09/10] Fixed warning: "DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version". --- .github/workflows/pytest.yml | 2 +- README.md | 1 + pyproject.toml | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 5f16739..a5ceead 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -57,7 +57,7 @@ jobs: - name: Run pytest run: | - poetry run pytest --cov=. --cov-report=xml -s + poetry run pytest --cov=. --cov-report=xml shell: bash - name: Upload coverage reports to Codecov diff --git a/README.md b/README.md index 5c6d648..ec35695 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ or via poetry: ./venv/bin/poetry run pytest + ./venv/bin/poetry run pytest --cov=. --cov-report=xml -s