samba.mjs 161 lignes · 9331 octets
// Parcours « Postes Windows (Samba AD) » (OI-129, fiche E-009b), sur une instance vierge de
// l'installeur, Ansible et ssh simulés : « Plusieurs serveurs » avec srv1.exemple.fr (annuaire,
// outils, supervision) et dc.exemple.fr, serveur dédié au contrôleur (l'annuaire avec Samba est
// refusé en E-003) ; samba coché en E-004 ; nom du contrôleur proposé puis vérifié en E-005 ;
// carte, plan et avertissement de D-068 en E-010 ; section « Contrôleur Samba » en E-012.
export async function parcourirSamba(p, verifier, { controlerA11y = async () => {} } = {}) {
  const attendreUrl = (fragment, delai = 10000) =>
    p.waitForFunction((f) => location.pathname.endsWith(f), { timeout: delai }, fragment).then(() => true, () => false);
  const remplir = async (selecteur, valeur) => {
    await p.$eval(selecteur, (el) => (el.value = ""));
    await p.type(selecteur, valeur);
  };
  const soumettre = async (suivante) => {
    await p.click("form button[type=submit]");
    const atteinte = await attendreUrl(suivante);
    if (!atteinte) console.error(`Samba : attendu ${suivante}, page ${p.url()} :\n${(await p.evaluate(() => document.body.innerText)).slice(0, 1200)}`);
    verifier(atteinte, `Samba : passage à ${suivante}`);
    await p.waitForNetworkIdle({ idleTime: 200 });
    return atteinte;
  };
  const texte = () => p.evaluate(() => document.body.innerText);
  const carte = (titre) =>
    p.$$eval("section", (sections, t) => sections.map((s) => s.innerText).find((x) => x.startsWith(t)) ?? "", titre);

  verifier(p.url().endsWith("/install"), "Samba : jeton accepté, arrivée sur l'assistant");
  await p.waitForSelector('[hx-post="/install/commencer"]', { timeout: 30000 });
  await p.waitForNetworkIdle({ idleTime: 300 });
  await p.click('[hx-post="/install/commencer"]');
  if (!(await attendreUrl("/install/organisation"))) return verifier(false, "Samba : passage à l'organisation");
  await p.waitForNetworkIdle({ idleTime: 200 });
  await remplir("#nom", "Mairie d'Exemple");
  await remplir("#domaine", "exemple.fr");
  if (!(await soumettre("/install/cibles"))) return false;

  // E-003 : « Plusieurs serveurs », srv1 puis le serveur dédié au contrôleur.
  await p.click('label:has(input[name="topologie"][value="multi"])');
  const ouvrirModale = async () => {
    await p.click('summary[aria-haspopup="menu"]');
    await p.click('[data-dialogue="dialogue-hote"]');
    await p.waitForSelector("#dialogue-hote[open] #form-hote", { visible: true, timeout: 5000 });
    await p.waitForNetworkIdle({ idleTime: 200 });
  };
  const saisirHote = async (nom, roles) => {
    await remplir("#form-hote #nom", nom);
    await remplir("#form-hote #adresse", "ok.exemple.fr");
    await remplir("#form-hote #port", "22");
    await remplir("#form-hote #utilisateur", "root");
    for (const role of roles) await p.click(`#form-hote input[name="roles_${role}"]`);
  };
  const ajoute = (nom) =>
    p
      .waitForFunction((n) => document.querySelector("#hotes")?.innerText.includes(n) && !document.querySelector("#dialogue-hote").open, { timeout: 10000 }, nom)
      .then(() => true, () => false);
  await ouvrirModale();
  verifier(Boolean(await p.$('#form-hote input[name="roles_samba"]')), "E-003 : case « Contrôleur Samba (postes Windows) »");
  await saisirHote("srv1.exemple.fr", ["annuaire", "outils", "supervision"]);
  await p.click("#form-hote button[type=submit]");
  verifier(await ajoute("srv1.exemple.fr"), "E-003 : srv1.exemple.fr ajouté");
  await p.waitForNetworkIdle({ idleTime: 300 });
  await ouvrirModale();
  await saisirHote("dc.exemple.fr", ["annuaire", "samba"]);
  await p.click("#form-hote button[type=submit]");
  verifier(
    await p
      .waitForFunction(() => document.querySelector("#erreur-roles")?.textContent.includes("serveur de l'annuaire"), { timeout: 5000 })
      .then(() => true, () => false),
    "E-003 : le contrôleur Samba refusé sur le serveur de l'annuaire (422)",
  );
  await p.waitForNetworkIdle({ idleTime: 200 });
  await controlerA11y("E-003 modale, contrôleur Samba refusé avec l'annuaire");
  await p.click('#form-hote input[name="roles_annuaire"]');
  await p.click("#form-hote button[type=submit]");
  verifier(await ajoute("dc.exemple.fr"), "E-003 : dc.exemple.fr ajouté, contrôleur Samba seul");
  await p.waitForNetworkIdle({ idleTime: 300 });
  await p.click('[hx-post="/install/cibles/hotes/test"]');
  verifier(
    await p
      .waitForFunction(() => (document.querySelector("#hotes")?.innerText.match(/connexion vérifiée/g) ?? []).length === 2, { timeout: 15000 })
      .then(() => true, () => false),
    "E-003 : les deux serveurs testés (faux ssh)",
  );
  await p.waitForNetworkIdle({ idleTime: 200 });
  if (!(await soumettre("/install/composants"))) return false;

  // E-004 : Postes Windows disponibles.
  verifier(!(await p.$eval("#composant-samba", (el) => el.disabled)), "E-004 : « Postes Windows (Samba AD) » disponible");
  await p.click("label:has(#composant-samba)");
  await p.waitForNetworkIdle({ idleTime: 300 });
  verifier(await p.$eval("#composant-samba", (el) => el.checked), "E-004 : les postes Windows se cochent");
  await controlerA11y("E-004 avec les postes Windows");
  if (!(await soumettre("/install/securite"))) return false;

  // E-005 : nom du contrôleur proposé, puis vérifié.
  verifier((await p.$eval("#samba", (el) => el.value)) === "dc.ad.exemple.fr", "E-005 : contrôleur proposé dc.ad.exemple.fr");
  await remplir("#samba", "dc.exemple.fr");
  await p.click("form button[type=submit]");
  verifier(
    await p
      .waitForFunction(() => document.querySelector("#erreur-samba")?.textContent.includes("domaine de l'organisation"), { timeout: 5000 })
      .then(() => true, () => false),
    "E-005 : dc.exemple.fr refusé (domaine Active Directory = domaine de l'organisation)",
  );
  await p.waitForNetworkIdle({ idleTime: 200 });
  await controlerA11y("E-005 nom du contrôleur refusé");
  await remplir("#samba", "dc.ad.exemple.fr");
  if (!(await soumettre("/install/messagerie"))) return false;
  await remplir("#hote", "127.0.0.1");
  await remplir("#port", "1025");
  await p.click('input[name="chiffrement"][value="aucun"]');
  await remplir("#destinataire", "jean.dupont@exemple.fr");
  if (!(await soumettre("/install/comptes"))) return false;
  await p.click('[hx-post="/install/comptes/generer"]');
  await p.waitForSelector('input[name="urgence_genere"]');
  await remplir("#prenom", "Jean");
  await remplir("#nom", "Dupont");
  await remplir("#uid", "jean.dupont");
  await remplir("#email", "jean.dupont@exemple.fr");
  await remplir("#mot_de_passe", "Cheval-Batterie-42");
  await remplir("#confirmation", "Cheval-Batterie-42");
  if (!(await soumettre("/install/telemetrie"))) return false;
  if (!(await soumettre("/install/recapitulatif"))) return false;

  // E-010 : carte, plan, avertissement de D-068.
  await controlerA11y("E-010 avec les postes Windows");
  const c = await carte("Postes Windows (Samba AD)");
  verifier(
    ["dc.ad.exemple.fr", "dc.exemple.fr", "AD.EXEMPLE.FR", "EXEMPLE", "OU=Agents"].every((m) => c.includes(m)),
    `E-010 : carte « Postes Windows (Samba AD) » (${c.replace(/\n/g, " ").slice(0, 240)})`,
  );
  const lignes = await p.$$eval("#titre-plan + div tbody tr", (trs) => trs.map((tr) => tr.innerText));
  const ligne = (serveur) => lignes.find((l) => l.startsWith(serveur)) ?? "";
  verifier(
    ligne("dc.exemple.fr").includes("Contrôleur Samba") && !ligne("dc.exemple.fr").includes("Annuaire OpenLDAP") && !ligne("srv1.exemple.fr").includes("Contrôleur Samba"),
    `E-010 : « Contrôleur Samba » sur dc.exemple.fr seul (${lignes.join(" | ").slice(0, 300)})`,
  );
  verifier((await texte()).includes("par ldappasswd"), "E-010 : avertissement D-068 (ldappasswd non propagé)");
  await p.click('[hx-post="/install/verification"]');
  verifier(
    await p.waitForSelector("#lancer:not([disabled])", { timeout: 30000 }).then(() => true, () => false),
    "Samba : vérification préalable réussie",
  );
  await p.click("#lancer");
  await p.click('#confirm-lancer [hx-post="/install/execution"]');
  verifier(await attendreUrl("/install/execution"), "Samba : lancement de l'installation");
  const fin = await p.waitForSelector("#fin [role=status], #fin [role=alert]", { timeout: 30000 }).catch(() => null);
  const reussie = fin && (await fin.evaluate((el) => el.getAttribute("role") === "status"));
  verifier(reussie, "Samba : installation terminée avec succès (flux SSE)");
  if (!reussie) return false;
  verifier(await attendreUrl("/install/resultat"), "Samba : passage automatique au résultat");
  await p.waitForNetworkIdle({ idleTime: 200 });
  await controlerA11y("E-012 avec le contrôleur Samba");
  const section = await p.$eval("#controleur-samba", (el) => el.innerText).catch(() => "");
  verifier(
    ["dc.ad.exemple.fr", "ad.exemple.fr", "EXEMPLE\\Administrator", "fichier récapitulatif"].every((m) => section.includes(m)),
    `E-012 : section « Contrôleur Samba » (${section.replace(/\n/g, " ").slice(0, 240)})`,
  );
  await p.click('input[name="conserve"]');
  await p.click('form[hx-post="/install/terminer"] button[type=submit]');
  verifier(await attendreUrl("/verrouille"), "Samba : assistant verrouillé après « Terminer »");
  return true;
}