discourse/spec/serializers/about_serializer_spec.rb
Andrei Prigorshnev d91456fd53
DEV: Ability to collect stats without exposing them via API (#23933)
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
2023-11-10 00:44:05 +04:00

74 lines
2.7 KiB
Ruby

# frozen_string_literal: true
RSpec.describe AboutSerializer do
fab!(:user) { Fabricate(:user) }
context "when login_required is enabled" do
before do
SiteSetting.login_required = true
SiteSetting.contact_url = "https://example.com/contact"
SiteSetting.contact_email = "example@foobar.com"
end
it "contact details are hidden from anonymous users" do
json = AboutSerializer.new(About.new(nil), scope: Guardian.new(nil), root: nil).as_json
expect(json[:contact_url]).to eq(nil)
expect(json[:contact_email]).to eq(nil)
end
it "contact details are visible to regular users" do
json = AboutSerializer.new(About.new(user), scope: Guardian.new(user), root: nil).as_json
expect(json[:contact_url]).to eq(SiteSetting.contact_url)
expect(json[:contact_email]).to eq(SiteSetting.contact_email)
end
end
context "when login_required is disabled" do
before do
SiteSetting.login_required = false
SiteSetting.contact_url = "https://example.com/contact"
SiteSetting.contact_email = "example@foobar.com"
end
it "contact details are visible to anonymous users" do
json = AboutSerializer.new(About.new(nil), scope: Guardian.new(nil), root: nil).as_json
expect(json[:contact_url]).to eq(SiteSetting.contact_url)
expect(json[:contact_email]).to eq(SiteSetting.contact_email)
end
it "contact details are visible to regular users" do
json = AboutSerializer.new(About.new(user), scope: Guardian.new(user), root: nil).as_json
expect(json[:contact_url]).to eq(SiteSetting.contact_url)
expect(json[:contact_email]).to eq(SiteSetting.contact_email)
end
end
describe "#stats" do
let(:plugin) { Plugin::Instance.new }
it "serialize exposable stats only" do
Discourse.redis.del(About.stats_cache_key)
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
serializer = AboutSerializer.new(About.new(user), scope: Guardian.new(user), root: nil)
json = serializer.as_json
stats = json[:stats]
expect(stats["exposable_stat_last_day"]).to be(11)
expect(stats["exposable_stat_7_days"]).to be(12)
expect(stats["exposable_stat_30_days"]).to be(13)
expect(stats["exposable_stat_count"]).to be(14)
expect(stats["private_stat_last_day"]).not_to be_present
expect(stats["private_stat_7_days"]).not_to be_present
expect(stats["private_stat_30_days"]).not_to be_present
expect(stats["private_stat_count"]).not_to be_present
end
end
end