A platform for community discussion. Free, open, simple.
DEV: Improve test coverage for `/filters` route (#20890)
Alan Guo Xiang Tana154d7207dcb
diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 6cd361baf37..28ad84a6a6a 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -269,13 +269,12 @@ class TopicQueryenddef list_filter- create_list(- :filter,- {},+ results =TopicsFilter.new(guardian: @guardian, scope: latest_results).filter_from_query_string(@options[:q],- ),- )+ )++ create_list(:filter, {}, results)enddef list_readdiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 413567dc3b4..06cb1aaa7c1 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1088,8 +1088,6 @@ RSpec.describe ListController dodescribe "#filter" dofab!(:category) { Fabricate(:category) }fab!(:tag) { Fabricate(:tag, name: "tag1") }- fab!(:topic_with_tag) { Fabricate(:topic, tags: [tag]) }- fab!(:topic2_with_tag) { Fabricate(:topic, tags: [tag]) }before { SiteSetting.experimental_topics_filter = true }@@ -1121,16 +1119,9 @@ RSpec.describe ListController doexpect(response.status).to eq(200)- parsed = response.parsed_body-- expect(parsed["topic_list"]["topics"].length).to eq(4)-- expect(parsed["topic_list"]["topics"].map { |topic| topic["id"] }).to contain_exactly(- topic.id,- topic_with_tag.id,- topic2_with_tag.id,- category_topic.id,- )+ expect(+ response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },+ ).to contain_exactly(topic.id, category_topic.id)endit "does not return category definition topics if `show_category_definitions_in_topic_lists` site setting is disabled" do@@ -1145,22 +1136,19 @@ RSpec.describe ListController doexpect(response.status).to eq(200)- parsed = response.parsed_body-- expect(parsed["topic_list"]["topics"].length).to eq(3)-- expect(parsed["topic_list"]["topics"].map { |topic| topic["id"] }).to contain_exactly(- topic.id,- topic_with_tag.id,- topic2_with_tag.id,- )+ expect(+ response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] },+ ).to contain_exactly(topic.id)endit "should accept 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) dosign_in(user)- get "/filter.json?q=tags:tag1"+ get "/filter.json", params: { q: "tags:tag1" }expect(response.status).to eq(200)@@ -1169,7 +1157,7 @@ RSpec.describe ListController doexpect(parsed["topic_list"]["topics"].length).to eq(1)expect(parsed["topic_list"]["topics"].first["id"]).to eq(topic2_with_tag.id)- get "/filter.json?q=tags:tag1&page=1"+ get "/filter.json", params: { q: "tags:tag1", page: 1 }expect(response.status).to eq(200)@@ -1185,18 +1173,72 @@ RSpec.describe ListController dofab!(: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: true)+ 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`" dogroup.add(user)sign_in(user)- get "/filter.json?q=status:public"+ 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, topic_with_tag.id, topic2_with_tag.id)+ ).to contain_exactly(topic.id)endendend