2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::DashboardController do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2022-11-02 23:42:44 -04:00
|
|
|
|
2013-07-07 22:25:38 -04:00
|
|
|
before do
|
2013-08-02 18:31:25 -04:00
|
|
|
AdminDashboardData.stubs(:fetch_cached_stats).returns(reports: [])
|
2024-02-23 01:12:28 -05:00
|
|
|
Jobs::CallDiscourseHub.any_instance.stubs(:execute).returns(true)
|
2013-07-07 22:25:38 -04:00
|
|
|
end
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-12-15 12:12:53 -05:00
|
|
|
def populate_new_features(date1 = nil, date2 = nil)
|
2022-11-02 23:42:44 -04:00
|
|
|
sample_features = [
|
|
|
|
{
|
|
|
|
"id" => "1",
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Cool Beans",
|
|
|
|
"description" => "Now beans are included",
|
2022-12-15 12:12:53 -05:00
|
|
|
"created_at" => date1 || (Time.zone.now - 40.minutes),
|
2022-11-02 23:42:44 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id" => "2",
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Fancy Legumes",
|
|
|
|
"description" => "Legumes too!",
|
2022-12-15 12:12:53 -05:00
|
|
|
"created_at" => date2 || (Time.zone.now - 20.minutes),
|
2022-11-02 23:42:44 -04:00
|
|
|
},
|
|
|
|
]
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(sample_features))
|
|
|
|
end
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
describe "#index" do
|
|
|
|
shared_examples "version info present" do
|
|
|
|
it "returns discourse version info" do
|
|
|
|
get "/admin/dashboard.json"
|
2021-01-22 10:09:02 -05:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["version_check"]).to be_present
|
|
|
|
end
|
2021-01-22 10:09:02 -05:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "version info absent" do
|
|
|
|
before { SiteSetting.version_checks = false }
|
|
|
|
|
|
|
|
it "does not return discourse version info" do
|
|
|
|
get "/admin/dashboard.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["version_check"]).not_to be_present
|
|
|
|
end
|
2018-06-11 00:47:42 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
context "when version checking is enabled" do
|
2017-07-07 02:09:14 -04:00
|
|
|
before { SiteSetting.version_checks = true }
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "version info present"
|
|
|
|
end
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when version checking is disabled" do
|
|
|
|
before { SiteSetting.version_checks = false }
|
|
|
|
|
|
|
|
include_examples "version info absent"
|
2013-03-14 18:26:12 -04:00
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when version checking is enabled" do
|
|
|
|
before { SiteSetting.version_checks = true }
|
2013-03-14 18:26:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "version info present"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when version checking is disabled" do
|
|
|
|
before { SiteSetting.version_checks = false }
|
|
|
|
|
|
|
|
include_examples "version info absent"
|
|
|
|
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/dashboard.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2013-03-14 18:26:12 -04:00
|
|
|
end
|
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#problems" do
|
2024-05-22 21:29:08 -04:00
|
|
|
before { ProblemCheck.stubs(:realtime).returns(stub(run_all: [])) }
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2013-03-29 15:48:26 -04:00
|
|
|
|
|
|
|
context "when there are no problems" do
|
|
|
|
it "returns an empty array" do
|
2018-06-11 00:47:42 -04:00
|
|
|
get "/admin/dashboard/problems.json"
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["problems"].size).to eq(0)
|
2013-03-29 15:48:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are problems" do
|
|
|
|
before do
|
2024-05-22 21:29:08 -04:00
|
|
|
Fabricate(:admin_notice, subject: "problem", identifier: "foo")
|
|
|
|
Fabricate(:admin_notice, subject: "problem", identifier: "bar")
|
2013-03-29 15:48:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an array of strings" do
|
2018-06-11 00:47:42 -04:00
|
|
|
get "/admin/dashboard/problems.json"
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(json["problems"].size).to eq(2)
|
2013-03-29 15:48:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-22 10:09:02 -05:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
2021-01-22 10:09:02 -05:00
|
|
|
before do
|
2022-11-02 23:42:44 -04:00
|
|
|
sign_in(moderator)
|
2024-05-22 21:29:08 -04:00
|
|
|
|
|
|
|
Fabricate(:admin_notice, subject: "problem", identifier: "foo")
|
|
|
|
Fabricate(:admin_notice, subject: "problem", identifier: "bar")
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a list of problems" do
|
|
|
|
get "/admin/dashboard/problems.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["problems"].size).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/dashboard/problems.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#new_features" do
|
2022-12-23 07:41:30 -05:00
|
|
|
after { DiscourseUpdates.clean_state }
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2021-01-22 10:09:02 -05:00
|
|
|
|
|
|
|
it "is empty by default" do
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2021-01-22 10:09:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["new_features"]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fails gracefully for invalid JSON" do
|
|
|
|
Discourse.redis.set("new_features", "INVALID JSON")
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2021-01-22 10:09:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["new_features"]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes new features when available" do
|
|
|
|
populate_new_features
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2021-01-22 10:09:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["new_features"].length).to eq(2)
|
|
|
|
expect(json["new_features"][0]["emoji"]).to eq("🙈")
|
|
|
|
expect(json["new_features"][0]["title"]).to eq("Fancy Legumes")
|
2021-02-10 13:12:04 -05:00
|
|
|
expect(json["has_unseen_features"]).to eq(true)
|
|
|
|
end
|
|
|
|
|
2024-11-26 18:40:55 -05:00
|
|
|
it "allows for forcing a refresh of new features, busting the cache" do
|
|
|
|
populate_new_features
|
|
|
|
|
|
|
|
get "/admin/whats-new.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["new_features"].length).to eq(2)
|
|
|
|
|
|
|
|
get "/admin/whats-new.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["new_features"].length).to eq(2)
|
|
|
|
|
|
|
|
DiscourseUpdates.stubs(:new_features_payload).returns(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id" => "3",
|
|
|
|
"emoji" => "🚀",
|
|
|
|
"title" => "Space platform launched!",
|
|
|
|
"description" => "Now to make it to the next planet unscathed...",
|
|
|
|
"created_at" => 1.minute.ago,
|
|
|
|
},
|
|
|
|
].to_json,
|
|
|
|
)
|
|
|
|
|
|
|
|
get "/admin/whats-new.json?force_refresh=true"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["new_features"].length).to eq(1)
|
|
|
|
expect(json["new_features"][0]["id"]).to eq("3")
|
|
|
|
end
|
|
|
|
|
2021-02-10 13:12:04 -05:00
|
|
|
it "passes unseen feature state" do
|
|
|
|
populate_new_features
|
|
|
|
DiscourseUpdates.mark_new_features_as_seen(admin.id)
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2021-02-10 13:12:04 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["has_unseen_features"]).to eq(false)
|
2021-01-22 10:09:02 -05:00
|
|
|
end
|
2022-12-15 12:12:53 -05:00
|
|
|
|
|
|
|
it "sets/bumps the last viewed feature date for the admin" do
|
|
|
|
date1 = 30.minutes.ago
|
|
|
|
date2 = 20.minutes.ago
|
|
|
|
populate_new_features(date1, date2)
|
|
|
|
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(admin.id)).to eq(nil)
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-12-15 12:12:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(admin.id)).to be_within_one_second_of(
|
|
|
|
date2,
|
|
|
|
)
|
|
|
|
|
|
|
|
date2 = 10.minutes.ago
|
|
|
|
populate_new_features(date1, date2)
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-12-15 12:12:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(admin.id)).to be_within_one_second_of(
|
|
|
|
date2,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-11-26 17:32:28 -05:00
|
|
|
it "marks new features as seen" do
|
|
|
|
date1 = 30.minutes.ago
|
|
|
|
date2 = 20.minutes.ago
|
|
|
|
populate_new_features(date1, date2)
|
|
|
|
|
|
|
|
expect(DiscourseUpdates.new_features_last_seen(admin.id)).to eq(nil)
|
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(true)
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2023-11-26 17:32:28 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(DiscourseUpdates.new_features_last_seen(admin.id)).not_to eq(nil)
|
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(false)
|
|
|
|
|
|
|
|
expect(DiscourseUpdates.new_features_last_seen(moderator.id)).to eq(nil)
|
|
|
|
expect(DiscourseUpdates.has_unseen_features?(moderator.id)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-12-15 12:12:53 -05:00
|
|
|
it "doesn't error when there are no new features" do
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-12-15 12:12:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2021-01-22 10:09:02 -05:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
it "includes new features when available" do
|
|
|
|
populate_new_features
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-11-02 23:42:44 -04:00
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["new_features"].length).to eq(2)
|
|
|
|
expect(json["new_features"][0]["emoji"]).to eq("🙈")
|
|
|
|
expect(json["new_features"][0]["title"]).to eq("Fancy Legumes")
|
|
|
|
expect(json["has_unseen_features"]).to eq(true)
|
|
|
|
end
|
2022-12-15 12:12:53 -05:00
|
|
|
|
|
|
|
it "doesn't set last viewed feature date for moderators" do
|
|
|
|
populate_new_features
|
|
|
|
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(moderator.id)).to eq(nil)
|
|
|
|
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-12-15 12:12:53 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(moderator.id)).to eq(nil)
|
|
|
|
end
|
2022-11-02 23:42:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "denies access with a 404 response" do
|
2024-08-15 19:12:24 -04:00
|
|
|
get "/admin/whats-new.json"
|
2022-11-02 23:42:44 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2013-03-14 18:26:12 -04:00
|
|
|
end
|
2013-07-07 22:25:38 -04:00
|
|
|
end
|