def test_remove_sub__removes_subscription_case_insensitive(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) 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_str == "test": assert subscriptions[0].destination == 18747 assert subscriptions[1].query_str == "example" assert subscriptions[1].destination == 18749 else: assert subscriptions[0].query_str == "example" assert subscriptions[0].destination == 18749 assert subscriptions[1].query_str == "test" assert subscriptions[1].destination == 18747
def test_pause_destination__not_in_other_destination(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("test", 18749)) watcher.subscriptions.add(Subscription("example", 12345)) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._pause_destination(18749) assert "Paused all subscriptions." in resp assert len(watcher.subscriptions) == 2 assert list_subs.called assert list_subs.args[0] == 18749 assert "Listing subscriptions" in resp sub1, sub2 = list(watcher.subscriptions)[:2] if sub1.destination != 18749: sub2, sub1 = sub1, sub2 assert sub1.destination == 18749 assert sub1.query_str == "test" assert sub1.paused is True assert sub2.destination == 12345 assert sub2.query_str == "example" assert sub2.paused is False
def test_pause_subscription__case_insensitive(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("EXAMPLE", 18749)) watcher.subscriptions.add(Subscription("TEST", 18749)) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._pause_subscription(18749, "test") assert f"Paused subscription: \"test\"." in resp assert list_subs.called assert list_subs.args[0] == 18749 assert "Listing subscriptions" in resp assert len(watcher.subscriptions) == 2 sub1, sub2 = watcher.subscriptions if sub1.query_str != "TEST": sub1, sub2 = sub2, sub1 assert sub1.query_str == "TEST" assert sub1.destination == 18749 assert sub1.paused is True assert sub2.query_str == "EXAMPLE" assert sub2.destination == 18749 assert sub2.paused is False
def test_resume_subscription__one_matching(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) sub1 = Subscription("example", 18749) sub1.paused = True watcher.subscriptions.add(sub1) sub2 = Subscription("test", 18749) sub2.paused = True watcher.subscriptions.add(sub2) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._resume_subscription(18749, "test") assert "Resumed subscription: \"test\"." in resp assert list_subs.called assert list_subs.args[0] == 18749 assert "Listing subscriptions" in resp assert len(watcher.subscriptions) == 2 sub1, sub2 = watcher.subscriptions if sub1.query_str != "test": sub1, sub2 = sub2, sub1 assert sub1.query_str == "test" assert sub1.destination == 18749 assert sub1.paused is False assert sub2.query_str == "example" assert sub2.destination == 18749 assert sub2.paused is True
def test_pause_destination__no_subs(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) resp = func._pause_destination(18749) assert resp == "There are no subscriptions posting here to pause." assert len(watcher.subscriptions) == 0
def test_add_sub__invalid_query(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) resp = func._add_sub(18749, "(hello") assert resp.startswith("Failed to parse subscription query") assert len(watcher.subscriptions) == 0
def test_add_sub__no_add_blank(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) resp = func._add_sub(18749, "") assert resp == "Please specify the subscription query you wish to add." assert len(watcher.subscriptions) == 0
def test_add_sub__no_add_duplicate_case_insensitive(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("test", 18749)) func = SubscriptionFunctionality(watcher) resp = func._add_sub(18749, "TEST") assert resp == "A subscription already exists for \"TEST\"." assert len(watcher.subscriptions) == 1
def test_remove_sub__non_existent_subscription(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 18747)) func = SubscriptionFunctionality(watcher) resp = func._remove_sub(18749, "test") assert resp == "There is not a subscription for \"test\" in this chat." assert len(watcher.subscriptions) == 2
def test_list_subs__alphabetical_case_insensitive(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("Example", 18749)) watcher.subscriptions.add(Subscription("test", 18749)) watcher.subscriptions.add(Subscription("deer", 18749)) func = SubscriptionFunctionality(watcher) resp = func._list_subs(18749) assert "Current subscriptions in this chat:" in resp assert "- deer\n- Example\n- test" in resp
def test_pause_subscription__one_matching_in_wrong_destination(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 12345)) func = SubscriptionFunctionality(watcher) resp = func._pause_subscription(18749, "test") assert resp == "There is not a subscription for \"test\" in this chat." assert len(watcher.subscriptions) == 2 for subscription in watcher.subscriptions: assert subscription.paused is False
def test_list_subs__some_paused(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("example", 18749)) sub_paused = Subscription("test", 18749) sub_paused.paused = True watcher.subscriptions.add(sub_paused) watcher.subscriptions.add(Subscription("deer", 18749)) func = SubscriptionFunctionality(watcher) resp = func._list_subs(18749) assert "Current subscriptions in this chat:" in resp assert "- deer\n- example\n- ⏸<s>test</s>" in resp
def test_list_subs(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("example", 18749)) watcher.subscriptions.add(Subscription("test", 18747)) watcher.subscriptions.add(Subscription("deer", 18749)) func = SubscriptionFunctionality(watcher) resp = func._list_subs(18749) assert "Current subscriptions in this chat:" in resp assert "- deer" in resp assert "- example" in resp assert "- test" not in resp
async def test_call__route_resume_destination(mock_client): event = MockTelegramEvent.with_message(chat_id=14358, text="/resume") api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) resume_dest = MockMethod("Resumed all subscriptions") func._resume_destination = resume_dest.call with pytest.raises(StopPropagation): await func.call(event) assert resume_dest.called assert resume_dest.args is not None assert resume_dest.args[0] == event.chat_id event.reply.assert_called() assert event.reply.call_args[0][0] == "Resumed all subscriptions"
async def test_call__route_pause_destination_with_handle(mock_client): event = MockTelegramEvent.with_message(chat_id=14358, text="/pause@FASearchBot") api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) pause_dest = MockMethod("Paused all subscriptions") func._pause_destination = pause_dest.call with pytest.raises(StopPropagation): await func.call(event) assert pause_dest.called assert pause_dest.args is not None assert pause_dest.args[0] == event.chat_id event.reply.assert_called() assert event.reply.call_args[0][0] == "Paused all subscriptions"
async def test_call__route_list_subscriptions(mock_client): event = MockTelegramEvent.with_message(chat_id=14358, text="/list_subscriptions") api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call with pytest.raises(StopPropagation): await func.call(event) assert list_subs.called assert list_subs.args is not None assert list_subs.args[0] == 14358 event.reply.assert_called() assert event.reply.call_args[0][0] == "Listing subscriptions"
def test_resume_subscription__no_matching(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) sub1 = Subscription("example", 18749) sub1.paused = True watcher.subscriptions.add(sub1) sub2 = Subscription("deer", 18749) sub2.paused = True watcher.subscriptions.add(sub2) func = SubscriptionFunctionality(watcher) resp = func._resume_subscription(18749, "test") assert resp == "There is not a subscription for \"test\" in this chat." assert len(watcher.subscriptions) == 2 for subscription in watcher.subscriptions: assert subscription.paused is True
async def test_call__route_remove_subscription_with_username(mock_client): event = MockTelegramEvent.with_message( chat_id=14358, text="/remove_subscription@FASearchBot example") api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) delete_sub = MockMethod("Removed subscription test") func._remove_sub = delete_sub.call with pytest.raises(StopPropagation): await func.call(event) assert delete_sub.called assert delete_sub.args is not None assert delete_sub.args[0] == 14358 assert delete_sub.args[1] == "example" event.reply.assert_called() assert event.reply.call_args[0][0] == "Removed subscription test"
async def test_call__route_add_subscription(mock_client): event = MockTelegramEvent.with_message(chat_id=14358, text="/add_subscription test") api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) func = SubscriptionFunctionality(watcher) add_sub = MockMethod("Added subscription test") func._add_sub = add_sub.call with pytest.raises(StopPropagation): await func.call(event) assert add_sub.called assert add_sub.args is not None assert add_sub.args[0] == 14358 assert add_sub.args[1] == "test" event.reply.assert_called() assert event.reply.call_args[0][0] == "Added subscription test"
def test_add_sub__adds_subscription(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) 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_str == "test" assert subscription.destination == 18749 assert subscription.latest_update is None
def test_pause_destination__all_paused(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) sub1 = Subscription("test", 18749) sub1.paused = True watcher.subscriptions.add(sub1) sub2 = Subscription("example", 18749) sub2.paused = True watcher.subscriptions.add(sub2) func = SubscriptionFunctionality(watcher) resp = func._pause_destination(18749) assert resp == "All subscriptions are already paused." assert len(watcher.subscriptions) == 2 for subscription in watcher.subscriptions: assert subscription.query_str in ["test", "example"] assert subscription.destination == 18749 assert subscription.paused is True
def test_pause_destination__one_sub(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("test", 18749)) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._pause_destination(18749) assert "Paused all subscriptions." 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_str == "test" assert subscription.destination == 18749 assert subscription.paused is True
def test_pause_destination__multiple_subs(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("test", 18749)) watcher.subscriptions.add(Subscription("example", 18749)) func = SubscriptionFunctionality(watcher) list_subs = MockMethod("Listing subscriptions") func._list_subs = list_subs.call resp = func._pause_destination(18749) assert "Paused all subscriptions." in resp assert len(watcher.subscriptions) == 2 assert list_subs.called assert list_subs.args[0] == 18749 assert "Listing subscriptions" in resp for subscription in watcher.subscriptions: assert subscription.query_str in ["test", "example"] assert subscription.destination == 18749 assert subscription.paused is True
def test_pause_subscription__already_paused(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) watcher.subscriptions.add(Subscription("example", 18749)) sub = Subscription("test", 18749) sub.paused = True watcher.subscriptions.add(sub) func = SubscriptionFunctionality(watcher) resp = func._pause_subscription(18749, "test") assert resp == f"Subscription for \"test\" is already paused." assert len(watcher.subscriptions) == 2 sub1, sub2 = watcher.subscriptions if sub1.query_str != "test": sub1, sub2 = sub2, sub1 assert sub1.query_str == "test" assert sub1.destination == 18749 assert sub1.paused is True assert sub2.query_str == "example" assert sub2.destination == 18749 assert sub2.paused is False
def initialise_functionalities(self): return [ BeepFunctionality(), WelcomeFunctionality(), ImageHashRecommendFunctionality(), NeatenFunctionality(self.api), InlineNeatenFunctionality(self.api), InlineFunctionality(self.api), InlineEditFunctionality(self.api, self.client), InlineEditButtonPress(self.api), SubscriptionFunctionality(self.subscription_watcher), BlocklistFunctionality(self.subscription_watcher), SupergroupUpgradeFunctionality(self.subscription_watcher), UnhandledMessageFunctionality(), ]
def test_pause_destination__all_paused_except_elsewhere(mock_client): api = MockExportAPI() watcher = SubscriptionWatcher(api, mock_client) sub1 = Subscription("test", 18749) sub1.paused = True watcher.subscriptions.add(sub1) sub2 = Subscription("example", 12345) sub2.paused = False watcher.subscriptions.add(sub2) func = SubscriptionFunctionality(watcher) resp = func._pause_destination(18749) assert resp == "All subscriptions are already paused." assert len(watcher.subscriptions) == 2 sub1, sub2 = list(watcher.subscriptions)[:2] if sub1.destination != 18749: sub2, sub1 = sub1, sub2 assert sub1.destination == 18749 assert sub1.query_str == "test" assert sub1.paused is True assert sub2.destination == 12345 assert sub2.query_str == "example" assert sub2.paused is False