async def test_upload_image_returns_submission(client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.return_value = { 'files': [{ 'original_url': 'http://image_url', 'thumbnail_url': 'http://thumbnail_url', 'url': 'http://web_url', }] } sub = await g._upload_image('foo.jpg', 'mock filetuple', None) assert sub == Submission( filepath='foo.jpg', image_url='http://image_url', thumbnail_url='http://thumbnail_url', web_url='http://web_url', gallery_url=g.url, edit_url=g.edit_url, )
async def test_upload_image_makes_correct_upload_request(client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.return_value = { 'files': [{ 'original_url': 'http://image_url', 'thumbnail_url': 'http://thumbnail_url', 'url': 'http://web_url', }] } await g._upload_image('foo.jpg', 'mock filetuple', None) assert client.post.call_args_list == [ call( url=_const.PROCESS_URL, data={ 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd', 'content_type': _const.CONTENT_TYPES['family'], 'thumbnail_size': _const.THUMBNAIL_SIZES_KEEP_ASPECT[100], 'comments_enabled': '0', }, files={'files[]': 'mock filetuple'}, json=True, ) ]
def test_Gallery_property_edit_url(): g = Gallery() assert g.edit_url is None g._gallery_token = { 'token_id': 'mock token', 'token_secret': 'mock secret' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'mock csrf' assert g.edit_url == _const.EDIT_URL_FORMAT.format(**g._gallery_token)
def test_Gallery_property_comments_enabled(): assert isinstance(Gallery().comments_enabled, bool) g = Gallery(comments_enabled=True) assert g.comments_enabled is True g.comments_enabled = False assert g.comments_enabled is False g.comments_enabled = 1 assert g.comments_enabled is True g._gallery_token = 'mock token' g._client.headers[_const.CSRF_TOKEN_HEADER] = 'mock csrf' with pytest.raises(RuntimeError, match=r'^Gallery was already created$'): g.comments_enabled = False
async def test_upload_image_catches_exception_from_upload_request(client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.side_effect = ConnectionError('The Error') sub = await g._upload_image('foo.jpg', 'mock filetuple', None) assert sub == Submission(filepath='foo.jpg', error='The Error')
def test_Gallery_property_title(): assert Gallery().title is None g = Gallery(title='Foo, Bar, Baz') assert g.title == 'Foo, Bar, Baz' g.title = (1, 2, 3) assert g.title == '(1, 2, 3)' g.title = None assert g.title is None g._gallery_token = {'something': 'truthy'} g._client.headers[_const.CSRF_TOKEN_HEADER] = 'mock token' with pytest.raises(RuntimeError, match=r'^Gallery was already created$'): g.title = 'Foo'
async def test_upload_image_catches_unexpected_response( unexpected_response, exp_cause, exp_cause_msg, client): g = Gallery() g._gallery_token = { 'token_id': 'a', 'token_secret': 'b', 'gallery_id': 'c', 'gallery_secret': 'd' } g._client.headers[_const.CSRF_TOKEN_HEADER] = 'csrf_token' client.post.return_value = unexpected_response with pytest.raises( RuntimeError, match= rf'Unexpected response: {re.escape(repr(unexpected_response))}$' ) as exc_info: await g._upload_image('foo.jpg', 'mock filetuple', None) assert isinstance(exc_info.value.__cause__, exp_cause) assert str(exc_info.value.__cause__) == exp_cause_msg
def test_Gallery_property_url(): g = Gallery() assert g.url is None g._gallery_token = {'gallery_id': 'mock token'} g._client.headers[_const.CSRF_TOKEN_HEADER] = 'mock csrf' assert g.url == _const.GALLERY_URL_FORMAT.format(**g._gallery_token)