#!/usr/bin/python3

# checkpoint:
# 1. export blueprint package list (copy)
# 2. blueprint description edit/update
# 3. hostname setting
# 4. user setting
import composerlib
import testlib


@testlib.nondestructive
class TestCustom(composerlib.ComposerCase):

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

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        # go to blueprint openssh-server
        b.click("#openssh-server-name")

        # update description (cancel first)
        updated_description = "edit ssh server image description"
        with b.wait_timeout(300):
            b.click("a:contains('ssh server image')")
        b.wait_visible("#cmpsr-modal-edit-description")
        b.set_input_text("#textInput-modal-markup", updated_description)
        b.click("#cmpsr-modal-edit-description button:contains('Cancel')")
        b.wait_not_present("#cmpsr-modal-edit-description")
        b.wait_visible("a:contains('ssh server image')")

        # update description
        actions_drop_down_sel = ".cmpsr-header__actions #dropdownKebab"
        b.click(actions_drop_down_sel)
        b.wait_attr(actions_drop_down_sel, "aria-expanded", "true")
        b.click("a:contains('Edit description')")
        b.wait_visible("#cmpsr-modal-edit-description")
        b.set_input_text("#textInput-modal-markup", updated_description)
        b.click("#cmpsr-modal-edit-description button:contains('Save')")
        b.wait_not_present("#cmpsr-modal-edit-description")
        b.wait_visible("a:contains('{}')".format(updated_description))
        # backend got updated as well
        desc = m.execute("""
            composer-cli blueprints show openssh-server | grep description | \
            awk -F '"' '{print $2}'
            """).rstrip()
        self.assertEqual(desc, updated_description)

        # collect code coverage result
        self.check_coverage()

    def testHostname(self):
        b = self.browser

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        b.click("#openssh-server-name")
        with b.wait_timeout(300):
            b.click("#blueprint-tabs-tab-customizations")
        # set hostname to "openssh-server"
        b.click("button[aria-describedby=Hostname-help2]")
        b.set_input_text("input[aria-label=Hostname]", "hostname-openssh-server")
        b.click("#input-hostname .form-control-pf-save")
        b.wait_visible("span:contains(hostname-openssh-server)")

        # update hostname
        b.click("#input-hostname .form-control-pf-value")
        b.set_input_text("input[aria-label=Hostname]", "update-openssh-server")
        b.click("#input-hostname .form-control-pf-save")
        b.wait_visible("span:contains(update-openssh-server)")

        # clicking x button will not update
        b.click("#input-hostname .form-control-pf-value")
        b.set_input_text("input[aria-label=Hostname]", "no-update-openssh-server")
        b.click("#input-hostname .form-control-pf-cancel")
        b.wait_visible("span:contains(update-openssh-server)")

        # invalid character, save button is disabled
        b.click("#input-hostname .form-control-pf-value")
        b.set_input_text("input[aria-label=Hostname]", "?")
        b.wait_attr("#input-hostname .form-control-pf-save", "disabled", "")

        # collect code coverage result
        self.check_coverage()

    def testDevice(self):
        b = self.browser

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        b.click("#openssh-server-name")
        with b.wait_timeout(300):
            b.click("#blueprint-tabs-tab-customizations")
        # set device to "dev0"
        b.click("#input-device .form-control-pf-value")
        b.set_input_text("input[aria-label='Installation device']", "dev0")
        b.click("#input-device .form-control-pf-save")
        b.wait_visible("span:contains(dev0)")

        # update device to "dev1"
        b.click("#input-device .form-control-pf-value")
        b.set_input_text("input[aria-label='Installation device']", "dev1")
        b.click("#input-device .form-control-pf-save")
        b.wait_visible("span:contains(dev1)")

        # clicking x button will not update
        b.click("#input-device .form-control-pf-value")
        b.set_input_text("input[aria-label='Installation device']", "")
        b.click("#input-device .form-control-pf-cancel")
        b.wait_visible("span:contains(dev1)")

        # collect code coverage result
        self.check_coverage()

    def testUserAccount(self):
        b = self.browser

        self.login_and_go("/composer", superuser=True)
        b.wait_visible("#main")

        b.click("#openssh-server-name")
        with b.wait_timeout(300):
            b.click("#blueprint-tabs-tab-customizations")

        # add a new user
        b.click("button:contains('Create user account')")
        b.wait_visible("#user-account-modal")
        b.set_input_text("#user-account-name", "user name")
        b.set_input_text("#user-account-username", "username")
        b.click("#user-account-role")
        b.set_input_text("#user-account-password", "fooBAR!@#123")
        b.set_input_text("#user-account-password-confirm", "fooBAR!@#123")
        ssh_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUOtNJdBEXyKxBB898rdT54ULjMGuO6v4" \
                  "jLXmRsdRhR5Id/lKNc9hsdioPWUePgYlqML2iSV72vKQoVhkyYkpcsjr3zvBny9+5xej3+T" \
                  "BLoEMAm2hmllKPmxYJDU8jQJ7wJuRrOVOnk0iSNF+FcY/yaQ0owSF02Nphx47j2KWc0IjGG" \
                  "lt4fl0fmHJuZBA2afN/4IYIIsEWZziDewVtaEjWV3InMRLllfdqGMllhFR+ed2hQz9PN2Qc" \
                  "apmEvUR4UCy/mJXrke5htyFyHi8ECfyMMyYeHwbWLFQIve4CWix9qtksvKjcetnxT+WWrut" \
                  "dr3c9cfIj/c0v/Zg/c4zETxtp cockpit-test"
        b.set_input_text("#user-account-ssh-key", ssh_key)
        b.click("#user-account-modal button:contains('Create')")
        b.wait_not_present("#user-account-modal")
        b.wait_text("tr td[data-label='Full name']", "user name")
        b.wait_text("tr td[data-label='User name']", "username")
        b.wait_visible("tr td[data-label='Server administrator'] .fa-check")
        b.wait_visible("tr td[data-label='Password'] .fa-check")
        b.wait_visible("tr td[data-label='SSH key'] .fa-check")

        # update password
        b.click("table:contains('username') #button-edit-user")
        b.wait_visible("#user-account-modal")
        b.set_input_text("#user-account-password", "c456rty$%^RTY")
        b.set_input_text("#user-account-password-confirm", "c456rty$%^RTY")
        b.click("#user-account-modal button:contains('Update')")
        b.wait_not_present("#user-account-modal")
        b.wait_visible("tr td[data-label=Password] .fa-check")

        # remove password
        b.click("table:contains('username') #button-edit-user")
        b.wait_visible("#user-account-modal")
        b.set_input_text("#user-account-password", "")
        b.set_input_text("#user-account-password-confirm", "")
        b.click("#user-account-modal button:contains('Update')")
        b.wait_not_present("#user-account-modal")
        b.wait_visible("table[aria-label='Users List']")
        b.wait_not_present("tr td[data-label='Password'] .fa-check")

        # duplicated user
        b.click("button:contains('Create user account')")
        b.wait_visible("#user-account-modal")
        b.set_input_text("#user-account-username", "username")
        b.wait_in_text("#user-account-username-helper", "This user name already exists.")
        b.click("#user-account-modal button:contains('Cancel')")
        b.wait_not_present("#user-account-modal")

        # password checking error
        b.click("button:contains('Create user account')")
        b.wait_visible("#user-account-modal")
        b.set_input_text("#user-account-password", "aaa")
        b.set_input_text("#user-account-password-confirm", "bbb")
        b.wait_in_text("#user-account-password-confirm-helper",
                       "The values entered for password do not match.")
        b.click("#user-account-modal button:contains('Cancel')")
        b.wait_not_present("#user-account-modal")

        # delete user
        delete_drop_down_sel = "button[aria-label='User account actions username']"
        b.click(delete_drop_down_sel)
        b.wait_attr(delete_drop_down_sel, "aria-expanded", "true")
        b.click("a:contains('Delete user account')")
        b.wait_not_present("table[aria-label='Users List']")

        # collect code coverage result
        self.check_coverage()


if __name__ == '__main__':
    testlib.test_main()
