#!/usr/bin/python3

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import re
import time

import parent

import packagelib
from testlib import *


WAIT_KRB_SCRIPT = """
set -ex
# HACK: This needs to work, but may take a minute
for x in $(seq 1 60); do
    if getent passwd {0}; then
        break
    fi
    if systemctl --quiet is-failed sssd.service; then
        systemctl status --lines=100 sssd.service >&2
        exit 1
    fi
    sss_cache -E || true
    systemctl restart sssd.service
    sleep $x
done
# ensure this works now, if the above loop timed out
getent passwd {0}

# This directory should be owned by the domain user
chown -R {0} /home/admin

# HACK: This needs to work but may take a minute
for x in $(seq 1 60); do
    if ssh -oStrictHostKeyChecking=no -oBatchMode=yes -l {0} x0.cockpit.lan true; then
        break
    fi
    sss_cache -E || true
    systemctl restart sssd.service
    sleep $x
done
"""


@skipImage("No realmd available", "fedora-atomic")
@skipImage("freeipa not currently in testing", "debian-testing")
class TestRealms(MachineCase):
    provision = {
        "0": {"address": "10.111.113.1/20", "dns": "10.111.112.100"},
        "services": {"image": "services", "memory_mb": 2048}
    }

    def setUp(self):
        super().setUp()
        self.op_address = "#realms-op-address"
        self.op_admin = "#realms-op-admin"
        self.op_admin_password = "#realms-op-admin-password"
        self.machines['services'].execute("/run-freeipa")

    def testIpa(self):
        m = self.machine
        b = self.browser

        # Tell realmd to enable domain-qualified logins; unqualified ones are covered in testIpaUnqualifiedUsers
        m.execute("printf '[cockpit.lan]\\nfully-qualified-names = yes\\n'  >> /etc/realmd.conf")

        m.execute("hostnamectl set-hostname x0.cockpit.lan")
        self.login_and_go("/system")

        # Wait for DNS to work as expected.
        # https://bugzilla.redhat.com/show_bug.cgi?id=1071356#c11
        #
        wait(lambda: m.execute("nslookup -type=SRV _ldap._tcp.cockpit.lan"))

        def wait_number_domains(n):
            if n == 0:
                b.wait_text("#system-info-domain a", "Join Domain")
            else:
                b.wait_text_not("#system-info-domain a", "Join Domain")
            b.wait_not_attr("#system-info-domain a", "disabled", "disabled")

        wait_number_domains(0)

        # Join cockpit.lan
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        with b.wait_timeout(180):
            b.set_val(self.op_address, "cockpit.lan")
            b.wait_text(".realms-op-address-error", "Contacted domain")
            b.wait_attr(self.op_admin, "placeholder", 'e.g. "admin"')
            b.set_val(self.op_admin, "admin")
            b.set_val(self.op_admin_password, "foobarfoo")
            b.wait_not_visible(".realms-op-leave-only-row")
            b.click(".realms-op-apply")
            b.wait_popdown("realms-op")

            # Check that this has worked
            wait_number_domains(1)

        # when joined to a domain, changing the hostname is fatal, so should be disabled
        b.wait_present("#system_information_hostname_button[disabled]")
        b.mouse("#system_information_hostname_button", "mouseover")
        b.wait_in_text(".tooltip-inner", "Host name should not be changed in a domain")
        b.mouse("#system_information_hostname_button", "mouseout")
        b.wait_not_present(".tooltip-inner")

        # should not have any leftover tickets from the joining
        m.execute("! klist")
        m.execute("! su -c klist admin")
        b.logout()

        # validate Kerberos setup for ws
        m.execute("echo foobarfoo | kinit -f admin@COCKPIT.LAN")
        m.execute(script=WAIT_KRB_SCRIPT.format("admin@cockpit.lan"), timeout=300)

        # should have added SPN to ws keytab
        output = m.execute(['klist', '-k', '/etc/cockpit/krb5.keytab'])
        self.assertIn('HTTP/x0.cockpit.lan@COCKPIT.LAN', output)

        # kerberos login should work
        output = m.execute(['curl', '-s', '--negotiate', '--delegation', 'always', '-u', ':', "-D", "-",
                            'http://x0.cockpit.lan:9090/cockpit/login'])
        self.assertIn("HTTP/1.1 200 OK", output)
        self.assertIn('"csrf-token"', output)

        # Restart cockpit with SSL enabled, this should have gotten an SSL cert from FreeIPA
        m.stop_cockpit()
        m.start_cockpit(tls=True)
        # OpenSSL and curl should use the system PKI which should trust the IPA server CA
        out = m.execute("openssl s_client -verify 5 -verify_return_error -connect localhost:9090")
        self.assertRegex(out, "subject=/?O *= *COCKPIT.LAN.*CN *= *x0.cockpit.lan", out)
        self.assertRegex(out, "issuer=/?O *= *COCKPIT.LAN.*CN *= *Certificate Authority")
        self.assertIn("Content-Type: text/html", m.execute("curl --head https://x0.cockpit.lan:9090"))
        # don't leave the secret key copy behind
        m.execute("! test -e /run/cockpit/ipa.key")
        # remotectl agrees
        self.assertIn("/etc/cockpit/ws-certs.d/10-ipa.cert", m.execute("remotectl certificate"))
        # cert is being tracked
        self.assertIn("MONITORING", m.execute("ipa-getcert list"))
        # Restart without SSL (IPA certificate is not on the testing host)
        m.stop_cockpit()
        m.start_cockpit()

        # wait until IPA user works
        m.execute('while ! su - -c "echo foobarfoo | sudo -S true" admin@cockpit.lan; do sleep 5; sss_cache -E || true; systemctl try-restart sssd; done',
                  timeout=300)

        # log in as IPA admin and check that we can do privileged operations
        orig_password = b.password
        b.password = 'foobarfoo'

        self.login_and_go('/system/services#/systemd-tmpfiles-clean.timer', user='admin@cockpit.lan')
        if m.image in ["rhel-8-1-distropkg"]: # Changed in #12265
            b.wait_in_text('#service-unit', "active")
            b.click('#service-unit-primary-action button[data-action="StopUnit"]')
            b.wait_in_text('#service-unit', "inactive")
        else:
            b.wait_in_text("#statuses", "Running")
            b.click(".service-top-panel .dropdown-kebab-pf button")
            b.click(".service-top-panel .dropdown-menu a:contains('Stop')")
            b.wait_in_text("#statuses", "Not running")
        b.logout()
        b.password = orig_password
        m.execute("chown -R admin /home/admin")

        self.allow_authorize_journal_messages()

        # check respecting FreeIPA's/sssd's ssh known host keys; this requires the new
        # --privkey option of sss_ssh_knownhostproxy, thus only works on recent images
        b.login_and_go("/dashboard")
        b.click('#dashboard-add')
        b.wait_popup('dashboard_setup_server_dialog')
        b.set_val('#add-machine-address', 'x0.cockpit.lan')
        b.wait_text('#dashboard_setup_server_dialog .btn-primary', "Add")
        b.click('#dashboard_setup_server_dialog .btn-primary')

        # with --pubkey support this should Just Work, otherwise confirm fingerprint
        if '--pubkey' not in m.execute("sss_ssh_knownhostsproxy --help || true"):
            b.wait_in_text('#dashboard_setup_server_dialog', "Fingerprint")
            b.click('#dashboard_setup_server_dialog .btn-primary')
        b.wait_popdown('dashboard_setup_server_dialog')
        b.wait_present("#dashboard-hosts a[data-address='x0.cockpit.lan']")
        b.logout()

        # Test domain info (PR #11096), leave the domain
        b.login_and_go("/system")
        b.wait_in_text("#system-info-domain a", "cockpit.lan")
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.wait_text("#realms-op-info-domain", "cockpit.lan")
        b.wait_text("#realms-op-info-login-format", "username@cockpit.lan")
        b.wait_text("#realms-op-info-server-sw", "ipa")
        b.wait_text("#realms-op-info-client-sw", "sssd")
        # leave button should be hidden behind expander by default
        b.wait_not_visible("button.realms-op-leave")
        b.wait_not_visible("#realms-op-alert")
        b.click("#realms-op-leave-toggle")
        b.wait_visible("#realms-op-alert")
        b.wait_visible("button.realms-op-leave")
        b.click(".realms-op-leave")

        b.wait_popdown("realms-op")
        wait_number_domains(0)
        # re-enables hostname changing
        b.wait_present("#system_information_hostname_button:not([disabled])")

        # should have cleaned up ws keytab
        m.execute("! klist -k /etc/cockpit/krb5.keytab | grep COCKPIT.LAN")
        # should have cleaned up certificates
        m.execute("! test -e /etc/cockpit/ws-certs.d/10-ipa.cert")
        # should have stopped cert tracking
        self.assertNotIn("status:", m.execute("ipa-getcert list"))

        # Send a wrong password
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.set_val(self.op_address, "cockpit.lan")
        b.wait_attr(self.op_admin, "placeholder", 'e.g. "admin"')
        b.set_val(self.op_admin, "admin")
        b.set_val(self.op_admin_password, "foo")
        b.click(".realms-op-apply")
        b.wait_text_not(".realms-op-message", "")
        b.wait_not_visible(".realms-op-leave-only-row")
        b.click(".realms-op-cancel")
        b.wait_popdown("realms-op")

        # Try to join a non-existing domain
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.set_val(self.op_address, "NOPE")
        b.wait_text(".realms-op-address-error", "Domain NOPE could not be contacted")
        b.wait_not_visible(".realms-op-address-spinner")
        b.click(".realms-op-cancel")
        b.wait_popdown("realms-op")

        # Cancel a join
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.set_val(self.op_address, "cockpit.lan")
        b.wait_attr(self.op_admin, "placeholder", 'e.g. "admin"')
        b.set_val(self.op_admin, "admin")
        b.set_val(self.op_admin_password, "foobarfoo")
        b.click(".realms-op-apply")
        b.wait_visible(".realms-op-spinner")
        b.click(".realms-op-cancel")
        b.wait_popdown("realms-op")

        self.allow_restart_journal_messages()
        # sometimes polling for info and joining a domain creates this noise
        self.allow_journal_messages('.*org.freedesktop.DBus.Error.Spawn.ChildExited.*')

    def testIpaUnqualifiedUsers(self):
        m = self.machine
        b = self.browser

        m.execute("hostnamectl set-hostname x0.cockpit.lan")

        # Wait for DNS to work as expected.
        # https://bugzilla.redhat.com/show_bug.cgi?id=1071356#c11
        wait(lambda: m.execute("nslookup -type=SRV _ldap._tcp.cockpit.lan"))

        # delete the local admin user, going to use the IPA one instead
        m.execute("userdel admin; systemctl try-restart sssd")

        # Tell realmd to not enable domain-qualified logins
        # (https://bugzilla.redhat.com/show_bug.cgi?id=1575538)
        m.execute("printf '[cockpit.lan]\\nfully-qualified-names = no\\n'  >> /etc/realmd.conf")
        m.execute("echo foobarfoo | realm join -vU admin cockpit.lan")

        # wait until IPA user works
        m.execute('while ! su - -c "echo foobarfoo | sudo -S true" admin; do sleep 5; sss_cache -E || true; systemctl try-restart sssd; done',
                  timeout=300)

        # login should now work with the IPA admin user
        b.password = 'foobarfoo'
        self.login_and_go("/system")
        b.wait_in_text("#system-info-domain a", "cockpit.lan")

        # Show domain information
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.wait_text("#realms-op-info-domain", "cockpit.lan")
        b.wait_text("#realms-op-info-login-format", "username")  # no @domain
        b.wait_text("#realms-op-info-server-sw", "ipa")
        b.wait_text("#realms-op-info-client-sw", "sssd")
        b.click(".realms-op-cancel")
        b.wait_popdown("realms-op")

        # should be able to run admin operations
        b.go('/system/services#/systemd-tmpfiles-clean.timer')
        b.enter_page('/system/services')

        if m.image in ["rhel-8-1-distropkg"]: # Changed in #12265
            b.wait_in_text('#service-unit', "active")
            b.click('#service-unit-primary-action button[data-action="StopUnit"]')
            b.wait_in_text('#service-unit', "inactive")
        else:
            b.wait_in_text("#statuses", "Running")
            b.click(".service-top-panel .dropdown-kebab-pf button")
            b.click(".service-top-panel .dropdown-menu a:contains('Stop')")
            b.wait_in_text("#statuses", "Not running")

        b.go('/system')
        b.enter_page('/system')
        # shutdown button should be enabled and working
        # it takes a while for the permission check to finish, it is always enabled at first
        b.click("#shutdown-group button[data-action=restart][data-stable=yes]")
        b.wait_popup("shutdown-dialog")
        b.wait_in_text("#shutdown-dialog .btn-danger", 'Restart')
        b.click("#shutdown-dialog .dropdown button")
        b.click("a:contains('No Delay')")
        b.click("#shutdown-dialog .btn-danger")
        b.switch_to_top()
        b.wait_in_text(".curtains-ct h1", "Disconnected")
        m.wait_reboot()

        self.allow_authorize_journal_messages()
        self.allow_restart_journal_messages()
        # sometimes polling for info and joining a domain creates this noise
        self.allow_journal_messages('.*org.freedesktop.DBus.Error.Spawn.ChildExited.*')

    def testNotSupported(self):
        m = self.machine
        b = self.browser

        # Disable sssd support in realmd

        m.execute("echo -e '[providers]\nsssd = no\n' >> /usr/lib/realmd/realmd-distro.conf")

        m.execute("hostnamectl set-hostname x0.cockpit.lan")
        self.login_and_go("/system")

        # Wait for DNS to work as expected.
        # https://bugzilla.redhat.com/show_bug.cgi?id=1071356#c11
        #
        wait(lambda: m.execute("nslookup -type=SRV _ldap._tcp.cockpit.lan"))

        # Join cockpit.lan
        b.click("#system-info-domain a")
        b.wait_popup("realms-op")
        b.set_val(self.op_address, "cockpit.lan")
        b.wait_in_text(".realms-op-address-error", "Domain cockpit.lan is not supported")
        b.set_val(self.op_admin, "admin")
        b.set_val(self.op_admin_password, "foobarfoo")
        b.click(".realms-op-apply")
        b.wait_text(".realms-op-message", "Joining this domain is not supported")

        self.allow_journal_messages(".*couldn't introspect /org/freedesktop/realmd.*")


JOIN_SCRIPT = """
set -ex
# Wait until zones from LDAP get loaded
for x in $(seq 1 20); do
    if nslookup -type=SRV _ldap._tcp.cockpit.lan; then
        break
    else
        sleep $x
    fi
done

if ! echo '%(password)s' | realm join -vU admin cockpit.lan; then
    if systemctl --quiet is-failed sssd.service; then
        systemctl status --lines=100 sssd.service >&2
    fi
    journalctl -u realmd.service
    exit 1
fi

# On certain OS's it takes time for sssd to come up properly
#   [8347] 1528294262.886088: Sending initial UDP request to dgram 172.27.0.15:88
#   kinit: Cannot contact any KDC for realm 'COCKPIT.LAN' while getting initial credentials
for x in $(seq 1 20); do
    if echo '%(password)s' | KRB5_TRACE=/dev/stderr kinit -f admin@COCKPIT.LAN; then
        break
    else
        sleep $x
    fi
done

# C.UTF-8 has existed for a long time, but still not on RHEL 7
if locale -a | grep -iq ^'C\.utf'; then
   loc=C.UTF-8
else
   loc=en_US.UTF-8
fi

# create SPN and keytab for ws
if type ipa >/dev/null 2>&1; then
    LC_ALL=$loc ipa service-add --ok-as-delegate=true --force HTTP/x0.cockpit.lan@COCKPIT.LAN
else
    curl --insecure -s --negotiate -u : https://services.cockpit.lan/ipa/json --header 'Referer: https://services.cockpit.lan/ipa' --header "Content-Type: application/json" --header "Accept: application/json" --data '{"params": [["HTTP/x0.cockpit.lan@COCKPIT.LAN"], {"raw": false, "all": false, "version": "2.101", "force": true, "no_members": false, "ipakrbokasdelegate": true}], "method": "service_add", "id": 0}'
fi
ipa-getkeytab -p HTTP/x0.cockpit.lan -k %(keytab)s

# HACK: due to sudo's "last rule wins", our /etc/sudoers rule becomes trumped by sssd's, so swap the order
sed -i '/^sudoers:/ s/files sss/sss files/' /etc/nsswitch.conf
"""

# This is here because our test framework can't run ipa VM's twice


@skipImage("No realmd available", "fedora-atomic")
@skipImage("freeipa not currently in testing", "debian-testing")
class TestKerberos(MachineCase):
    provision = {
        "0": {"address": "10.111.113.1/20", "dns": "10.111.112.100"},
        "services": {"image": "services", "memory_mb": 2048}
    }

    def configure_kerberos(self, keytab):
        self.machines["services"].execute("/run-freeipa")

        # Setup a place for kerberos caches
        args = {"addr": "10.111.112.100", "password": "foobarfoo", "keytab": keytab}
        self.machine.execute("hostnamectl set-hostname x0.cockpit.lan")
        if "ubuntu" in self.machine.image:
            # no nss-myhostname there
            self.machine.execute("echo '10.111.113.1 x0.cockpit.lan' >> /etc/hosts")
        self.machine.execute(script=JOIN_SCRIPT % args, timeout=1800)
        self.machine.execute(script=WAIT_KRB_SCRIPT.format("admin"), timeout=300)

    @skipBrowser("Firefox cannot work with cookies", "firefox")
    def testNegotiate(self):
        self.allow_authorize_journal_messages()
        self.allow_hostkey_messages()
        b = self.browser
        m = self.machine

        # Tell realmd to not enable domain-qualified logins
        # (https://bugzilla.redhat.com/show_bug.cgi?id=1575538)
        m.execute("printf '[cockpit.lan]\\nfully-qualified-names = no\\n'  >> /etc/realmd.conf")

        # delete the local admin user, going to use the IPA one instead
        m.execute("userdel admin")

        # HACK: There is no operating system where the domain admins can do passwordless sudo
        # while having a kerberos ticket, so we can't start a root bridge.
        # This is something that needs to be worked on at an OS level. We use admin level
        # features below, such as adding a machine to the dashboard
        m.execute("echo 'admin        ALL=(ALL)       NOPASSWD: ALL' >> /etc/sudoers")

        # Make sure negotiate auth is not offered first
        m.start_cockpit()

        output = m.execute(['/usr/bin/curl -v -s',
                            '--resolve', 'x0.cockpit.lan:9090:10.111.113.1',
                            'http://x0.cockpit.lan:9090/cockpit/login', '2>&1'])
        self.assertIn("HTTP/1.1 401", output)
        self.assertNotIn("WWW-Authenticate: Negotiate", output)

        self.configure_kerberos("/etc/cockpit/krb5.keytab")
        m.restart_cockpit()

        output = m.execute(['/usr/bin/curl', '-s', '--negotiate', '--delegation', 'always', '-u', ':', "-D", "-",
                            '--resolve', 'x0.cockpit.lan:9090:10.111.113.1',
                            'http://x0.cockpit.lan:9090/cockpit/login'])
        self.assertIn("HTTP/1.1 200 OK", output)
        self.assertIn('"csrf-token"', output)

        cookie = re.search("Set-Cookie: cockpit=([^ ;]+)", output).group(1)
        b.open("/system/terminal", cookie={"name": "cockpit", "value": cookie, "domain": m.web_address, "path": "/"})
        b.wait_visible('#content')
        b.enter_page("/system/terminal")
        b.focus(".terminal")

        def line_sel(i):
            return '.terminal .xterm-accessibility-tree div:nth-child(%d)' % i

        def wait_prompt(b, line):
            b.wait_js_func("(function (sel) { return ph_text(sel).trim() != 'Blank line'})", line_sel(line))

        wait_prompt(b, 1)

        # run kinit and see if got forwarded the kerberos ticket into the session
        b.key_press("klist\r")
        wait_prompt(b, 2)
        text = b.text(line_sel(2)).strip()
        self.assertIn("Ticket cache", text)

        # Now connect to another machine
        self.assertNotIn("admin", m.execute("ps -xa | grep sshd"))
        b.switch_to_top()
        b.go("/@x0.cockpit.lan/system/terminal")
        b.click("#machine-troubleshoot")
        b.wait_popup('troubleshoot-dialog')

        b.set_val("#add-machine-address", "bad-user@x0.cockpit.lan")
        b.wait_text('#troubleshoot-dialog .btn-primary', "Add")
        b.click('#troubleshoot-dialog .btn-primary')
        # with --pubkey support the fingerprint is already known, otherwise confirm fingerprint
        if '--pubkey' not in m.execute("sss_ssh_knownhostsproxy --help || true"):
            b.wait_in_text('#troubleshoot-dialog', "Fingerprint")
            b.click('#troubleshoot-dialog .btn-primary')
        b.wait_in_text('#troubleshoot-dialog h4', "Log in to")
        b.click("#login-type button")
        b.click("#login-type li[value=stored] a")
        b.wait_in_text("#login-type button span", "Using available credentials")
        b.wait_not_visible("#login-diff-password")
        b.wait_not_in_text("#login-available", "Login Password")
        b.wait_in_text("#login-available", "Kerberos Ticket")
        b.wait_not_present("#login-available .empty")
        b.set_val("#login-custom-user", "")
        b.click('#troubleshoot-dialog .btn-primary')

        b.wait_popdown('troubleshoot-dialog')
        b.enter_page("/system/terminal", host="x0.cockpit.lan")
        b.wait_visible(".terminal")

        # Make sure we connected via SSH
        self.assertIn("admin", m.execute("ps -xa | grep sshd"))
        b.kill()

        # Remove cockpit keytab
        m.execute("mv /etc/cockpit/krb5.keytab /etc/cockpit/bk.keytab")
        output = m.execute(['/usr/bin/curl', '-s', '--negotiate', '--delegation', 'always', '-u', ':', "-D", "-",
                            '--resolve', 'x0.cockpit.lan:9090:10.111.113.1',
                            'http://x0.cockpit.lan:9090/cockpit/login'])
        self.assertIn("HTTP/1.1 401", output)

        # Pull http into default keytab
        m.execute('printf "rkt /etc/cockpit/bk.keytab\nwkt /etc/krb5.keytab\nq" | ktutil')
        output = m.execute(['/usr/bin/curl', '-s', '--negotiate', '--delegation', 'always', '-u', ':', "-D", "-",
                            '--resolve', 'x0.cockpit.lan:9090:10.111.113.1',
                            'http://x0.cockpit.lan:9090/cockpit/login'])
        self.assertIn("HTTP/1.1 200 OK", output)
        self.assertIn('"csrf-token"', output)
        self.allow_restart_journal_messages()


@skipImage("Package (un)install does not work on Atomic", "fedora-atomic")
class TestPackageInstall(packagelib.PackageCase):

    def testInstall(self):
        m = self.machine
        b = self.browser

        m.execute("dpkg --purge realmd 2>/dev/null || rpm --erase realmd || yum remove -y realmd")

        # case 1: disable PackageKit
        m.execute("systemctl mask packagekit && systemctl stop packagekit.service || true")
        self.login_and_go("/system")
        b.wait_text("#system-info-domain a", "Join Domain")
        b.wait_present("#system-info-domain a.disabled")
        # check tooltip
        b.wait_present("#system-info-domain [data-original-title]")
        b.mouse("#system-info-domain span", "mouseover")
        b.wait_in_text(".tooltip-inner", "realmd is not available on this system")
        b.mouse("#system-info-domain span", "mouseout")
        b.logout()

        # case 2: enable PackageKit, but no realmd package available
        m.execute("systemctl unmask packagekit")
        self.login_and_go("/system")
        # Joining a domain should bring up the install dialog
        b.wait_text("#system-info-domain a", "Join Domain")
        # check tooltip
        b.wait_present("#system-info-domain [data-original-title]")
        b.mouse("#system-info-domain span", "mouseover")
        b.wait_in_text(".tooltip-inner", "requires installation of realmd")
        b.mouse("#system-info-domain span", "mouseout")

        b.click("#system-info-domain a")
        if m.image == "rhel-8-1-distropkg":
            b.wait_in_text("#cockpit_modal_dialog .dialog-error", "realmd is not available")
        else:
            b.wait_in_text("#cockpit_modal_dialog .pf-c-alert__title", "realmd is not available")
        b.wait_present("#cockpit_modal_dialog .modal-footer button.btn-primary:disabled")
        b.click("#cockpit_modal_dialog .modal-footer button.cancel")
        b.wait_not_present("#cockpit_modal_dialog")
        b.logout()

        # case 3: provide an available realmd package
        self.createPackage("realmd", "1", "1", content={"/realmd-stub": ""})
        self.enableRepo()
        m.execute("pkcon refresh")

        self.login_and_go("/system")

        # Joining a domain should bring up the install dialog
        b.wait_text("#system-info-domain a", "Join Domain")
        # check tooltip
        b.wait_present("#system-info-domain [data-original-title]")
        b.mouse("#system-info-domain span", "mouseover")
        b.wait_in_text(".tooltip-inner", "requires installation of realmd")
        b.mouse("#system-info-domain span", "mouseout")

        b.click("#system-info-domain a")
        b.click("#cockpit_modal_dialog .modal-footer button.btn-primary")
        b.wait_not_present("#cockpit_modal_dialog")

        # the stub package doesn't provide a realmd D-Bus service, so the "join
        # domain" dialog won't ever appear; just check that it was installed
        m.execute("test -e /realmd-stub")

    def testDialogTransition(self):
        m = self.machine
        b = self.browser

        # disable the realmd package's service, so that we can restore it, but
        # the package install code path will be triggered
        m.execute("systemctl stop realmd && systemctl mask realmd")

        self.login_and_go("/system")

        # Joining a domain should bring up the install dialog
        b.wait_text("#system-info-domain a", "Join Domain")
        # check tooltip
        b.wait_present("#system-info-domain [data-original-title]")
        b.mouse("#system-info-domain span", "mouseover")
        b.wait_in_text(".tooltip-inner", "requires installation of realmd")
        b.mouse("#system-info-domain span", "mouseout")

        b.click("#system-info-domain a")
        # restore realmd service, to pretend that package install completed
        m.execute("systemctl unmask realmd")
        b.click("#cockpit_modal_dialog .modal-footer button.btn-primary")
        b.wait_not_present("#cockpit_modal_dialog")

        # should continue straight to join dialog
        b.wait_present("#realms-op")
        b.wait_popup("realms-op")

        # no running IPA server for this test, so just cancel
        b.click(".realms-op-cancel")
        b.wait_popdown("realms-op")

        # should not have a tooltip any more
        b.mouse("#system-info-domain span", "mouseover")
        time.sleep(2)
        self.assertFalse(b.is_present(".tooltip-inner"))


if __name__ == '__main__':
    test_main()
