diff --git a/test_suite.py b/test_suite.py new file mode 100644 index 00000000..a93d5abd --- /dev/null +++ b/test_suite.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 +import sys +import xml.etree.ElementTree as ET +import requests + +def test_stage_robots(): + print("[1/7] Testing Stage robots.txt...") + r = requests.get('https://stage.canina.ir/robots.txt', timeout=10) + assert r.status_code == 200, f"Expected 200, got {r.status_code}" + assert "Disallow: /" in r.text, "Stage robots.txt does not disallow crawlers!" + print(" ✓ PASS: Staging robots.txt disallows all crawlers (Disallow: /)") + +def test_prod_robots(): + print("[2/7] Testing Production robots.txt...") + r = requests.get('https://canina.ir/robots.txt', timeout=10) + assert r.status_code == 200, f"Expected 200, got {r.status_code}" + assert "Disallow: /" not in r.text or "Allow: /" in r.text, "Prod robots.txt blocks crawlers!" + print(" ✓ PASS: Production robots.txt allows crawlers and maintains sitemaps") + +def test_x_robots_tag_headers(): + print("[3/7] Testing X-Robots-Tag Headers on Stage vs Prod...") + r_stage = requests.get('https://stage.canina.ir/', timeout=10) + assert 'noindex' in r_stage.headers.get('x-robots-tag', '').lower(), "Stage lacks X-Robots-Tag: noindex" + + r_prod = requests.get('https://canina.ir/', timeout=10) + assert 'noindex' not in r_prod.headers.get('x-robots-tag', '').lower(), "Prod has unexpected noindex tag!" + print(" ✓ PASS: Headers correctly isolate Stage (noindex) and Production (clean indexable)") + +def test_torob_feed_contract(): + print("[4/7] Testing Torob Marketplace API Contract...") + r = requests.get('https://stage.canina.ir/api/torob/products', timeout=10) + assert r.status_code == 200, f"Expected 200, got {r.status_code}" + data = r.json() + assert 'count' in data and 'results' in data, "Torob feed schema missing count/results" + items = data['results'] + assert len(items) > 0, "Torob feed returned empty items" + for item in items: + assert 'title' in item and item['title'], "Item missing title" + assert 'price' in item and isinstance(item['price'], (int, float)), "Item missing valid price" + assert item['price'] > 0, f"Item price {item['price']} <= 0" + assert 'availability' in item, "Item missing availability" + assert item['availability'] in ['instock', 'outofstock'], f"Invalid availability: {item['availability']}" + assert 'page_url' in item, "Item missing page_url" + print(f" ✓ PASS: Torob API contract verified on {len(items)} products") + +def test_rss_xml_feed_structure(): + print("[5/7] Testing RSS XML Shop Feed Structure...") + r = requests.get('https://stage.canina.ir/shop/feed.xml', timeout=10) + assert r.status_code == 200, f"Expected 200, got {r.status_code}" + root = ET.fromstring(r.text) + channel = root.find('channel') + assert channel is not None, "RSS feed missing channel tag" + items = channel.findall('item') + assert len(items) > 0, "RSS feed has no items" + for item in items: + title = item.find('title') + link = item.find('link') + assert title is not None and title.text, "RSS item missing title" + assert link is not None and link.text, "RSS item missing link" + print(f" ✓ PASS: RSS XML feed parsed and verified with {len(items)} valid entries") + +def test_backend_products_inventory(): + print("[6/7] Testing Backend Product Inventory & Price Consistency...") + r = requests.get('https://stageapi.canina.ir/api/products?limit=10', timeout=10) + assert r.status_code == 200, f"Expected 200, got {r.status_code}" + data = r.json() + items = data.get('data', []) + assert len(items) > 0, "Products API returned empty array" + for p in items: + assert 'id' in p, "Product missing id" + assert 'slug' in p and p['slug'], "Product missing slug" + assert 'priceValue' in p, "Product missing priceValue" + int_price = int(p['priceValue']) + assert int_price > 0, f"Invalid product price: {int_price}" + assert 'stock' in p, "Product missing inventory stock field" + assert isinstance(p['stock'], int), f"Stock not integer: {type(p['stock'])}" + assert 'stockStatus' in p, "Product missing stockStatus field" + print(f" ✓ PASS: Inventory stock and price consistency verified across {len(items)} products") + +def test_checkout_validation_rejection(): + print("[7/7] Testing Order Checkout Validation & Security Guardrails...") + # Attempt creating an order with invalid payload (empty cart, invalid contact) + invalid_order = { + "items": [], + "fullName": "", + "phoneNumber": "123", + "address": "" + } + r = requests.post('https://stageapi.canina.ir/api/orders', json=invalid_order, timeout=10) + # Backend must reject with 400 Bad Request or 401 Unauthorized, never 500 + assert r.status_code in [400, 401, 422], f"Unexpected status on invalid order: {r.status_code}" + print(f" ✓ PASS: Backend order validation correctly rejected invalid order (HTTP {r.status_code})") + +if __name__ == '__main__': + print("=" * 60) + print("CANINA E2E & DEEP BEHAVIORAL VERIFICATION SUITE") + print("=" * 60) + try: + test_stage_robots() + test_prod_robots() + test_x_robots_tag_headers() + test_torob_feed_contract() + test_rss_xml_feed_structure() + test_backend_products_inventory() + test_checkout_validation_rejection() + print("=" * 60) + print("ALL 7 DEEP INTEGRATION TESTS PASSED SUCCESSFULLY!") + print("=" * 60) + sys.exit(0) + except AssertionError as e: + print(f"\n❌ TEST FAILED: {e}") + sys.exit(1) + except Exception as e: + print(f"\n❌ UNEXPECTED ERROR: {e}") + sys.exit(2)