Add upload/replace from URL.

This commit is contained in:
sneakypete81 2013-09-12 18:26:51 +01:00
parent f1a9018918
commit 35b9a4d044
4 changed files with 109 additions and 10 deletions

View file

@ -101,6 +101,23 @@ class TestPhotos(test_base.TestBase):
self.photos = self.client.photos.list()
self.assertEqual(len(self.photos), 3)
def test_upload_from_url(self):
""" Ensure that a photo can be imported from a URL """
# Make an existing photo public
self.photos[0].update(permission=True)
# Upload a duplicate of an existing photo
self.client.photo.upload_from_url(self.photos[0].pathDownload,
allowDuplicate=True)
# Check there are now four photos
photos = self.client.photos.list()
self.assertEqual(len(photos), 4)
# Check that the new one is a duplicate
self.assertEqual(photos[0].hash, photos[1].hash)
# Put the environment back the way we found it
photos[1].delete()
self.photos[0].update(permission=False)
def test_update(self):
""" Update a photo by editing the title """
title = "\xfcmlaut" # umlauted umlaut
@ -178,16 +195,11 @@ class TestPhotos(test_base.TestBase):
allowDuplicate=True)
# Check that its new hash is correct
self.assertEqual(self.photos[0].hash, self.photos[1].hash)
# Put it back
self.photos[0].replace("tests/data/test_photo1.jpg",
allowDuplicate=True)
# Put it back using base64 encoding
self.photos[0].replace_encoded("tests/data/test_photo1.jpg",
allowDuplicate=True)
self.assertEqual(self.photos[0].hash, original_hash)
def test_replace_encoded(self):
""" If photo.replace_encoded gets implemented, write a test! """
with self.assertRaises(NotImplementedError):
self.client.photo.replace_encoded(None, None)
def test_dynamic_url(self):
""" If photo.dynamic_url gets implemented, write a test! """
with self.assertRaises(NotImplementedError):

View file

@ -233,6 +233,7 @@ class TestPhotoReplace(TestPhotos):
self.assertEqual(self.test_photos[1].get_fields(),
self.test_photos_dict[0])
class TestPhotoReplaceEncoded(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_replace_encoded(self, mock_post):
"""
@ -282,6 +283,50 @@ class TestPhotoReplace(TestPhotos):
self.assertEqual(self.test_photos[1].get_fields(),
self.test_photos_dict[0])
class TestPhotoReplaceFromUrl(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_replace_from_url(self, mock_post):
"""
Check that a photo can be imported from a url to
replace an existing photo.
"""
mock_post.return_value = self._return_value(self.test_photos_dict[0])
result = self.client.photo.replace_from_url(self.test_photos[1],
"test_url", title="Test")
mock_post.assert_called_with("/photo/%s/replace.json"
% self.test_photos[1].id,
photo="test_url", title="Test")
self.assertEqual(result.get_fields(), self.test_photos_dict[0])
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_id_replace_from_url(self, mock_post):
"""
Check that a photo can be imported from a url to
replace an existing photo using its ID.
"""
mock_post.return_value = self._return_value(self.test_photos_dict[0])
result = self.client.photo.replace_from_url(self.test_photos[1].id,
"test_url", title="Test")
mock_post.assert_called_with("/photo/%s/replace.json"
% self.test_photos[1].id,
photo="test_url", title="Test")
self.assertEqual(result.get_fields(), self.test_photos_dict[0])
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_object_replace_from_url(self, mock_post):
"""
Check that a photo can be imported from a url to
replace an existing photo when using the Photo object directly.
"""
photo_id = self.test_photos[1].id
mock_post.return_value = self._return_value(self.test_photos_dict[0])
self.test_photos[1].replace_from_url("test_url", title="Test")
mock_post.assert_called_with("/photo/%s/replace.json"
% photo_id,
photo="test_url", title="Test")
self.assertEqual(self.test_photos[1].get_fields(),
self.test_photos_dict[0])
class TestPhotoUpdate(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_update(self, mock_post):
@ -357,6 +402,7 @@ class TestPhotoUpload(TestPhotos):
self.assertIn("photo", files)
self.assertEqual(result.get_fields(), self.test_photos_dict[0])
class TestPhotoUploadEncoded(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_upload_encoded(self, mock_post):
"""Check that a photo can be uploaded using Base64 encoding"""
@ -368,6 +414,18 @@ class TestPhotoUpload(TestPhotos):
photo=encoded_file, title="Test")
self.assertEqual(result.get_fields(), self.test_photos_dict[0])
class TestPhotoUploadFromUrl(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'post')
def test_photo_upload_from_url(self, mock_post):
"""
Check that a photo can be imported from a url.
"""
mock_post.return_value = self._return_value(self.test_photos_dict[0])
result = self.client.photo.upload_from_url("test_url", title="Test")
mock_post.assert_called_with("/photo/upload.json",
photo="test_url", title="Test")
self.assertEqual(result.get_fields(), self.test_photos_dict[0])
class TestPhotoDynamicUrl(TestPhotos):
@mock.patch.object(trovebox.Trovebox, 'get')
def test_photo_dynamic_url(self, _):

View file

@ -110,7 +110,19 @@ class ApiPhoto(ApiBase):
**kwds)["result"]
return Photo(self._client, result)
# def replace_from_url(self, url, **kwds):
def replace_from_url(self, photo, url, **kwds):
"""
Endpoint: /photo/<id>replace.json
Import a photo from the specified URL to replace an existing
photo.
"""
result = self._client.post("/photo/%s/replace.json" %
self._extract_id(photo),
photo=url,
**kwds)["result"]
return Photo(self._client, result)
def update(self, photo, **kwds):
"""
@ -163,7 +175,15 @@ class ApiPhoto(ApiBase):
**kwds)["result"]
return Photo(self._client, result)
# def upload_from_url(self, url, **kwds):
def upload_from_url(self, url, **kwds):
"""
Endpoint: /photo/upload.json
Import a photo from the specified URL
"""
result = self._client.post("/photo/upload.json", photo=url,
**kwds)["result"]
return Photo(self._client, result)
def dynamic_url(self, photo, **kwds):
""" Not yet implemented """

View file

@ -49,6 +49,15 @@ class Photo(TroveboxObject):
**kwds)
self._replace_fields(result.get_fields())
def replace_from_url(self, url, **kwds):
"""
Endpoint: /photo/<id>replace.json
Import a photo from the specified URL to replace this photo.
"""
result = self._client.photo.replace_from_url(self, url, **kwds)
self._replace_fields(result.get_fields())
def update(self, **kwds):
"""
Endpoint: /photo/<id>/update.json