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
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
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
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
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
def create_community(name, description, user): """ Adds a new community to the database. """ community = Community(name=name, description=description, app_user=user) db.session.add(community) db.session.commit()
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
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
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
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
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