def test_list_posts_sorted(mocker, user_client, private_channel_and_contributor, reddit_factories, sort): """View the channel listing with sorted options""" # note: these sort types are difficult to reproduce unique sort orders in the span of a test, # so we're just checking that the APIs don't error channel, user = private_channel_and_contributor first_post = reddit_factories.text_post("my post", user, channel=channel) second_post = reddit_factories.text_post("my 2nd post", user, channel=channel) third_post = reddit_factories.text_post("my 3rd post", user, channel=channel) fourth_post = reddit_factories.text_post("my 4th post", user, channel=channel) url = reverse("post-list", kwargs={"channel_name": channel.name}) with raise_error_on_submission_fetch( mocker), raise_error_on_subreddit_fetch(mocker): resp = user_client.get(url, {"sort": sort}) assert resp.status_code == status.HTTP_200_OK assert resp.json() == { "posts": [ default_post_response_data(channel, post, user) for post in [fourth_post, third_post, second_post, first_post] ], "pagination": { "sort": sort }, }
def scenario(self, private_channel_and_contributor, reddit_factories, staff_user): """Fixture that sets up a common scenario for post-detail tests""" channel, user = private_channel_and_contributor post = reddit_factories.text_post("just a post", user, channel=channel) url = reverse("post-detail", kwargs={"post_id": post.id}) default_response = default_post_response_data(channel, post, user) default_staff_response = default_post_response_data( channel, post, staff_user) return SimpleNamespace( channel=channel, user=user, staff_user=staff_user, post=post, url=url, default_response=default_response, default_staff_response=default_staff_response, )
def test_list_reports(staff_client, private_channel_and_contributor, reddit_factories, staff_api): """List reported content""" channel, user = private_channel_and_contributor post = reddit_factories.text_post("post", user, channel=channel) comment = reddit_factories.comment("comment", user, post_id=post.id) # report both with a regular user api = Api(user) api.report_comment(comment.id, "spam") api.report_post(post.id, "bad") # report both with a moderator user staff_api.report_comment(comment.id, "spam") staff_api.report_post(post.id, "junk") url = reverse("channel-reports", kwargs={"channel_name": channel.name}) resp = staff_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == [ { "post": None, "comment": { "author_id": user.username, "created": comment.created, "id": comment.id, "parent_id": None, "post_id": post.id, "score": 1, "text": comment.text, "upvoted": False, "downvoted": False, "removed": False, "deleted": False, "subscribed": False, "profile_image": image_uri(user.profile), "author_name": user.profile.name, "author_headline": user.profile.headline, "edited": False, "comment_type": "comment", "num_reports": 2, }, "reasons": ["spam"], }, { "post": { **default_post_response_data(channel, post, user), "num_comments": 1, "num_reports": 2, "upvoted": False, }, "comment": None, "reasons": ["bad", "junk"], }, ]
def test_get_post_stickied(user_client, private_channel_and_contributor, reddit_factories, staff_api): """test that stickied posts come back that way""" channel, user = private_channel_and_contributor post = reddit_factories.text_post("just a post", user, channel=channel) staff_api.pin_post(post.id, True) url = reverse("post-detail", kwargs={"post_id": post.id}) get_resp = user_client.get(url) assert get_resp.status_code == status.HTTP_200_OK assert get_resp.json() == { **default_post_response_data(channel, post, user), "stickied": True, }
def test_update_article_post(user_client, reddit_factories, private_channel_and_contributor): """Test that we can update the content of an article post""" article_content = [{"data": "updated"}] channel, user = private_channel_and_contributor post = reddit_factories.article_post("post", user=user, channel=channel) url = reverse("post-detail", kwargs={"post_id": post.id}) resp = user_client.patch(url, {"article_content": article_content}) assert resp.status_code == status.HTTP_200_OK assert resp.json() == { **default_post_response_data(channel, post, user), "article_content": article_content, }
def test_get_post_anonymous(client, public_channel, reddit_factories): """Anonymous users can see posts for a public channel, if the feature flag is set""" user = UserFactory.create(username="******") post = reddit_factories.link_post("link_post", user=user, channel=public_channel) url = reverse("post-detail", kwargs={"post_id": post.id}) resp = client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == { **default_post_response_data(public_channel, post, user), "upvoted": False, }
def test_get_post_no_profile(user_client, private_channel_and_contributor, reddit_factories): """Get an existing post for a user with no profile""" channel, user = private_channel_and_contributor user.profile.delete() post = reddit_factories.text_post("my geat post", user, channel=channel) url = reverse("post-detail", kwargs={"post_id": post.id}) resp = user_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == { **default_post_response_data(channel, post, user), "author_name": "[deleted]", "author_headline": None, "profile_image": image_uri(None), }
def test_get_post(user_client, private_channel_and_contributor, reddit_factories, missing_image): """Get an existing post with no image""" channel, user = private_channel_and_contributor if missing_image: user.profile.image_small = None else: user.profile.image_small = "/just/a/great/image.png.jpg.gif" user.profile.save() post = reddit_factories.text_post("my geat post", user, channel=channel) url = reverse("post-detail", kwargs={"post_id": post.id}) resp = user_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == default_post_response_data(channel, post, user)
def test_list_posts_stickied(mocker, user_client, private_channel_and_contributor, reddit_factories, staff_api): """test that the stickied post is first""" channel, user = private_channel_and_contributor posts = [ reddit_factories.text_post("great post!{}".format(i), user, channel=channel) for i in range(4) ] post = posts[2] staff_api.pin_post(post.id, True) url = reverse("post-list", kwargs={"channel_name": channel.name}) with raise_error_on_submission_fetch( mocker), raise_error_on_subreddit_fetch(mocker): resp = user_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json()["posts"][0] == { **default_post_response_data(channel, post, user), "stickied": True, }
def test_list_posts(mocker, user_client, missing_user, private_channel_and_contributor, reddit_factories): """List posts in a channel""" channel, user = private_channel_and_contributor posts = list( reversed([ reddit_factories.link_post("link_post", user=user, channel=channel), reddit_factories.text_post("text_post", user=user, channel=channel), ])) if missing_user: user.username = "******" user.save() url = reverse("post-list", kwargs={"channel_name": channel.name}) with raise_error_on_submission_fetch( mocker), raise_error_on_subreddit_fetch(mocker): resp = user_client.get(url) assert resp.status_code == status.HTTP_200_OK if missing_user: # all posts should be filtered out assert resp.json() == { "posts": [], "pagination": { "sort": POSTS_SORT_HOT } } else: assert resp.json() == { "posts": [ default_post_response_data(channel, post, user) for post in posts ], "pagination": { "sort": POSTS_SORT_HOT }, }
def test_list_posts_anonymous(mocker, client, public_channel, reddit_factories): """Anonymous users can see posts for a public channel, if the feature flag is set""" user = UserFactory.create(username="******") post = reddit_factories.link_post("link_post", user=user, channel=public_channel) url = reverse("post-list", kwargs={"channel_name": public_channel.name}) with raise_error_on_submission_fetch( mocker), raise_error_on_subreddit_fetch(mocker): resp = client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.json() == { "pagination": { "sort": "hot" }, "posts": [{ **default_post_response_data(public_channel, post, user), "upvoted": False }], }