async def test_result_missing_between_pages(mock_client): username = "******" image_id = 1560331512 post_id = 232347 event = MockTelegramEvent.with_message( text="http://d.furaffinity.net/art/{0}/{1}/{1}.pic_of_me.png".format( username, image_id)) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_user_folder( username, "gallery", [ MockSubmission(post_id + 1, image_id=image_id + 16), MockSubmission(post_id, image_id=image_id + 3) ], page=1) neaten.api.with_user_folder( username, "gallery", [ MockSubmission(post_id - 2, image_id=image_id - 27), MockSubmission(post_id - 3, image_id=image_id - 34) ], page=2) with pytest.raises(StopPropagation): await neaten.call(event) event.reply.assert_called_with( f"Error finding submission: Could not locate the image by {username} with image id {image_id}.", )
async def test_result_last_on_page(mock_client): username = "******" image_id = 1560331512 post_id = 232347 event = MockTelegramEvent.with_message( text="http://d.furaffinity.net/art/{0}/{1}/{1}.pic_of_me.png".format( username, image_id), client=mock_client, ) submission = MockSubmission(post_id, image_id=image_id) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_user_folder(username, "gallery", [ MockSubmission(post_id + 4, image_id=image_id + 16), MockSubmission(post_id + 3, image_id=image_id + 2), MockSubmission(post_id + 2, image_id=image_id + 1), submission ]) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id
async def test_result_on_third_page(mock_client): username = "******" image_id = 1560331512 post_id = 232347 event = MockTelegramEvent.with_message( text="http://d.furaffinity.net/art/{0}/{1}/{1}.pic_of_me.png".format( username, image_id), client=mock_client, ) neaten = NeatenFunctionality(MockExportAPI()) for page in [1, 2, 3]: neaten.api.with_user_folder( username, "gallery", [ MockSubmission(post_id + 1 + (3 - page) * 5, image_id=image_id + 16 + (3 - page) * 56), MockSubmission(post_id + (3 - page) * 5, image_id=image_id + (3 - page) * 56), MockSubmission(post_id - 2 + (3 - page) * 5, image_id=image_id - 27 + (3 - page) * 56), MockSubmission(post_id - 3 + (3 - page) * 5, image_id=image_id - 34 + (3 - page) * 56) ], page=page) submission = await neaten.api.get_full_submission(str(post_id)) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id
async def test_ignore_link(mock_client): event = MockTelegramEvent.with_message(text="http://example.com") neaten = NeatenFunctionality(MockExportAPI()) await neaten.call(event) event.reply.assert_not_called()
async def test_direct_link_and_matching_submission_link(mock_client): username = "******" image_id = 1560331512 post_id = 232347 event = MockTelegramEvent.with_message( text= "http://d.furaffinity.net/art/{0}/{1}/{1}.pic_of_me.png https://furaffinity.net/view/{2}/" .format(username, image_id, post_id), client=mock_client, ) submission = MockSubmission(post_id, image_id=image_id) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_user_folder( username, "gallery", [submission, MockSubmission(post_id - 1, image_id=image_id - 15)]) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id
async def test_two_submission_links(mock_client): post_id1 = 23636984 post_id2 = 23636996 event = MockTelegramEvent.with_message( text="furaffinity.net/view/{}\nfuraffinity.net/view/{}".format( post_id1, post_id2), client=mock_client, ) submission1 = MockSubmission(post_id1) submission2 = MockSubmission(post_id2) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submissions([submission1, submission2]) with pytest.raises(StopPropagation): await neaten.call(event) submission1._send_message.assert_called_once() args1, kwargs1 = submission1._send_message.call_args assert args1[0] == mock_client assert args1[1] == event.input_chat assert kwargs1['reply_to'] == event.message.id submission2._send_message.assert_called_once() args2, kwargs2 = submission2._send_message.call_args assert args2[0] == mock_client assert args2[1] == event.input_chat assert kwargs2['reply_to'] == event.message.id
async def test_submission_link_and_different_direct_link(mock_client): username = "******" image_id1 = 1560331512 image_id2 = image_id1 + 300 post_id1 = 232347 post_id2 = 233447 event = MockTelegramEvent.with_message( text= "https://furaffinity.net/view/{2}/ http://d.furaffinity.net/art/{0}/{1}/{1}.pic_of_me.png" .format(username, image_id1, post_id2), client=mock_client) submission1 = MockSubmission(post_id1, image_id=image_id1) submission2 = MockSubmission(post_id2, image_id=image_id2) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_user_folder(username, "gallery", [ submission2, submission1, MockSubmission(post_id1 - 1, image_id=image_id1 - 15) ]) with pytest.raises(StopPropagation): await neaten.call(event) submission1._send_message.assert_called_once() args1, kwargs1 = submission1._send_message.call_args assert args1[0] == mock_client assert args1[1] == event.input_chat assert kwargs1['reply_to'] == event.message.id submission2._send_message.assert_called_once() args2, kwargs2 = submission2._send_message.call_args assert args2[0] == mock_client assert args2[1] == event.input_chat assert kwargs2['reply_to'] == event.message.id
async def test_ignore_message(mock_client): event = MockTelegramEvent.with_message(text="hello world") neaten = NeatenFunctionality(MockExportAPI()) await neaten.call(event) event.reply.assert_not_called()
async def test_thumb_and_different_submission_link(mock_client): post_id1 = 382632 post_id2 = 382672 event = MockTelegramEvent.with_message( text= f"https://t.furaffinity.net/{post_id1}@1600-1562445328.jpg\nhttps://furaffinity.net/view/{post_id2}", client=mock_client, ) submission1 = MockSubmission(post_id1) submission2 = MockSubmission(post_id2) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submissions([submission1, submission2]) with pytest.raises(StopPropagation): await neaten.call(event) submission1._send_message.assert_called_once() args1, kwargs1 = submission1._send_message.call_args assert args1[0] == mock_client assert args1[1] == event.input_chat assert kwargs1['reply_to'] == event.message.id submission2._send_message.assert_called_once() args2, kwargs2 = submission2._send_message.call_args assert args2[0] == mock_client assert args2[1] == event.input_chat assert kwargs2['reply_to'] == event.message.id
async def test_unhandled_group_message(mock_client): event = MockTelegramEvent.with_message(text="Hey friendo, how are you?", chat_type=ChatType.GROUP) unhandled = UnhandledMessageFunctionality() await unhandled.call(event) event.reply.assert_not_called()
def test_can_create_message(self): event = MockTelegramEvent.with_message() assert event.callback_query is None assert event.message is not None assert event.message.message_id is not None assert event.message.chat_id is not None assert isinstance(event.message.photo, list) assert len(event.message.photo) == 0
async def test_no_reply_in_group(mock_client): event = MockTelegramEvent.with_message( text=None, chat_type=ChatType.GROUP).with_photo() func = ImageHashRecommendFunctionality() await func.call(event) event.reply.assert_not_called()
async def test_ignore_journal_link(mock_client): event = MockTelegramEvent.with_message( text="https://www.furaffinity.net/journal/9150534/") neaten = NeatenFunctionality(MockExportAPI()) await neaten.call(event) event.reply.assert_not_called()
async def test_unhandled_message(mock_client): event = MockTelegramEvent.with_message(text="Hello can I have a picture") unhandled = UnhandledMessageFunctionality() with pytest.raises(StopPropagation): await unhandled.call(event) event.reply.assert_called_with( "Sorry, I'm not sure how to handle that message", )
async def test_beep(mock_client): event = MockTelegramEvent.with_message(text="/beep") beep = BeepFunctionality() with pytest.raises(StopPropagation): await beep.call(event) event.respond.assert_called() assert event.respond.call_args[0][0] == "boop"
async def test_doesnt_fire_on_avatar(mock_client): event = MockTelegramEvent.with_message( text="https://a.furaffinity.net/1538326752/geordie79.gif", client=mock_client, ) neaten = NeatenFunctionality(MockExportAPI()) await neaten.call(event) event.reply.assert_not_called()
async def test_deleted_submission(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message( text="furaffinity.net/view/{}".format(post_id)) neaten = NeatenFunctionality(MockExportAPI()) with pytest.raises(StopPropagation): await neaten.call(event) event.reply.assert_called_with( f"This doesn't seem to be a valid FA submission: https://www.furaffinity.net/view/{post_id}/" )
async def test_submission_link_in_group_caption(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message( chat_type=ChatType.GROUP, text=f"https://www.furaffinity.net/view/{post_id}/").with_document() submission = MockSubmission(post_id) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submission(submission) await neaten.call(event) submission._send_message.assert_not_called()
async def test_sends_recommendation(mock_client): event = MockTelegramEvent.with_message(text=None).with_photo() func = ImageHashRecommendFunctionality() with pytest.raises(StopPropagation): await func.call(event) event.reply.assert_called() message_text = event.reply.call_args[0][0] assert "@FindFurryPicBot" in message_text assert "@FoxBot" in message_text assert "@reverseSearchBot" in message_text
async def test_deleted_submission_group_chat(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message( text="furaffinity.net/view/{}".format(post_id), chat_type=ChatType.GROUP) neaten = NeatenFunctionality(MockExportAPI()) with pytest.raises(StopPropagation): await neaten.call(event) event.reply.assert_called_with("⏳ Neatening image link") event.reply.return_value.delete.assert_called_once()
def test_can_create_message_with_document(self): event = MockTelegramEvent.with_message().with_document() assert event.callback_query is None assert event.message is not None assert event.message.message_id is not None assert event.message.chat_id is not None assert isinstance(event.message.photo, list) assert len(event.message.photo) == 0 assert event.message.document is not None assert isinstance(event.message.document, _MockDocument) assert event.message.document.file_id is not None assert event.message.document.mime_type is None
async def test_welcome_message(mock_client): event = MockTelegramEvent.with_message(text="/start") func = WelcomeFunctionality() with pytest.raises(StopPropagation): await func.call(event) event.respond.assert_called() message_text = event.respond.call_args[0][0] assert "@deerspangle" in message_text assert "https://github.com/Deer-Spangle/FA-search-bot" in message_text assert FASearchBot.VERSION in message_text
async def test_unknown_type_submission_groupchat(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message( text="https://www.furaffinity.net/view/{}/".format(post_id), chat_type=ChatType.GROUP) submission = MockSubmission(post_id, file_ext="zzz") neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submission(submission) with pytest.raises(StopPropagation): await neaten.call(event) event.reply.assert_called_with("⏳ Neatening image link", ) event.reply.return_value.delete.assert_called_once()
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_submission_link_in_caption(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message(client=mock_client).with_photo( caption=f"https://www.furaffinity.net/view/{post_id}/") submission = MockSubmission(post_id) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submission(submission) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id
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"
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_thumbnail_link_big(mock_client): post_id = 382632 event = MockTelegramEvent.with_message( text=f"https://t.furaffinity.net/{post_id}@1600-1562445328.jpg", client=mock_client, ) submission = MockSubmission(post_id) neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submission(submission) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id
async def test_mp3_submission(mock_client): post_id = 23636984 event = MockTelegramEvent.with_message( text="https://www.furaffinity.net/view/{}/".format(post_id), client=mock_client, ) submission = MockSubmission(post_id, file_ext="mp3") neaten = NeatenFunctionality(MockExportAPI()) neaten.api.with_submission(submission) with pytest.raises(StopPropagation): await neaten.call(event) submission._send_message.assert_called_once() args, kwargs = submission._send_message.call_args assert args[0] == mock_client assert args[1] == event.input_chat assert kwargs['reply_to'] == event.message.id