Beispiel #1
0
    def test_post_create_post(self, test_client):
        """
        Test POST request to the /community/_/post/create route to assert the post is
        created successfully.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            f"/community/{community.name}/post/create",
            data={
                "title": "mockposttitle",
                "post": "mockpost"
            },
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created post" in response.data
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
Beispiel #6
0
    def test_post_reply(self, test_client):
        """
        Test POST request to the /community/_/post/_/reply route to assert the reply is
        successfully created.
        """
        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,
        )
        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}/reply",
            data={"reply": "mockreply"},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created reply" in response.data
Beispiel #7
0
def register_user(username, password):
    """
    Hashes the given password and registers a new user in the database.
    """
    hashed_password = bcrypt.hash(password)
    app_user = AppUser(username=username.lower(), password=hashed_password)
    db.session.add(app_user)
    db.session.commit()
Beispiel #8
0
    def test_get_user(self, test_client):
        """
        Test GET request to the /user/_ route to assert the user's profile page is
        displayed.
        """
        app_user = AppUser(username="******", password="******")
        db.session.add(app_user)
        db.session.commit()

        response = test_client.get(f"/user/{app_user.username}")

        assert response is not None
        assert response.status_code == 200
        assert bytes(app_user.username, "utf-8") in response.data
Beispiel #9
0
    def test_get_create_community(self, test_client):
        """
        Test GET request to the /community/create route to assert the community
        creation page is returned.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.get("/community/create")

        assert response is not None
        assert response.status_code == 200
        assert b"Create Community" in response.data
Beispiel #10
0
    def test_post_logout(self, test_client):
        """
        Test POST request to the /logout route to assert the user is successfully
        logged out.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post("/logout", follow_redirects=True)

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully logged out" in response.data
Beispiel #11
0
    def test_get_top_community(self, test_client):
        """
        Test GET request to the /community/_/top route to assert the community page is
        displayed.
        """
        app_user = AppUser(username="******", password="******")
        community = Community(
            name="mockcommunity", description="mockdescription", app_user=app_user
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()

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

        assert response is not None
        assert response.status_code == 200
        assert bytes(community.name, "utf-8") in response.data
Beispiel #12
0
    def test_post_login(self, test_client):
        """
        Test POST request to the /login route to assert the user is successfully logged
        in.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()

        response = test_client.post(
            "/login",
            data={"username": app_user.username, "password": password},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully logged in" in response.data
Beispiel #13
0
    def test_get_update_community(self, test_client):
        """
        Test GET request to the /community/_/update route to assert the community
        update page is returned.
        """
        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
        )
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

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

        assert response is not None
        assert response.status_code == 200
        assert b"Update Community" in response.data
Beispiel #14
0
    def test_post_create_community(self, test_client):
        """
        Test POST request to the /community/create route to assert the community is
        successfully created.
        """
        password = "******"
        hashed_password = bcrypt.generate_password_hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        db.session.add(app_user)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

        response = test_client.post(
            "/community/create",
            data={"name": "mockcommunity", "description": "mockdescription"},
            follow_redirects=True,
        )

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully created community" in response.data
Beispiel #15
0
    def test_post_leave_community(self, test_client):
        """
        Test POST request to the /community/_/leave route to assert the user
        successfully left the community.
        """
        password = "******"
        hashed_password = bcrypt.hash(password)
        app_user = AppUser(username="******", password=hashed_password)
        community = Community(name="mockcommunity",
                              description="mockdescription",
                              app_user=app_user)
        db.session.add(app_user)
        db.session.add(community)
        db.session.commit()
        helpers.login(test_client, app_user.username, password)

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

        assert response is not None
        assert response.status_code == 200
        assert b"Successfully left community" in response.data
Beispiel #16
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