93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Test puntual del fallback que insertamos:
|
|
- Crea contacto en PINOTEPA SIN poblar el CF id_contacto_sucursal.
|
|
- Dispara el webhook Sucursal->Marca.
|
|
- Espera unos segundos.
|
|
- Verifica que el CF en la SUCURSAL quedo poblado == contact.id.
|
|
Eso prueba que el IF entro por la rama "false" y el PUT autoenlazo.
|
|
|
|
- Cleanup: borra el contacto sucursal y cualquier contacto Marca que se haya
|
|
creado con CF apuntando al sucursal.
|
|
"""
|
|
import os
|
|
import sys
|
|
import time
|
|
import json
|
|
import datetime
|
|
|
|
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, ROOT_DIR)
|
|
sys.path.insert(0, os.path.join(ROOT_DIR, "scripts"))
|
|
|
|
# Importa helpers del harness E2E
|
|
from scripts import n8n_e2e_test as t
|
|
|
|
PINOTEPA = t.PINOTEPA_LOCATION_ID
|
|
BRAND = t.BRAND_LOCATION_ID
|
|
WEBHOOK = t.WEBHOOK_URL_SUC_TO_MARCA
|
|
CF_KEY = t.CF_KEY
|
|
|
|
|
|
def log(msg):
|
|
ts = datetime.datetime.now().strftime("%H:%M:%S")
|
|
print(f"[{ts}] {msg}", flush=True)
|
|
|
|
|
|
def main():
|
|
cleanup = t.Cleanup()
|
|
try:
|
|
# 1) Resolver field_id del CF en sucursal (varia por sucursal)
|
|
cf_id_branch = t.resolve_cf_id_in_location(PINOTEPA, CF_KEY)
|
|
log(f"CF field_id en PINOTEPA: {cf_id_branch}")
|
|
assert cf_id_branch, "No se resolvio field_id del CF en sucursal"
|
|
|
|
# 2) Crear contacto sucursal SIN CF poblado
|
|
phone = "+5219991119911"
|
|
payload = t._new_contact_payload("FB", phone=phone, email="fbtest@e3.local")
|
|
branch_id = t.create_contact(PINOTEPA, payload)
|
|
cleanup.register(PINOTEPA, branch_id, "fallback branch")
|
|
log(f"Branch contact creado: {branch_id}")
|
|
|
|
# 3) Sanity: confirmar que el CF arranca VACIO en sucursal
|
|
c0 = t.get_contact(PINOTEPA, branch_id)
|
|
cf0 = t.cf_value_of(c0, cf_id_branch)
|
|
log(f"CF pre-webhook en sucursal: {cf0!r} (esperado: vacio)")
|
|
assert not cf0, f"CF deberia estar vacio antes del webhook, valor={cf0!r}"
|
|
|
|
# 4) Disparar webhook Sucursal -> Marca
|
|
webhook_payload = t.build_webhook_payload_suc_to_marca(c0)
|
|
s, body = t.fire_webhook(WEBHOOK, webhook_payload)
|
|
log(f"Webhook status: {s} body: {body[:100]}")
|
|
assert s == 200, f"Webhook fallo: {s}"
|
|
|
|
# 5) Esperar a que el workflow termine + GHL indexe
|
|
log("Esperando 15s para que el fallback PUT corra y GHL indexe...")
|
|
time.sleep(15)
|
|
|
|
# 6) Verificar que el CF en SUCURSAL quedo poblado con branch_id
|
|
c1 = t.get_contact(PINOTEPA, branch_id)
|
|
cf1 = t.cf_value_of(c1, cf_id_branch)
|
|
log(f"CF post-webhook en sucursal: {cf1!r}")
|
|
|
|
if cf1 == branch_id:
|
|
log("[OK] El fallback autoenlazo correctamente: rama false -> PUT funciono.")
|
|
else:
|
|
log(f"[FAIL] CF post-webhook = {cf1!r}, esperaba {branch_id!r}")
|
|
sys.exit(2)
|
|
|
|
# 7) Buscar contacto Marca creado (deberia existir con CF apuntando)
|
|
brand_matches = t._find_brand_with_cf(branch_id)
|
|
if brand_matches:
|
|
for m in brand_matches:
|
|
cleanup.register(BRAND, m["id"], "fallback brand (creado por workflow)")
|
|
log(f"[OK] Brand creado: {[m['id'] for m in brand_matches]}")
|
|
else:
|
|
log("[WARN] No se encontro contacto Marca con CF=branch_id (puede tardar mas en indexar).")
|
|
|
|
finally:
|
|
cleanup.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|