def test_image_album_refresh_error(app, error, is_album):
    """Test Image.refresh() and Album.refresh() with bad JSON.

    Asserting line number of: parsed = response.json()
    Asserting line number of: return self._parse(response['data'])

    :param app: conftest fixture.
    :param str error: Error to test for.
    :param bool is_album: Test Album instead of Image.
    """
    url = API_URL.format(type='album' if is_album else 'image', id='imgur_id')
    if error == 'not json':
        body = '<html></html>'
    elif error == 'no data':
        body = '{"success":true}'
    else:
        body = '{"success":true, "data":{"id":"imgur_id"}}'
    httpretty.register_uri(httpretty.GET, url, body=body)

    # Test.
    instance = Album('imgur_id') if is_album else Image('imgur_id')
    with pytest.raises(APIError):
        instance.refresh(app, 'client_id', 30)

    # Verify log.
    if error == 'not json':
        assert app.messages[-1][:2] == ['warn', 'failed to parse JSON from {}'.format(url)]
        assert re.search(r'sphinxcontrib[/\\]imgur[/\\]imgur_api\.pyc?:60$', app.messages[-1][2])
    elif error == 'no data':
        assert app.messages[-1][:2] == ['warn', "unexpected JSON for imgur_id: KeyError('data',)"]
        assert re.search(r'sphinxcontrib[/\\]imgur[/\\]imgur_api\.pyc?:135$', app.messages[-1][2])
    else:
        assert app.messages[-1][:2] == ['warn', "unexpected JSON for imgur_id: KeyError('description',)"]
        assert re.search(r'sphinxcontrib[/\\]imgur[/\\]imgur_api\.pyc?:135$', app.messages[-1][2])
def test_album_minor_error(app):
    """Test Album.refresh() with bad images JSON.

    Asserting line number of: return self._parse(response['data'])

    :param app: conftest fixture.
    """
    url = API_URL.format(type='album', id='imgur_id')
    body = ('{"success":true, "data":{"id":"imgur_id", "cover": "imgur_id", "title": null, "description": null, '
            '"in_gallery": false, "images": [{}]}}')
    httpretty.register_uri(httpretty.GET, url, body=body)

    # Test.
    instance = Album('imgur_id')
    with pytest.raises(APIError):
        instance.refresh(app, 'client_id', 30)

    # Verify log.
    assert app.messages[-1][:2] == ['warn', "unexpected JSON for imgur_id: KeyError('id',)"]
    assert re.search(r'sphinxcontrib[/\\]imgur[/\\]imgur_api\.pyc?:135$', app.messages[-1][2])
def test_image_album_refresh_ttl(app, is_album):
    """Test Image.refresh() and Album.refresh() successfully with ttl.

    :param app: conftest fixture.
    :param bool is_album: Test Album instead of Image.
    """
    if is_album:
        imgur_id = 'VMlM6'
        title = 'Screenshots'
        description = 'Screenshots of my various devices.'
        line = ['debug2', 'Imgur ID VMlM6 still has 30 seconds before needing refresh. Skipping.']
    else:
        imgur_id = '2QcXR3R'
        title = None
        description = None
        line = ['debug2', 'Imgur ID 2QcXR3R still has 30 seconds before needing refresh. Skipping.']

    # Test.
    instance = Album(imgur_id) if is_album else Image(imgur_id)
    instance.refresh(app, 'client_id', 30)

    # Verify instance.
    assert instance.imgur_id == imgur_id
    assert instance.title == title
    assert instance.description == description
    assert instance.in_gallery is False
    if is_album:
        assert instance.cover_id == '2QcXR3R'
        assert instance.image_ids == ['2QcXR3R', 'Hqw7KHM']
        assert '2QcXR3R' in instance
        assert 'Hqw7KHM' in instance
        assert 'abc123' not in instance
    else:
        assert instance.type == 'image/png'
        assert instance.width == 3072
        assert instance.height == 1280

    # Verify log.
    assert line not in app.messages

    # Test again.
    instance.refresh(app, 'client_id', 30)

    # Verify instance.
    assert instance.imgur_id == imgur_id
    assert instance.title == title
    assert instance.description == description
    assert instance.in_gallery is False
    if is_album:
        assert instance.cover_id == '2QcXR3R'
        assert instance.image_ids == ['2QcXR3R', 'Hqw7KHM']
        assert '2QcXR3R' in instance
        assert Image('Hqw7KHM') in instance
        assert 'abc123' not in instance
    else:
        assert instance.type == 'image/png'
        assert instance.width == 3072
        assert instance.height == 1280

    # Verify log.
    assert line in app.messages