UPDATE
This commit is contained in:
+6
-20
@@ -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
|
||||
|
||||
|
||||
+4
-30
@@ -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)
|
||||
<frozen importlib._bootstrap>:1050: in _gcd_import
|
||||
???
|
||||
<frozen importlib._bootstrap>:1027: in _find_and_load
|
||||
???
|
||||
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
|
||||
???
|
||||
<frozen importlib._bootstrap>: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 <module>
|
||||
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 ===============================
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+28
-4
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user