#!/usr/bin/python
# pylint: disable=missing-module-docstring,invalid-name

# pylint: disable-next=unused-import
import settestpath

# lots of useful util methods for building/tearing down
# test enviroments...
import testutils

from rhn.stringutils import ustr
from up2date_client import config

import unittest

test_up2date = "../etc-conf/up2date.config"


# pylint: disable-next=missing-class-docstring
class TestConfig(unittest.TestCase):
    def setUp(self):
        # in this stuff, we get weird stuff existing, so restore
        # a config first, then change anything test specifc
        testutils.restoreConfig()
        self.__setupData()

    # pylint: disable-next=invalid-name
    def __setupData(self):
        pass

    def tearDown(self):
        config.cfg = None
        testutils.restoreConfig()

    def testEmptyInit(self):
        "Verify that the class can be created with no arguments"
        # pylint: disable-next=unused-variable
        cfg = config.initUp2dateConfig(test_up2date)

    def testConfigString(self):
        "Verify that Config loads a string as a string"
        cfg = config.initUp2dateConfig(test_up2date)
        # pylint: disable-next=unidiomatic-typecheck
        assert type(cfg["systemIdPath"]) == type(ustr(""))

    def testConfigList(self):
        "Verify that Config loads a list as a list"
        cfg = config.initUp2dateConfig(test_up2date)
        # pylint: disable-next=unidiomatic-typecheck
        assert type(cfg["disallowConfChanges"]) == type([])

    def testConfigBool(self):
        "Verify that Config loads a bool int as a bool"
        cfg = config.initUp2dateConfig(test_up2date)
        # pylint: disable-next=unidiomatic-typecheck
        assert type(cfg["enableProxy"]) == type(1)

    def testConfigSave(self):
        "Verify that Config saves a file without error"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg.save()

    def testConfigSetItem(self):
        "Verify that Config.__setitem__ works"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg["blippyfoobarbazblargh"] = 1
        assert cfg["blippyfoobarbazblargh"] == 1

    def testConfigInfo(self):
        "Verify that Config.into() runs without error"
        cfg = config.initUp2dateConfig(test_up2date)
        # pylint: disable-next=unused-variable
        blargh = cfg.info("enableProxy")

    def testConfigRuntimeStore(self):
        "Verify that values Config['value'] are set for runtime only and not saved"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg["blippy12345"] = "wantafreehat?"
        cfg.save()
        # cfg is a fairly persistent singleton, blow it awy to get a new referece
        del config.cfg

        cfg2 = config.initUp2dateConfig(test_up2date)
        # if this returns a value, it means we saved the config file...
        # pylint: disable-next=singleton-comparison
        assert cfg2["blippy12345"] == None

    def testConfigRuntimeStoreNoDir(self):
        "Verify that saving a file into a non existent dir works"
        # bugzilla: 125179
        cfg = config.initUp2dateConfig(test_up2date)
        cfg["blippy321"] = "blumblim"
        cfg.save()

    def testConfigKeysReturnsAList(self):
        "Verify that Config.keys() returns a list"
        cfg = config.initUp2dateConfig(test_up2date)
        blip = cfg.keys()
        # pylint: disable-next=unidiomatic-typecheck
        assert type(blip) == type([])

    def testConfigKeys(self):
        "Verify that Config.keys() returns a list with the right stuff"
        cfg = config.initUp2dateConfig(test_up2date)
        blip = cfg.keys()
        assert "enableProxy" in blip

    def testConfigHasKeyDoesntExist(self):
        "Verify that Config.has_key() is correct on non existent keys"
        cfg = config.initUp2dateConfig(test_up2date)
        assert cfg.has_key("234wfj34ruafho34rhkfe") == 0

    def testConfigHasKeyDoesExist(self):
        "Verify that Config.has_key() is correct on existing keys"
        cfg = config.initUp2dateConfig(test_up2date)
        assert cfg.has_key("enableProxy") == 1

    def testConfigHasKeyRuntime(self):
        "Verify that Config.has_key() is correct for runtime keys"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg["runtimekey"] = "blippy"
        assert cfg.has_key("runtimekey") == 1

    def testConfigValues(self):
        "Verify that Config.values() runs without error"
        cfg = config.initUp2dateConfig(test_up2date)
        ret = cfg.values()
        # pylint: disable-next=unidiomatic-typecheck
        assert type(ret) == type([])

    def testConfigItems(self):
        "Verify that Config.items() runs without error"
        cfg = config.initUp2dateConfig(test_up2date)
        ret = cfg.items()
        # pylint: disable-next=unidiomatic-typecheck
        assert type(ret) == type([])

    def testConfigSet(self):
        "Verify that Config.set() sets items into the persistent layer"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg.set("permItem", 1)

        assert cfg.stored["permItem"] == 1

    def testConfigSetOverride(self):
        "Verify that Config.set() sets items in the persitent layer, overriding runtime"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg["semiPermItem"] = 1
        cfg.set("semiPermItem", 0)
        assert cfg.stored["semiPermItem"] == 0

    def testConfigLoad(self):
        "Verify that Config.load() works without exception"
        cfg = config.initUp2dateConfig(test_up2date)
        cfg.load("/etc/sysconfig/rhn/up2date")


# pylint: disable-next=missing-class-docstring
class TestGetProxySetting(unittest.TestCase):
    def setUp(self):
        self.cfg = config.initUp2dateConfig(test_up2date)
        self.proxy1 = "http://proxy.company.com:8080"
        self.proxy2 = "proxy.company.com:8080"

    def testHttpSpecified(self):
        "Verify that http:// gets stripped from proxy settings"
        self.cfg["httpProxy"] = self.proxy1
        res = config.getProxySetting()
        assert res == "proxy.company.com:8080"

    def testHttpUnSpecified(self):
        "Verify that proxies with no http:// work correctly"
        self.cfg["httpProxy"] = self.proxy2
        res = config.getProxySetting()
        assert res == "proxy.company.com:8080"


def suite():
    # pylint: disable-next=redefined-outer-name
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestConfig))
    suite.addTest(unittest.makeSuite(TestGetProxySetting))
    return suite


if __name__ == "__main__":
    unittest.main(defaultTest="suite")
