2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Admin::ReportsController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2024-09-10 07:51:49 +08:00
|
|
|
describe "#index" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "excludes page view mobile reports" do
|
|
|
|
get "/admin/reports.json"
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(
|
|
|
|
"page_view_anon_browser_mobile_reqs",
|
|
|
|
"page_view_logged_in_browser_mobile_reqs",
|
|
|
|
"page_view_anon_mobile_reqs",
|
|
|
|
"page_view_logged_in_mobile_reqs",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "excludes about and storage stats reports" do
|
|
|
|
get "/admin/reports.json"
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(
|
|
|
|
"report_about",
|
|
|
|
"report_storage_stats",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when use_legacy_pageviews is true" do
|
|
|
|
before { SiteSetting.use_legacy_pageviews = true }
|
|
|
|
|
|
|
|
it "excludes the site_traffic report and includes legacy pageview reports" do
|
|
|
|
get "/admin/reports.json"
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include("site_traffic")
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).to include(
|
|
|
|
*Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when use_legacy_pageviews is false" do
|
|
|
|
before { SiteSetting.use_legacy_pageviews = false }
|
|
|
|
|
|
|
|
it "includes the site_traffic report and excludes legacy pageview reports" do
|
|
|
|
get "/admin/reports.json"
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).to include("site_traffic")
|
|
|
|
expect(response.parsed_body["reports"].map { |r| r[:type] }).not_to include(
|
|
|
|
*Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#bulk" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with valid params" do
|
2018-08-24 21:28:01 +08:00
|
|
|
it "renders the reports as JSON" do
|
|
|
|
Fabricate(:topic)
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
likes: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2018-08-24 21:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
expect(response.parsed_body["reports"].count).to eq(2)
|
2018-08-24 21:28:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with invalid params" do
|
2023-07-28 19:53:46 +08:00
|
|
|
context "when limit param is invalid" do
|
|
|
|
include_examples "invalid limit params",
|
|
|
|
"/admin/reports/topics.json",
|
|
|
|
described_class::REPORTS_LIMIT
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with nonexistent report" do
|
2018-11-12 20:47:24 +08:00
|
|
|
it "returns not found reports" do
|
2018-08-24 21:28:01 +08:00
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
2018-11-12 20:47:24 +08:00
|
|
|
not_found: {
|
|
|
|
limit: 10,
|
2018-08-24 21:28:01 +08:00
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2018-08-24 21:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
expect(response.parsed_body["reports"].count).to eq(2)
|
|
|
|
expect(response.parsed_body["reports"][0]["type"]).to eq("topics")
|
|
|
|
expect(response.parsed_body["reports"][1]["type"]).to eq("not_found")
|
2018-08-24 21:28:01 +08:00
|
|
|
end
|
|
|
|
end
|
2020-01-03 22:01:38 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with invalid start or end dates" do
|
2020-01-03 22:01:38 +08:00
|
|
|
it "doesn't return 500 error" do
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
start_date: "2015-0-1",
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2020-01-03 22:01:38 +08:00
|
|
|
}
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
end_date: "2015-0-1",
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2020-01-03 22:01:38 +08:00
|
|
|
}
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
end
|
2018-08-24 21:28:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
it "returns report" do
|
|
|
|
Fabricate(:topic)
|
|
|
|
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
likes: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2022-11-03 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["reports"].count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
topics: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
not_found: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2022-11-03 11:42:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2024-09-10 07:51:49 +08:00
|
|
|
|
|
|
|
context "when use_legacy_pageviews is true" do
|
|
|
|
before do
|
|
|
|
SiteSetting.use_legacy_pageviews = true
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "marks the site_traffic report as not_found and does not run it" do
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
site_traffic: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
consolidated_page_views: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
consolidated_page_views_browser_detection: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
page_view_anon_reqs: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
page_view_logged_in_reqs: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["reports"].count).to eq(5)
|
|
|
|
expect(response.parsed_body["reports"][0]).to include("error" => "not_found", "data" => nil)
|
|
|
|
expect(response.parsed_body["reports"][1]["type"]).to eq("consolidated_page_views")
|
|
|
|
expect(response.parsed_body["reports"][2]["type"]).to eq(
|
|
|
|
"consolidated_page_views_browser_detection",
|
|
|
|
)
|
|
|
|
expect(response.parsed_body["reports"][3]["type"]).to eq("page_view_anon_reqs")
|
|
|
|
expect(response.parsed_body["reports"][4]["type"]).to eq("page_view_logged_in_reqs")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when use_legacy_pageviews is false" do
|
|
|
|
before do
|
|
|
|
SiteSetting.use_legacy_pageviews = false
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "marks the legacy pageview reports as not_found and does not run them" do
|
|
|
|
get "/admin/reports/bulk.json",
|
|
|
|
params: {
|
|
|
|
reports: {
|
|
|
|
site_traffic: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
consolidated_page_views: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
consolidated_page_views_browser_detection: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
page_view_anon_reqs: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
page_view_logged_in_reqs: {
|
|
|
|
limit: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["reports"].count).to eq(5)
|
|
|
|
expect(response.parsed_body["reports"][0]["type"]).to eq("site_traffic")
|
2024-10-22 08:06:22 +08:00
|
|
|
expect(response.parsed_body["reports"][1]["type"]).to eq("consolidated_page_views")
|
2024-09-10 07:51:49 +08:00
|
|
|
expect(response.parsed_body["reports"][2]).to include("error" => "not_found", "data" => nil)
|
|
|
|
expect(response.parsed_body["reports"][3]).to include("error" => "not_found", "data" => nil)
|
|
|
|
expect(response.parsed_body["reports"][4]).to include("error" => "not_found", "data" => nil)
|
|
|
|
end
|
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#show" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with invalid id form" do
|
2013-02-28 11:39:42 +08:00
|
|
|
let(:invalid_id) { "!!&asdfasdf" }
|
|
|
|
|
|
|
|
it "returns 404" do
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/#{invalid_id}.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(404)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with valid type form" do
|
|
|
|
context "with missing report" do
|
2018-06-11 12:49:28 +08:00
|
|
|
it "returns a 404 error" do
|
|
|
|
get "/admin/reports/nonexistent.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(404)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when a report is found" do
|
2013-02-28 11:39:42 +08:00
|
|
|
it "renders the report as JSON" do
|
2018-06-11 12:49:28 +08:00
|
|
|
Fabricate(:topic)
|
|
|
|
get "/admin/reports/topics.json"
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
expect(response.parsed_body["report"]["total"]).to eq(1)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
2023-07-28 19:53:46 +08:00
|
|
|
|
|
|
|
context "when limit param is invalid" do
|
|
|
|
include_examples "invalid limit params",
|
|
|
|
"/admin/reports/topics.json",
|
|
|
|
described_class::REPORTS_LIMIT
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
2017-04-13 17:10:55 +08:00
|
|
|
describe "when report is scoped to a category" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:category)
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:other_topic) { Fabricate(:topic) }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
it "should render the report as JSON" do
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/topics.json", params: { category_id: category.id }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
report = response.parsed_body["report"]
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
expect(report["type"]).to eq("topics")
|
|
|
|
expect(report["data"].count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when report is scoped to a group" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:other_user) { Fabricate(:user) }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
it "should render the report as JSON" do
|
|
|
|
group.add(user)
|
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/signups.json", params: { group_id: group.id }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
report = response.parsed_body["report"]
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
expect(report["type"]).to eq("signups")
|
|
|
|
expect(report["data"].count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
it "returns report" do
|
|
|
|
Fabricate(:topic)
|
|
|
|
|
|
|
|
get "/admin/reports/topics.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["report"]["total"]).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/reports/topics.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2024-09-10 07:51:49 +08:00
|
|
|
|
|
|
|
context "when use_legacy_pageviews is true" do
|
|
|
|
before do
|
|
|
|
SiteSetting.use_legacy_pageviews = true
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow running site_traffic report" do
|
|
|
|
Admin::ReportsController::HIDDEN_PAGEVIEW_REPORTS.each do |report_type|
|
|
|
|
get "/admin/reports/#{report_type}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2024-10-22 08:06:22 +08:00
|
|
|
|
|
|
|
it "does not allow running the page_view_legacy_total_reqs report" do
|
|
|
|
get "/admin/reports/page_view_legacy_total_reqs.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
2024-09-10 07:51:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when use_legacy_pageviews is false" do
|
|
|
|
before do
|
|
|
|
SiteSetting.use_legacy_pageviews = false
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow running legacy pageview reports" do
|
|
|
|
Admin::ReportsController::HIDDEN_LEGACY_PAGEVIEW_REPORTS.each do |report_type|
|
|
|
|
get "/admin/reports/#{report_type}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2024-10-22 08:06:22 +08:00
|
|
|
|
|
|
|
it "does allow running the page_view_legacy_total_reqs report" do
|
|
|
|
get "/admin/reports/page_view_legacy_total_reqs.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2024-09-10 07:51:49 +08:00
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|