feat(e2e): replicar patrón de autenticación de aduanasoft-hub
- fixtures/keycloak.ts: Admin REST API helpers (createTestUser, assignRoles, deleteTestUser) — copia exacta del hub - global-setup.ts: crea usuario e2e-anexo76 en KC antes de los tests - global-teardown.ts: elimina el usuario al finalizar - auth.setup.ts: navega a /login → Workspace → llena form → /dashboard (ya no usa password grant ni Direct Access Grants) - playwright.config.ts: agrega globalSetup y globalTeardown - Jenkinsfile: a76-e2e-credentials ahora son credenciales de KC admin; usuario de prueba es efímero (creado/borrado por global-setup/teardown) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
51
Jenkinsfile
vendored
51
Jenkinsfile
vendored
@@ -169,17 +169,21 @@ pipeline {
|
||||
|
||||
stage('E2E (Playwright)') {
|
||||
steps {
|
||||
// auth.setup.ts obtiene tokens directamente de Keycloak (password grant)
|
||||
// sin pasar por el browser ni por Workspace — /login hace 303 inmediato.
|
||||
// Patrón idéntico al de aduanasoft-hub:
|
||||
// globalSetup crea usuario de prueba en KC vía Admin API
|
||||
// auth.setup.ts navega a /login → Workspace → llena form → /dashboard
|
||||
// globalTeardown elimina el usuario al finalizar
|
||||
//
|
||||
// Credenciales requeridas en Jenkins:
|
||||
// a76-public-url-dev : URL pública base — la URL de KC se deriva de ella ({url}/kcauth)
|
||||
// a76-e2e-credentials : Username with password — cuenta de Workspace para pruebas E2E
|
||||
// a76-public-url-dev : URL pública base de Anexo76
|
||||
// a76-e2e-credentials : Username with password — admin de Keycloak
|
||||
// (para que globalSetup pueda crear/eliminar usuarios)
|
||||
withCredentials([
|
||||
string(credentialsId: 'a76-public-url-dev', variable: 'A76_URL'),
|
||||
usernamePassword(
|
||||
credentialsId: 'a76-e2e-credentials',
|
||||
usernameVariable: 'E2E_USER',
|
||||
passwordVariable: 'E2E_PASS'
|
||||
usernameVariable: 'E2E_KC_ADMIN_USER',
|
||||
passwordVariable: 'E2E_KC_ADMIN_PASS'
|
||||
)
|
||||
]) {
|
||||
sh '''
|
||||
@@ -200,17 +204,19 @@ pipeline {
|
||||
-e PLAYWRIGHT_TEST_BASE_URL=http://localhost:5173 \
|
||||
-e "VITE_API_URL=${A76_URL}/api/" \
|
||||
-e "INTERNAL_API_URL=${A76_URL}/api/" \
|
||||
-e "VITE_KEYCLOAK_URL=${A76_URL}/kcauth" \
|
||||
-e "KEYCLOAK_URL=${A76_URL}/kcauth" \
|
||||
-e "VITE_KEYCLOAK_URL=${KC_URL}" \
|
||||
-e "KEYCLOAK_URL=${KC_URL}" \
|
||||
-e VITE_KEYCLOAK_REALM=master \
|
||||
-e KEYCLOAK_REALM=master \
|
||||
-e VITE_KEYCLOAK_CLIENT_ID=anexo76-frontend \
|
||||
-e KEYCLOAK_CLIENT_ID=anexo76-frontend \
|
||||
-e ORIGIN=http://localhost:5173 \
|
||||
-e "KC_URL=${KC_URL}" \
|
||||
-e "E2E_KEYCLOAK_URL=${KC_URL}" \
|
||||
-e "E2E_TEST_USER=${E2E_USER}" \
|
||||
-e "E2E_TEST_PASSWORD=${E2E_PASS}" \
|
||||
-e "E2E_KC_ADMIN_USER=${E2E_KC_ADMIN_USER}" \
|
||||
-e "E2E_KC_ADMIN_PASSWORD=${E2E_KC_ADMIN_PASS}" \
|
||||
-e E2E_TEST_USER=e2e-anexo76 \
|
||||
-e E2E_TEST_PASSWORD=E2eAnexo76Test! \
|
||||
-e E2E_TEST_EMAIL=e2e-anexo76@test.local \
|
||||
-w /workspace/frontend \
|
||||
"$C" bash -lc '
|
||||
set -euxo pipefail
|
||||
@@ -218,29 +224,6 @@ pipeline {
|
||||
pnpm install --frozen-lockfile
|
||||
pnpm run i18n:compile
|
||||
|
||||
# Verificar que Keycloak acepta el password grant
|
||||
# (auth.setup.ts lo usa directamente — sin pasar por /login)
|
||||
echo "--- Verificando Keycloak token endpoint ---"
|
||||
KC_TOKEN_EP="${KC_URL}/realms/master/protocol/openid-connect/token"
|
||||
# -s sin -f: curl no falla en HTTP 4xx/5xx, devuelve el código real
|
||||
# || echo "000" solo se activa si curl no puede conectar (timeout, DNS, etc.)
|
||||
KC_STATUS=$(curl -s --max-time 10 -o /dev/null -w "%{http_code}" \
|
||||
-X POST "$KC_TOKEN_EP" \
|
||||
-d "grant_type=password&client_id=anexo76-frontend&username=${E2E_TEST_USER}&password=${E2E_TEST_PASSWORD}&scope=openid" \
|
||||
2>/dev/null || echo "000")
|
||||
echo "Keycloak token endpoint → HTTP ${KC_STATUS}"
|
||||
if [ "$KC_STATUS" = "401" ]; then
|
||||
echo "ERROR 401 — Credenciales inválidas o Direct Access Grants deshabilitado."
|
||||
echo " Verifica en Keycloak: Clients → anexo76-frontend → Settings → Direct access grants = ON"
|
||||
echo " Verifica que el usuario '${E2E_TEST_USER}' exista en el realm y la contraseña sea correcta."
|
||||
exit 1
|
||||
elif [ "$KC_STATUS" != "200" ]; then
|
||||
echo "ERROR: Keycloak no accesible (HTTP ${KC_STATUS})."
|
||||
echo "URL: ${KC_TOKEN_EP}"
|
||||
exit 1
|
||||
fi
|
||||
echo "Keycloak OK"
|
||||
|
||||
# Arrancar dev server
|
||||
pnpm run dev &
|
||||
DEV_PID=$!
|
||||
|
||||
Reference in New Issue
Block a user