mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 08:13:45 +08: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
105 lines
3.8 KiB
Ruby
105 lines
3.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe DiscourseHub do
|
|
describe ".discourse_version_check" do
|
|
it "should return just return the json that the hub returns" do
|
|
hub_response = { "success" => "OK", "latest_version" => "0.8.1", "critical_updates" => false }
|
|
|
|
stub_request(
|
|
:get,
|
|
(ENV["HUB_BASE_URL"] || "http://local.hub:3000/api") + "/version_check",
|
|
).with(query: DiscourseHub.version_check_payload).to_return(
|
|
status: 200,
|
|
body: hub_response.to_json,
|
|
)
|
|
|
|
expect(DiscourseHub.discourse_version_check).to eq(hub_response)
|
|
end
|
|
end
|
|
|
|
describe ".version_check_payload" do
|
|
describe "when Discourse Hub has not fetched stats since past 7 days" do
|
|
it "should include stats" do
|
|
DiscourseHub.stats_fetched_at = 8.days.ago
|
|
json = JSON.parse(DiscourseHub.version_check_payload.to_json)
|
|
|
|
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
|
|
expect(json["installed_version"]).to be_present
|
|
expect(json["branch"]).to be_present
|
|
end
|
|
end
|
|
|
|
describe "when Discourse Hub has fetched stats in past 7 days" do
|
|
it "should not include stats" do
|
|
DiscourseHub.stats_fetched_at = 2.days.ago
|
|
json = JSON.parse(DiscourseHub.version_check_payload.to_json)
|
|
|
|
expect(json["topic_count"]).not_to be_present
|
|
expect(json["post_count"]).not_to be_present
|
|
expect(json["user_count"]).not_to be_present
|
|
expect(json["like_count"]).not_to be_present
|
|
expect(json["likes_7_days"]).not_to be_present
|
|
expect(json["likes_30_days"]).not_to be_present
|
|
expect(json["installed_version"]).to be_present
|
|
expect(json["branch"]).to be_present
|
|
end
|
|
end
|
|
|
|
describe "when send_anonymize_stats is disabled" do
|
|
describe "when Discourse Hub has not fetched stats for the past year" do
|
|
it "should not include stats" do
|
|
DiscourseHub.stats_fetched_at = 1.year.ago
|
|
SiteSetting.share_anonymized_statistics = false
|
|
json = JSON.parse(DiscourseHub.version_check_payload.to_json)
|
|
|
|
expect(json["topic_count"]).not_to be_present
|
|
expect(json["post_count"]).not_to be_present
|
|
expect(json["user_count"]).not_to be_present
|
|
expect(json["like_count"]).not_to be_present
|
|
expect(json["likes_7_days"]).not_to be_present
|
|
expect(json["likes_30_days"]).not_to be_present
|
|
expect(json["installed_version"]).to be_present
|
|
expect(json["branch"]).to be_present
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
describe ".collection_action" do
|
|
before do
|
|
@orig_logger = Rails.logger
|
|
Rails.logger = @fake_logger = FakeLogger.new
|
|
end
|
|
|
|
after { Rails.logger = @orig_logger }
|
|
|
|
it "should log correctly on error" do
|
|
stub_request(:get, (ENV["HUB_BASE_URL"] || "http://local.hub:3000/api") + "/test").to_return(
|
|
status: 500,
|
|
body: "",
|
|
headers: {
|
|
},
|
|
)
|
|
|
|
DiscourseHub.collection_action(:get, "/test")
|
|
|
|
expect(@fake_logger.warnings).to eq([DiscourseHub.response_status_log_message("/test", 500)])
|
|
|
|
expect(@fake_logger.errors).to eq([DiscourseHub.response_body_log_message("")])
|
|
end
|
|
end
|
|
end
|