Fix #1077 - Fix updating comment via API and add test
authorJessica Tallon <jessica@megworld.co.uk>
Tue, 16 Dec 2014 12:05:18 +0000 (12:05 +0000)
committerJessica Tallon <jessica@megworld.co.uk>
Tue, 16 Dec 2014 12:05:18 +0000 (12:05 +0000)
mediagoblin/db/models.py
mediagoblin/federation/views.py
mediagoblin/tests/test_api.py

index 440aa6826a1a9c5cfa14722d9a5ca6f8f4300fbf..fba58ca89234e7fffc2493a4e4db84dcf6d7bf15 100644 (file)
@@ -803,31 +803,25 @@ class MediaComment(Base, MediaCommentMixin):
 
     def unserialize(self, data, request):
         """ Takes API objects and unserializes on existing comment """
-        # Do initial checks to verify the object is correct
-        required_attributes = ["content", "inReplyTo"]
-        for attr in required_attributes:
-            if attr not in data:
+        # Handle changing the reply ID
+        if "inReplyTo" in data:
+            # Validate that the ID is correct
+            try:
+                media_id = int(extract_url_arguments(
+                    url=data["inReplyTo"]["id"],
+                    urlmap=request.app.url_map
+                )["id"])
+            except ValueError:
                 return False
 
-        # Validate inReplyTo has ID
-        if "id" not in data["inReplyTo"]:
-            return False
+            media = MediaEntry.query.filter_by(id=media_id).first()
+            if media is None:
+                return False
 
-        # Validate that the ID is correct
-        try:
-            media_id = int(extract_url_arguments(
-                url=data["inReplyTo"]["id"],
-                urlmap=request.app.url_map
-            )["id"])
-        except ValueError:
-            return False
-
-        media = MediaEntry.query.filter_by(id=media_id).first()
-        if media is None:
-            return False
-
-        self.media_entry = media.id
-        self.content = data["content"]
+            self.media_entry = media.id
+
+        if "content" in data:
+            self.content = data["content"]
 
         if "location" in data:
             Location.create(data["location"], self)
index 69dee7fbd317a60b9ec52313fe1a57cfc7df1799..5b10fb5b5504bb4e385f60b9381f141f316de33f 100644 (file)
@@ -360,9 +360,9 @@ def feed_endpoint(request, outbox=None):
                         status=403
                     )
 
-                if not comment.unserialize(data["object"]):
+                if not comment.unserialize(data["object"], request):
                     return json_error(
-                        "Invalid 'comment' with id '{0}'".format(obj_id)
+                        "Invalid 'comment' with id '{0}'".format(obj["id"])
                     )
 
                 comment.save()
@@ -382,7 +382,7 @@ def feed_endpoint(request, outbox=None):
                 image = MediaEntry.query.filter_by(id=obj_id).first()
                 if image is None:
                     return json_error(
-                        "No such 'image' with the id '{0}'.".format(obj_id)
+                        "No such 'image' with the id '{0}'.".format(obj["id"])
                     )
 
                 # Check that the person trying to update the comment is
index a4cb21d6d55824d7dfbd43cf13976426ab1f8ae8..83003875906c98a50f8b23491755b34f741acd59 100644 (file)
@@ -561,3 +561,40 @@ class TestAPI(object):
         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"]
+