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_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_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_imgur_same_session():
    session = connections.get_imgur_session()
    assert connections.get_imgur_session() is session
async def process_imgur_post(post):
    imgur_session = connections.get_imgur_session()
    text = get_caption(post) + '\n' + post['url']

    messages = [
        {
            'type': 'message',
            'params': {
                'text': text,
                'disable_web_page_preview': True,
                'parse_mode': 'HTML'
            }
        },
    ]

    n = 0

    # noinspection PyTypeChecker
    async for image in imgur_session.get_imgur_images(post['url']):
        if not image:
            continue
        n += 1
        if n > 20:
            messages.append({
                'type': 'message',
                'params': {
                    'text': 'More images in album!'
                }
            })
            break
        kwargs = {}
        if image.get('description', None):
            kwargs['caption'] = image['description'][:200]
        if image['type'] == 'image/jpeg':
            messages.append({
                'type': 'photo',
                'params': {
                    'photo': image['link'],
                    **kwargs
                }
            })
        elif image['type'] == 'image/png':
            messages.append({
                'type': 'photo',
                'params': {
                    'photo': image['link'],
                    **kwargs
                }
            })
        elif image['type'] == 'image/gif':
            if 'mp4' in image:
                messages.append({
                    'type': 'video',
                    'params': {
                        'video': image['mp4'],
                        **kwargs
                    }
                })
            else:
                messages.append({
                    'type': 'document',
                    'params': {
                        'document': image['link'],
                        **kwargs
                    }
                })
        else:
            messages.append({
                'type': 'message',
                'params': {
                    'text': image['link'],
                    **kwargs
                }
            })

    return messages