def test_delete_story_not_found(self): with patch.object(Persistence, "delete_one") as mocked_delete_one,\ self.assertRaises(StoryNotFoundException) as context,\ patch.object(StoryComment, "delete_comments_on_story") as mocked_delete_comments: mocked_internal_story = dict(story_mock_private_with_reaction) mocked_internal_story["_id"] = object_id_mock mocked_delete_one.side_effect = MagicMock(return_value=None) mocked_delete_comments.side_effect = MagicMock() # do nothing Story.delete_story(object_id_mock) exc = context.exception self.assertEqual(exc.error_code, 404) self.assertEqual(exc.message, "Story was not found")
def delete(self, story_id): try: # get token from header token = self._get_token_from_header() # identify with token username = Token.identify(token) # get story by story_id story = Story.get_story(story_id) # if username is not the uploader return 403 story_uploader = story["username"] self.logger.debug("At DELETE@/stories requesting user is {} and uploader is {}".format( username, story_uploader)) if username != story_uploader: return ResponseBuilder.build_error_response("Story is not own", 403) # delete story deleted_story_obj = Story.delete_story(story_id) deleted_story_id = str(deleted_story_obj) # generate response output = {"target_story_id": deleted_story_id} # return response return ResponseBuilder.build_response(output) except (InvalidTokenException, ExpiredTokenException, StoryNotFoundException, MissingFieldException) as e: return ResponseBuilder.build_error_response(e.message, e.error_code)
def test_successful_delete_story(self): with patch.object(Persistence, "delete_one") as mocked_delete_one,\ patch.object(StoryComment, "delete_comments_on_story") as mocked_delete_comments: mocked_internal_story = dict(story_mock_private_with_reaction) mocked_internal_story["_id"] = object_id_mock mocked_delete_one.side_effect = MagicMock( return_value=mocked_internal_story) mocked_delete_comments.side_effect = MagicMock() # do nothing self.assertEqual(Story.delete_story(object_id_mock), object_id_mock)