Ejemplo n.º 1
0
    def test_get_posts(self):
        """
        Test users post list works correctly
        """
        # Create test user
        user1 = create_account('user1', '*****@*****.**', 'Password')
        # Ensure the users post list is empty
        self.assertEqual(len(get_posts(user1).items), 0)

        # Create a few test posts, ensure they appears in the users list
        post1 = create_post(user1, 'user1', 'Test post 1')
        post2 = create_post(user1, 'user1', 'Test post 2')
        create_post(user1, 'user1', 'Test post 3')
        self.assertEqual(len(get_posts(user1).items), 3)
        self.assertEqual(get_posts(user1).total, 3)

        # Delete one of the posts and ensure that it does not appear in the
        # list.
        delete_post(post1)

        # Ensure the above is now correct with post1 missing
        self.assertEqual(len(get_posts(user1).items), 2)
        self.assertEqual(get_posts(user1).total, 2)

        # Delete a post from MongoDB with the driver
        m.db.posts.remove({'_id': post2})
        # Ensure the above is now correct with post2 missing
        self.assertEqual(len(get_posts(user1).items), 1)
        self.assertEqual(get_posts(user1).total, 1)
Ejemplo n.º 2
0
    def test_get_posts(self):
        """
        Test users post list works correctly
        """
        # Create test user
        user1 = create_account('user1', '*****@*****.**', 'Password')
        # Ensure the users post list is empty
        self.assertEqual(len(get_posts(user1).items), 0)

        # Create a few test posts, ensure they appears in the users list
        post1 = create_post(user1, 'user1', 'Test post 1')
        post2 = create_post(user1, 'user1', 'Test post 2')
        create_post(user1, 'user1', 'Test post 3')
        self.assertEqual(len(get_posts(user1).items), 3)
        self.assertEqual(get_posts(user1).total, 3)

        # Delete one of the posts and ensure that it does not appear in the
        # list.
        delete_post(post1)

        # Ensure the above is now correct with post1 missing
        self.assertEqual(len(get_posts(user1).items), 2)
        self.assertEqual(get_posts(user1).total, 2)

        # Delete a post from MongoDB with the driver
        m.db.posts.remove({'_id': post2})
        # Ensure the above is now correct with post2 missing
        self.assertEqual(len(get_posts(user1).items), 1)
        self.assertEqual(get_posts(user1).total, 1)
Ejemplo n.º 3
0
Archivo: backend.py Proyecto: hnk/pjuu
def delete_account(user_id):
    """Will delete a users account.

    This **REMOVES ALL** details, posts, replies, etc. Not votes though.

    .. note: Ensure the user has authenticated this request. This is going to
             be the most *expensive* task in Pjuu, be warned.

    :param user_id: The `user_id` of the user to delete
    :type user_id: str

    """
    # Delete the user from MongoDB
    m.db.users.remove({"_id": user_id})

    # Remove all posts a user has ever made. This includes all votes
    # on the posts and all comments of the posts.
    # This calls the backend function from posts to do the deed
    posts_cursor = m.db.posts.find({"user_id": user_id}, {})
    for post in posts_cursor:
        delete_post(post.get("_id"))

    # Remove all the following relationships from Redis

    # Delete all references to followers of the user.
    # This will remove the user from the other users following list

    # TODO Replace with ZSCAN
    follower_cursor = r.zrange(k.USER_FOLLOWERS.format(user_id), 0, -1)

    for follower_id in follower_cursor:
        # Clear the followers following list of the uid
        r.zrem(k.USER_FOLLOWING.format(follower_id), user_id)
    # Delete the followers list
    r.delete(k.USER_FOLLOWERS.format(user_id))

    # Delete all references to the users the user is following
    # This will remove the user from the others users followers list

    # TODO Replace with ZSCAN
    followee_cursor = r.zrange(k.USER_FOLLOWING.format(user_id), 0, -1)

    for followee_id in followee_cursor:
        # Clear the followers list of people uid is following
        r.zrem(k.USER_FOLLOWERS.format(followee_id), user_id)
    # Delete the following list
    r.delete(k.USER_FOLLOWING.format(user_id))

    # Delete the users feed, this may have been added too during this process.
    # Probably not but let's be on the safe side
    r.delete(k.USER_FEED.format(user_id))

    # Delete the users alert list
    # DO NOT DELETE ANY ALERTS AS THESE ARE GENERIC
    r.delete(k.USER_ALERTS.format(user_id))
Ejemplo n.º 4
0
    def test_get_posts(self):
        """
        Test users post list works correctly
        """
        # Create test user
        user1 = create_user('user1', '*****@*****.**', 'Password')
        # Ensure the users post list is empty
        self.assertEqual(len(get_posts(user1).items), 0)

        # Create a few test posts, ensure they appears in the users list
        post1 = create_post(user1, 'Test post 1')
        post2 = create_post(user1, 'Test post 2')
        post3 = create_post(user1, 'Test post 3')
        self.assertEqual(len(get_posts(user1).items), 3)
        self.assertEqual(get_posts(user1).total, 3)

        # Ensure the post ids are in the Redis list
        self.assertIn(post1, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertIn(post2, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertIn(post3, r.lrange(K.USER_POSTS.format(user1), 0, -1))

        # Delete one of the posts and ensure that it does not appear in the
        # list.
        delete_post(post1)

        # Ensure the above is now correct with post1 missing
        self.assertEqual(len(get_posts(user1).items), 2)
        self.assertEqual(get_posts(user1).total, 2)

        # Ensure the post ids are in the Redis list and post1 is NOT
        self.assertNotIn(post1, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertIn(post2, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertIn(post3, r.lrange(K.USER_POSTS.format(user1), 0, -1))

        # Delete a post from inside Redis. This will trigger the self cleaning
        # list feature. We call these orphaned pids
        r.delete(K.POST.format(post2))
        # Ensure the above is now correct with post2 missing
        self.assertEqual(len(get_posts(user1).items), 1)
        self.assertEqual(get_posts(user1).total, 1)

        # Ensure the post ids are not in the Redis list and post1 is NOT
        self.assertNotIn(post1, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertNotIn(post2, r.lrange(K.USER_POSTS.format(user1), 0, -1))
        self.assertIn(post3, r.lrange(K.USER_POSTS.format(user1), 0, -1))
Ejemplo n.º 5
0
    def test_get_replies(self):
        """Test getting all replies for a post

        """
        # Create two test users
        user1 = create_account('user1', '*****@*****.**', 'Password')
        user2 = create_account('user2', '*****@*****.**', 'Password')
        # Create a post for each user and a comment on each for both user
        post1 = create_post(user1, 'user1', 'Test post')
        post2 = create_post(user2, 'user2', 'Test post')
        # Ensure the comment lists are empty
        self.assertEqual(len(get_replies(post1).items), 0)
        self.assertEqual(len(get_replies(post2).items), 0)

        reply1 = create_post(user1, 'user1', 'Test comment', post1)
        reply2 = create_post(user1, 'user1', 'Test comment', post2)
        reply3 = create_post(user2, 'user2', 'Test comment', post1)
        reply4 = create_post(user2, 'user2', 'Test comment', post2)
        # Ensure each comment appears in each users list
        self.assertEqual(len(get_replies(post1).items), 2)
        self.assertEqual(len(get_replies(post2).items), 2)
        # Ensure the totals are correct
        self.assertEqual(get_replies(post1).total, 2)
        self.assertEqual(get_replies(post2).total, 2)
        # Ensure comments are in MongoDB
        comment_ids = \
            [doc['_id'] for doc in m.db.posts.find({'reply_to': post1})]
        self.assertIn(reply1, comment_ids)
        self.assertIn(reply3, comment_ids)
        comment_ids = \
            [doc['_id'] for doc in m.db.posts.find({'reply_to': post2})]
        self.assertIn(reply2, comment_ids)
        self.assertIn(reply4, comment_ids)

        # Delete 1 comment from post1 and ensure it does not exist
        delete_post(reply1)
        # Check that is has gone
        self.assertEqual(len(get_replies(post1).items), 1)
        self.assertEqual(get_replies(post1).total, 1)
Ejemplo n.º 6
0
    def test_get_replies(self):
        """Test getting all replies for a post

        """
        # Create two test users
        user1 = create_account('user1', '*****@*****.**', 'Password')
        user2 = create_account('user2', '*****@*****.**', 'Password')
        # Create a post for each user and a comment on each for both user
        post1 = create_post(user1, 'user1', 'Test post')
        post2 = create_post(user2, 'user2', 'Test post')
        # Ensure the comment lists are empty
        self.assertEqual(len(get_replies(post1).items), 0)
        self.assertEqual(len(get_replies(post2).items), 0)

        reply1 = create_post(user1, 'user1', 'Test comment', post1)
        reply2 = create_post(user1, 'user1', 'Test comment', post2)
        reply3 = create_post(user2, 'user2', 'Test comment', post1)
        reply4 = create_post(user2, 'user2', 'Test comment', post2)
        # Ensure each comment appears in each users list
        self.assertEqual(len(get_replies(post1).items), 2)
        self.assertEqual(len(get_replies(post2).items), 2)
        # Ensure the totals are correct
        self.assertEqual(get_replies(post1).total, 2)
        self.assertEqual(get_replies(post2).total, 2)
        # Ensure comments are in MongoDB
        comment_ids = \
            [doc['_id'] for doc in m.db.posts.find({'reply_to': post1})]
        self.assertIn(reply1, comment_ids)
        self.assertIn(reply3, comment_ids)
        comment_ids = \
            [doc['_id'] for doc in m.db.posts.find({'reply_to': post2})]
        self.assertIn(reply2, comment_ids)
        self.assertIn(reply4, comment_ids)

        # Delete 1 comment from post1 and ensure it does not exist
        delete_post(reply1)
        # Check that is has gone
        self.assertEqual(len(get_replies(post1).items), 1)
        self.assertEqual(get_replies(post1).total, 1)
Ejemplo n.º 7
0
    def test_delete(self):
        """
        Tests delete_post() does what it should.
        This will in turn test delete_comments() as this will be triggered when
        a post is deleted.
        """
        # Create three test users
        user1 = create_account('user1', '*****@*****.**', 'Password')
        user2 = create_account('user2', '*****@*****.**', 'Password')

        # Create a post
        post1 = create_post(user1, 'user1', 'Test post')
        # Create multiple comments
        reply1 = create_post(user1, 'user1', 'Test comment 1', post1)
        reply2 = create_post(user2, 'user2', 'Test comment 2', post1)
        reply3 = create_post(user1, 'user1', 'Test comment 3', post1)
        reply4 = create_post(user2, 'user2', 'Test comment 4', post1)

        # Check the comment count on post1 is correct
        self.assertEqual(get_post(post1).get('comment_count'), 4)

        # Test deleting one comment
        # This function does not actually test to see if the user has the
        # the rights to delete the post. This should be tested in the frontend
        # Check a comment can be deleted
        self.assertIsNone(delete_post(reply4))

        # Check that getting the comment returns None
        self.assertIsNone(get_post(reply4))

        # Ensure the comment count on post1 is correct
        self.assertEqual(get_post(post1).get('comment_count'), 3)

        # Delete the post. This should delete all the comments, we will check
        self.assertIsNone(delete_post(post1))
        # Check that the post does not exist
        self.assertIsNone(get_post(post1))
        # Check that non of the comments exist
        self.assertIsNone(get_post(reply1))
        self.assertIsNone(get_post(reply2))
        self.assertIsNone(get_post(reply3))

        # Test deleting posts with uploads
        # Ensuring that the images are gone.
        # The actual deleting is tested in 'test_uploads.py'
        image = io.BytesIO(open('tests/upload_test_files/otter.jpg').read())
        post1 = create_post(user1, 'user1', 'Test post #2', upload=image)
        self.assertIsNotNone(post1)
        post1_filename = get_post(post1).get('upload')

        # Reset the file position in the image
        image.seek(0)
        reply1 = create_post(user1, 'user1', 'Test comment 1', post1,
                             upload=image)
        self.assertIsNotNone(reply1)
        reply1_filename = get_post(reply1).get('upload')

        image.seek(0)
        reply2 = create_post(user2, 'user2', 'Test comment 2', post1,
                             upload=image)
        self.assertIsNotNone(reply2)
        reply2_filename = get_post(reply2).get('upload')

        # Create GridFS file checker
        grid = gridfs.GridFS(m.db, 'uploads')

        # Check that each file exists
        self.assertTrue(grid.exists({'filename': post1_filename}))
        self.assertTrue(grid.exists({'filename': reply1_filename}))
        self.assertTrue(grid.exists({'filename': reply2_filename}))

        self.assertIsNone(delete_post(post1))

        # Check that all the files no longer exist
        self.assertFalse(grid.exists({'filename': post1_filename}))
        self.assertFalse(grid.exists({'filename': reply1_filename}))
        self.assertFalse(grid.exists({'filename': reply2_filename}))
Ejemplo n.º 8
0
def delete_account(user_id):
    """Will delete a users account.

    This **REMOVES ALL** details, posts, replies, etc. Not votes though.

    .. note: Ensure the user has authenticated this request. This is going to
             be the most *expensive* task in Pjuu, be warned.

    :param user_id: The `user_id` of the user to delete
    :type user_id: str

    """
    # Get the user object we will need this to remove the avatar
    user = get_user(user_id)

    # Delete the user from MongoDB
    m.db.users.remove({'_id': user_id})

    # If the user has an avatar remove it
    if user.get('avatar'):
        delete_upload(user.get('avatar'))

    # Remove all posts a user has ever made. This includes all votes
    # on the posts and all comments of the posts.
    # This calls the backend function from posts to do the deed
    posts_cursor = m.db.posts.find({'user_id': user_id}, {})
    for post in posts_cursor:
        delete_post(post.get('_id'))

    # Remove all the following relationships from Redis

    # Delete all references to followers of the user.
    # This will remove the user from the other users following list

    # TODO Replace with ZSCAN
    follower_cursor = r.zrange(k.USER_FOLLOWERS.format(user_id), 0, -1)

    for follower_id in follower_cursor:
        # Clear the followers following list of the uid
        r.zrem(k.USER_FOLLOWING.format(follower_id), user_id)
    # Delete the followers list
    r.delete(k.USER_FOLLOWERS.format(user_id))

    # Delete all references to the users the user is following
    # This will remove the user from the others users followers list

    # TODO Replace with ZSCAN
    followee_cursor = r.zrange(k.USER_FOLLOWING.format(user_id), 0, -1)

    for followee_id in followee_cursor:
        # Clear the followers list of people uid is following
        r.zrem(k.USER_FOLLOWERS.format(followee_id), user_id)
    # Delete the following list
    r.delete(k.USER_FOLLOWING.format(user_id))

    # Delete the users feed, this may have been added too during this process.
    # Probably not but let's be on the safe side
    r.delete(k.USER_FEED.format(user_id))

    # Delete the users alert list
    # DO NOT DELETE ANY ALERTS AS THESE ARE GENERIC
    r.delete(k.USER_ALERTS.format(user_id))
Ejemplo n.º 9
0
    def test_delete(self):
        """
        Tests delete_post() does what it should.
        This will in turn test delete_comments() as this will be triggered when
        a post is deleted.
        """
        # Create three test users
        user1 = create_account('user1', '*****@*****.**', 'Password')
        user2 = create_account('user2', '*****@*****.**', 'Password')

        # Create a post
        post1 = create_post(user1, 'user1', 'Test post')
        # Create multiple comments
        reply1 = create_post(user1, 'user1', 'Test comment 1', post1)
        reply2 = create_post(user2, 'user2', 'Test comment 2', post1)
        reply3 = create_post(user1, 'user1', 'Test comment 3', post1)
        reply4 = create_post(user2, 'user2', 'Test comment 4', post1)

        # Check the comment count on post1 is correct
        self.assertEqual(get_post(post1).get('comment_count'), 4)

        # Test deleting one comment
        # This function does not actually test to see if the user has the
        # the rights to delete the post. This should be tested in the frontend
        # Check a comment can be deleted
        self.assertIsNone(delete_post(reply4))

        # Check that getting the comment returns None
        self.assertIsNone(get_post(reply4))

        # Ensure the comment count on post1 is correct
        self.assertEqual(get_post(post1).get('comment_count'), 3)

        # Delete the post. This should delete all the comments, we will check
        self.assertIsNone(delete_post(post1))
        # Check that the post does not exist
        self.assertIsNone(get_post(post1))
        # Check that non of the comments exist
        self.assertIsNone(get_post(reply1))
        self.assertIsNone(get_post(reply2))
        self.assertIsNone(get_post(reply3))

        # Test deleting posts with uploads
        # Ensuring that the images are gone.
        # The actual deleting is tested in 'test_uploads.py'
        image = io.BytesIO(open('tests/upload_test_files/otter.jpg').read())
        post1 = create_post(user1, 'user1', 'Test post #2', upload=image)
        self.assertIsNotNone(post1)
        post1_filename = get_post(post1).get('upload')

        # Reset the file position in the image
        image.seek(0)
        reply1 = create_post(user1,
                             'user1',
                             'Test comment 1',
                             post1,
                             upload=image)
        self.assertIsNotNone(reply1)
        reply1_filename = get_post(reply1).get('upload')

        image.seek(0)
        reply2 = create_post(user2,
                             'user2',
                             'Test comment 2',
                             post1,
                             upload=image)
        self.assertIsNotNone(reply2)
        reply2_filename = get_post(reply2).get('upload')

        # Create GridFS file checker
        grid = gridfs.GridFS(m.db, 'uploads')

        # Check that each file exists
        self.assertTrue(grid.exists({'filename': post1_filename}))
        self.assertTrue(grid.exists({'filename': reply1_filename}))
        self.assertTrue(grid.exists({'filename': reply2_filename}))

        self.assertIsNone(delete_post(post1))

        # Check that all the files no longer exist
        self.assertFalse(grid.exists({'filename': post1_filename}))
        self.assertFalse(grid.exists({'filename': reply1_filename}))
        self.assertFalse(grid.exists({'filename': reply2_filename}))