2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe DiscourseHub do
|
2017-03-27 20:47:07 +08:00
|
|
|
describe ".discourse_version_check" do
|
2013-02-20 04:16:50 +08:00
|
|
|
it "should return just return the json that the hub returns" do
|
|
|
|
hub_response = { "success" => "OK", "latest_version" => "0.8.1", "critical_updates" => false }
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2017-06-17 03:31:13 +08:00
|
|
|
stub_request(
|
|
|
|
:get,
|
|
|
|
(ENV["HUB_BASE_URL"] || "http://local.hub:3000/api") + "/version_check",
|
2017-06-19 03:40:28 +08:00
|
|
|
).with(query: DiscourseHub.version_check_payload).to_return(
|
2017-06-16 08:08:15 +08:00
|
|
|
status: 200,
|
|
|
|
body: hub_response.to_json,
|
|
|
|
)
|
|
|
|
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(DiscourseHub.discourse_version_check).to eq(hub_response)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2017-03-27 20:47:07 +08:00
|
|
|
|
2024-02-23 14:12:28 +08:00
|
|
|
describe ".discover_enrollment" do
|
|
|
|
it "should trigger a POST request to hub" do
|
|
|
|
stub_request(
|
|
|
|
:post,
|
|
|
|
(ENV["HUB_BASE_URL"] || "http://local.hub:3000/api") + "/discover/enroll",
|
|
|
|
).with(body: JSON[DiscourseHub.discover_enrollment_payload]).to_return(
|
|
|
|
status: 200,
|
|
|
|
body: "",
|
|
|
|
headers: {
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
DiscourseHub.discover_enrollment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-25 13:12:50 +08:00
|
|
|
describe ".discover_enrollment_payload" do
|
|
|
|
it "should return the correct payload" do
|
|
|
|
payload = DiscourseHub.discover_enrollment_payload
|
|
|
|
expect(payload[:forum_url]).to eq(Discourse.base_url)
|
|
|
|
expect(payload[:forum_title]).to eq(SiteSetting.title)
|
|
|
|
expect(payload[:locale]).to eq(I18n.locale)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-27 20:47:07 +08:00
|
|
|
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)
|
|
|
|
|
2023-11-10 04:44:05 +08:00
|
|
|
expect(json["topics_count"]).to be_present
|
|
|
|
expect(json["posts_count"]).to be_present
|
|
|
|
expect(json["users_count"]).to be_present
|
2017-03-27 20:47:07 +08:00
|
|
|
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
|
2023-11-10 04:44:05 +08:00
|
|
|
expect(json["likes_count"]).to be_present
|
2017-03-27 20:47:07 +08:00
|
|
|
expect(json["likes_7_days"]).to be_present
|
|
|
|
expect(json["likes_30_days"]).to be_present
|
2024-08-13 05:47:13 +08:00
|
|
|
expect(json["participating_users_7_days"]).to be_present
|
|
|
|
expect(json["participating_users_30_days"]).to be_present
|
2017-03-27 20:47:07 +08:00
|
|
|
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
|
2018-10-09 22:05:31 +08:00
|
|
|
|
|
|
|
describe ".collection_action" do
|
2024-11-13 08:47:39 +08:00
|
|
|
let(:fake_logger) { FakeLogger.new }
|
|
|
|
|
|
|
|
before { Rails.logger.broadcast_to(fake_logger) }
|
2018-10-09 22:05:31 +08:00
|
|
|
|
2024-11-13 08:47:39 +08:00
|
|
|
after { Rails.logger.stop_broadcasting_to(fake_logger) }
|
2018-10-09 22:05:31 +08:00
|
|
|
|
2018-10-10 09:34:50 +08:00
|
|
|
it "should log correctly on error" do
|
2018-10-16 03:06:02 +08:00
|
|
|
stub_request(:get, (ENV["HUB_BASE_URL"] || "http://local.hub:3000/api") + "/test").to_return(
|
2018-10-10 09:34:50 +08:00
|
|
|
status: 500,
|
|
|
|
body: "",
|
|
|
|
headers: {
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2018-10-10 09:34:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
DiscourseHub.collection_action(:get, "/test")
|
2018-10-09 22:05:31 +08:00
|
|
|
|
2024-11-13 08:47:39 +08:00
|
|
|
expect(fake_logger.warnings).to eq([DiscourseHub.response_status_log_message("/test", 500)])
|
2018-10-09 22:05:31 +08:00
|
|
|
|
2024-11-13 08:47:39 +08:00
|
|
|
expect(fake_logger.errors).to eq([DiscourseHub.response_body_log_message("")])
|
2018-10-09 22:05:31 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|