mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 12:43:54 -06:00
d91456fd53
This adds the ability to collect stats without exposing them
among other stats via API.
The most important thing I wanted to achieve is to provide
an API where stats are not exposed by default, and a developer
has to explicitly specify that they should be
exposed (`expose_via_api: true`). Implementing an opposite
solution would be simpler, but that's less safe in terms of
potential security issues.
When working on this, I had to refactor the current solution.
I would go even further with the refactoring, but the next steps
seem to be going too far in changing the solution we have,
and that would also take more time. Two things that can be
improved in the future:
1. Data structures for holding stats can be further improved
2. Core stats are hard-coded in the About template (it's hard
to fix it without correcting data structures first, see point 1):
63a0700d45/app/views/about/index.html.erb (L61-L101)
The most significant refactorings are:
1. Introducing the `Stat` model
2. Aligning the way the core and the plugin stats' are registered
94 lines
3.4 KiB
Ruby
94 lines
3.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe SiteController do
|
|
describe "#basic_info" do
|
|
it "is visible always even for sites requiring login" do
|
|
upload = Fabricate(:upload)
|
|
|
|
SiteSetting.login_required = true
|
|
SiteSetting.title = "Hammer Time"
|
|
SiteSetting.site_description = "A time for Hammer"
|
|
SiteSetting.logo = upload
|
|
SiteSetting.logo_small = upload
|
|
SiteSetting.apple_touch_icon = upload
|
|
SiteSetting.mobile_logo = upload
|
|
Theme.clear_default!
|
|
|
|
get "/site/basic-info.json"
|
|
json = response.parsed_body
|
|
|
|
expected_url = UrlHelper.absolute(upload.url)
|
|
|
|
expect(json["title"]).to eq("Hammer Time")
|
|
expect(json["description"]).to eq("A time for Hammer")
|
|
expect(json["logo_url"]).to eq(expected_url)
|
|
expect(json["apple_touch_icon_url"]).to eq(expected_url)
|
|
expect(json["logo_small_url"]).to eq(expected_url)
|
|
expect(json["mobile_logo_url"]).to eq(expected_url)
|
|
expect(json["header_primary_color"]).to eq("333333")
|
|
expect(json["header_background_color"]).to eq("ffffff")
|
|
expect(json["login_required"]).to eq(true)
|
|
end
|
|
end
|
|
|
|
describe "#statistics" do
|
|
it "is visible for sites requiring login" do
|
|
SiteSetting.login_required = true
|
|
SiteSetting.share_anonymized_statistics = true
|
|
|
|
get "/site/statistics.json"
|
|
json = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
|
expect(json["topics_count"]).to be_present
|
|
expect(json["posts_count"]).to be_present
|
|
expect(json["users_count"]).to be_present
|
|
expect(json["topics_7_days"]).to be_present
|
|
expect(json["topics_30_days"]).to be_present
|
|
expect(json["posts_7_days"]).to be_present
|
|
expect(json["posts_30_days"]).to be_present
|
|
expect(json["users_7_days"]).to be_present
|
|
expect(json["users_30_days"]).to be_present
|
|
expect(json["active_users_7_days"]).to be_present
|
|
expect(json["active_users_30_days"]).to be_present
|
|
expect(json["likes_count"]).to be_present
|
|
expect(json["likes_7_days"]).to be_present
|
|
expect(json["likes_30_days"]).to be_present
|
|
end
|
|
|
|
it "is not visible if site setting share_anonymized_statistics is disabled" do
|
|
SiteSetting.share_anonymized_statistics = false
|
|
|
|
get "/site/statistics.json"
|
|
expect(response).to redirect_to "/"
|
|
end
|
|
|
|
it "returns exposable stats only" do
|
|
Discourse.redis.del(About.stats_cache_key)
|
|
|
|
SiteSetting.login_required = true
|
|
SiteSetting.share_anonymized_statistics = true
|
|
|
|
plugin = Plugin::Instance.new
|
|
plugin.register_stat("private_stat", expose_via_api: false) do
|
|
{ :last_day => 1, "7_days" => 2, "30_days" => 3, :count => 4 }
|
|
end
|
|
plugin.register_stat("exposable_stat", expose_via_api: true) do
|
|
{ :last_day => 11, "7_days" => 12, "30_days" => 13, :count => 14 }
|
|
end
|
|
|
|
get "/site/statistics.json"
|
|
json = response.parsed_body
|
|
|
|
expect(json["exposable_stat_last_day"]).to be(11)
|
|
expect(json["exposable_stat_7_days"]).to be(12)
|
|
expect(json["exposable_stat_30_days"]).to be(13)
|
|
expect(json["exposable_stat_count"]).to be(14)
|
|
expect(json["private_stat_last_day"]).not_to be_present
|
|
expect(json["private_stat_7_days"]).not_to be_present
|
|
expect(json["private_stat_30_days"]).not_to be_present
|
|
expect(json["private_stat_count"]).not_to be_present
|
|
end
|
|
end
|
|
end
|