aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_api.py
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tests/test_api.py')
-rw-r--r--mediagoblin/tests/test_api.py164
1 files changed, 138 insertions, 26 deletions
diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py
index 93e82f18..83003875 100644
--- a/mediagoblin/tests/test_api.py
+++ b/mediagoblin/tests/test_api.py
@@ -15,7 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
-import mock
+try:
+ import mock
+except ImportError:
+ import unittest.mock as mock
import pytest
from webtest import AppError
@@ -23,6 +26,7 @@ from webtest import AppError
from .resources import GOOD_JPG
from mediagoblin import mg_globals
from mediagoblin.db.models import User, MediaEntry, MediaComment
+from mediagoblin.tools.routing import extract_url_arguments
from mediagoblin.tests.tools import fixture_add_user
from mediagoblin.moderation.tools import take_away_privileges
@@ -55,7 +59,7 @@ class TestAPI(object):
headers=headers
)
- return response, json.loads(response.body)
+ return response, json.loads(response.body.decode())
def _upload_image(self, test_app, image):
""" Uploads and image to MediaGoblin via pump.io API """
@@ -72,7 +76,7 @@ class TestAPI(object):
data,
headers=headers
)
- image = json.loads(response.body)
+ image = json.loads(response.body.decode())
return response, image
@@ -142,7 +146,7 @@ class TestAPI(object):
headers=headers
)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_unable_to_post_feed_as_someone_else(self, test_app):
""" Tests that can't post an image to someone else's feed """
@@ -165,7 +169,7 @@ class TestAPI(object):
headers=headers
)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_only_able_to_update_own_image(self, test_app):
""" Test's that the uploader is the only person who can update an image """
@@ -184,7 +188,8 @@ class TestAPI(object):
# Lets change the image uploader to be self.other_user, this is easier
# than uploading the image as someone else as the way self.mocked_oauth_required
# and self._upload_image.
- media = MediaEntry.query.filter_by(id=data["object"]["id"]).first()
+ id = int(data["object"]["id"].split("/")[-2])
+ media = MediaEntry.query.filter_by(id=id).first()
media.uploader = self.other_user.id
media.save()
@@ -197,7 +202,7 @@ class TestAPI(object):
headers=headers
)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_upload_image_with_filename(self, test_app):
""" Tests that you can upload an image with filename and description """
@@ -224,16 +229,17 @@ class TestAPI(object):
headers={"Content-Type": "application/json"}
)
- image = json.loads(response.body)["object"]
+ image = json.loads(response.body.decode())["object"]
# Check everything has been set on the media correctly
- media = MediaEntry.query.filter_by(id=image["id"]).first()
+ id = int(image["id"].split("/")[-2])
+ media = MediaEntry.query.filter_by(id=id).first()
assert media.title == title
assert media.description == description
assert media.license == license
# Check we're being given back everything we should on an update
- assert image["id"] == media.id
+ assert int(image["id"].split("/")[-2]) == media.id
assert image["displayName"] == title
assert image["content"] == description
assert image["license"] == license
@@ -260,7 +266,7 @@ class TestAPI(object):
)
# Assert that we've got a 403
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_object_endpoint(self, test_app):
""" Tests that object can be looked up at endpoint """
@@ -281,11 +287,11 @@ class TestAPI(object):
with self.mock_oauth():
request = test_app.get(object_uri)
- image = json.loads(request.body)
- entry = MediaEntry.query.filter_by(id=image["id"]).first()
+ image = json.loads(request.body.decode())
+ entry_id = int(image["id"].split("/")[-2])
+ entry = MediaEntry.query.filter_by(id=entry_id).first()
assert request.status_code == 200
- assert entry.id == image["id"]
assert "image" in image
assert "fullImage" in image
@@ -313,7 +319,8 @@ class TestAPI(object):
assert response.status_code == 200
# Find the objects in the database
- media = MediaEntry.query.filter_by(id=data["object"]["id"]).first()
+ media_id = int(data["object"]["id"].split("/")[-2])
+ media = MediaEntry.query.filter_by(id=media_id).first()
comment = media.get_comments()[0]
# Tests that it matches in the database
@@ -321,7 +328,6 @@ class TestAPI(object):
assert comment.content == content
# Test that the response is what we should be given
- assert comment.id == comment_data["object"]["id"]
assert comment.content == comment_data["object"]["content"]
def test_unable_to_post_comment_as_someone_else(self, test_app):
@@ -351,7 +357,7 @@ class TestAPI(object):
headers=headers
)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_unable_to_update_someone_elses_comment(self, test_app):
""" Test that you're able to update someoen elses comment. """
@@ -376,7 +382,7 @@ class TestAPI(object):
response, comment_data = self._activity_to_feed(test_app, activity)
# change who uploaded the comment as it's easier than changing
- comment_id = comment_data["object"]["id"]
+ comment_id = int(comment_data["object"]["id"].split("/")[-2])
comment = MediaComment.query.filter_by(id=comment_id).first()
comment.author = self.other_user.id
comment.save()
@@ -396,14 +402,14 @@ class TestAPI(object):
headers=headers
)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
def test_profile(self, test_app):
""" Tests profile endpoint """
uri = "/api/user/{0}/profile".format(self.user.username)
with self.mock_oauth():
response = test_app.get(uri)
- profile = json.loads(response.body)
+ profile = json.loads(response.body.decode())
assert response.status_code == 200
@@ -417,7 +423,7 @@ class TestAPI(object):
uri = "/api/user/{0}/".format(self.user.username)
with self.mock_oauth():
response = test_app.get(uri)
- user = json.loads(response.body)
+ user = json.loads(response.body.decode())
assert response.status_code == 200
@@ -433,7 +439,7 @@ class TestAPI(object):
with pytest.raises(AppError) as excinfo:
response = test_app.get("/api/whoami")
- assert "401 UNAUTHORIZED" in excinfo.value.message
+ assert "401 UNAUTHORIZED" in excinfo.value.args[0]
def test_read_feed(self, test_app):
""" Test able to read objects from the feed """
@@ -443,7 +449,7 @@ class TestAPI(object):
uri = "/api/user/{0}/feed".format(self.active_user.username)
with self.mock_oauth():
response = test_app.get(uri)
- feed = json.loads(response.body)
+ feed = json.loads(response.body.decode())
assert response.status_code == 200
@@ -468,9 +474,9 @@ class TestAPI(object):
with pytest.raises(AppError) as excinfo:
self._post_image_to_feed(test_app, data)
- assert "403 FORBIDDEN" in excinfo.value.message
+ assert "403 FORBIDDEN" in excinfo.value.args[0]
- def test_object_endpoint(self, test_app):
+ def test_object_endpoint_requestable(self, test_app):
""" Test that object endpoint can be requested """
response, data = self._upload_image(test_app, GOOD_JPG)
response, data = self._post_image_to_feed(test_app, data)
@@ -478,7 +484,7 @@ class TestAPI(object):
with self.mock_oauth():
response = test_app.get(data["object"]["links"]["self"]["href"])
- data = json.loads(response.body)
+ data = json.loads(response.body.decode())
assert response.status_code == 200
@@ -486,3 +492,109 @@ class TestAPI(object):
assert "url" in data
assert "links" in data
assert data["objectType"] == "image"
+
+ def test_delete_media_by_activity(self, test_app):
+ """ Test that an image can be deleted by a delete activity to feed """
+ response, data = self._upload_image(test_app, GOOD_JPG)
+ response, data = self._post_image_to_feed(test_app, data)
+ object_id = data["object"]["id"]
+
+ activity = {
+ "verb": "delete",
+ "object": {
+ "id": object_id,
+ "objectType": "image",
+ }
+ }
+
+ response = self._activity_to_feed(test_app, activity)[1]
+
+ # Check the media is no longer in the database
+ media_id = int(object_id.split("/")[-2])
+ media = MediaEntry.query.filter_by(id=media_id).first()
+
+ assert media is None
+
+ # Check we've been given the full delete activity back
+ assert "id" in response
+ assert response["verb"] == "delete"
+ assert "object" in response
+ assert response["object"]["id"] == object_id
+ assert response["object"]["objectType"] == "image"
+
+ def test_delete_comment_by_activity(self, test_app):
+ """ Test that a comment is deleted by a delete activity to feed """
+ # First upload an image to comment against
+ response, data = self._upload_image(test_app, GOOD_JPG)
+ response, data = self._post_image_to_feed(test_app, data)
+
+ # Post a comment to delete
+ activity = {
+ "verb": "post",
+ "object": {
+ "objectType": "comment",
+ "content": "This is a comment.",
+ "inReplyTo": data["object"],
+ }
+ }
+
+ comment = self._activity_to_feed(test_app, activity)[1]
+
+ # Now delete the image
+ activity = {
+ "verb": "delete",
+ "object": {
+ "id": comment["object"]["id"],
+ "objectType": "comment",
+ }
+ }
+
+ delete = self._activity_to_feed(test_app, activity)[1]
+
+ # Verify the comment no longer exists
+ comment_id = int(comment["object"]["id"].split("/")[-2])
+ assert MediaComment.query.filter_by(id=comment_id).first() is None
+
+ # Check we've got a delete activity back
+ assert "id" in delete
+ assert delete["verb"] == "delete"
+ assert "object" in delete
+ assert delete["object"]["id"] == comment["object"]["id"]
+ assert delete["object"]["objectType"] == "comment"
+
+ def test_edit_comment(self, test_app):
+ """ Test that someone can update their own comment """
+ # First upload an image to comment against
+ response, data = self._upload_image(test_app, GOOD_JPG)
+ response, data = self._post_image_to_feed(test_app, data)
+
+ # Post a comment to edit
+ activity = {
+ "verb": "post",
+ "object": {
+ "objectType": "comment",
+ "content": "This is a comment",
+ "inReplyTo": data["object"],
+ }
+ }
+
+ comment = self._activity_to_feed(test_app, activity)[1]
+
+ # Now create an update activity to change the content
+ activity = {
+ "verb": "update",
+ "object": {
+ "id": comment["object"]["id"],
+ "content": "This is my fancy new content string!",
+ "objectType": "comment",
+ },
+ }
+
+ comment = self._activity_to_feed(test_app, activity)[1]
+
+ # Verify the comment reflects the changes
+ comment_id = int(comment["object"]["id"].split("/")[-2])
+ model = MediaComment.query.filter_by(id=comment_id).first()
+
+ assert model.content == activity["object"]["content"]
+