def test_process_reddit_post():
    post = utils.reddit_post()
    messages = services.process_reddit_post(post)

    assert len(messages) == 2
    assert messages[0]['type'] == 'message'
    assert messages[1]['type'] == 'photo'
async def test_process_post_imgur():
    post = utils.reddit_post(domain='imgur.com')

    with mock.patch('newsbot.services.process_imgur_post') as process_imgur_post_mock:
        process_imgur_post_mock.side_effect = utils.make_coro()
        messages = await services.process_post(post)
        process_imgur_post_mock.assert_called_with(post)
Exemple #3
0
async def test_pagination(reddit_session):
    page1_posts = [utils.reddit_post()]
    page2_posts = [utils.reddit_post()]
    page1_response, subreddit, charts, url = utils.default_reddit_response(
        reddit_session, *page1_posts)
    page2_response, _, _, _ = utils.default_reddit_response(
        reddit_session, *page2_posts)

    with aiohttp_utils.mock_session([page1_response, page2_response],
                                    reddit_session) as mocked_session:
        reddit_posts = await utils.list_async_gen(
            mocked_session.get_posts(subreddit, charts=charts, pages=2))
        assert len(reddit_posts) == len(page1_posts + page2_posts)
        assert reddit_posts[0] == page1_posts[0]
        assert reddit_posts[1] == page2_posts[0]

        assert reddit_session.mock.call_args_list[1] == \
               mock.call('GET', url, allow_redirects=True, params={'after': page1_posts[-1]['id']})
async def test_process_imgur_post_image_unknown_type():
    post = utils.reddit_post(url='http://imgur.com/test')

    images = [utils.imgur_image(type='image/ktulhu', description='Undescriptible horror')]

    with mock.patch.object(connections.get_imgur_session(), 'get_imgur_images') as patch:
        patch.side_effect = utils.make_async_gen(*images)
        messages = await services.process_imgur_post(post)
        patch.assert_called_with(post['url'])

    assert len(messages) == 2
    assert messages[0]['type'] == 'message'
    assert messages[1]['type'] == 'message'
    assert messages[1]['params']['text'] == images[0]['link']
async def test_process_imgur_post_image_gif_video():
    post = utils.reddit_post(url='http://imgur.com/test')

    images = [utils.imgur_image(type='image/gif', mp4='http://example.com/video.mp4')]

    with mock.patch.object(connections.get_imgur_session(), 'get_imgur_images') as patch:
        patch.side_effect = utils.make_async_gen(*images)
        messages = await services.process_imgur_post(post)
        patch.assert_called_with(post['url'])

    assert len(messages) == 2
    assert messages[0]['type'] == 'message'
    assert messages[1]['type'] == 'video'
    assert messages[1]['params']['video'] == images[0]['mp4']
async def test_process_imgur_post_image_jpeg():
    post = utils.reddit_post(url='http://imgur.com/test')

    images = [utils.imgur_image()]

    with mock.patch.object(connections.get_imgur_session(), 'get_imgur_images') as patch:
        patch.side_effect = utils.make_async_gen(*images)
        messages = await services.process_imgur_post(post)
        patch.assert_called_with(post['url'])

    assert len(messages) == 2
    assert messages[0]['type'] == 'message'
    assert messages[1]['type'] == 'photo'
    assert messages[1]['params']['photo'] == images[0]['link']
async def test_process_posts():
    posts = [utils.reddit_post()]
    posts_queue = MockQueue(list(map(json.dumps, posts)))
    messages_queue = MockQueue()

    with mock.patch('newsbot.queues.posts_queue') as posts_queue_patch:
        posts_queue_patch.side_effect = utils.make_coro(posts_queue)
        with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch:
            messages_queue_patch.side_effect = utils.make_coro(messages_queue)

            await services.process_posts()

    assert not len(posts_queue.queue)
    assert len(messages_queue.queue)
async def test_process_messages_too_many_requests():
    post = utils.reddit_post()
    messages = services.process_reddit_post(post)
    messages_queue = MockQueue([json.dumps(messages)])

    telegram_session = connections.get_telegram_session()

    with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch:
        messages_queue_patch.side_effect = utils.make_coro(messages_queue)

        with mock.patch.object(telegram_session, 'process_message') as process_message_mock:
            process_message_mock.side_effect = utils.make_coro(connections.TelegramTooManyRequests())
            await services.process_messages()

    assert await messages_queue.size() == 1
    assert await messages_queue.get() == messages
async def test_process_messages():
    post = utils.reddit_post()
    messages = services.process_reddit_post(post)
    messages_queue = MockQueue([json.dumps(messages)])

    telegram_session = connections.get_telegram_session()

    with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch:
        messages_queue_patch.side_effect = utils.make_coro(messages_queue)

        with mock.patch.object(telegram_session, 'process_message') as process_message_mock:
            process_message_mock.side_effect = utils.make_coro({'ok': True})

            await services.process_messages()

    assert not len(messages_queue.queue)
async def test_gather_posts(reddit_session):
    posts = [utils.reddit_post()]
    posts_queue = MockQueue()
    settings.CONFIG['subreddits'] = {'/r/test': {}}

    with mock.patch('newsbot.queues.posts_queue') as posts_queue_patch:
        posts_queue_patch.side_effect = utils.make_coro(posts_queue)

        with mock.patch.object(reddit_session, 'get_posts') as get_posts_mock:
            get_posts_mock.side_effect = utils.make_async_gen(*posts)

            with mock.patch('newsbot.services.filter_post') as filter_post_mock:
                filter_post_mock.side_effect = utils.make_coro(False)

                with mock.patch('newsbot.connections._reddit_session', new=reddit_session):
                    await services.gather_posts()

    assert len(posts_queue.queue) == len(posts)
async def test_process_post_stickied():
    post = utils.reddit_post(stickied=True)
    messages = await services.process_post(post)
    assert not messages
    assert messages == []
async def test_process_post_generic():
    post = utils.reddit_post(domain='gfycat.com', url='http://gfycat.com/test')

    with mock.patch('newsbot.services.process_generic_post') as process_generic_post_mock:
        messages = await services.process_post(post)
        process_generic_post_mock.assert_called_with(post)
async def test_process_post_reddit():
    post = utils.reddit_post(domain='reddituploads.com')

    with mock.patch('newsbot.services.process_reddit_post') as process_reddit_post_mock:
        messages = await services.process_post(post)
        process_reddit_post_mock.assert_called_with(post)