A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
RSpec.describe ListController do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:group) { Fabricate(:group, name: "AwesomeGroup") }
fab!(:admin)
before do
admin # to skip welcome wizard at home page `/`
SiteSetting.top_menu = "latest|new|categories"
end
describe "#filter unified New membership" do
before { SiteSetting.enable_unified_new = true }
it "omits membership for anonymous and disabled users" do
get "/filter.json", params: { q: "" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]).not_to have_key("filter_new_topic_ids")
sign_in(user)
SiteSetting.enable_unified_new = false
get "/filter.json", params: { q: "" }
expect(response.parsed_body["topic_list"]).not_to have_key("filter_new_topic_ids")
end
it "includes both subsets across pages while applying the full filter" do
sign_in(user)
category = Fabricate(:category)
matches = Fabricate.times(31, :topic, category: category)
unread = Fabricate(:topic, category: category, highest_post_number: 2)
Fabricate(
:topic_user,
user: user,
topic: unread,
last_read_post_number: 1,
notification_level: 2,
)
Fabricate(:topic, category: category, closed: true)
Fabricate(:topic)
%w[new new-topics new-replies].each do |subset|
get "/filter.json", params: { q: "category:#{category.slug} status:open", subset: subset }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["filter_new_topic_ids"]).to contain_exactly(
*matches.map(&:id),
unread.id,
)
end
expect(response.parsed_body["topic_list"]["topics"].map { |entry| entry["id"] }).to eq(
[unread.id],
)
end
it "preserves the original query and subset in pagination links" do
sign_in(user)
category = Fabricate(:category)
Fabricate.times(31, :topic, category: category)
query = "category:#{category.slug}"
get "/filter.json", params: { q: query, subset: "new-topics" }
params =
Rack::Utils.parse_query(
URI.parse(response.parsed_body["topic_list"]["more_topics_url"]).query,
)
expect(params).to include("q" => query, "subset" => "new-topics", "page" => "1")
end
it "keeps manually entered New conditions in the membership query" do
sign_in(user)
unread = Fabricate(:topic, highest_post_number: 2)
Fabricate(
:topic_user,
user: user,
topic: unread,
last_read_post_number: 1,
notification_level: 2,
)
Fabricate(:topic)
get "/filter.json", params: { q: "in:new-replies", subset: "new-topics" }
expect(response.parsed_body["topic_list"]["topics"]).to be_empty
expect(response.parsed_body["topic_list"]["filter_new_topic_ids"]).to eq([unread.id])
end
it "ignores unknown subsets and disables subset filtering with unified New" do
sign_in(user)
%w[unsupported new-replies].each do |subset|
SiteSetting.enable_unified_new = subset == "unsupported"
get "/filter.json", params: { q: "topic:#{topic.id}", subset: subset }
expect(response.parsed_body["topic_list"]["topics"].map { |entry| entry["id"] }).to eq(
[topic.id],
)
end
end
it "applies permissions, muting and custom plugin filters" do
sign_in(user)
allowed = Fabricate(:topic)
private_topic = Fabricate(:topic, category: Fabricate(:private_category, group: group))
muted = Fabricate(:topic)
TopicUser.change(user.id, muted.id, notification_level: TopicUser.notification_levels[:muted])
deleted = Fabricate(:topic, deleted_at: Time.zone.now)
unlisted = Fabricate(:topic, visible: false)
Plugin::Instance
.new
.add_filter_custom_filter("selected") do |scope, values, _guardian|
scope.where(id: values.map(&:to_i))
end
ids = [allowed.id, private_topic.id, muted.id, deleted.id, unlisted.id]
get "/filter.json", params: { q: "selected:#{ids.join(",")}" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["filter_new_topic_ids"]).to eq([allowed.id])
ensure
DiscoursePluginRegistry.reset_register!(:custom_filter_mappings)
end
it "respects the user's upcoming change group membership" do
Fabricate(:site_setting_group, name: :enable_unified_new, group_ids: group.id.to_s)
sign_in(user)
get "/filter.json", params: { q: "" }
expect(response.parsed_body["topic_list"]).not_to have_key("filter_new_topic_ids")
group.add(user)
get "/filter.json", params: { q: "" }
expect(response.parsed_body["topic_list"]).to have_key("filter_new_topic_ids")
end
it "returns an empty array when no tracked topics match" do
sign_in(user)
get "/filter.json", params: { q: "topic:0 in:new-replies" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["filter_new_topic_ids"]).to eq([])
end
end
describe "#index" do
it "does not expose the Klipy API key in anonymous preloaded site settings" do
SiteSetting.klipy_api_key = "super-secret-klipy-key"
get "/latest"
expect(response.status).to eq(200)
expect(response.body).not_to include(SiteSetting.klipy_api_key)
expect(response.body).to have_tag("script#data-preloaded") do |element|
data_preloaded = JSON.parse(element.current_scope.text)
site_settings = JSON.parse(data_preloaded["siteSettings"])
expect(site_settings).not_to have_key("klipy_api_key")
end
end
context "when params are invalid" do
it "returns 400 when page is a negative integer string" do
get "/latest?page=-1"
expect(response.status).to eq(400)
end
it "returns 400 when page exceeds the maximum integer" do
get "/latest?page=2147483648"
expect(response.status).to eq(400)
get "/latest?page=1111111111111111111111111111111111111111"
expect(response.status).to eq(400)
end
it "returns 400 when before is not an integer string" do
get "/latest?before[1]=haxx"
expect(response.status).to eq(400)
end
it "returns 400 when bumped_before is not an integer string" do
get "/latest?bumped_before[1]=haxx"
expect(response.status).to eq(400)
end
it "returns 400 when topic_ids is not an integer string" do
get "/latest?topic_ids[1]=haxx"
expect(response.status).to eq(400)
end
it "returns 400 when category is not an integer string" do
get "/latest?category[1]=haxx"
expect(response.status).to eq(400)
end
it "returns 400 when order is not a string" do
get "/latest?order[1]=haxx"
expect(response.status).to eq(400)
end
it "returns 400 when ascending is not a boolean string" do
get "/latest?ascending=maybe"
expect(response.status).to eq(400)
end
it "returns 400 when min_posts is not an integer string" do
get "/latest?min_posts=bob"
expect(response.status).to eq(400)
end
it "returns 400 when max_posts is not an integer string" do
get "/latest?max_posts=bob"
expect(response.status).to eq(400)
end
it "returns 400 when max_posts exceeds the maximum integer" do
get "/latest?max_posts=1111111111111111111111111111111111111111"
expect(response.status).to eq(400)
end
it "returns 400 when status is not a string" do
get "/latest?status%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when the list filter is not a string" do
get "/latest?filter%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when state is not a string" do
get "/latest?state%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when search is not a string" do
get "/latest?search%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when q is not a string" do
get "/latest?q%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when f is not a string" do
get "/latest?f%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when subset is not a string" do
get "/latest?subset%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when group_name is not a string" do
get "/latest?group_name%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when tags is neither an array nor a string" do
get "/latest?tags[1]=hello"
expect(response.status).to eq(400)
end
it "returns 400 when the user filter is not a string" do
get "/latest?filter%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
it "returns 400 when match_all_tags is not a boolean string" do
get "/latest?match_all_tags=something"
expect(response.status).to eq(400)
end
it "returns 400 when no_subcategories is not a boolean string" do
get "/latest?no_subcategories=something"
expect(response.status).to eq(400)
end
it "returns 400 when no_tags is not a boolean string" do
get "/latest?no_tags=something"
expect(response.status).to eq(400)
end
it "returns 400 when exclude_tag is not a string" do
get "/latest?exclude_tag%5Bsomehash%5D=something"
expect(response.status).to eq(400)
end
end
it "returns 200 for legit requests" do
get "/latest.json?no_definitions=true&no_subcategories=false&page=1&_=1534296100767"
expect(response.status).to eq(200)
get "/latest.json?max_posts=12"
expect(response.status).to eq(200)
get "/latest.json?min_posts=0"
expect(response.status).to eq(200)
get "/latest?page=0"
expect(response.status).to eq(200)
get "/latest?page=1"
expect(response.status).to eq(200)
get "/latest.json?page=1999"
expect(response.status).to eq(200)
get "/latest?search="
expect(response.status).to eq(200)
get "/latest.json?topic_ids%5B%5D=14583&topic_ids%5B%5D=14584"
expect(response.status).to eq(200)
get "/latest.json?topic_ids=14583%2C14584"
expect(response.status).to eq(200)
get "/latest?tags[]=hello"
expect(response.status).to eq(200)
end
(Discourse.anonymous_filters - [:categories]).each do |filter|
context "with #{filter}" do
it "returns the requested list" do
get "/#{filter}"
expect(response.status).to eq(200)
end
end
end
it "allows users to filter on a set of topic ids" do
p = create_post
get "/latest.json", params: { topic_ids: "#{p.topic_id}" }
expect(response.status).to eq(200)
parsed = response.parsed_body
expect(parsed["topic_list"]["topics"].length).to eq(1)
end
it "shows correct title if topic list is set for homepage" do
get "/latest"
expect(response.body).to have_tag "title", text: "Discourse"
SiteSetting.short_site_description = "Best community"
get "/latest"
expect(response.body).to have_tag "title", text: "Discourse - Best community"
end
it "returns structured data" do
get "/latest"
expect(response.status).to eq(200)
topic_list = Nokogiri.HTML5(response.body).css(".topic-list")
first_item = topic_list.css('[itemprop="itemListElement"]')
expect(first_item.css('[itemprop="position"]')[0]["content"]).to eq("1")
expect(first_item.css('[itemprop="url"]')[0]["href"]).to eq(topic.url)
end
it "does not result in N+1 queries when topics have tags and tagging_enabled site setting is enabled" do
SiteSetting.tagging_enabled = true
tag = Fabricate(:tag)
topic.tags << tag
# warm up
get "/latest.json"
expect(response.status).to eq(200)
initial_sql_queries_count =
track_sql_queries do
get "/latest.json"
expect(response.status).to eq(200)
body = response.parsed_body
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(topic.id)
expect(body["topic_list"]["topics"][0]["tags"]).to contain_exactly(
{ "id" => tag.id, "name" => tag.name, "slug" => tag.slug },
)
end.count
tag2 = Fabricate(:tag)
topic2 = Fabricate(:topic, tags: [tag2])
new_sql_queries_count =
track_sql_queries do
get "/latest.json"
expect(response.status).to eq(200)
body = response.parsed_body
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
topic.id,
topic2.id,
)
expect(body["topic_list"]["topics"][0]["tags"]).to contain_exactly(
{ "id" => tag2.id, "name" => tag2.name, "slug" => tag2.slug },
)
expect(body["topic_list"]["topics"][1]["tags"]).to contain_exactly(
{ "id" => tag.id, "name" => tag.name, "slug" => tag.slug },
)
end.count
expect(new_sql_queries_count).to eq(initial_sql_queries_count)
end
it "does not N+1 queries when topic featured users have different primary groups" do
user.update!(primary_group: group)
# warm up
get "/latest.json"
expect(response.status).to eq(200)
initial_sql_queries_count =
track_sql_queries do
get "/latest.json"
expect(response.status).to eq(200)
body = response.parsed_body
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(topic.id)
expect(
body["topic_list"]["topics"][0]["posters"].map { |p| p["user_id"] },
).to contain_exactly(user.id)
end.count
group2 = Fabricate(:group)
user2 = Fabricate(:user, primary_group: group2)
topic.update!(last_post_user_id: user2.id)
group3 = Fabricate(:group)
user3 = Fabricate(:user, flair_group: group3)
topic.update!(featured_user3_id: user3.id)
new_sql_queries_count =
track_sql_queries do
get "/latest.json"
expect(response.status).to eq(200)
body = response.parsed_body
expect(body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(topic.id)
expect(
body["topic_list"]["topics"][0]["posters"].map { |p| p["user_id"] },
).to contain_exactly(user.id, user2.id, user3.id)
end.count
expect(new_sql_queries_count).to be <= initial_sql_queries_count
end
context "with topics with tags" do
let(:tag_group) { Fabricate.build(:tag_group) }
let(:tag_group_permission) { Fabricate.build(:tag_group_permission, tag_group: tag_group) }
let(:restricted_tag) { Fabricate(:tag) }
let(:public_tag) { Fabricate(:tag) }
before do
tag_group.tag_group_permissions << tag_group_permission
tag_group.save!
tag_group_permission.tag_group.tags << restricted_tag
topic.tags << [public_tag, restricted_tag]
end
it "does not show hidden tags" do
get "/latest"
expect(response.status).to eq(200)
expect(response.body).to include(public_tag.name)
expect(response.body).not_to include(restricted_tag.name)
end
end
context "with lazy load categories enabled" do
fab!(:category)
fab!(:subcategory) { Fabricate(:category, parent_category: category) }
before { topic.update!(category: subcategory) }
it "returns categories and parent categories if true" do
SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:anonymous_users]}"
get "/latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].length).to eq(1)
expect(response.parsed_body["topic_list"]["topics"][0]["id"]).to eq(topic.id)
expect(response.parsed_body["topic_list"]["categories"].length).to eq(2)
expect(response.parsed_body["topic_list"]["categories"].map { |c| c["id"] }).to eq(
[category.id, subcategory.id],
)
end
it "does not return categories if not true" do
SiteSetting.lazy_load_categories_groups = ""
get "/latest.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].length).to eq(1)
expect(response.parsed_body["topic_list"]["topics"][0]["id"]).to eq(topic.id)
expect(response.parsed_body["topic_list"]["categories"]).to eq(nil)
end
end
context "when login required" do
before do
SiteSetting.login_required = true
SiteSetting.has_login_hint = false
end
it "returns nothing from topic list on homepage for login required" do
get "/"
expect(response.status).to eq(200)
expect(response.body).not_to include(topic.title)
end
end
end
describe "categories and X" do
let(:category) { Fabricate(:category_with_definition) }
let(:sub_category) { Fabricate(:category_with_definition, parent_category: category) }
it "returns top topics" do
Fabricate(:topic, like_count: 1000, posts_count: 100)
TopTopic.refresh!
get "/categories_and_top.json"
data = response.parsed_body
expect(data["topic_list"]["topics"].length).to eq(1)
get "/categories_and_latest.json"
data = response.parsed_body
expect(data["topic_list"]["topics"].length).to eq(2)
end
it "returns topics from subcategories when no_subcategories=false" do
Fabricate(:topic, category: sub_category)
get "/c/#{category.slug}/#{category.id}/l/latest.json?no_subcategories=false"
expect(response.parsed_body["topic_list"]["topics"].length).to eq(2)
end
end
describe "titles for crawler layout" do
it "has no title for the default URL" do
topic
filter = Discourse.anonymous_filters[0]
get "/#{filter}", params: { _escaped_fragment_: "true" }
expect(response.body).to include(I18n.t("rss_description.posts"))
expect(response.body).to_not include(I18n.t("js.filters.with_topics", filter: filter))
end
it "has a title for non-default URLs" do
topic
filter = Discourse.anonymous_filters[1]
get "/#{filter}", params: { _escaped_fragment_: "true" }
expect(response.body).to include(I18n.t("js.filters.with_topics", filter: filter))
end
end
describe "rss feed discovery" do
it "advertises the feed for anonymous filters with a feed route" do
topic
get "/latest", params: { _escaped_fragment_: "true" }
expect(response.status).to eq(200)
expect(response.body).to include("latest.rss")
end
it "omits the feed for anonymous filters without a feed route" do
topic
sign_in(user)
SiteSetting.anonymous_menu_items
anonymous_filters = Discourse.anonymous_filters
Discourse.stubs(:anonymous_filters).returns(anonymous_filters + [:new])
get "/new", params: { _escaped_fragment_: "true" }
expect(response.status).to eq(200)
expect(response.body).not_to include("application/rss+xml")
end
end
describe "crawler homepage rendering" do
fab!(:homepage_topic) { Fabricate(:post).topic }
before do
SiteSetting.has_login_hint = false
SiteSetting.top_menu = "latest|new|bookmarks|categories"
end
it "renders the crawler homepage without error for each reachable filter" do
%w[latest categories top hot new bookmarks].each do |filter|
SiteSetting.default_homepage = filter
get "/", params: { _escaped_fragment_: "true" }
expect(response.status).to eq(200), "expected 200 for default_homepage=#{filter}"
expect(response.body).not_to include("finish-installation")
end
end
it "falls back to an anon-visible list when the homepage is a user-scoped filter" do
SiteSetting.default_homepage = "bookmarks"
get "/", params: { _escaped_fragment_: "true" }
expect(response.status).to eq(200)
expect(response.body).to include(homepage_topic.title)
end
end
describe "filter private messages by tag" do
fab!(:user)
fab!(:moderator)
fab!(:admin)
let(:tag) { Fabricate(:tag) }
let(:private_message) { Fabricate(:private_message_topic, user: admin) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.pm_tags_allowed_for_groups = "1|2|3"
Fabricate(:topic_tag, tag: tag, topic: private_message)
end
it "fails for non-staff users" do
sign_in(user)
get "/topics/private-messages-tags/#{user.username}/#{tag.name}.json"
expect(response.status).to eq(404)
end
it "fails for staff users when empty" do
SiteSetting.pm_tags_allowed_for_groups = ""
[moderator, admin].each do |user|
sign_in(user)
get "/topics/private-messages-tags/#{user.username}/#{tag.name}.json"
expect(response.status).to eq(404)
end
end
it "succeeds for staff users" do
[moderator, admin].each do |user|
sign_in(user)
get "/topics/private-messages-tags/#{user.username}/#{tag.name}.json"
expect(response.status).to eq(200)
end
end
it "supports a tag with a Unicode name" do
unicode_tag = Fabricate(:tag, name: "hello-🇺🇸")
Fabricate(:topic_tag, tag: unicode_tag, topic: private_message)
sign_in(admin)
get "/topics/private-messages-tags/#{admin.username}/#{UrlHelper.encode_component(unicode_tag.name)}.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].first["id"]).to eq(private_message.id)
end
it "supports direct links for authorized users" do
SiteSetting.pm_tags_allowed_for_groups = group.name
group.add(user)
sign_in(user)
get "/u/#{user.username}/messages/tags/#{tag.name}"
expect(response.status).to eq(200)
end
it "returns only visible tagged private messages" do
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:staff]
SiteSetting.pm_tags_allowed_for_groups = group.name
group.add(user)
group.update!(has_messages: true)
direct_message = Fabricate(:private_message_topic, user: admin, recipient: user)
group_message = Fabricate(:group_private_message_topic, user: admin, recipient_group: group)
Fabricate(:topic_tag, tag: tag, topic: direct_message)
Fabricate(:topic_tag, tag: tag, topic: group_message)
sign_in(user)
get "/topics/private-messages-group/#{user.username}/#{group.name}.json"
expect(response.status).to eq(404)
get "/topics/private-messages-tags/#{user.username}/#{tag.name}.json"
expect(response.status).to eq(200)
topic_ids = response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }
expect(topic_ids).to contain_exactly(direct_message.id)
end
end
describe "#private_messages_group" do
describe "#private_messages_group_new and #private_messages_group_unread" do
before do
group.add(user)
sign_in(user)
end
it "enforces can_see_group_messages? when personal messages are disabled for the user" do
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:staff]
get "/topics/private-messages-group/#{user.username}/#{group.name}/new.json"
expect(response.status).to eq(404)
get "/topics/private-messages-group/#{user.username}/#{group.name}/unread.json"
expect(response.status).to eq(404)
end
end
describe "when user not in personal_message_enabled_groups group" do
let!(:topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }
before do
group.add(user)
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:staff]
end
it "displays group private messages for an admin" do
sign_in(Fabricate(:admin))
get "/topics/private-messages-group/#{user.username}/#{group.name}.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].first["id"]).to eq(topic.id)
end
it "displays moderator-group private messages for a moderator" do
moderator = Fabricate(:moderator)
group = Group.find(Group::AUTO_GROUPS[:moderators])
Fabricate(:private_message_topic, allowed_groups: [group])
sign_in(moderator)
get "/topics/private-messages-group/#{moderator.username}/#{group.name}.json"
expect(response.status).to eq(200)
end
it "does not display private messages for a moderator's group" do
moderator = Fabricate(:moderator)
sign_in(moderator)
get "/topics/private-messages-group/#{user.username}/#{group.name}.json"
expect(response.status).to eq(404)
end
it "sorts group private messages by posts_count" do
topic2 = Fabricate(:private_message_topic, allowed_groups: [group])
topic3 = Fabricate(:private_message_topic, allowed_groups: [group])
2.times { Fabricate(:post, topic: topic2) }
Fabricate(:post, topic: topic3)
sign_in(Fabricate(:admin))
get "/topics/private-messages-group/#{user.username}/#{group.name}.json",
params: {
order: "posts",
}
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to eq(
[topic2.id, topic3.id, topic.id],
)
end
end
describe "with unicode_usernames disabled" do
before do
group.add(user)
sign_in(user)
SiteSetting.unicode_usernames = false
end
it "returns not found when the user does not belong to the group" do
Fabricate(:private_message_topic, allowed_groups: [group])
group.remove(user)
get "/topics/private-messages-group/#{user.username}/#{group.name}.json"
expect(response.status).to eq(404)
end
it "returns the group's private messages" do
topic = Fabricate(:private_message_topic, allowed_groups: [group])
get "/topics/private-messages-group/#{user.username}/awesomegroup.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].first["id"]).to eq(topic.id)
end
end
describe "with unicode_usernames enabled" do
before do
sign_in(user)
SiteSetting.unicode_usernames = true
end
it "Returns a 200 with unicode group name" do
unicode_group = Fabricate(:group, name: "群群组")
unicode_group.add(user)
topic = Fabricate(:private_message_topic, allowed_groups: [unicode_group])
get "/topics/private-messages-group/#{user.username}/#{UrlHelper.encode_component(unicode_group.name)}.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].first["id"]).to eq(topic.id)
end
end
end
describe "#group_topics" do
%i[user user2].each do |user|
let(user) do
user = Fabricate(:user)
group.add(user)
user
end
end
let!(:topic) { Fabricate(:topic, user: user) }
let!(:topic2) { Fabricate(:topic, user: user2) }
let!(:another_topic) { Fabricate(:topic) }
describe "when an invalid group name is given" do
it "returns the expected response" do
get "/topics/groups/something.json"
expect(response.status).to eq(404)
end
end
describe "for an anon user" do
describe "public visible group" do
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]).to be_present
end
end
describe "group restricted to logged-on-users" do
before { group.update!(visibility_level: Group.visibility_levels[:logged_on_users]) }
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(403)
end
end
describe "restricted group" do
before { group.update!(visibility_level: Group.visibility_levels[:staff]) }
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(403)
end
end
describe "group members visibility restricted to logged-on-users" do
before do
group.update!(members_visibility_level: Group.visibility_levels[:logged_on_users])
end
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(403)
end
end
end
describe "for a normal user" do
before { sign_in(Fabricate(:user)) }
describe "restricted group" do
before { group.update!(visibility_level: Group.visibility_levels[:staff]) }
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(403)
end
end
describe "group restricted to logged-on-users" do
before { group.update!(visibility_level: Group.visibility_levels[:logged_on_users]) }
it "returns the expected response" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(200)
end
end
end
describe "for a group user" do
before { sign_in(user) }
it "allows viewing topics started by group members" do
get "/topics/groups/#{group.name}.json"
expect(response.status).to eq(200)
topics = response.parsed_body["topic_list"]["topics"]
expect(topics.map { |topic| topic["id"] }).to contain_exactly(topic.id, topic2.id)
end
end
end
describe "RSS feeds" do
it "renders latest RSS" do
get "/latest.rss"
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
expect(response.headers["X-Robots-Tag"]).to eq("noindex")
end
it "localizes latest RSS from Accept-Language for clients classified as crawlers" do
SiteSetting.allow_user_locale = true
SiteSetting.set_locale_from_param = true
SiteSetting.set_locale_from_accept_language_header = true
SiteSetting.content_localization_enabled = true
SiteSetting.content_localization_crawler_param = true
SiteSetting.content_localization_supported_locales = "ja"
get "/latest.rss",
headers: {
"HTTP_USER_AGENT" => "curl/8.0",
"HTTP_ACCEPT" => "*/*",
"HTTP_ACCEPT_LANGUAGE" => "ja",
}
title = Nokogiri.XML(response.body).at_xpath("/rss/channel/title").text
expect(title).to eq("#{SiteSetting.title} - #{I18n.t("rss_description.latest", locale: :ja)}")
end
it "does not allow a topic author to forge RSS items" do
attacker = Fabricate(:user, refresh_auto_groups: true)
forged_title = "Forged RSS item"
sign_in(attacker)
post "/posts.json",
params: {
title: "Actual RSS topic title",
raw:
"[link](https://example.com \"]]></description></item><item><title>#{forged_title}</title><description><![CDATA[\")",
category: topic.category_id,
}
expect(response.status).to eq(200)
sign_out
get "/latest.rss"
document = Nokogiri.XML(response.body) { |config| config.strict }
titles = document.xpath("/rss/channel/item/title").map(&:text)
expect(response.status).to eq(200)
expect(titles).to include("Actual RSS topic title")
expect(titles).not_to include(forged_title)
end
it "renders latest RSS with query params" do
get "/latest.rss?status=closed"
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
expect(response.body).to_not include("<item>")
end
it "advertises sanitized filtered feed URLs in RSS metadata" do
TopTopic.create!(topic: topic, yearly_score: 1.0)
api_key = ApiKey.create!(user_id: user.id, created_by_id: Discourse.system_user)
get "/latest.rss",
params: {
exclude_tag: "excludeme",
api_key: api_key.key,
api_username: user.username_lower,
}
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
latest_doc = Nokogiri::XML::Document.parse(response.body)
latest_atom_link =
URI.parse(
latest_doc.at_xpath(
"/rss/channel/atom:link",
{ "atom" => "http://www.w3.org/2005/Atom" },
)[
"href"
],
)
latest_link = URI.parse(latest_doc.at_xpath("/rss/channel/link").text)
expect(latest_atom_link.path).to eq("/latest.rss")
expect(Rack::Utils.parse_nested_query(latest_atom_link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
)
expect(latest_link.path).to eq("/latest")
expect(Rack::Utils.parse_nested_query(latest_link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
)
get "/top.rss",
params: {
period: "yearly",
exclude_tag: "excludeme",
api_key: api_key.key,
api_username: user.username_lower,
}
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
top_doc = Nokogiri::XML::Document.parse(response.body)
top_atom_link =
URI.parse(
top_doc.at_xpath("/rss/channel/atom:link", { "atom" => "http://www.w3.org/2005/Atom" })[
"href"
],
)
top_link = URI.parse(top_doc.at_xpath("/rss/channel/link").text)
expect(top_atom_link.path).to eq("/top.rss")
expect(Rack::Utils.parse_nested_query(top_atom_link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
"period" => "yearly",
)
expect(top_link.path).to eq("/top")
expect(Rack::Utils.parse_nested_query(top_link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
"period" => "yearly",
)
end
it "renders links correctly with subfolder" do
set_subfolder "/forum"
_post = Fabricate(:post, topic: topic, user: user)
get "/latest.rss"
expect(response.status).to eq(200)
expect(response.body).to_not include("/forum/forum")
expect(response.body).to include("http://test.localhost/forum/t/#{topic.slug}")
end
it "renders top RSS" do
get "/top.rss"
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
end
it "errors for invalid periods on top RSS" do
get "/top.rss?period=decadely"
expect(response.status).to eq(400)
end
TopTopic.periods.each do |period|
it "renders #{period} top RSS" do
get "/top.rss?period=#{period}"
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
end
end
describe "RSS feeds ignore current user" do
fab!(:muted_topic, :topic)
before do
Fabricate(:post, topic: muted_topic)
sign_in(user)
TopicUser.change(
user.id,
muted_topic.id,
notification_level: TopicUser.notification_levels[:muted],
)
end
it "includes muted topics in top RSS" do
TopTopic.create!(topic: muted_topic, yearly_score: 1.0)
get "/top.rss?period=yearly"
expect(response.status).to eq(200)
expect(response.body).to include(muted_topic.title)
end
it "includes muted topics in hot RSS" do
TopicHotScore.create!(topic: muted_topic, score: 1.0)
get "/hot.rss"
expect(response.status).to eq(200)
expect(response.body).to include(muted_topic.title)
end
end
describe "exclude_tag" do
fab!(:tag) { Fabricate(:tag, name: "excludeme") }
fab!(:tagged_topic) { Fabricate(:topic, tags: [tag]) }
fab!(:untagged_topic, :topic)
before do
Fabricate(:post, topic: tagged_topic)
Fabricate(:post, topic: untagged_topic)
end
it "is respected in latest RSS" do
get "/latest.rss?exclude_tag=excludeme"
expect(response.status).to eq(200)
expect(response.body).to include(untagged_topic.title)
expect(response.body).not_to include(tagged_topic.title)
end
it "is respected in top RSS" do
TopTopic.create!(topic: tagged_topic, yearly_score: 1.0)
TopTopic.create!(topic: untagged_topic, yearly_score: 1.0)
get "/top.rss?period=yearly&exclude_tag=excludeme"
expect(response.status).to eq(200)
expect(response.body).to include(untagged_topic.title)
expect(response.body).not_to include(tagged_topic.title)
end
it "is respected in hot RSS" do
TopicHotScore.create!(topic: tagged_topic, score: 1.0)
TopicHotScore.create!(topic: untagged_topic, score: 1.0)
get "/hot.rss?exclude_tag=excludeme"
expect(response.status).to eq(200)
expect(response.body).to include(untagged_topic.title)
expect(response.body).not_to include(tagged_topic.title)
end
it "is respected in user topics RSS" do
sign_in(user)
tagged_topic.update!(user: user)
untagged_topic.update!(user: user)
get "/u/#{user.username}/activity/topics.rss?exclude_tag=excludeme"
expect(response.status).to eq(200)
expect(response.body).to include(untagged_topic.title)
expect(response.body).not_to include(tagged_topic.title)
end
end
end
describe "Top" do
it "renders top" do
get "/top"
expect(response.status).to eq(200)
end
it "renders top with a period" do
get "/top?period=weekly"
expect(response.status).to eq(200)
end
it "errors for invalid periods on top" do
get "/top?period=decadely"
expect(response.status).to eq(400)
end
describe "per page" do
it "uses value from params when present" do
get "/top.json?per_page=5"
expect(response.parsed_body["topic_list"]["per_page"]).to eq(5)
end
it "uses SiteSetting.topics_per_period_in_top_page when per_page param isn't present" do
SiteSetting.topics_per_period_in_top_page = 32
get "/top.json"
expect(response.parsed_body["topic_list"]["per_page"]).to eq(32)
end
end
end
describe "category" do
context "when in a category" do
let(:category) { Fabricate(:category_with_definition) }
let(:group) { Fabricate(:group) }
let(:private_category) { Fabricate(:private_category, group: group) }
context "without access to see the category" do
it "responds with a 404 error" do
get "/c/#{private_category.slug}/l/latest"
expect(response.status).to eq(404)
end
end
context "with access to see the category" do
it "returns the category list" do
get "/c/#{category.slug}/#{category.id}/l/latest"
expect(response.status).to eq(200)
end
end
context "with encoded slug in the category" do
let(:category) { Fabricate(:category, slug: "தமிழ்") }
before { SiteSetting.slug_generation_method = "encoded" }
it "returns the encoded-slug category list" do
get "/c/#{category.slug}/#{category.id}/l/latest"
expect(response.status).to eq(200)
end
end
context "with a link that has a parent slug, slug and id in its path" do
let(:child_category) { Fabricate(:category_with_definition, parent_category: category) }
context "with valid slug" do
it "returns the child-category list" do
get "/c/#{category.slug}/#{child_category.slug}/#{child_category.id}/l/latest"
expect(response.status).to eq(200)
end
end
context "with invalid slug" do
it "redirects" do
get "/c/random_slug/another_random_slug/#{child_category.id}/l/latest"
expect(response).to redirect_to("#{child_category.url}/l/latest")
end
end
end
context "when another category exists with a number at the beginning of its name" do
# One category has another category's id at the beginning of its name
let!(:other_category) do
# Our validations don't allow this to happen now, but did historically
Fabricate(
:category_with_definition,
name: "#{category.id} name",
slug: "will-be-changed",
).tap { |category| category.update_column(:slug, "#{category.id}-name") }
end
it "uses the correct category" do
get "/c/#{other_category.slug}/#{other_category.id}/l/latest.json"
expect(response.status).to eq(200)
body = response.parsed_body
expect(body["topic_list"]["topics"].first["category_id"]).to eq(other_category.id)
end
end
context "with a child category" do
let(:sub_category) { Fabricate(:category_with_definition, parent_category_id: category.id) }
context "when parent and child are requested" do
it "returns the nested child-category list" do
get "/c/#{category.slug}/#{sub_category.slug}/#{sub_category.id}/l/latest"
expect(response.status).to eq(200)
end
end
context "when child is requested with the wrong parent" do
it "responds with a 404 error" do
get "/c/not-the-right-slug/#{sub_category.slug}/l/latest"
expect(response.status).to eq(404)
end
end
end
describe "feed" do
it "renders RSS" do
get "/c/#{category.slug}/#{category.id}.rss"
expect(response.status).to eq(200)
expect(response.media_type).to eq("application/rss+xml")
end
it "renders RSS in subfolder correctly" do
set_subfolder "/forum"
get "/c/#{category.slug}/#{category.id}.rss"
expect(response.status).to eq(200)
expect(response.body).to_not include("/forum/forum")
expect(response.body).to include("http://test.localhost/forum/c/#{category.slug}")
end
it "respects exclude_tag query param" do
tag = Fabricate(:tag, name: "excludeme")
tagged_topic = Fabricate(:topic, category: category, tags: [tag])
Fabricate(:post, topic: tagged_topic)
untagged_topic = Fabricate(:topic, category: category)
Fabricate(:post, topic: untagged_topic)
get "/c/#{category.slug}/#{category.id}.rss?exclude_tag=excludeme"
expect(response.status).to eq(200)
expect(response.body).to include(untagged_topic.title)
expect(response.body).not_to include(tagged_topic.title)
end
it "does not advertise the route-derived category param in the self URL" do
get "/c/#{category.slug}/#{category.id}.rss?exclude_tag=excludeme"
expect(response.status).to eq(200)
doc = Nokogiri::XML::Document.parse(response.body)
atom_link =
URI.parse(
doc.at_xpath("/rss/channel/atom:link", { "atom" => "http://www.w3.org/2005/Atom" })[
"href"
],
)
link = URI.parse(doc.at_xpath("/rss/channel/link").text)
expect(atom_link.path).to eq("/c/#{category.slug}/#{category.id}.rss")
expect(Rack::Utils.parse_nested_query(atom_link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
)
expect(link.path).to eq("/c/#{category.slug}/#{category.id}")
expect(Rack::Utils.parse_nested_query(link.query.to_s)).to eq(
"exclude_tag" => "excludeme",
)
end
end
describe "category default views" do
it "has a top default view" do
category.update!(default_view: "top", default_top_period: "monthly")
get "/c/#{category.slug}/#{category.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["for_period"]).to eq("monthly")
end
it "falls back to the site default period when default_top_period is an unsupported value" do
SiteSetting.top_page_default_timeframe = "monthly"
category.update!(default_view: "top")
category.update_column(:default_top_period, "user~'^d'AND all")
get "/c/#{category.slug}/#{category.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["for_period"]).to eq("monthly")
end
it "has a default view of nil" do
category.update!(default_view: nil)
get "/c/#{category.slug}/#{category.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["for_period"]).to be_blank
end
it "has a default view of ''" do
category.update!(default_view: "")
get "/c/#{category.slug}/#{category.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["for_period"]).to be_blank
end
it "has a default view of latest" do
category.update!(default_view: "latest")
get "/c/#{category.slug}/#{category.id}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["for_period"]).to be_blank
end
def filter_for(default_view, path = "")
category.update!(default_view: default_view)
get "/c/#{category.slug}/#{category.id}#{path}.json"
expect(response.status).to eq(200)
response.parsed_body["topic_list"]["filter"]
end
it "falls back to latest when the default view is not a filter this request can reach" do
expect(filter_for("destroy")).to eq("latest")
expect(filter_for("categories")).to eq("latest")
expect(filter_for("unread")).to eq("latest")
end
it "honours a logged in only default view for logged in users" do
sign_in(user)
expect(filter_for("unread")).to eq("unread")
end
it "honours the default view when excluding subcategories" do
expect(filter_for("hot", "/none")).to eq("hot")
end
end
describe "renders canonical tag" do
it "for category default view" do
get "/c/#{category.slug}/#{category.id}"
expect(response.status).to eq(200)
expect(css_select("link[rel=canonical]").length).to eq(1)
end
it "for category latest view" do
get "/c/#{category.slug}/#{category.id}/l/latest"
expect(response.status).to eq(200)
expect(css_select("link[rel=canonical]").length).to eq(1)
end
it "keeps the page number on later pages of a category" do
category_url = "#{Discourse.base_url}/c/#{category.slug}/#{category.id}"
get "/c/#{category.slug}/#{category.id}?page=2&order=views"
expect(css_select("link[rel=canonical]").first["href"]).to eq("#{category_url}?page=2")
get "/c/#{category.slug}/#{category.id}/l/latest?page=2"
expect(css_select("link[rel=canonical]").first["href"]).to eq("#{category_url}?page=2")
get "/c/#{category.slug}/#{category.id}?page=0"
expect(css_select("link[rel=canonical]").first["href"]).to eq(category_url)
end
end
context "for category default view" do
let!(:amazing_category) { Fabricate(:category_with_definition, name: "Amazing Category") }
it "renders correct title" do
get "/c/#{amazing_category.slug}/#{amazing_category.id}"
expect(response.body).to have_tag "title", text: "Amazing Category - Discourse"
end
it "renders og:description and twitter:description without HTML tags" do
amazing_category.update!(
description: "This is <strong>bold</strong> and <em>italic</em> text",
)
get "/c/#{amazing_category.slug}/#{amazing_category.id}"
expect(response.body).to have_tag(
:meta,
with: {
property: "og:description",
content: "This is bold and italic text",
},
)
expect(response.body).to have_tag(
:meta,
with: {
name: "twitter:description",
content: "This is bold and italic text",
},
)
end
it "escapes the description exactly once instead of double-escaping entities" do
amazing_category.update!(description: "<p>Tom & Jerry’s adventures</p>")
get "/c/#{amazing_category.slug}/#{amazing_category.id}"
expect(response.body).to have_tag(
:meta,
with: {
name: "description",
content: "Tom & Jerry’s adventures",
},
)
expect(response.body).to have_tag(
:meta,
with: {
property: "og:description",
content: "Tom & Jerry’s adventures",
},
)
end
end
context "for category latest view" do
let!(:amazing_category) { Fabricate(:category_with_definition, name: "Amazing Category") }
it "renders correct title" do
SiteSetting.short_site_description = "Best community"
get "/c/#{amazing_category.slug}/#{amazing_category.id}/l/latest"
expect(response.body).to have_tag "title", text: "Amazing Category - Discourse"
end
end
end
end
describe "topics_by" do
fab!(:topic2) { Fabricate(:topic, user: user) }
fab!(:user2, :user)
before do
user.user_stat.update!(post_count: 1)
sign_in(user2)
end
it "responds with a list" do
get "/topics/created-by/#{user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(2)
end
it "supports a period in the username" do
user.update!(username: "myname.test")
get "/topics/created-by/#{user.username}", xhr: true
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(2)
end
context "with unicode usernames" do
before { SiteSetting.unicode_usernames = true }
it "returns more_topics_url in encoded form" do
stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 1) do
user.update!(username: "快快快")
get "/topics/created-by/#{UrlHelper.encode(user.username)}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["more_topics_url"]).to eq(
"/topics/created-by/%E5%BF%AB%E5%BF%AB%E5%BF%AB?page=1",
)
end
end
end
context "when `hide_profile` is true" do
before { user.user_option.update_columns(hide_profile: true) }
it "returns 404" do
get "/topics/created-by/#{user.username}.json"
expect(response.status).to eq(404)
end
it "responds with a list when profile hiding is disabled" do
SiteSetting.allow_users_to_hide_profile = false
get "/topics/created-by/#{user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(2)
end
end
end
describe "topics_by with public profiles hidden" do
fab!(:profile_user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:profile_topic) { Fabricate(:topic, user: profile_user) }
before do
profile_user.user_stat.update!(post_count: 1)
SiteSetting.hide_user_profiles_from_public = true
end
it "does not disclose a user's topics to anonymous users" do
get "/topics/created-by/#{profile_user.username}.json"
expect(response).to have_http_status(:not_found)
expect(response.body).not_to include(profile_topic.title)
end
end
describe "private_messages" do
it "returns 403 error when the user can't see private message" do
sign_in(Fabricate(:user))
get "/topics/private-messages/#{user.username}.json"
expect(response).to be_forbidden
end
it "succeeds when the user can see private messages" do
pm = Fabricate(:private_message_topic, user: Fabricate(:user))
pm.topic_allowed_users.create!(user: user)
sign_in(user)
get "/topics/private-messages/#{user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(1)
end
it "sorts private messages by activity" do
topic_ids = []
[1.year.ago, 1.week.ago, 1.month.ago].each do |date|
pm =
Fabricate(
:private_message_topic,
user: Fabricate(:user),
created_at: date,
bumped_at: date,
)
pm.topic_allowed_users.create!(user: user)
topic_ids << pm.id
end
sign_in(user)
get "/topics/private-messages/#{user.username}.json", params: { order: "activity" }
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].pluck("id")).to eq(
[topic_ids[1], topic_ids[2], topic_ids[0]],
)
end
end
describe "private_messages_sent" do
before do
pm = Fabricate(:private_message_topic, user: user)
Fabricate(:post, user: user, topic: pm, post_number: 1)
end
it "returns 403 error when the user can't see private message" do
sign_in(Fabricate(:user))
get "/topics/private-messages-sent/#{user.username}.json"
expect(response).to be_forbidden
end
it "succeeds when the user can see private messages" do
sign_in(user)
get "/topics/private-messages-sent/#{user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(1)
end
end
describe "#private_messages_unread" do
fab!(:pm_user, :user)
fab!(:pm) do
Fabricate(:private_message_topic).tap do |t|
t.allowed_users << pm_user
create_post(user: pm_user, topic_id: t.id)
end
end
it "returns 404 when the user can't see private message" do
sign_in(Fabricate(:user))
get "/topics/private-messages-unread/#{pm_user.username}.json"
expect(response.status).to eq(404)
end
it "succeeds when the user can see private messages" do
TopicUser.find_by(topic: pm, user: pm_user).update!(
notification_level: TopicUser.notification_levels[:tracking],
last_read_post_number: 0,
)
sign_in(pm_user)
get "/topics/private-messages-unread/#{pm_user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(1)
expect(json["topic_list"]["topics"][0]["id"]).to eq(pm.id)
end
end
describe "#private_messages_warnings" do
fab!(:target_user, :user)
fab!(:admin)
fab!(:moderator1, :moderator)
fab!(:moderator2, :moderator)
let(:create_args) do
{
title: "you need a warning buddy!",
raw: "you did something bad and I'm telling you about it!",
is_warning: true,
target_usernames: target_user.username,
archetype: Archetype.private_message,
}
end
let(:warning_post) do
creator = PostCreator.new(moderator1, create_args)
creator.create
end
let(:warning_topic) { warning_post.topic }
before { warning_topic }
it "returns 403 error for unrelated users" do
sign_in(Fabricate(:user))
get "/topics/private-messages-warnings/#{target_user.username}.json"
expect(response.status).to eq(403)
end
it "shows the warning to moderators and admins" do
[moderator1, moderator2, admin].each do |viewer|
sign_in(viewer)
get "/topics/private-messages-warnings/#{target_user.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(1)
expect(json["topic_list"]["topics"][0]["id"]).to eq(warning_topic.id)
end
end
it "does not show the warning as applying to the authoring moderator" do
sign_in(admin)
get "/topics/private-messages-warnings/#{moderator1.username}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(0)
end
end
describe "read" do
it "raises an error when not logged in" do
get "/read"
expect(response.status).to eq(404)
end
context "when logged in" do
it "returns the read-topic list" do
sign_in(user)
get "/read"
expect(response.status).to eq(200)
end
end
end
describe "best_periods_for" do
it "returns the applicable best periods" do
expect(ListController.best_periods_for(nil)).to eq([:all])
expect(ListController.best_periods_for(5.years.ago)).to eq([:all])
expect(ListController.best_periods_for(2.years.ago)).to eq(%i[yearly all])
expect(ListController.best_periods_for(6.months.ago)).to eq(%i[quarterly yearly all])
expect(ListController.best_periods_for(2.months.ago)).to eq(%i[monthly quarterly yearly all])
expect(ListController.best_periods_for(2.weeks.ago)).to eq(
%i[weekly monthly quarterly yearly all],
)
expect(ListController.best_periods_for(2.days.ago)).to eq(
%i[daily weekly monthly quarterly yearly all],
)
end
it "supports default period" do
expect(ListController.best_periods_for(nil, :yearly)).to eq(%i[yearly all])
expect(ListController.best_periods_for(nil, :quarterly)).to eq(%i[quarterly all])
expect(ListController.best_periods_for(nil, :monthly)).to eq(%i[monthly all])
expect(ListController.best_periods_for(nil, :weekly)).to eq(%i[weekly all])
expect(ListController.best_periods_for(nil, :daily)).to eq(%i[daily all])
end
end
describe "user_topics_feed" do
it "returns 404 if `hide_profile` user option is checked" do
user.user_option.update_columns(hide_profile: true)
get "/u/#{user.username}/activity/topics.rss"
expect(response.status).to eq(404)
end
end
describe "set_category" do
let(:category) { Fabricate(:category_with_definition) }
let(:subcategory) { Fabricate(:category_with_definition, parent_category_id: category.id) }
let(:subsubcategory) do
Fabricate(:category_with_definition, parent_category_id: subcategory.id)
end
before { SiteSetting.max_category_nesting = 3 }
it "redirects to URL with the updated slug" do
get "/c/hello/world/bye/#{subsubcategory.id}"
expect(response.status).to eq(301)
expect(response).to redirect_to(
"/c/#{category.slug}/#{subcategory.slug}/#{subsubcategory.slug}/#{subsubcategory.id}",
)
get "/c/#{category.slug}/#{subcategory.slug}/#{subsubcategory.slug}/#{subsubcategory.id}"
expect(response.status).to eq(200)
end
it "redirects to the canonical slug without altering query parameters" do
# Simulate a request with just the category ID in the path, and a query param that matches the ID
get "/c/#{category.id}.json?page=#{category.id}"
expect(response.status).to eq(301)
expect(response).to redirect_to("/c/#{category.slug}/#{category.id}.json?page=#{category.id}")
end
it "redirects to URL with correct case slug" do
category.update!(slug: "hello")
get "/c/Hello/#{category.id}"
expect(response).to redirect_to("/c/hello/#{category.id}")
get "/c/hello/#{category.id}"
expect(response.status).to eq(200)
end
it "does not disclose restricted topic titles through category route permalink fallbacks" do
private_category = Fabricate(:private_category, group: Fabricate(:group))
private_topic =
Fabricate(:topic, category: private_category, title: "Restricted list fallback topic title")
Permalink.create!(url: "c/old-category/999", topic: private_topic)
get "/c/old-category/999"
expect(response).to have_http_status(:not_found)
expect(response.headers["Location"]).to be_nil
expect(response.body).not_to include(private_topic.title)
end
context "with encoded slugs" do
it "does not create a redirect loop" do
category = Fabricate(:category)
category.update_columns(slug: CGI.escape("systèmes"))
get "/c/syst%C3%A8mes/#{category.id}"
expect(response.status).to eq(200)
end
end
context "with lowercase encoded slugs" do
it "does not create a redirect loop" do
category = Fabricate(:category)
category.update_columns(slug: CGI.escape("systèmes").downcase)
get "/c/syst%C3%A8mes/#{category.id}"
expect(response.status).to eq(200)
end
end
context "with encoded non-ASCII slugs" do
before { SiteSetting.slug_generation_method = "encoded" }
{
"zh_CN" => %w[产品 问题],
"zh_TW" => %w[產品 問題],
"ja" => %w[製品 質問],
}.each do |locale, (old_name, new_name)|
context "with a #{locale} category" do
let(:category) { Fabricate(:category, name: old_name) }
let!(:old_slug) { category.slug }
before { category.update!(slug: new_name) }
it "redirects an outdated slug to the current encoded slug" do
get "/c/#{old_slug}/#{category.id}"
expect(response.status).to eq(301)
expect(response).to redirect_to("/c/#{CGI.escape(new_name)}/#{category.id}")
get response.location
expect(response.status).to eq(200)
end
it "redirects the slug without an id to the URL with the id" do
get "/c/#{category.slug}"
expect(response.status).to eq(301)
expect(response).to redirect_to("/c/#{category.slug}/#{category.id}")
end
it "keeps the filter and the query string" do
get "/c/#{old_slug}/#{category.id}/l/latest?page=2"
expect(response.status).to eq(301)
expect(response).to redirect_to("/c/#{category.slug}/#{category.id}/l/latest?page=2")
end
it "keeps the format" do
get "/c/#{old_slug}/#{category.id}.json"
expect(response.status).to eq(301)
expect(response).to redirect_to("/c/#{category.slug}/#{category.id}.json")
end
end
end
it "redirects an outdated parent slug for a subcategory" do
parent = Fabricate(:category, name: "製品")
old_parent_slug = parent.slug
subcategory = Fabricate(:category, name: "質問", parent_category: parent)
parent.update!(slug: "新製品")
get "/c/#{old_parent_slug}/#{subcategory.slug}/#{subcategory.id}/none"
expect(response.status).to eq(301)
expect(response).to redirect_to(
"/c/#{parent.slug}/#{subcategory.slug}/#{subcategory.id}/none",
)
end
it "keeps the subfolder" do
set_subfolder "/forum"
category = Fabricate(:category, name: "製品")
old_slug = category.slug
category.update!(slug: "質問")
get "/c/#{old_slug}/#{category.id}/l/latest"
expect(response.status).to eq(301)
expect(response).to redirect_to("/forum/c/#{category.slug}/#{category.id}/l/latest")
end
end
context "with subfolder" do
it "main category redirects to URL containing the updated slug" do
set_subfolder "/forum"
get "/c/#{category.slug}"
expect(response.status).to eq(301)
expect(response).to redirect_to("/forum/c/#{category.slug}/#{category.id}")
end
it "sub-sub-category redirects to URL containing the updated slug" do
set_subfolder "/forum"
get "/c/hello/world/bye/#{subsubcategory.id}"
expect(response.status).to eq(301)
expect(response).to redirect_to(
"/forum/c/#{category.slug}/#{subcategory.slug}/#{subsubcategory.slug}/#{subsubcategory.id}",
)
end
end
context "when redirect raises an unsafe redirect error" do
let(:fake_logger) { FakeLogger.new }
before do
ListController
.any_instance
.stubs(:redirect_to)
.raises(ActionController::Redirecting::UnsafeRedirectError)
Rails.logger.broadcast_to(fake_logger)
end
after { Rails.logger.stop_broadcasting_to(fake_logger) }
it "renders a 404" do
get "/c/hello/world/bye/#{subsubcategory.id}"
expect(response).to have_http_status :not_found
end
it "doesn’t log an error" do
get "/c/hello/world/bye/#{subsubcategory.id}"
expect(fake_logger.fatals).to be_empty
end
end
context "when provided slug is gibberish" do
it "redirects to the proper category" do
get "/c/summit'%22()&%25%3Czzz%3E%3CScRiPt%20%3EqlJ2(9585)%3C%2FScRiPt%3E/#{category.id}"
expect(response).to have_http_status :moved_permanently
expect(response).to redirect_to("/c/#{category.slug}/#{category.id}")
end
end
end
describe "category_noindex modifier" do
fab!(:category, :category_with_definition)
fab!(:subcategory) { Fabricate(:category_with_definition, parent_category_id: category.id) }
fab!(:topic_in_category) { Fabricate(:topic, category: category) }
let(:plugin) { Plugin::Instance.new }
let(:modifier_args) { [] }
let(:noindex_category_ids) { [] }
let(:modifier) do
Proc.new do |noindex, modifier_category|
modifier_args << [noindex, modifier_category]
noindex || noindex_category_ids.include?(modifier_category.id)
end
end
before { DiscoursePluginRegistry.register_modifier(plugin, :category_noindex, &modifier) }
after { DiscoursePluginRegistry.unregister_modifier(plugin, :category_noindex, &modifier) }
it "receives false and the category, and sends no noindex by default" do
get "/c/#{category.slug}/#{category.id}", headers: { "HTTP_USER_AGENT" => "Googlebot" }
expect(response.status).to eq(200)
expect(modifier_args).to eq([[false, category]])
expect(response.headers["X-Robots-Tag"]).to eq(nil)
end
it "receives the subcategory for its own lists" do
get "/c/#{category.slug}/#{subcategory.slug}/#{subcategory.id}"
expect(response.status).to eq(200)
expect(modifier_args).to eq([[false, subcategory]])
end
it "lets a plugin send noindex on every topic list of the category" do
SiteSetting.enable_markdown_endpoints = true
noindex_category_ids << category.id
[
"/c/#{category.slug}/#{category.id}",
"/c/#{category.slug}/#{category.id}?page=1",
"/c/#{category.slug}/#{category.id}/none",
"/c/#{category.slug}/#{category.id}/l/top",
"/c/#{category.slug}/#{category.id}/l/latest.json",
"/c/#{category.slug}/#{category.id}.md",
"/latest?category=#{category.id}",
"/top?category=#{category.slug}",
].each do |path|
get path, headers: { "HTTP_USER_AGENT" => "Googlebot" }
expect(response.status).to eq(200), path
expect(response.headers["X-Robots-Tag"]).to eq("noindex"), path
end
end
it "keeps the site-wide nofollow when the site is not indexable" do
SiteSetting.allow_index_in_robots_txt = false
noindex_category_ids << category.id
get "/c/#{category.slug}/#{category.id}.json"
expect(response.headers["X-Robots-Tag"]).to eq("noindex, nofollow")
end
it "is not consulted for topic lists outside a category" do
noindex_category_ids << category.id
get "/latest", headers: { "HTTP_USER_AGENT" => "Googlebot" }
expect(response.status).to eq(200)
expect(modifier_args).to be_empty
expect(response.headers["X-Robots-Tag"]).to eq(nil)
end
end
describe "shared drafts" do
fab!(:category1, :category)
fab!(:category2, :category)
fab!(:topic1) { Fabricate(:topic, category: category1) }
fab!(:topic2) { Fabricate(:topic, category: category2) }
fab!(:shared_draft_topic) { Fabricate(:topic, category: category1) }
fab!(:shared_draft) { Fabricate(:shared_draft, topic: shared_draft_topic, category: category2) }
it "are not displayed if they are disabled" do
SiteSetting.shared_drafts_category = ""
sign_in(admin)
get "/c/#{category1.slug}/#{category1.id}.json"
expect(response.parsed_body["topic_list"]["shared_drafts"]).to eq(nil)
expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
topic1.id,
shared_draft_topic.id,
)
end
it "are displayed in both shared drafts category and target category" do
SiteSetting.shared_drafts_category = category1.id
sign_in(admin)
get "/c/#{category1.slug}/#{category1.id}.json"
expect(response.parsed_body["topic_list"]["shared_drafts"]).to be_nil
expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
topic1.id,
shared_draft_topic.id,
)
get "/c/#{category2.slug}/#{category2.id}.json"
expect(
response.parsed_body["topic_list"]["shared_drafts"].map { |t| t["id"] },
).to contain_exactly(shared_draft_topic.id)
expect(response.parsed_body["topic_list"]["topics"].map { |t| t["id"] }).to contain_exactly(
topic2.id,
)
end
end
describe "body class" do
it "pre-renders the correct body class for categories" do
c = Fabricate(:category, slug: "myparentslug")
sub_c = Fabricate(:category, parent_category: c, slug: "mychildslug")
get "/c/#{c.slug}/#{sub_c.slug}/#{sub_c.id}"
expect(response.body).to have_tag "body", with: { class: "category-myparentslug-mychildslug" }
end
end
describe "#filter" do
fab!(:category) { Fabricate(:category, slug: "category-slug") }
fab!(:tag) { Fabricate(:tag, name: "tag1") }
fab!(:group)
fab!(:private_category) { Fabricate(:private_category, group:, slug: "private-category-slug") }
fab!(:private_message_topic)
fab!(:topic_in_private_category) { Fabricate(:topic, category: private_category) }
fab!(:user2, :user)
it "does not return topics the user cannot view" do
sign_in(user)
get "/filter.json"
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "does not return topics an anonymous user cannot view" do
get "/filter.json"
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "returns category definition topics if `show_category_definitions_in_topic_lists` site setting is enabled" do
category_topic = Fabricate(:topic, category: category)
category.update!(topic: category_topic)
SiteSetting.show_category_definitions_in_topic_lists = true
sign_in(user)
get "/filter.json"
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id, category_topic.id)
end
it "does not return category definition topics if `show_category_definitions_in_topic_lists` site setting is disabled" do
category_topic = Fabricate(:topic, category: category)
category.update!(topic: category_topic)
SiteSetting.show_category_definitions_in_topic_lists = false
sign_in(user)
get "/filter.json"
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "accepts the page query parameter" do
topic_with_tag = Fabricate(:topic, tags: [tag])
topic2_with_tag = Fabricate(:topic, tags: [tag])
stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 1) do
sign_in(user)
get "/filter.json", params: { q: "tags:tag1" }
expect(response.status).to eq(200)
parsed = response.parsed_body
expect(parsed["topic_list"]["topics"].length).to eq(1)
expect(parsed["topic_list"]["topics"].first["id"]).to eq(topic2_with_tag.id)
get "/filter.json", params: { q: "tags:tag1", page: 1 }
expect(response.status).to eq(200)
parsed = response.parsed_body
expect(parsed["topic_list"]["topics"].length).to eq(1)
expect(parsed["topic_list"]["topics"].first["id"]).to eq(topic_with_tag.id)
end
end
it "returns topics in the order specified by the topic filter" do
topic_1 =
Fabricate(:topic, bumped_at: 3.days.ago, pinned_at: 1.day.ago, pinned_globally: true)
topic_2 = Fabricate(:topic, bumped_at: 2.days.ago)
topic_3 = Fabricate(:topic, bumped_at: 1.day.ago)
get "/filter.json", params: { q: "topic:#{topic_3.id},#{topic_1.id},#{topic_2.id}" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
[topic_3.id, topic_1.id, topic_2.id],
)
end
it "keeps query params encoded in more_topics_url when unicode usernames are enabled" do
SiteSetting.unicode_usernames = true
topic_1 = Fabricate(:topic)
Fabricate(:post, topic: topic_1, user: user)
Fabricate(:post, topic: topic_1, user: user2)
topic_2 = Fabricate(:topic)
Fabricate(:post, topic: topic_2, user: user)
Fabricate(:post, topic: topic_2, user: user2)
stub_const(TopicQuery, "DEFAULT_PER_PAGE_COUNT", 1) do
sign_in(user)
get "/filter.json", params: { q: "users:#{user.username}+#{user2.username}" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["more_topics_url"]).to eq(
"/filter?no_definitions=true&page=1&q=users%3A#{user.username}%2B#{user2.username}",
)
end
end
it "includes filter_option_info in the response" do
get "/filter.json"
parsed = response.parsed_body
expect(response.status).to eq(200)
expect(parsed["topic_list"]["filter_option_info"].length).to eq(
TopicsFilter.option_info(Guardian.new).length,
)
end
it "filters with the tag_group option" do
topic_with_tag = Fabricate(:topic, tags: [tag])
topic2_with_tag = Fabricate(:topic, tags: [tag])
tag_group = Fabricate(:tag_group, tags: [tag])
sign_in(user)
get "/filter.json", params: { q: "tag_group:#{tag_group.name}" }
parsed = response.parsed_body
expect(response.status).to eq(200)
expect(parsed["topic_list"]["topics"].length).to eq(2)
expect(parsed["topic_list"]["topics"].map { |topic| topic["id"] }).to contain_exactly(
topic_with_tag.id,
topic2_with_tag.id,
)
end
describe "when filtering with the `created-by:<username>` filter" do
fab!(:topic2) { Fabricate(:topic, user: admin) }
before do
topic.update!(user: user)
user.update!(username: "username")
admin.update!(username: "username2")
end
it "returns only topics created by the user when `q` query param is `created-by:username`" do
sign_in(user)
get "/filter.json", params: { q: "created-by:username" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "returns only topics created by either user when `q` query param is `created-by:username,username2`" do
sign_in(user)
get "/filter.json", params: { q: "created-by:username,username2" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id, topic2.id)
end
end
describe "when filtering with the `category:<category_slug>` filter" do
fab!(:topic_in_category) { Fabricate(:topic, category:) }
it "does not return any topics when `q` query param is `category:private-category-slug` and user is not allowed to see category" do
sign_in(user)
get "/filter.json", params: { q: "category:private-category-slug" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq([])
end
it "returns only topics in the category when `q` query param is `category:private-category-slug` and user can see category" do
group.add(user)
sign_in(user)
get "/filter.json", params: { q: "category:private-category-slug" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic_in_private_category.id)
end
end
describe "when filtering with the `in:<topic_notification_level>` filter" do
fab!(:user_muted_topic) do
Fabricate(:topic).tap do |topic|
TopicUser.change(
user.id,
topic.id,
notification_level: TopicUser.notification_levels[:muted],
)
end
end
fab!(:user_tracking_topic) do
Fabricate(:topic).tap do |topic|
TopicUser.change(
user.id,
topic.id,
notification_level: TopicUser.notification_levels[:tracking],
)
end
end
it "does not return topics that are muted by the user when `q` query param does not include `in:muted`" do
sign_in(user)
get "/filter.json", params: { q: "in:tracking" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
[user_tracking_topic.id],
)
end
it "only return topics that are muted by the user when `q` query param is `in:muted`" do
sign_in(user)
get "/filter.json", params: { q: "in:muted" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
[user_muted_topic.id],
)
end
end
describe "when ordering using the `order:` filter" do
fab!(:topic2) { Fabricate(:topic, views: 2) }
fab!(:topic3) { Fabricate(:topic, views: 3) }
fab!(:topic4) { Fabricate(:topic, views: 1) }
it "return topics ordered by topic bumped at date in descending order when `q` query param is not present" do
sign_in(user)
get "/filter.json"
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
[topic4.id, topic3.id, topic2.id, topic.id],
)
end
it "return topics ordered by views when `q` query param is `order:views`" do
sign_in(user)
get "/filter.json", params: { q: "order:views" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
[topic3.id, topic2.id, topic4.id, topic.id],
)
end
end
describe "when filtering by status" do
fab!(:group)
fab!(:private_category) { Fabricate(:private_category, group: group) }
fab!(:topic_in_private_category) { Fabricate(:topic, category: private_category) }
it "does not return topics that are unlisted when `q` query param is `status:unlisted` for a user that cannot view unlisted topics" do
Topic.update_all(deleted_at: Time.current)
topic.update!(visible: false)
sign_in(user)
get "/filter.json", params: { q: "status:unlisted" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq([])
end
it "returns topics that are unlisted when `q` query param is `status:unlisted` for a user that can view unlisted topics" do
Topic.update_all(visible: true)
topic.update!(visible: false)
sign_in(admin)
get "/filter.json", params: { q: "status:unlisted" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "ignores the `status` filter for a user that cannot view deleted topics when `q` query param is `status:deleted`" do
Topic.update_all(deleted_at: nil)
topic.update!(deleted_at: Time.zone.now)
sign_in(user)
get "/filter.json", params: { q: "status:deleted" }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq([])
end
it "returns topics that are deleted when `q` query param is `status:deleted` for a user that can view deleted topics" do
Topic.update_all(deleted_at: nil)
topic.update!(deleted_at: Time.zone.now)
sign_in(admin)
get "/filter.json", params: { q: "status:deleted" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
it "does not return topics from read restricted categories when `q` query param is `status:public`" do
group.add(user)
sign_in(user)
get "/filter.json", params: { q: "status:public" }
expect(response.status).to eq(200)
expect(
response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },
).to contain_exactly(topic.id)
end
end
end
describe "#new" do
def extract_topic_ids(response)
response.parsed_body["topic_list"]["topics"].map { |topics| topics["id"] }
end
context "when unified new is enabled for the user" do
fab!(:category)
fab!(:tag)
fab!(:new_reply) { Fabricate(:new_reply_topic, current_user: user) }
fab!(:new_topic) { Fabricate(:post).topic }
fab!(:old_topic) { Fabricate(:read_topic, current_user: user) }
fab!(:new_reply_in_category) do
Fabricate(:new_reply_topic, category: category, current_user: user)
end
fab!(:new_topic_in_category) do
Fabricate(:post, topic: Fabricate(:topic, category: category)).topic
end
fab!(:old_topic_in_category) do
Fabricate(:read_topic, category: category, current_user: user)
end
fab!(:new_reply_with_tag) { Fabricate(:new_reply_topic, tags: [tag], current_user: user) }
fab!(:new_topic_with_tag) { Fabricate(:post, topic: Fabricate(:topic, tags: [tag])).topic }
fab!(:old_topic_with_tag) { Fabricate(:read_topic, tags: [tag], current_user: user) }
before do
TopicUser.update_last_read(user, topic.id, 1, 1, 1)
SiteSetting.enable_unified_new = true
sign_in(user)
end
it "returns new topics and topics with new replies" do
get "/new.json"
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(
new_reply.id,
new_topic.id,
new_reply_in_category.id,
new_topic_in_category.id,
new_reply_with_tag.id,
new_topic_with_tag.id,
)
end
context "when the subset param is set to topics" do
it "returns only new topics" do
get "/new.json", params: { subset: "topics" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(
new_topic.id,
new_topic_in_category.id,
new_topic_with_tag.id,
)
end
end
context "when the subset param is set to replies" do
it "returns only topics with new replies" do
get "/new.json", params: { subset: "replies" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(
new_reply.id,
new_reply_in_category.id,
new_reply_with_tag.id,
)
end
end
context "when filtering the list to a specific category" do
it "returns new topics in that category" do
get "/c/#{category.slug}/#{category.id}/l/new.json"
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_topic_in_category.id, new_reply_in_category.id)
end
it "respects the subset param" do
get "/c/#{category.slug}/#{category.id}/l/new.json", params: { subset: "topics" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_topic_in_category.id)
get "/c/#{category.slug}/#{category.id}/l/new.json", params: { subset: "replies" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_reply_in_category.id)
end
end
context "when filtering the list to topics with a specific tag" do
it "returns new topics with the specified tag" do
get "/tag/#{tag.name}/l/new.json"
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_topic_with_tag.id, new_reply_with_tag.id)
end
it "respects the subset param" do
get "/tag/#{tag.name}/l/new.json", params: { subset: "topics" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_topic_with_tag.id)
get "/tag/#{tag.name}/l/new.json", params: { subset: "replies" }
ids = extract_topic_ids(response)
expect(ids).to contain_exactly(new_reply_with_tag.id)
end
end
end
end
context "when content localization is enabled" do
fab!(:category)
before do
SiteSetting.content_localization_enabled = true
topic.update!(category:)
end
describe "when tl param is absent" do
fab!(:pt_topic) do
Fabricate(
:topic_localization,
topic:,
locale: "pt",
title: "This is a localized portuguese title",
)
end
fab!(:pt_category) do
Fabricate(:category_localization, category:, locale: "pt", name: "Localized Category Name")
end
it "localizes topic title for crawler to default locale when localization exists" do
# topic is in english but default locale is portuguese
topic.update!(locale: "en")
topic.category.update!(locale: "en")
SiteSetting.default_locale = "pt"
filter = Discourse.anonymous_filters[0]
get "/#{filter}"
expect(response.body).to include(pt_topic.title)
expect(response.body).to include(pt_category.name)
end
it "leaves topic title as-is if no localization" do
# no spanish localizations exist for the default locale spanish
topic.update!(locale: "en")
SiteSetting.default_locale = "es"
filter = Discourse.anonymous_filters[0]
get "/#{filter}"
expect(response.body).to include(topic.title)
expect(response.body).to include(category.name)
expect(response.body).not_to include(pt_topic.title)
expect(response.body).not_to include(pt_category.name)
end
end
describe "when tl param is present ?tl=ja" do
fab!(:ja_topic) { Fabricate(:topic_localization, topic:, locale: "ja", title: "こんにちは世界") }
fab!(:ja_category) do
Fabricate(:category_localization, category:, locale: "ja", name: "カテゴリ名")
end
before do
SiteSetting.set_locale_from_param = true
topic.update!(locale: "en")
topic.category.update!(locale: "en")
end
it "localizes topic title for crawler" do
get "/#{Discourse.anonymous_filters[0]}", params: { tl: "ja" }
expect(response.body).to include(ja_topic.title)
expect(response.body).to include(ja_category.name)
end
end
it "does not issue N+1 queries while loading localizations" do
Fabricate.times(5, :topic, category:, locale: "en")
Topic.all.each { |t| Fabricate(:topic_localization, topic: t, locale: "ja") }
initial_sql_queries =
track_sql_queries do
get "/#{Discourse.anonymous_filters[0]}", params: { tl: "ja" }
expect(response.status).to eq(200)
end.select { |q| q.include?("_localizations") }.count
new_category = Fabricate(:category, locale: "en")
Fabricate(:category_localization, category: new_category, locale: "ja")
new_topic = Fabricate(:topic, category: new_category, locale: "en")
Fabricate(:topic_localization, topic: new_topic, locale: "ja")
new_sql_queries =
track_sql_queries do
get "/#{Discourse.anonymous_filters[0]}", params: { tl: "ja" }
expect(response.status).to eq(200)
end.select { |q| q.include?("_localizations") }.count
expect(new_sql_queries).to eq(initial_sql_queries)
end
end
end