Esempio n. 1
0
    def add_comment(cls, post_id, user_id, content):
        if not is_id_valid(post_id):
            raise InvalidFieldError("post id is invalid", ["post_id"])

        # not need to check user id for logged in user
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        post = DBPost.get_by_id(post_id)
        if not post:
            raise PostNotFoundError(post_id=post_id)

        if len(content) < 10:
            raise InvalidFieldError("comment is too short", ["content"])

        args = {
            "post_id": post_id,
            "user_id": user_id,
            "content": content
        }
        comment = DBComment(**args)
        try:
            comment.save()
            return comment
        except:
            return None
Esempio n. 2
0
    def delete_comment(cls, user_id, comment_id):
        # not necessary to check user_id
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        if not is_id_valid(comment_id):
            raise InvalidFieldError("comment id is invalid", ["comment_id"])

        user = DBUser.get_by_id(user_id)
        if not user:
            raise UserNotFoundError("User with id = %d  does not exist" % user_id)

        comment = DBComment.get_by_id(comment_id)
        if not comment:
            raise CommentNotFoundError(comment_id=comment_id)

        # only allow commenter/post author to delete comment
        if comment.user_id != user_id and comment.post.author.id != user_id:
            raise AccessDeniedError("You cannot delete others comment")

        try:
            comment.delete()
            return comment
        except:
            raise
Esempio n. 3
0
    def update_comment(cls, user_id, comment_id, content):

        # not necessary to check user_id
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        if not is_id_valid(comment_id):
            raise InvalidFieldError("comment id is invalid", ["comment_id"])

        commenter = DBUser.get_by_id(user_id)
        if not commenter:
            raise UserNotFoundError("User with id = %d  does not exist" % user_id)

        comment = DBComment.get_by_id(comment_id)
        if not comment:
            raise CommentNotFoundError(comment_id=comment_id)

        if len(content) < 10:
            raise InvalidFieldError("comment is too short", ["content"])

        # only allow commenter to update comment
        if comment.user_id != user_id:
            raise AccessDeniedError("You cannot edit others comment")

        comment.content = content
        try:
            comment.update()
            return comment
        except:
            raise
Esempio n. 4
0
    def test_delete_comment_by_post_author(self):
        args={
            "user_id": 2,
            "comment_id": 10,
        }

        Comment.delete_comment(**args)

        comment = DBComment.get_by_id(10)
        self.assertIsNone(comment)
Esempio n. 5
0
    def test_delete_comment_ideal_case(self):
        args={
            "user_id": 2,
            "comment_id": 1,
        }

        Comment.delete_comment(**args)

        comment = DBComment.get_by_id(1)
        self.assertIsNone(comment)
Esempio n. 6
0
    def test_update_comment_ideal_case(self):
        args={
            "user_id": 2,
            "comment_id": 1,
            "content": "new comment content here"
        }

        cm = Comment.update_comment(**args)
        self.assertIsNotNone(cm)

        comment = DBComment.get_by_id(1)
        self.assertIsNotNone(comment)
        self.assertEqual(comment.user_id, args["user_id"])
        self.assertEqual(comment.content, args["content"])
Esempio n. 7
0
    def test_add_comment_ideal_case(self):
        args={
            "user_id": 1,
            "post_id": 2,
            "content": "This is my first comment on this page"
        }

        cm = Comment.add_comment(**args)
        self.assertIsNotNone(cm)

        comment = DBComment.get_by_id(cm.id)
        self.assertIsNotNone(comment)
        self.assertEqual(comment.user_id, args["user_id"])
        self.assertEqual(comment.post_id, args["post_id"])
        self.assertEqual(comment.content, args["content"])
Esempio n. 8
0
def set_up_db():
    args = {
        "email": "*****@*****.**",
        "password": hashlib.md5("123456").hexdigest(),
        "first_name": "Admin",
        "last_name": "Nguyen",
        "brief": "Hello world",
        "role": "manager"
    }
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor2"
    args["role"] = 'editor'
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor3"
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor4"
    user = User(**args)
    user.save()

    args = {
        "title": "Post 1 title",
        "content": """<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eveniet doloribus qui, adipisci inventore sequi fugiat dolores ullam, provident a, accusantium, necessitatibus ab nisi aliquam. Ipsam voluptas dolores magni necessitatibus provident.</p>
        <p>Sunt quo placeat fugiat nesciunt vel assumenda dolorem incidunt provident eligendi ipsa, quam autem optio id nostrum beatae corporis a. Tempore saepe quod nemo hic magni in veritatis illum natus.</p>
        <p>Et beatae ipsam repellat officiis similique cupiditate distinctio expedita rem at, aut aspernatur, voluptate quibusdam! Voluptatum aut quos porro eos nulla officiis adipisci magnam perferendis, dicta minima quis eligendi enim.</p>
        <p>Sed itaque dignissimos eligendi reprehenderit, nesciunt ducimus voluptates dolores suscipit fugit ipsam aperiam praesentium laborum odit qui libero ipsum tempora, eos quis hic, sapiente perspiciatis amet labore voluptatibus alias. Vitae.</p>""",
        "user_id": 1,
        "categories": ["c++","python"]
    }

    post = Post(**args)
    post.save()

    args["title"] = "Post 2 title"
    args["user_id"] = 2

    post = Post(**args)
    post.save()

    args["title"] = "Post 3 title"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "Post 4 title"
    args["user_id"] = 4

    post = Post(**args)
    post.save()

    args["title"] = "Post Hello title"
    args["user_id"] = 1
    args["categories"] = ["non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Good morning"
    args["user_id"] = 2

    post = Post(**args)
    post.save()

    args["title"] = "First day at Moscow"
    args["user_id"] = 2
    args["categories"] = ["journey","non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Surprising"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "So awesome lake"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "My new Phone"
    args["user_id"] = 3
    args["categories"] = ["photo","non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Photo with new phone"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "List of useful app for Blackberry"
    args["user_id"] = 3
    args["categories"] = ["uncategorized"]

    post = Post(**args)
    post.save()


    cmt_args={
        "content":"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut ipsum ad, mollitia repellendus harum dignissimos rem beatae, dolore minus. Sapiente saepe mollitia magnam molestiae natus officiis corrupti voluptatibus, qui repudiandae.</p>",
        "post_id":1,
        "user_id":2,
    }
    cmt = Comment(**cmt_args)
    cmt.save()


    cmt_args["post_id"] = 1
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 1
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 4
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 7
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 4
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 6
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 6
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()