#!/usr/bin/python3

# checkpoint:
# 1. check default source
# 2. add new source

import composerlib
import testlib


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

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

        # new source info
        source_url = "https://mirrors.fedoraproject.org/mirrorlist?repo=fedora-30&arch=x86_64"
        source_name = "fedora-30-mirrorlist"

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

        # open manage sources dialog
        drop_down_sel = ".toolbar-pf-action-right #dropdownKebab"
        b.click(drop_down_sel)
        b.wait_attr(drop_down_sel, "aria-expanded", "true")
        b.click("a:contains('Manage sources')")
        b.wait_attr(drop_down_sel, "aria-expanded", "false")
        b.wait_visible("#cmpsr-modal-manage-sources")

        # check default loaded sources
        sources = m.execute("composer-cli sources list").split()
        for source in sources:
            b.wait_visible("div[data-source={}]".format(source))

        # add new source
        b.click("input[value='Add source']")

        # can't add source if type is empty
        b.set_input_text("#textInput1-modal-source", source_name)
        b.set_input_text("#textInput2-modal-source", source_url)
        b.wait_attr("button:contains('Add source')", "disabled", "")
        b.click("button:contains('Cancel')")
        # new source
        b.click("input[value='Add source']")
        b.set_input_text("#textInput1-modal-source", source_name)
        b.set_input_text("#textInput2-modal-source", source_url)
        b.set_val("#textInput3-modal-source", "yum-mirrorlist")
        b.wait_val("#textInput3-modal-source", "yum-mirrorlist")
        # groups action done
        b.click("button:contains('Add source')")

        # edit existing source to enable check SSL certificate and GPG key
        b.click("button[aria-label='Edit source {}']".format(source_name))
        # SSL certificate
        b.click("#checkboxInput4-modal-source")
        # GPG key
        b.click("#checkboxInput5-modal-source")
        b.click("button:contains('Update source')")

        # go to manage source
        b.click(drop_down_sel)
        b.wait_attr(drop_down_sel, "aria-expanded", "true")
        b.click("a:contains('Manage sources')")
        b.wait_attr(drop_down_sel, "aria-expanded", "false")
        b.wait_visible("#cmpsr-modal-manage-sources")

        # duplicated source can't be added
        b.click("input[value='Add source']")
        b.set_input_text("#textInput2-modal-source", source_url)
        b.wait_text("#textInput2-modal-source-help", "This source path already exists.")
        b.click("button:contains('Cancel')")

        # delete added source
        delete_drop_down_sel = "button[aria-label='Source actions {}']".format(source_name)
        b.click(delete_drop_down_sel)
        b.wait_attr(delete_drop_down_sel, "aria-expanded", "true")
        b.click("a:contains('Remove source')")
        b.wait_not_present("div[data-source={}]".format(source_name))

        # close manage source dialog
        b.click("#cmpsr-modal-manage-sources button[aria-label='Close']")
        b.wait_not_present("#cmpsr-modal-manage-sources")

        # collect code coverage result
        self.check_coverage()


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