Example #1
0
    def test_get_post(self, test_client):
        """
        Test GET request to the /community/_/post/_ route to assert the community's
        post page is displayed.
        """
        app_user = AppUser(username="******", password="******")
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()

        response = test_client.get(
            f"/community/{community.name}/post/{post.title}")

        assert response is not None
        assert response.status_code == 200
        assert bytes(post.title, "utf-8") in response.data
Example #2
0
    def test_post_delete_post(self, test_client):
        """
        Test POST request to the /community/_/post/_/delete route to assert the post is
        deleted successfully.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/delete",
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully deleted post" in response.data
Example #3
0
    def test_post_downvote_post(self, test_client):
        """
        Test POST request to the /community/_/post/_/downvote route to assert the user
        successfully downvotes the post.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/downvote")

        assert response is not None
        assert response.status_code == 302
        post_vote = PostVote.query.filter_by(user_id=app_user.id,
                                             post_id=post.id).first()
        assert post_vote is not None
        assert post_vote.vote == -1
Example #4
0
    def test_get_update_post(self, test_client):
        """
        Test GET request to the /community/_/post/_/update route to assert the post
        update page is displayed.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get(
            f"/community/{community.name}/post/{post.title}/update")

        assert response is not None
        assert response.status_code == 200
        assert b"Update Post" in response.data
Example #5
0
    def test_post_update_reply(self, test_client):
        """
        Test POST request to the /community/_/post/_/reply/_/edit route to assert the
        reply is successfully updated.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        reply = Reply(reply="mockreply", app_user=app_user, post=post)
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.add(reply)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/reply/{reply.id}/edit",
            data={"reply": "mockupdatedreply"},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully updated reply" in response.data
Example #6
0
def create_post(title, post, community, user):
    """
    Adds a new post for the specified community to the database.
    """
    post = Post(title=title, post=post, community=community, app_user=user)
    db.session.add(post)
    db.session.commit()
Example #7
0
    def test_get_feed(self, test_client):
        """
        Test GET request to the / route to assert posts from the users joined
        communities are displayed.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        community_member = CommunityMember(app_user=app_user,
                                           community=community)
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(community_member)
        db.session.add(post)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get("/")

        assert response is not None
        assert response.status_code == 200
        assert bytes(post.title, "utf-8") in response.data
Example #8
0
    def test_post_upvote_reply(self, test_client):
        """
        Test POST request to the /community/_/post/_/reply/_/upvote route to assert the
        user successfuly upvotes the reply.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        post = Post(
            title="mockposttitle",
            post="mockpost",
            app_user=app_user,
            community=community,
        )
        reply = Reply(reply="mockreply", app_user=app_user, post=post)
        db.session.add(app_user)
        db.session.add(community)
        db.session.add(post)
        db.session.add(reply)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/{post.title}/reply/{reply.id}/upvote"
        )

        assert response is not None
        assert response.status_code == 302
        reply_vote = ReplyVote.query.filter_by(
            user_id=app_user.id, reply_id=reply.id
        ).first()
        assert reply_vote is not None
        assert reply_vote.vote == 1