def test_run__passes_correct_blocklists_to_subscriptions(self, bot):
        submission = MockSubmission("12322")
        api = MockExportAPI().with_submission(submission)
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}}
        sub1 = MockSubscription("deer", 156)
        sub2 = MockSubscription("dog", -232)
        watcher.subscriptions = [sub1, sub2]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission in sub1.submissions_checked
        assert len(sub1.blocklists) == 1
        assert len(sub1.blocklists[0]) == 2
        assert "test" in sub1.blocklists[0]
        assert "ych" in sub1.blocklists[0]
        assert submission in sub2.submissions_checked
        assert len(sub2.blocklists) == 1
        assert len(sub2.blocklists[0]) == 0
        assert method_called.called
Exemple #2
0
def test_remove_sub__removes_subscription(context):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    watcher.subscriptions.add(Subscription("example", 18749))
    watcher.subscriptions.add(Subscription("test", 18747))
    new_sub = Subscription("test", 18749)
    new_sub.latest_update = datetime.datetime.now()
    watcher.subscriptions.add(new_sub)
    func = SubscriptionFunctionality(watcher)
    list_subs = MockMethod("Listing subscriptions")
    func._list_subs = list_subs.call

    resp = func._remove_sub(18749, "test")

    assert "Removed subscription: \"test\"." in resp
    assert list_subs.called
    assert list_subs.args[0] == 18749
    assert "Listing subscriptions" in resp
    assert len(watcher.subscriptions) == 2
    subscriptions = list(watcher.subscriptions)
    if subscriptions[0].query == "test":
        assert subscriptions[0].destination == 18747
        assert subscriptions[1].query == "example"
        assert subscriptions[1].destination == 18749
    else:
        assert subscriptions[0].query == "example"
        assert subscriptions[0].destination == 18749
        assert subscriptions[1].query == "test"
        assert subscriptions[1].destination == 18747
    def test_run__calls_update_latest_ids(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        mock_new_results = MockMethod([submission1, submission2])
        watcher._get_new_results = mock_new_results.call
        mock_update_latest = MockMethod()
        watcher._update_latest_ids = mock_update_latest.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert mock_update_latest.called
        assert mock_update_latest.args[0] == [submission2]
    def test_update_latest_ids(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        id_list = ["1234", "1233", "1230", "1229"]
        submissions = [MockSubmission(x) for x in id_list]
        mock_save_json = MockMethod()
        watcher.save_to_json = mock_save_json.call

        watcher._update_latest_ids(submissions)

        assert list(watcher.latest_ids) == id_list
        assert mock_save_json.called
    def test_run__calls_get_new_results(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([])
        watcher._get_new_results = method_called.call
        # Shorten the wait
        watcher.BACK_OFF = 1

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert method_called.called
Exemple #6
0
def test_call__route_list_subscriptions(context):
    update = MockTelegramUpdate.with_message(chat_id=14358, text="/list_subscriptions")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = SubscriptionFunctionality(watcher)
    list_subs = MockMethod("Listing subscriptions")
    func._list_subs = list_subs.call

    func.call(update, context)

    assert list_subs.called
    assert list_subs.args is not None
    assert list_subs.args[0] == 14358
    context.bot.send_message.assert_called()
    assert context.bot.send_message.call_args[1]['chat_id'] == update.message.chat_id
    assert context.bot.send_message.call_args[1]['text'] == "Listing subscriptions"
Exemple #7
0
def test_call__route_remove_subscription(context):
    update = MockTelegramUpdate.with_message(chat_id=14358, text="/remove_subscription@FASearchBot example")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = SubscriptionFunctionality(watcher)
    delete_sub = MockMethod("Removed subscription test")
    func._remove_sub = delete_sub.call

    func.call(update, context)

    assert delete_sub.called
    assert delete_sub.args is not None
    assert delete_sub.args[0] == 14358
    assert delete_sub.args[1] == "example"
    context.bot.send_message.assert_called()
    assert context.bot.send_message.call_args[1]['chat_id'] == update.message.chat_id
    assert context.bot.send_message.call_args[1]['text'] == "Removed subscription test"
Exemple #8
0
def test_call__route_add_subscription_with_username(context):
    update = MockTelegramUpdate.with_message(chat_id=14358, text="/add_subscription@FASearchBot test")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = SubscriptionFunctionality(watcher)
    add_sub = MockMethod("Added subscription test")
    func._add_sub = add_sub.call

    func.call(update, context)

    assert add_sub.called
    assert add_sub.args is not None
    assert add_sub.args[0] == 14358
    assert add_sub.args[1] == "test"
    context.bot.send_message.assert_called()
    assert context.bot.send_message.call_args[1]['chat_id'] == update.message.chat_id
    assert context.bot.send_message.call_args[1]['text'] == "Added subscription test"
Exemple #9
0
def test_add_to_blocklist__creates_blocklist_for_channel(context):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = BlocklistFunctionality(watcher)
    list_tags = MockMethod("Listing blocklisted tags")
    func._list_blocklisted_tags = list_tags.call

    resp = func._add_to_blocklist(18749, "test")

    assert "Added tag to blocklist" in resp
    assert "\"test\"" in resp
    assert list_tags.called
    assert list_tags.args[0] == 18749
    assert "Listing blocklisted tags" in resp
    assert len(watcher.blocklists[18749]) == 1
    assert isinstance(watcher.blocklists[18749], set)
    tag = list(watcher.blocklists[18749])[0]
    assert tag == "test"
Exemple #10
0
def test_add_to_blocklist__add_tag_to_blocklist(context):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    watcher.blocklists[18749] = {"example"}
    func = BlocklistFunctionality(watcher)
    list_tags = MockMethod("Listing blocklisted tags")
    func._list_blocklisted_tags = list_tags.call

    resp = func._add_to_blocklist(18749, "test")

    assert "Added tag to blocklist" in resp
    assert "\"test\"" in resp
    assert list_tags.called
    assert list_tags.args[0] == 18749
    assert "Listing blocklisted tags" in resp
    assert len(watcher.blocklists[18749]) == 2
    assert isinstance(watcher.blocklists[18749], set)
    assert "example" in watcher.blocklists[18749]
    assert "test" in watcher.blocklists[18749]
Exemple #11
0
def test_add_sub__adds_subscription(context):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = SubscriptionFunctionality(watcher)
    list_subs = MockMethod("Listing subscriptions")
    func._list_subs = list_subs.call

    resp = func._add_sub(18749, "test")

    assert "Added subscription" in resp
    assert "\"test\"" in resp
    assert list_subs.called
    assert list_subs.args[0] == 18749
    assert "Listing subscriptions" in resp
    assert len(watcher.subscriptions) == 1
    subscription = list(watcher.subscriptions)[0]
    assert subscription.query == "test"
    assert subscription.destination == 18749
    assert subscription.latest_update is None
Exemple #12
0
def test_call__route_add_blocklisted_tag(context):
    update = MockTelegramUpdate.with_message(chat_id=14358,
                                             text="/add_blocklisted_tag test")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = BlocklistFunctionality(watcher)
    add_tag = MockMethod("Added to blocklist: test")
    func._add_to_blocklist = add_tag.call

    func.call(update, context)

    assert add_tag.called
    assert add_tag.args is not None
    assert add_tag.args[0] == 14358
    assert add_tag.args[1] == "test"
    context.bot.send_message.assert_called()
    assert context.bot.send_message.call_args[1][
        'chat_id'] == update.message.chat_id
    assert context.bot.send_message.call_args[1][
        'text'] == "Added to blocklist: test"
Exemple #13
0
def test_call__route_remove_blocklisted_tag(context):
    update = MockTelegramUpdate.with_message(
        chat_id=14358, text="/remove_blocklisted_tag example")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    func = BlocklistFunctionality(watcher)
    remove_tag = MockMethod("Removed from blocklist: example")
    func._remove_from_blocklist = remove_tag.call

    func.call(update, context)

    assert remove_tag.called
    assert remove_tag.args is not None
    assert remove_tag.args[0] == 14358
    assert remove_tag.args[1] == "example"
    context.bot.send_message.assert_called()
    assert context.bot.send_message.call_args[1][
        'chat_id'] == update.message.chat_id
    assert context.bot.send_message.call_args[1][
        'text'] == "Removed from blocklist: example"
Exemple #14
0
def test_remove_from_blocklist__removes_tag_from_blocklist(context):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, context.bot)
    watcher.blocklists[18749] = {"example", "test"}
    watcher.blocklists[18747] = {"test"}
    func = BlocklistFunctionality(watcher)
    list_tags = MockMethod("Listing blocklisted tags")
    func._list_blocklisted_tags = list_tags.call

    resp = func._remove_from_blocklist(18749, "test")

    assert "Removed tag from blocklist: \"test\"." in resp
    assert list_tags.called
    assert list_tags.args[0] == 18749
    assert "Listing blocklisted tags" in resp
    assert len(watcher.blocklists) == 2
    assert len(watcher.blocklists[18749]) == 1
    assert len(watcher.blocklists[18747]) == 1
    assert watcher.blocklists[18749] == {"example"}
    assert watcher.blocklists[18747] == {"test"}
    def test_run__checks_all_new_results(self, bot):
        submission1 = MockSubmission("12322")
        submission2 = MockSubmission("12324")
        api = MockExportAPI().with_submissions([submission1, submission2])
        watcher = SubscriptionWatcher(api, bot)
        method_called = MockMethod([submission1, submission2])
        watcher._get_new_results = method_called.call
        watcher.BACK_OFF = 1
        sub = MockSubscription("deer", 0)
        watcher.subscriptions = [sub]

        thread = Thread(target=lambda: self.watcher_killer(watcher))
        thread.start()
        # Run watcher
        watcher.run()
        thread.join()

        assert submission1 in sub.submissions_checked
        assert submission2 in sub.submissions_checked
        assert method_called.called