From dfb103e16a9b62fba6138d2e45e79d2372426568 Mon Sep 17 00:00:00 2001 From: Mohammad Sajad Pourajam Date: Wed, 6 May 2026 22:41:53 +0330 Subject: [PATCH] UPDATE --- config/apis.yaml | 26 ++++++-------------------- logs/test.log | 34 ++++------------------------------ tests/test_authentication.py | 25 +++++-------------------- utils/http_client.py | 32 ++++++++++++++++++++++++++++---- utils/yaml_loader.py | 4 ++-- 5 files changed, 45 insertions(+), 76 deletions(-) diff --git a/config/apis.yaml b/config/apis.yaml index 5ade919..959b482 100644 --- a/config/apis.yaml +++ b/config/apis.yaml @@ -1,30 +1,16 @@ -base_url: http://backend:8000/api/auth +base_url: http://backend-web:8000/api flows: - register_login: + auth: - register: - method: POST - path: /register/ - body: - username: "{random_username}" - email: "{random_username}@example.com" - phone_number: "09120000000" - password: "test123456" - first_name: "test" - last_name: "user" - - expected_status: 201 - expected_json: - msg: success login: method: POST - path: /login/ + path: /auth/login/ body: - identifier: "{random_username}" - password: "test123456" + identifier: "admin" + password: "admin123456" expected_status: 200 @@ -32,6 +18,6 @@ flows: token: token store_redis: - key: "test_token:{random_username}" + key: "test_token" ttl: 3600 diff --git a/logs/test.log b/logs/test.log index c40c6f5..76c0e6b 100644 --- a/logs/test.log +++ b/logs/test.log @@ -3,34 +3,8 @@ platform linux -- Python 3.10.20, pytest-9.0.3, pluggy-1.6.0 -- /usr/local/bin/p cachedir: .pytest_cache rootdir: /app configfile: pytest.ini -collecting ... collected 0 items / 1 error +collecting ... collected 1 item -==================================== ERRORS ==================================== -________________ ERROR collecting tests/test_authentication.py _________________ -ImportError while importing test module '/app/tests/test_authentication.py'. -Hint: make sure your test modules/packages have valid Python names. -Traceback: -/usr/local/lib/python3.10/site-packages/_pytest/python.py:507: in importtestmodule - mod = import_path( -/usr/local/lib/python3.10/site-packages/_pytest/pathlib.py:587: in import_path - importlib.import_module(module_name) -/usr/local/lib/python3.10/importlib/__init__.py:126: in import_module - return _bootstrap._gcd_import(name[level:], package, level) -:1050: in _gcd_import - ??? -:1027: in _find_and_load - ??? -:1006: in _find_and_load_unlocked - ??? -:688: in _load_unlocked - ??? -/usr/local/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:197: in exec_module - exec(co, module.__dict__) -tests/test_authentication.py:5: in - from utils.yaml_loader import load_config -E ImportError: cannot import name 'load_config' from 'utils.yaml_loader' (/app/utils/yaml_loader.py) -=========================== short test summary info ============================ -ERROR tests/test_authentication.py -!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!! -!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!! -=============================== 1 error in 0.52s =============================== +tests/test_authentication.py::test_auth_flow PASSED [100%] + +============================== 1 passed in 0.67s =============================== diff --git a/tests/test_authentication.py b/tests/test_authentication.py index a903e61..05d4d84 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -9,7 +9,7 @@ from utils.template import render config = load_config() BASE_URL = config["base_url"] -flow = config["flows"]["register_login"] +flow = config["flows"]["auth"] redis_client = redis.Redis( host="redis", @@ -22,26 +22,9 @@ def test_auth_flow(): context = {} - context["random_username"] = f"user_{uuid.uuid4().hex[:6]}" # -------- register -------- - register = flow["register"] - - body = render(register["body"], context) - - res = http_request( - register["method"], - BASE_URL + register["path"], - json=body - ) - - assert res["status_code"] == register["expected_status"] - - assert res["json"]["msg"] == register["expected_json"]["msg"] - - # -------- login -------- - login = flow["login"] body = render(login["body"], context) @@ -49,14 +32,16 @@ def test_auth_flow(): res = http_request( login["method"], BASE_URL + login["path"], + authenticated=False, json=body ) + print(res) - assert res["status_code"] == login["expected_status"] + assert res["status"] == login["expected_status"] token_field = login["extract"]["token"] - token = res["json"][token_field] + token = res["data"][token_field] assert token is not None diff --git a/utils/http_client.py b/utils/http_client.py index b93b5f5..dc4a890 100644 --- a/utils/http_client.py +++ b/utils/http_client.py @@ -1,16 +1,41 @@ import requests import time +import redis +from utils.yaml_loader import load_config -def http_request(method, url, **kwargs): +redis_client = redis.Redis( + host="redis", + port=6379, + decode_responses=True +) + +config = load_config() + + +def http_request(method, url, authenticated=True, **kwargs): start = time.time() - response = requests.request(method, url, **kwargs) + headers = kwargs.pop("headers", {}) + + if authenticated: + token_key = config["flows"]["auth"]["login"]["store_redis"]["key"] + token = redis_client.get(token_key) + + if token: + headers["Authorization"] = f"Bearer {token}" + + response = requests.request( + method, + url, + headers=headers, + **kwargs + ) latency = time.time() - start try: data = response.json() - except: + except Exception: data = response.text return { @@ -18,4 +43,3 @@ def http_request(method, url, **kwargs): "data": data, "latency": latency } - diff --git a/utils/yaml_loader.py b/utils/yaml_loader.py index 6bd969c..267d307 100644 --- a/utils/yaml_loader.py +++ b/utils/yaml_loader.py @@ -1,6 +1,6 @@ import yaml -def load_apis(): - with open("apis.yaml", "r") as f: +def load_config(): + with open("config/apis.yaml", "r") as f: return yaml.safe_load(f)