def test_create_short_dict_makes_thumb_bigger_75(self):
        builder = SubmissionBuilder(thumb_size=75)
        big_thumb_link = builder.thumbnail_url.replace("@75-", "@1600-")

        submission = FASubmission.from_short_dict(
            builder.build_search_json()
        )

        assert submission.thumbnail_url == big_thumb_link
    def test_unknown_type_submission(self, bot):
        submission = SubmissionBuilder(file_ext="zzz", file_size=47453).build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        try:
            submission.send_message(bot, chat_id, message_id)
            assert False, "Should have thrown exception."
        except CantSendFileType as e:
            assert str(e) == "I'm sorry, I don't understand that file extension (zzz)."
    def test_swf_submission(self, bot):
        submission = SubmissionBuilder(file_ext="swf", file_size=47453).build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        try:
            submission.send_message(bot, chat_id, message_id)
            assert False, "Should have thrown exception."
        except CantSendFileType as e:
            assert str(e) == "I'm sorry, I can't neaten \".swf\" files."
    def test_get_browse_page_specify_page(self, r):
        builder = SubmissionBuilder()
        api = FAExportAPI("http://example.com/")
        r.get(f"http://example.com/browse.json?page=5",
              json=[builder.build_search_json()])

        results = api.get_browse_page(5)

        assert len(results) == 1
        assert isinstance(results[0], FASubmissionShort)
        assert results[0].submission_id == builder.submission_id
    def test_to_json_and_back(self, bot):
        test_watcher_file = "./test_subscription_watcher.json"
        if os.path.exists(test_watcher_file):
            os.remove(test_watcher_file)
        old_filename = SubscriptionWatcher.FILENAME
        SubscriptionWatcher.FILENAME = test_watcher_file
        api = MockExportAPI()
        latest_submissions = [
            SubmissionBuilder(submission_id="123243").build_short_submission(),
            SubmissionBuilder(submission_id="123242").build_short_submission(),
            SubmissionBuilder(submission_id="123240").build_short_submission()
        ]
        subscription1 = Subscription("query", 1234)
        subscription2 = Subscription("example", 5678)
        watcher = SubscriptionWatcher(api, bot)
        watcher._update_latest_ids(latest_submissions)
        watcher.subscriptions.add(subscription1)
        watcher.subscriptions.add(subscription2)
        watcher.blocklists[3452] = {"test", "example"}
        watcher.blocklists[1453] = {"ych"}

        try:
            watcher.save_to_json()
            new_watcher = SubscriptionWatcher.load_from_json(api, bot)

            assert len(new_watcher.latest_ids) == 3
            assert "123243" in new_watcher.latest_ids
            assert "123242" in new_watcher.latest_ids
            assert "123240" in new_watcher.latest_ids
            assert list(watcher.latest_ids) == list(new_watcher.latest_ids)
            assert len(new_watcher.subscriptions) == 2
            list_subs = list(new_watcher.subscriptions)
            if list_subs[0].query == "query":
                assert list_subs[0].destination == 1234
                assert list_subs[1].query == "example"
                assert list_subs[1].destination == 5678
            else:
                assert list_subs[0].query == "example"
                assert list_subs[0].destination == 5678
                assert list_subs[1].query == "query"
                assert list_subs[1].destination == 1234
            assert len(new_watcher.blocklists) == 2
            assert 3452 in new_watcher.blocklists
            assert len(new_watcher.blocklists[3452]) == 2
            assert isinstance(new_watcher.blocklists[3452], set)
            assert "test" in new_watcher.blocklists[3452]
            assert "example" in new_watcher.blocklists[3452]
            assert 1453 in new_watcher.blocklists
            assert len(new_watcher.blocklists[1453]) == 1
            assert isinstance(new_watcher.blocklists[1453], set)
            assert "ych" in new_watcher.blocklists[1453]
        finally:
            SubscriptionWatcher.FILENAME = old_filename
            os.remove(test_watcher_file)
    def test_save_to_json(self, bot):
        test_watcher_file = "./test_subscription_watcher.json"
        if os.path.exists(test_watcher_file):
            os.remove(test_watcher_file)
        api = MockExportAPI()
        latest_submissions = [
            SubmissionBuilder(submission_id="123243").build_short_submission(),
            SubmissionBuilder(submission_id="123242").build_short_submission(),
            SubmissionBuilder(submission_id="123240").build_short_submission()
        ]
        subscription1 = Subscription("query", 1234)
        subscription2 = Subscription("example", 5678)
        watcher = SubscriptionWatcher(api, bot)
        watcher._update_latest_ids(latest_submissions)
        watcher.subscriptions.add(subscription1)
        watcher.subscriptions.add(subscription2)
        watcher.blocklists[3452] = {"test", "example"}
        watcher.blocklists[1453] = {"ych"}
        watcher.FILENAME = test_watcher_file

        try:
            watcher.save_to_json()

            assert os.path.exists(test_watcher_file)
            with open(test_watcher_file, "r") as f:
                data = json.load(f)
            assert data is not None
            assert len(data['latest_ids']) == 3
            assert "123240" in data['latest_ids']
            assert "123242" in data['latest_ids']
            assert "123243" in data['latest_ids']
            assert len(data['subscriptions']) == 2
            if data['subscriptions'][0]['query'] == "query":
                assert data['subscriptions'][0]['destination'] == 1234
                assert data['subscriptions'][1]['query'] == "example"
                assert data['subscriptions'][1]['destination'] == 5678
            else:
                assert data['subscriptions'][0]['query'] == "example"
                assert data['subscriptions'][0]['destination'] == 5678
                assert data['subscriptions'][1]['query'] == "query"
                assert data['subscriptions'][1]['destination'] == 1234
            assert len(data["blacklists"]) == 2
            assert "3452" in data["blacklists"]
            assert len(data["blacklists"]["3452"]) == 2
            assert isinstance(data["blacklists"]["3452"], list)
            assert "test" in data["blacklists"]["3452"]
            assert "example" in data["blacklists"]["3452"]
            assert "1453" in data["blacklists"]
            assert len(data["blacklists"]["1453"]) == 1
            assert isinstance(data["blacklists"]["1453"], list)
            assert data["blacklists"]["1453"] == ["ych"]
        finally:
            os.remove(test_watcher_file)
    def test_get_search_results_specified_page(self, r):
        builder = SubmissionBuilder()
        search = "deer"
        api = FAExportAPI("http://example.com/")
        r.get(
            f"http://example.com/search.json?full=1&perpage=48&q={search}&page=2",
            json=[builder.build_search_json()])

        results = api.get_search_results(search, 2)

        assert len(results) == 1
        assert isinstance(results[0], FASubmissionShort)
        assert results[0].submission_id == builder.submission_id
    def test_pdf_submission(self, bot):
        submission = SubmissionBuilder(file_ext="gif", file_size=47453).build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        submission.send_message(bot, chat_id, message_id)

        bot.send_photo.assert_not_called()
        bot.send_document.assert_called_once()
        assert bot.send_document.call_args[1]['chat_id'] == chat_id
        assert bot.send_document.call_args[1]['document'] == submission.download_url
        assert bot.send_document.call_args[1]['caption'] == submission.link
        assert bot.send_document.call_args[1]['reply_to_message_id'] == message_id
    def test_send_message__without_prefix(self, bot):
        submission = SubmissionBuilder(file_ext="jpg", file_size=FASubmission.SIZE_LIMIT_IMAGE - 1)\
            .build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        submission.send_message(bot, chat_id, message_id)

        bot.send_photo.assert_called_once()
        assert bot.send_photo.call_args[1]['chat_id'] == chat_id
        assert bot.send_photo.call_args[1]['photo'] == submission.download_url
        assert bot.send_photo.call_args[1]['caption'] == submission.link
        assert bot.send_photo.call_args[1]['reply_to_message_id'] == message_id
Esempio n. 10
0
    def test_get_search_results_with_extended_modifiers(self, r):
        builder = SubmissionBuilder()
        search = "(deer & !lion) | (goat & !tiger)"
        search_safe = "(deer%20&%20!lion)%20%7C%20(goat%20&%20!tiger)"
        api = FAExportAPI("http://example.com/")
        r.get(
            f"http://example.com/search.json?full=1&perpage=48&q={search_safe}&page=1",
            json=[builder.build_search_json()])

        results = api.get_search_results(search)

        assert len(results) == 1
        assert isinstance(results[0], FASubmissionShort)
        assert results[0].submission_id == builder.submission_id
Esempio n. 11
0
    def test_get_user_folder_specified_page(self, r):
        builder = SubmissionBuilder()
        username = "******"
        api = FAExportAPI("http://example.com/")
        r.get(f"http://example.com/user/{username}/gallery.json?page=2&full=1",
              json=[builder.build_search_json()])

        results = api.get_user_folder(username, "gallery", 2)

        assert len(results) == 1
        assert isinstance(results[0], FASubmissionShort)
        assert results[0].submission_id == builder.submission_id
        assert results[0].link == builder.link
        assert results[0].thumbnail_url == builder.thumbnail_url
Esempio n. 12
0
    def test_image_over_document_size_limit(self, bot):
        submission = SubmissionBuilder(file_ext="jpg", file_size=FASubmission.SIZE_LIMIT_DOCUMENT + 1)\
            .build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        submission.send_message(bot, chat_id, message_id)

        bot.send_photo.assert_called_once()
        assert bot.send_photo.call_args[1]['chat_id'] == chat_id
        assert bot.send_photo.call_args[1]['photo'] == submission.thumbnail_url
        assert bot.send_photo.call_args[1]['caption'] == \
            f"{submission.link}\n[Direct download]({submission.download_url})"
        assert bot.send_photo.call_args[1]['reply_to_message_id'] == message_id
        assert bot.send_photo.call_args[1]['parse_mode'] == telegram.ParseMode.MARKDOWN
Esempio n. 13
0
    def test_get_full_submission(self, r):
        builder = SubmissionBuilder(thumb_size=300)
        api = FAExportAPI("http://example.com/")
        r.get(f"http://example.com/submission/{builder.submission_id}.json",
              json=builder.build_submission_json())

        submission = api.get_full_submission(builder.submission_id)

        assert isinstance(submission, FASubmissionFull)
        assert submission.submission_id == builder.submission_id
        assert submission.link == builder.link
        assert submission.thumbnail_url == builder.thumbnail_url.replace(
            "@300-", "@1600-")
        assert submission.download_url == builder.download_url
        assert submission.full_image_url == builder.download_url
Esempio n. 14
0
    def test_auto_doc_just_under_size_limit(self, bot):
        submission = SubmissionBuilder(file_ext="gif", file_size=FASubmission.SIZE_LIMIT_DOCUMENT - 1)\
            .build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        submission.send_message(bot, chat_id, message_id)

        bot.send_document.assert_called_once()
        bot.send_photo.assert_not_called()
        bot.send_message.assert_not_called()
        assert bot.send_document.call_args[1]['chat_id'] == chat_id
        assert bot.send_document.call_args[1]['document'] == submission.download_url
        assert bot.send_document.call_args[1]['caption'] == submission.link
        assert bot.send_document.call_args[1]['reply_to_message_id'] == message_id
Esempio n. 15
0
    def test_get_user_folder_awkward_characters(self, r):
        builder = SubmissionBuilder()
        username = "******"
        safe_username = "******"
        api = FAExportAPI("http://example.com/")
        r.get(
            f"http://example.com/user/{safe_username}/gallery.json?page=1&full=1",
            json=[builder.build_search_json()])

        results = api.get_user_folder(username, "gallery")

        assert len(results) == 1
        assert isinstance(results[0], FASubmissionShort)
        assert results[0].submission_id == builder.submission_id
        assert results[0].link == builder.link
        assert results[0].thumbnail_url == builder.thumbnail_url
Esempio n. 16
0
    def test_create_from_short_dict(self):
        builder = SubmissionBuilder()

        submission = FASubmission.from_short_dict(
            builder.build_search_json()
        )

        assert isinstance(submission, FASubmissionShort)
        assert submission.submission_id == builder.submission_id
        assert submission.link == builder.link

        assert submission.thumbnail_url == builder.thumbnail_url
        assert submission.title == builder.title
        assert submission.author.profile_name == builder.author.profile_name
        assert submission.author.name == builder.author.name
        assert submission.author.link == builder.author.link
Esempio n. 17
0
    def test_txt_submission(self, bot):
        submission = SubmissionBuilder(file_ext="txt").build_full_submission()
        chat_id = -9327622
        message_id = 2873292

        submission.send_message(bot, chat_id, message_id)

        bot.send_message.assert_not_called()
        bot.send_photo.assert_called_once()
        bot.send_document.assert_not_called()
        bot.send_audio.assert_not_called()
        assert bot.send_photo.call_args[1]['chat_id'] == chat_id
        assert bot.send_photo.call_args[1]['photo'] == submission.full_image_url
        assert bot.send_photo.call_args[1]['caption'] == \
            f"{submission.link}\n[Direct download]({submission.download_url})"
        assert bot.send_photo.call_args[1]['reply_to_message_id'] == message_id
        assert bot.send_photo.call_args[1]['parse_mode'] == telegram.ParseMode.MARKDOWN
    def test_send_update__updates_latest(self, bot):
        api = MockExportAPI()
        watcher = SubscriptionWatcher(api, bot)
        subscription = Subscription("test", 12345)
        submission = SubmissionBuilder().build_mock_submission()

        watcher._send_update(subscription, submission)

        assert subscription.latest_update is not None
Esempio n. 19
0
def test_matches_not_negative_rating():
    query = "deer -rating:general"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Deer plays in woods",
        rating=Rating.GENERAL).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert not match
Esempio n. 20
0
def test_matches_not_rating():
    query = "deer rating:general"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Deer 'plays' in woods",
        rating=Rating.ADULT).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert not match
Esempio n. 21
0
def test_matches_result__two_words_in_description_matches():
    query = "example submission"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="test",
        description="this submission is just an example",
        keywords=["example", "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert match
Esempio n. 22
0
def test_matches_result__substring_in_keywords_no_match():
    query = "keyword"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="test submission",
        description="this submission is just an example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert not match
Esempio n. 23
0
def test_matches_word_in_tag():
    query = "deer"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is <b>deer</b>",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert match
Esempio n. 24
0
def test_matches_result__doesnt_match_blocklisted_tag():
    query = "an-example"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is just an-example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, {"test"})

    assert not match
Esempio n. 25
0
def test_matches_result__matches_query_with_hyphen():
    query = "an-example"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is just an-example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert match
Esempio n. 26
0
def test_matches_result__does_not_match_query_with_applicable_negated_query():
    query = "test -example"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is just an example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert not match
Esempio n. 27
0
def test_matches_result__does_not_match_negated_query_case_insensitive():
    query = "-test"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="Test submission",
        description="this submission is just an example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert not match
Esempio n. 28
0
def test_matches_result__case_insensitive_query():
    query = "SUBMISSION"
    subscription = Subscription(query, 12432)
    submission = SubmissionBuilder(
        title="test submission",
        description="this submission is just an example",
        keywords=["example", "submission",
                  "keywords"]).build_full_submission()

    match = subscription.matches_result(submission, set())

    assert match
Esempio n. 29
0
    def test_create_from_full_dict(self):
        builder = SubmissionBuilder()

        submission = FASubmission.from_full_dict(
            builder.build_submission_json()
        )

        assert isinstance(submission, FASubmissionFull)
        assert submission.submission_id == builder.submission_id
        assert submission.link == builder.link

        assert submission.thumbnail_url == builder.thumbnail_url
        assert submission.title == builder.title
        assert submission.author.profile_name == builder.author.profile_name
        assert submission.author.name == builder.author.name
        assert submission.author.link == builder.author.link

        assert submission.download_url == builder.download_url
        assert submission.full_image_url == builder.full_image_url
        assert submission.description == builder.description
        assert submission.keywords == builder.keywords
Esempio n. 30
0
def test_matches_explicit_rating():
    query1 = "deer rating:adult"
    query2 = "deer rating:explicit"
    subscription1 = Subscription(query1, 12432)
    subscription2 = Subscription(query2, 12432)
    submission = SubmissionBuilder(
        title="Deer plays in woods",
        rating=Rating.ADULT).build_full_submission()

    match1 = subscription1.matches_result(submission, set())
    match2 = subscription2.matches_result(submission, set())

    assert match1
    assert match2