def test_init(room_config, client_constructor, thread):
    client = Mock()
    client_constructor.return_value = client

    client.login.side_effect = Exception()

    # https://stackoverflow.com/questions/23337471/
    with pytest.raises(Exception) as e:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    assert str(e).endswith(
        "Failed to log into {}, max retries exceeded".format(
            next(iter(chatcommunicate._clients))))

    client.login.side_effect = None
    client.login.reset_mock()
    client_constructor.reset_mock()

    room_config.side_effect = lambda _: room_config.get_original()(
        "test/test_rooms.yml")
    GlobalVars.standby_mode = True
    # See GitHub Issue #2498, temporary workaround
    try:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    except Exception:
        return  # This interferes with the following tests

    assert len(chatcommunicate._rooms) == 0

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner",
                           target=chatcommunicate.pickle_last_messages,
                           daemon=True)
    thread.assert_any_call(name="message sender",
                           target=chatcommunicate.send_messages,
                           daemon=True)

    client.login.reset_mock()
    client_constructor.reset_mock()
    thread.reset_mock()

    GlobalVars.standby_mode = False

    counter = 0

    def throw_every_other(*_):
        nonlocal counter

        counter += 1
        if counter & 1:
            raise Exception()

    client.login.side_effect = throw_every_other
    # See GitHub Issue #2498, temporary workaround
    try:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    except Exception as e:
        return  # Because this causes the following checks to fail

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner",
                           target=chatcommunicate.pickle_last_messages,
                           daemon=True)
    thread.assert_any_call(name="message sender",
                           target=chatcommunicate.send_messages,
                           daemon=True)

    assert len(chatcommunicate._rooms) == 3
    assert chatcommunicate._rooms[("stackexchange.com",
                                   11540)].deletion_watcher is True
    assert chatcommunicate._rooms[("stackexchange.com",
                                   30332)].deletion_watcher is False
    assert chatcommunicate._rooms[("stackoverflow.com",
                                   111347)].deletion_watcher is False
def test_init(room_config, client_constructor, thread):
    client = Mock()
    client_constructor.return_value = client

    client.login.side_effect = Exception()
    threw_exception = False

    try:
        chatcommunicate.init("shoutouts", "to simpleflips")
    except Exception as e:
        assert str(e) == "Failed to log into " + next(iter(chatcommunicate._clients))
        threw_exception = True

    assert threw_exception

    client.login.side_effect = None
    client.login.reset_mock()
    client_constructor.reset_mock()

    room_config.side_effect = lambda _: room_config.get_original()("test/test_rooms.yml")
    GlobalVars.standby_mode = True
    chatcommunicate.init("shoutouts", "to simpleflips")

    assert len(chatcommunicate._rooms) == 0
    assert client.login.call_count == 3

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    client.login.reset_mock()
    client_constructor.reset_mock()
    thread.reset_mock()

    GlobalVars.standby_mode = False

    counter = 0

    def throw_every_other(*_):
        nonlocal counter

        counter += 1
        if counter & 1:
            raise Exception()

    client.login.side_effect = throw_every_other
    chatcommunicate.init("shoutouts", "to simpleflips")

    assert client.login.call_count == 6
    assert counter == 6

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    assert len(chatcommunicate._rooms) == 3
    assert chatcommunicate._rooms[("stackexchange.com", 11540)].deletion_watcher is True
    assert chatcommunicate._rooms[("stackexchange.com", 30332)].deletion_watcher is False
    assert chatcommunicate._rooms[("stackoverflow.com", 111347)].deletion_watcher is False
示例#3
0
                        GlobalVars.location +\
                        ")"
GlobalVars.standby_message = "[ " + GlobalVars.chatmessage_prefix + " ] " \
                             "SmokeDetector started in [standby mode](" + \
                             "https://charcoal-se.org/smokey/SmokeDetector-Statuses#standby-mode) " + \
                             "at [rev " +\
                             GlobalVars.commit_with_author +\
                             "](" + GlobalVars.bot_repository + "/commit/" +\
                             GlobalVars.commit['id'] +\
                             ") (running on " +\
                             GlobalVars.location +\
                             ")"

GlobalVars.standby_mode = "standby" in sys.argv

chatcommunicate.init(username, password)
Tasks.periodic(Metasmoke.send_status_ping, interval=60)
Tasks.periodic(Metasmoke.check_last_pingtime, interval=30)

if GlobalVars.standby_mode:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.standby_message)
    Metasmoke.send_status_ping()

    while GlobalVars.standby_mode:
        time.sleep(3)

    chatcommunicate.join_command_rooms()


# noinspection PyProtectedMember
def check_socket_connections():
示例#4
0
                        GlobalVars.location +\
                        ")"
GlobalVars.standby_message = "[ " + GlobalVars.chatmessage_prefix + " ] " \
                             "SmokeDetector started in [standby mode](" + \
                             "https://charcoal-se.org/smokey/SmokeDetector-Statuses#standby-mode) " + \
                             "at [rev " +\
                             GlobalVars.commit_with_author +\
                             "](" + GlobalVars.bot_repository + "/commit/" +\
                             GlobalVars.commit['id'] +\
                             ") (running on " +\
                             GlobalVars.location +\
                             ")"

GlobalVars.standby_mode = "standby" in sys.argv

chatcommunicate.init(username, password)
Tasks.periodic(Metasmoke.send_status_ping, interval=60)
Tasks.periodic(Metasmoke.check_last_pingtime, interval=30)

if GlobalVars.standby_mode:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.standby_message)
    Metasmoke.send_status_ping()

    while GlobalVars.standby_mode:
        time.sleep(3)

    chatcommunicate.init(username, password)  # to rejoin rooms


# noinspection PyProtectedMember
def check_socket_connections():
示例#5
0
                        GlobalVars.location +\
                        ")"
GlobalVars.standby_message = "[ " + GlobalVars.chatmessage_prefix + " ] " \
                             "SmokeDetector started in [standby mode](" + \
                             "https://charcoal-se.org/smokey/SmokeDetector-Statuses#standby-mode) " + \
                             "at [rev " +\
                             GlobalVars.commit_with_author +\
                             "](" + GlobalVars.bot_repository + "/commit/" +\
                             GlobalVars.commit['id'] +\
                             ") (running on " +\
                             GlobalVars.location +\
                             ")"

GlobalVars.standby_mode = "standby" in sys.argv

chatcommunicate.init(username, password)
Tasks.periodic(Metasmoke.send_status_ping, interval=60)
Tasks.periodic(Metasmoke.check_last_pingtime, interval=30)

if GlobalVars.standby_mode:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.standby_message)
    Metasmoke.send_status_ping()

    while GlobalVars.standby_mode:
        time.sleep(3)

    chatcommunicate.init(username, password)  # to rejoin rooms


# noinspection PyProtectedMember
def check_socket_connections():
def test_init(room_config, client_constructor, thread):
    client = Mock()
    client_constructor.return_value = client

    client.login.side_effect = Exception()
    threw_exception = False

    try:
        chatcommunicate.init("shoutouts", "to simpleflips")
    except Exception as e:
        assert str(e) == "Failed to log into stackexchange.com"
        threw_exception = True

    assert threw_exception

    client.login.side_effect = None
    client.login.reset_mock()
    client_constructor.reset_mock()

    room_config.side_effect = lambda _: room_config.get_original()("test/test_rooms.yml")
    GlobalVars.standby_mode = True
    chatcommunicate.init("shoutouts", "to simpleflips")

    assert len(chatcommunicate._rooms) == 0
    assert client.login.call_count == 3

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    client.login.reset_mock()
    client_constructor.reset_mock()
    thread.reset_mock()

    GlobalVars.standby_mode = False

    counter = 0

    def throw_every_other(*_):
        nonlocal counter

        counter += 1
        if counter & 1:
            raise Exception()

    client.login.side_effect = throw_every_other
    chatcommunicate.init("shoutouts", "to simpleflips")

    assert client.login.call_count == 6
    assert counter == 6

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    assert len(chatcommunicate._rooms) == 3
    assert chatcommunicate._rooms[("stackexchange.com", 11540)].deletion_watcher is True
    assert chatcommunicate._rooms[("stackexchange.com", 30332)].deletion_watcher is False
    assert chatcommunicate._rooms[("stackoverflow.com", 111347)].deletion_watcher is False
def test_init(room_config, client_constructor, thread):
    client = Mock()
    client_constructor.return_value = client

    client.login.side_effect = Exception()

    # https://stackoverflow.com/questions/23337471/
    with pytest.raises(Exception) as e:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    assert str(e).endswith("Failed to log into {}, max retries exceeded".format(next(iter(chatcommunicate._clients))))

    client.login.side_effect = None
    client.login.reset_mock()
    client_constructor.reset_mock()

    room_config.side_effect = lambda _: room_config.get_original()("test/test_rooms.yml")
    GlobalVars.standby_mode = True
    # See GitHub Issue #2498, temporary workaround
    try:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    except Exception:
        return  # This interferes with the following tests

    assert len(chatcommunicate._rooms) == 0

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    client.login.reset_mock()
    client_constructor.reset_mock()
    thread.reset_mock()

    GlobalVars.standby_mode = False

    counter = 0

    def throw_every_other(*_):
        nonlocal counter

        counter += 1
        if counter & 1:
            raise Exception()

    client.login.side_effect = throw_every_other
    # See GitHub Issue #2498, temporary workaround
    try:
        chatcommunicate.init("shoutouts", "to simpleflips", try_cookies=False)
    except Exception as e:
        return  # Because this causes the following checks to fail

    assert client_constructor.call_count == 3
    client_constructor.assert_any_call("stackexchange.com")
    client_constructor.assert_any_call("stackoverflow.com")
    client_constructor.assert_any_call("meta.stackexchange.com")

    assert thread.call_count == 2
    thread.assert_any_call(name="pickle ---rick--- runner", target=chatcommunicate.pickle_last_messages, daemon=True)
    thread.assert_any_call(name="message sender", target=chatcommunicate.send_messages, daemon=True)

    assert len(chatcommunicate._rooms) == 3
    assert chatcommunicate._rooms[("stackexchange.com", 11540)].deletion_watcher is True
    assert chatcommunicate._rooms[("stackexchange.com", 30332)].deletion_watcher is False
    assert chatcommunicate._rooms[("stackoverflow.com", 111347)].deletion_watcher is False
示例#8
0
else:
    log('error', "No ChatExchange password provided. Set it in config or provide it via environment variable")
    os._exit(6)

# We need an instance of bodyfetcher before load_files() is called
GlobalVars.bodyfetcher = BodyFetcher()
if GlobalVars.flovis_host:
    GlobalVars.flovis = Flovis(GlobalVars.flovis_host)

load_files()
filter_auto_ignored_posts()


GlobalVars.standby_mode = "standby" in sys.argv

chatcommunicate.init(username, password)
Tasks.periodic(Metasmoke.send_status_ping, interval=60)
Tasks.periodic(Metasmoke.check_last_pingtime, interval=30)

if GlobalVars.standby_mode:
    chatcommunicate.tell_rooms_with("debug", GlobalVars.standby_message)
    Metasmoke.send_status_ping()

    while GlobalVars.standby_mode:
        time.sleep(3)

    chatcommunicate.join_command_rooms()


# noinspection PyProtectedMember
def check_socket_connections():