Beispiel #1
0
async def test_convert_gif():
    submission = SubmissionBuilder(file_ext="gif",
                                   file_size=47453).build_full_submission()
    mock_run = MockMethod("Test docker")
    mock_filesize = MockMethod(submission.SIZE_LIMIT_GIF - 10)
    submission._run_docker = mock_run.async_call

    with mock.patch("os.path.getsize", mock_filesize.call):
        output_path = await submission._convert_gif(submission.download_url)

    assert output_path is not None
    assert output_path.endswith(".mp4")
    assert mock_run.called
    assert mock_run.args[1].startswith(f"-i {submission.download_url}")
    assert mock_run.args[1].endswith(f" /{output_path}")
async def test_run__passes_correct_blocklists_to_subscriptions(mock_client):
    submission = MockSubmission("12322")
    api = MockExportAPI().with_submission(submission)
    watcher = SubscriptionWatcher(api, mock_client)
    method_called = MockMethod([submission])
    watcher._get_new_results = method_called.async_call
    watcher.BACK_OFF = 1
    watcher.blocklists = {156: {"test", "ych"}, -200: {"example"}}
    sub1 = MockSubscription("deer", 156)
    sub2 = MockSubscription("dog", -232)
    watcher.subscriptions = [sub1, sub2]

    task = asyncio.get_event_loop().create_task(watcher_killer(watcher))
    # Run watcher
    await watcher.run()
    await task

    assert submission in sub1.submissions_checked
    assert len(sub1.blocklists) == 1
    assert sub1.blocklists[0] in [
        AndQuery([NotQuery(WordQuery("test")),
                  NotQuery(WordQuery("ych"))]),
        AndQuery([NotQuery(WordQuery("ych")),
                  NotQuery(WordQuery("test"))])
    ]
    assert submission in sub2.submissions_checked
    assert len(sub2.blocklists) == 1
    assert sub2.blocklists[0] == AndQuery([])
    assert method_called.called
Beispiel #3
0
async def test_convert_gif_two_pass():
    submission = SubmissionBuilder(file_ext="gif",
                                   file_size=47453).build_full_submission()
    mock_run = MockMultiMethod(["Test docker", "27.5", "ffmpeg1", "ffmpeg2"])
    mock_filesize = MockMethod(submission.SIZE_LIMIT_GIF + 10)
    submission._run_docker = mock_run.async_call

    with mock.patch("os.path.getsize", mock_filesize.call):
        output_path = await submission._convert_gif(submission.download_url)

    assert output_path is not None
    assert output_path.endswith(".mp4")
    assert mock_run.calls == 4
    # Initial ffmpeg call
    assert mock_run.args[0][1].startswith(f"-i {submission.download_url} ")
    # ffprobe call
    assert mock_run.args[1][1].startswith("-show_entries format=duration ")
    assert mock_run.kwargs[1]["entrypoint"] == "ffprobe"
    # First ffmpeg two pass call
    assert mock_run.args[2][1].startswith(f"-i {submission.download_url} ")
    assert " -pass 1 -f mp4 " in mock_run.args[2][1]
    assert mock_run.args[2][1].endswith(" /dev/null -y")
    # Second ffmpeg two pass call
    assert mock_run.args[3][1].startswith(f"-i {submission.download_url} ")
    assert " -pass 2 " in mock_run.args[3][1]
    assert mock_run.args[3][1].endswith(f" {output_path} -y")
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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
async def test_run__calls_update_latest_ids(mock_client):
    submission1 = MockSubmission("12322")
    submission2 = MockSubmission("12324")
    api = MockExportAPI().with_submissions([submission1, submission2])
    watcher = SubscriptionWatcher(api, mock_client)
    mock_new_results = MockMethod([submission1, submission2])
    watcher._get_new_results = mock_new_results.async_call
    mock_update_latest = MockMethod()
    watcher._update_latest_ids = mock_update_latest.call
    # Shorten the wait
    watcher.BACK_OFF = 1

    task = asyncio.get_event_loop().create_task(watcher_killer(watcher))
    # Run watcher
    await watcher.run()
    await task

    assert mock_update_latest.called
    assert mock_update_latest.args[0] == [submission2]
def test_update_latest_ids(mock_client):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    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
async def test_run__calls_get_new_results(mock_client):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    method_called = MockMethod([])
    watcher._get_new_results = method_called.async_call
    # Shorten the wait
    watcher.BACK_OFF = 1

    task = asyncio.get_event_loop().create_task(watcher_killer(watcher))
    # Run watcher
    await watcher.run()
    await task

    assert method_called.called
Beispiel #11
0
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"
Beispiel #12
0
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_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"
def test_add_to_blocklist__creates_blocklist_for_channel(mock_client):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    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"
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"
Beispiel #17
0
async def test_supergroup_upgrade(mock_client):
    old_chat_id = 12345
    new_chat_id = 54321
    event = MockTelegramEvent.with_migration(old_chat_id=old_chat_id,
                                             new_chat_id=new_chat_id)
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    func = SupergroupUpgradeFunctionality(watcher)
    migrate_chat = MockMethod("Migrate subscriptions")
    watcher.migrate_chat = migrate_chat.call

    with pytest.raises(StopPropagation):
        await func.call(event)

    assert migrate_chat.called
    assert migrate_chat.args is not None
    assert migrate_chat.args[0] == -12345
    assert migrate_chat.args[1] == -10054321
    event.reply.assert_not_called()
async def test_call__route_add_blocklisted_tag(mock_client):
    event = MockTelegramEvent.with_message(chat_id=14358,
                                           text="/add_blocklisted_tag test")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    func = BlocklistFunctionality(watcher)
    add_tag = MockMethod("Added to blocklist: test")
    func._add_to_blocklist = add_tag.call

    with pytest.raises(StopPropagation):
        # noinspection PyTypeChecker
        await func.call(event)

    assert add_tag.called
    assert add_tag.args is not None
    assert add_tag.args[0] == 14358
    assert add_tag.args[1] == "test"
    event.reply.assert_called()
    assert event.reply.call_args[0][0] == "Added to blocklist: test"
Beispiel #19
0
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
async def test_call__route_remove_blocklisted_tag(mock_client):
    event = MockTelegramEvent.with_message(
        chat_id=14358, text="/remove_blocklisted_tag example")
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    func = BlocklistFunctionality(watcher)
    remove_tag = MockMethod("Removed from blocklist: example")
    func._remove_from_blocklist = remove_tag.call

    with pytest.raises(StopPropagation):
        # noinspection PyTypeChecker
        await func.call(event)

    assert remove_tag.called
    assert remove_tag.args is not None
    assert remove_tag.args[0] == 14358
    assert remove_tag.args[1] == "example"
    event.reply.assert_called()
    assert event.reply.call_args[0][0] == "Removed from blocklist: example"
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_add_to_blocklist__add_tag_to_blocklist(mock_client):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    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]
async def test_run__checks_all_new_results(mock_client):
    submission1 = MockSubmission("12322")
    submission2 = MockSubmission("12324")
    api = MockExportAPI().with_submissions([submission1, submission2])
    watcher = SubscriptionWatcher(api, mock_client)
    method_called = MockMethod([submission1, submission2])
    watcher._get_new_results = method_called.async_call
    watcher.BACK_OFF = 1
    sub = MockSubscription("deer", 0)
    watcher.subscriptions = [sub]

    task = asyncio.get_event_loop().create_task(watcher_killer(watcher))
    # Run watcher
    await watcher.run()
    await task

    assert submission1 in sub.submissions_checked
    assert submission2 in sub.submissions_checked
    assert method_called.called
def test_remove_from_blocklist__removes_tag_from_blocklist(mock_client):
    api = MockExportAPI()
    watcher = SubscriptionWatcher(api, mock_client)
    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"}
Beispiel #25
0
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