def test_list_posts_pagination_non_offset_page(mocker, user_client, settings, private_channel_and_contributor, reddit_factories): """Test that post pagination works for a page that doesn't align to the number of results""" settings.OPEN_DISCUSSIONS_CHANNEL_POST_LIMIT = 5 channel, user = private_channel_and_contributor posts = list( reversed([ reddit_factories.text_post(idx, user=user, channel=channel) for idx in range(15) ])) params = {"after": "t3_{}".format(posts[5].id), "count": 5} expected = { "before": "t3_{}".format(posts[6].id), "before_count": 6, "after": "t3_{}".format(posts[10].id), "after_count": 10, "sort": POSTS_SORT_HOT, } 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, params) assert resp.status_code == status.HTTP_200_OK assert resp.json()["pagination"] == expected
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 test_list_posts_none(mocker, user_client, private_channel_and_contributor): """List posts in a channel""" channel, _ = private_channel_and_contributor 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": [], "pagination": {"sort": POSTS_SORT_HOT}}
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 }], }