def test_getIcingaUsers_BadArgs3(mock_urlopen, mock_json, mock_sslverify):
    icingaApi = OrderedDict(
        [
            ("cafile", "/etc/passwd"),
            ("username", "Test"),
            ("password", "test"),
            ("url", "https://localhost"),
        ]
    )

    testJson = '{ "test" : "test" }'
    mock_sslverify.retun_value = 0
    mock_urlopen.OpenerDirector.open.return_value = 0
    mock_urlopen.urlopen.return_value = testJson
    mock_json.return_value = dict()

    assert getUsers.getIcingaUsers(icingaApi) == 3
    mock_urlopen.assert_called()
def test_getIcingaUsers_OKArgs1(mock_urlopen, mock_json, mock_sslverify):
    icingaApi = OrderedDict(
        [
            ("cafile", "/etc/passwd"),
            ("username", "Test"),
            ("password", "test"),
            ("url", "https://localhost"),
        ]
    )

    testJson = '{ "test" : "test", "results" : "test" }'
    testDict = {"results": [{"attrs": {"__name": "Jozko"}}, {"attrs": {"__name": "Janko"}}]}
    expectDict = {"jozko": {"__name": "Jozko"}, "janko": {"__name": "Janko"}}
    mock_sslverify.retun_value = 0
    mock_urlopen.OpenerDirector.open.return_value = 0
    mock_urlopen.urlopen.return_value = testJson
    mock_json.return_value = testDict

    assert getUsers.getIcingaUsers(icingaApi) == expectDict
            # use elastic HA for that
            logging.debug("checking master state (elastic HA)")
            masterStatus = elastic.checkMasterStatus(esHost, esPort)
            if masterStatus[socket.gethostname()] != "*":
                logging.debug(
                    "I am not master, so no notifications.. sleeping 90")
                time.sleep(90)
                continue

        # User list management
        # Get list, if not possible, wait, tryagain..
        # Use old one if there is no possibility to get new
        for i in range(0, 3):
            # retry if there is exception during getting users
            try:
                icingaUsers = getUsers.getIcingaUsers(icingaApiObj)
            except Exception:
                logging.debug("Skipping getting users for %s time [exception]",
                              i)
                logging.debug("Debug info:", exc_info=True)
                noUsers = True
                time.sleep(10)
                continue

            if type(icingaUsers) is not dict:
                logging.debug("Skipping getting users for %s time [notadict]",
                              i)
                logging.debug("Debug info: %s", type(icingaUsers))
                noUsers = True
                time.sleep(10)
                continue
def test_getIcingaUsers_OtherArgTypeHandling():
    # we test only negative answers
    with pytest.raises(Exception):
        assert getUsers.getIcingaUsers(None)
def test_getIcingaUsers_BadArgs2():
    # Try to pass some bad dict
    icingaApi = OrderedDict()

    with pytest.raises(Exception):
        assert getUsers.getIcingaUsers(icingaApi) == 2
def test_getIcingaUsers_BadArgs1():
    # Try to pass some bad dict
    icingaApi = {"badKey": "apiUser", "url": "http://somebadthings"}

    with pytest.raises(Exception):
        assert getUsers.getIcingaUsers(icingaApi)