initial
This commit is contained in:
180
backendpanel/test_api.py
Normal file
180
backendpanel/test_api.py
Normal file
@@ -0,0 +1,180 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple Python script to test all backendpanel APIs, including Map API.
|
||||
Requires: pip install requests
|
||||
Usage: python test_api.py [--base-url URL] [--user USER] [--password PASS]
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
try:
|
||||
import requests
|
||||
except ImportError:
|
||||
print("Error: requests library required. Run: pip install requests")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def test_endpoint(name, method, url, auth=None, json_data=None, expect_status=None, accept_codes=None):
|
||||
"""Call an API endpoint and print result."""
|
||||
try:
|
||||
if method == "GET":
|
||||
r = requests.get(url, auth=auth, timeout=10)
|
||||
elif method == "POST":
|
||||
r = requests.post(
|
||||
url,
|
||||
auth=auth,
|
||||
json=json_data or {},
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=10,
|
||||
)
|
||||
else:
|
||||
print(f" [SKIP] {name}: unsupported method {method}")
|
||||
return False
|
||||
|
||||
if accept_codes is not None:
|
||||
ok = r.status_code in accept_codes
|
||||
elif expect_status is not None:
|
||||
ok = r.status_code == expect_status
|
||||
else:
|
||||
ok = 200 <= r.status_code < 300
|
||||
|
||||
try:
|
||||
body = r.json()
|
||||
preview = json.dumps(body)[:120] + "..." if len(json.dumps(body)) > 120 else json.dumps(body)
|
||||
except Exception:
|
||||
preview = r.text[:120] + "..." if len(r.text) > 120 else r.text
|
||||
|
||||
symbol = "OK" if ok else "FAIL"
|
||||
print(f" [{symbol}] {name}: {r.status_code} - {preview}")
|
||||
return ok
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f" [ERROR] {name}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Test backendpanel APIs")
|
||||
parser.add_argument(
|
||||
"--base-url",
|
||||
default="https://apitest.semestaterpadu.my.id",
|
||||
help="Base URL (default: https://apitest.semestaterpadu.my.id). Use http://localhost for local.",
|
||||
)
|
||||
parser.add_argument("--user", default="", help="Basic auth username (for protected endpoints)")
|
||||
parser.add_argument("--password", default="", help="Basic auth password")
|
||||
args = parser.parse_args()
|
||||
|
||||
base = args.base_url.rstrip("/")
|
||||
auth = (args.user, args.password) if args.user or args.password else None
|
||||
|
||||
print("=" * 60)
|
||||
print("Backendpanel API Test")
|
||||
print("Base URL:", base)
|
||||
print("=" * 60)
|
||||
|
||||
results = []
|
||||
|
||||
# --- Map API (user specifically asked for map) ---
|
||||
print("\n--- Map API ---")
|
||||
results.append(
|
||||
test_endpoint("GET /api/map", "GET", f"{base}/api/map")
|
||||
)
|
||||
results.append(
|
||||
test_endpoint(
|
||||
"POST /api/map/directions",
|
||||
"POST",
|
||||
f"{base}/api/map/directions",
|
||||
json_data={
|
||||
"origin_lat": -6.222320699570134,
|
||||
"origin_lng": 106.83289668750001,
|
||||
"dest_lat": -6.1751,
|
||||
"dest_lng": 106.8650,
|
||||
"mode": "driving",
|
||||
},
|
||||
)
|
||||
)
|
||||
results.append(
|
||||
test_endpoint(
|
||||
"POST /api/map/geocode",
|
||||
"POST",
|
||||
f"{base}/api/map/geocode",
|
||||
json_data={"lat": -6.222320699570134, "lng": 106.83289668750001},
|
||||
)
|
||||
)
|
||||
|
||||
# --- Notification API ---
|
||||
print("\n--- Notification API ---")
|
||||
results.append(
|
||||
test_endpoint("GET /api/notification", "GET", f"{base}/api/notification")
|
||||
)
|
||||
|
||||
# --- Driver API ---
|
||||
print("\n--- Driver API ---")
|
||||
results.append(
|
||||
test_endpoint("GET /api/driver", "GET", f"{base}/api/driver")
|
||||
)
|
||||
if auth:
|
||||
results.append(
|
||||
test_endpoint(
|
||||
"POST /api/driver/privacy",
|
||||
"POST",
|
||||
f"{base}/api/driver/privacy",
|
||||
auth=auth,
|
||||
)
|
||||
)
|
||||
else:
|
||||
print(" [SKIP] POST /api/driver/privacy: requires --user and --password")
|
||||
|
||||
# --- Pelanggan API ---
|
||||
print("\n--- Pelanggan API ---")
|
||||
results.append(
|
||||
test_endpoint("GET /api/pelanggan", "GET", f"{base}/api/pelanggan")
|
||||
)
|
||||
|
||||
# --- Merchant API ---
|
||||
print("\n--- Merchant API ---")
|
||||
results.append(
|
||||
test_endpoint("GET /api/merchant", "GET", f"{base}/api/merchant")
|
||||
)
|
||||
|
||||
# --- Xendit API (needs ServerKey - 500 = invalid key, 200 = possible redirect) ---
|
||||
print("\n--- Xendit API ---")
|
||||
results.append(
|
||||
test_endpoint(
|
||||
"POST /api/xendit/data",
|
||||
"POST",
|
||||
f"{base}/api/xendit/data",
|
||||
json_data={},
|
||||
accept_codes=[200, 500],
|
||||
)
|
||||
)
|
||||
|
||||
# --- Midtrans API (charge needs valid payload - verify endpoint) ---
|
||||
print("\n--- Midtrans API ---")
|
||||
results.append(
|
||||
test_endpoint(
|
||||
"POST /api/midtrans/charge",
|
||||
"POST",
|
||||
f"{base}/api/midtrans/charge",
|
||||
json_data={
|
||||
"transaction_details": {
|
||||
"order_id": "test-order-123",
|
||||
"gross_amount": 10000,
|
||||
},
|
||||
"customer_details": {"first_name": "Test"},
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
# --- Summary ---
|
||||
passed = sum(1 for r in results if r)
|
||||
total = len(results)
|
||||
print("\n" + "=" * 60)
|
||||
print(f"Result: {passed}/{total} tests passed")
|
||||
print("=" * 60)
|
||||
sys.exit(0 if passed == total else 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user