2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe StaffActionLogger do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2013-08-20 04:58:38 +08:00
|
|
|
let(:logger) { described_class.new(admin) }
|
|
|
|
|
2013-07-24 05:58:26 +08:00
|
|
|
describe "new" do
|
|
|
|
it "raises an error when user is nil" do
|
|
|
|
expect { described_class.new(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when user is not a User" do
|
|
|
|
expect { described_class.new(5) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_user_deletion" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:deleted_user) { Fabricate(:user) }
|
2013-07-24 05:58:26 +08:00
|
|
|
|
|
|
|
subject(:log_user_deletion) { described_class.new(admin).log_user_deletion(deleted_user) }
|
|
|
|
|
|
|
|
it "raises an error when user is nil" do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_user_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when user is not a User" do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_user_deletion(1) }.to raise_error(Discourse::InvalidParameters)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
2013-11-01 00:46:29 +08:00
|
|
|
it "creates a new UserHistory record" do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { log_user_deletion }.to change { UserHistory.count }.by(1)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-16 03:14:12 +08:00
|
|
|
describe "log_show_emails" do
|
|
|
|
it "logs the user history" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect { logger.log_show_emails([admin]) }.to change(UserHistory, :count).by(1)
|
2014-12-16 03:14:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an exception with nothing to log" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect { logger.log_show_emails([]) }.not_to raise_error
|
2014-12-16 03:14:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an exception with nil input" do
|
2015-04-25 23:18:35 +08:00
|
|
|
expect { logger.log_show_emails(nil) }.not_to raise_error
|
2014-12-16 03:14:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-01 23:40:13 +08:00
|
|
|
describe "log_post_deletion" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:deleted_post) { Fabricate(:post) }
|
2014-10-01 23:40:13 +08:00
|
|
|
|
|
|
|
subject(:log_post_deletion) { described_class.new(admin).log_post_deletion(deleted_post) }
|
|
|
|
|
|
|
|
it "raises an error when post is nil" do
|
|
|
|
expect { logger.log_post_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when post is not a Post" do
|
|
|
|
expect { logger.log_post_deletion(1) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_post_deletion }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2015-08-14 11:57:02 +08:00
|
|
|
|
|
|
|
it "does not explode if post does not have a user" do
|
|
|
|
expect {
|
|
|
|
deleted_post.update_columns(user_id: nil)
|
|
|
|
log_post_deletion
|
|
|
|
}.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
end
|
|
|
|
|
2018-03-21 12:15:16 +08:00
|
|
|
describe "log_topic_delete_recover" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
2014-10-01 23:40:13 +08:00
|
|
|
|
2018-03-21 12:15:16 +08:00
|
|
|
context "when deleting topic" do
|
|
|
|
subject(:log_topic_delete_recover) do
|
|
|
|
described_class.new(admin).log_topic_delete_recover(topic)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
|
2018-03-21 12:15:16 +08:00
|
|
|
it "raises an error when topic is nil" do
|
|
|
|
expect { logger.log_topic_delete_recover(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when topic is not a Topic" do
|
|
|
|
expect { logger.log_topic_delete_recover(1) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
|
2018-03-21 12:15:16 +08:00
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_topic_delete_recover }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
end
|
|
|
|
|
2018-03-21 12:15:16 +08:00
|
|
|
context "when recovering topic" do
|
|
|
|
subject(:log_topic_delete_recover) do
|
|
|
|
described_class.new(admin).log_topic_delete_recover(topic, "recover_topic")
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2018-03-21 12:15:16 +08:00
|
|
|
|
|
|
|
it "raises an error when topic is nil" do
|
|
|
|
expect { logger.log_topic_delete_recover(nil, "recover_topic") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when topic is not a Topic" do
|
|
|
|
expect { logger.log_topic_delete_recover(1, "recover_topic") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_topic_delete_recover }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
2014-10-01 23:40:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 05:58:26 +08:00
|
|
|
describe "log_trust_level_change" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2014-09-05 13:20:39 +08:00
|
|
|
let(:old_trust_level) { TrustLevel[0] }
|
|
|
|
let(:new_trust_level) { TrustLevel[1] }
|
2013-07-24 05:58:26 +08:00
|
|
|
|
2013-08-14 00:04:28 +08:00
|
|
|
subject(:log_trust_level_change) do
|
|
|
|
described_class.new(admin).log_trust_level_change(user, old_trust_level, new_trust_level)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2013-07-24 05:58:26 +08:00
|
|
|
|
|
|
|
it "raises an error when user or trust level is nil" do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect {
|
|
|
|
logger.log_trust_level_change(nil, old_trust_level, new_trust_level)
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { logger.log_trust_level_change(user, nil, new_trust_level) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
expect { logger.log_trust_level_change(user, old_trust_level, nil) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when user is not a User" do
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_trust_level_change(1, old_trust_level, new_trust_level) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when new trust level is not a Trust Level" do
|
2014-09-05 13:20:39 +08:00
|
|
|
max_level = TrustLevel.valid_range.max
|
2013-08-20 04:58:38 +08:00
|
|
|
expect { logger.log_trust_level_change(user, old_trust_level, max_level + 1) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
|
2013-11-01 00:46:29 +08:00
|
|
|
it "creates a new UserHistory record" do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { log_trust_level_change }.to change { UserHistory.count }.by(1)
|
2022-08-29 18:00:48 +08:00
|
|
|
expect(UserHistory.last.previous_value).to eq(old_trust_level.to_s)
|
|
|
|
expect(UserHistory.last.new_value).to eq(new_trust_level.to_s)
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-20 04:58:38 +08:00
|
|
|
|
|
|
|
describe "log_site_setting_change" do
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
expect { logger.log_site_setting_change(nil, "1", "2") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2017-02-14 03:34:15 +08:00
|
|
|
expect {
|
|
|
|
logger.log_site_setting_change("i_am_a_site_setting_that_will_never_exist", "1", "2")
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
2013-08-20 04:58:38 +08:00
|
|
|
end
|
|
|
|
|
2013-11-01 00:46:29 +08:00
|
|
|
it "creates a new UserHistory record" do
|
2013-09-11 09:21:16 +08:00
|
|
|
expect { logger.log_site_setting_change("title", "Discourse", "My Site") }.to change {
|
|
|
|
UserHistory.count
|
|
|
|
}.by(1)
|
2013-08-20 04:58:38 +08:00
|
|
|
end
|
2022-05-23 11:23:10 +08:00
|
|
|
|
|
|
|
it "logs boolean values" do
|
|
|
|
log_record = logger.log_site_setting_change("allow_user_locale", true, false)
|
|
|
|
expect(log_record.previous_value).to eq("true")
|
|
|
|
expect(log_record.new_value).to eq("false")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs nil values" do
|
|
|
|
log_record = logger.log_site_setting_change("title", nil, nil)
|
|
|
|
expect(log_record.previous_value).to be_nil
|
|
|
|
expect(log_record.new_value).to be_nil
|
|
|
|
end
|
2013-08-20 04:58:38 +08:00
|
|
|
end
|
2013-08-21 22:49:35 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
describe "log_theme_change" do
|
2013-08-21 22:49:35 +08:00
|
|
|
it "raises an error when params are invalid" do
|
2017-04-12 22:52:52 +08:00
|
|
|
expect { logger.log_theme_change(nil, nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
2019-05-07 11:12:20 +08:00
|
|
|
let! :theme do
|
2018-08-08 12:46:34 +08:00
|
|
|
Fabricate(:theme)
|
2013-08-21 22:49:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "logs new site customizations" do
|
2017-04-12 22:52:52 +08:00
|
|
|
log_record = logger.log_theme_change(nil, theme)
|
|
|
|
expect(log_record.subject).to eq(theme.name)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record.previous_value).to eq(nil)
|
|
|
|
expect(log_record.new_value).to be_present
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2013-08-21 22:49:35 +08:00
|
|
|
json = ::JSON.parse(log_record.new_value)
|
2017-04-12 22:52:52 +08:00
|
|
|
expect(json["name"]).to eq(theme.name)
|
2013-08-21 22:49:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "logs updated site customizations" do
|
2017-04-12 22:52:52 +08:00
|
|
|
old_json = ThemeSerializer.new(theme, root: false).to_json
|
|
|
|
|
2017-05-03 04:01:01 +08:00
|
|
|
theme.set_field(target: :common, name: :scss, value: "body{margin: 10px;}")
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
log_record = logger.log_theme_change(old_json, theme)
|
|
|
|
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record.previous_value).to be_present
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
json = ::JSON.parse(log_record.new_value)
|
2017-05-03 04:01:01 +08:00
|
|
|
expect(json["theme_fields"]).to eq(
|
2023-01-09 19:18:21 +08:00
|
|
|
[
|
2017-05-03 04:01:01 +08:00
|
|
|
{
|
|
|
|
"name" => "scss",
|
|
|
|
"target" => "common",
|
|
|
|
"value" => "body{margin: 10px;}",
|
|
|
|
"type_id" => 1,
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
],
|
2017-05-03 04:01:01 +08:00
|
|
|
)
|
2013-08-21 22:49:35 +08:00
|
|
|
end
|
|
|
|
end
|
2013-08-22 00:03:21 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
describe "log_theme_destroy" do
|
2013-08-22 00:03:21 +08:00
|
|
|
it "raises an error when params are invalid" do
|
2017-04-12 22:52:52 +08:00
|
|
|
expect { logger.log_theme_destroy(nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-08-22 00:03:21 +08:00
|
|
|
end
|
|
|
|
|
2013-11-01 00:46:29 +08:00
|
|
|
it "creates a new UserHistory record" do
|
2018-08-08 12:46:34 +08:00
|
|
|
theme = Fabricate(:theme)
|
2017-05-03 04:01:01 +08:00
|
|
|
theme.set_field(target: :common, name: :scss, value: "body{margin: 10px;}")
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
log_record = logger.log_theme_destroy(theme)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record.previous_value).to be_present
|
|
|
|
expect(log_record.new_value).to eq(nil)
|
2013-08-22 00:03:21 +08:00
|
|
|
json = ::JSON.parse(log_record.previous_value)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-05-03 04:01:01 +08:00
|
|
|
expect(json["theme_fields"]).to eq(
|
2023-01-09 19:18:21 +08:00
|
|
|
[
|
2017-05-03 04:01:01 +08:00
|
|
|
{
|
|
|
|
"name" => "scss",
|
|
|
|
"target" => "common",
|
|
|
|
"value" => "body{margin: 10px;}",
|
|
|
|
"type_id" => 1,
|
|
|
|
},
|
2023-01-09 19:18:21 +08:00
|
|
|
],
|
2017-05-03 04:01:01 +08:00
|
|
|
)
|
2013-08-22 00:03:21 +08:00
|
|
|
end
|
|
|
|
end
|
2013-11-01 22:47:03 +08:00
|
|
|
|
2019-06-22 01:49:14 +08:00
|
|
|
describe "log_theme_setting_change" do
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
expect { logger.log_theme_setting_change(nil, nil, nil, nil) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let! :theme do
|
|
|
|
Fabricate(:theme)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
theme.set_field(target: :settings, name: :yaml, value: "custom_setting: special")
|
|
|
|
theme.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when theme setting is invalid" do
|
|
|
|
expect {
|
|
|
|
logger.log_theme_setting_change(:inexistent_setting, nil, nil, theme)
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs theme setting changes" do
|
|
|
|
log_record =
|
|
|
|
logger.log_theme_setting_change(:custom_setting, "special", "notsospecial", theme)
|
|
|
|
expect(log_record.subject).to eq("#{theme.name}: custom_setting")
|
|
|
|
expect(log_record.previous_value).to eq("special")
|
|
|
|
expect(log_record.new_value).to eq("notsospecial")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-18 21:31:04 +08:00
|
|
|
describe "log_site_text_change" do
|
|
|
|
it "raises an error when params are invalid" do
|
|
|
|
expect { logger.log_site_text_change(nil, "new text", "old text") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { logger.log_site_text_change("created", "new text", "old text") }.to change {
|
|
|
|
UserHistory.count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
describe "log_user_suspend" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, suspended_at: 10.minutes.ago, suspended_till: 1.day.from_now) }
|
2013-11-01 22:47:03 +08:00
|
|
|
|
|
|
|
it "raises an error when arguments are missing" do
|
2013-11-08 02:53:32 +08:00
|
|
|
expect { logger.log_user_suspend(nil, nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
expect { logger.log_user_suspend(nil, "He was bad.") }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reason arg is optional" do
|
2013-11-08 02:53:32 +08:00
|
|
|
expect { logger.log_user_suspend(user, nil) }.to_not raise_error
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
reason = "He was a big meanie."
|
2013-11-08 02:53:32 +08:00
|
|
|
log_record = logger.log_user_suspend(user, reason)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record).to be_valid
|
|
|
|
expect(log_record.details).to eq(reason)
|
|
|
|
expect(log_record.target_user).to eq(user)
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
describe "log_user_unsuspend" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user, suspended_at: 1.day.ago, suspended_till: 7.days.from_now) }
|
2013-11-01 22:47:03 +08:00
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
2013-11-08 02:53:32 +08:00
|
|
|
expect { logger.log_user_unsuspend(nil) }.to raise_error(Discourse::InvalidParameters)
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
2013-11-08 02:53:32 +08:00
|
|
|
log_record = logger.log_user_unsuspend(user)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record).to be_valid
|
|
|
|
expect(log_record.target_user).to eq(user)
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
end
|
2014-03-20 03:30:12 +08:00
|
|
|
|
|
|
|
describe "log_badge_grant" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_badge_grant(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
log_record = logger.log_badge_grant(user_badge)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record).to be_valid
|
|
|
|
expect(log_record.target_user).to eq(user)
|
|
|
|
expect(log_record.details).to eq(badge.name)
|
2014-03-20 03:30:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_badge_revoke" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
2014-03-20 03:30:12 +08:00
|
|
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_badge_revoke(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
log_record = logger.log_badge_revoke(user_badge)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record).to be_valid
|
|
|
|
expect(log_record.target_user).to eq(user)
|
|
|
|
expect(log_record.details).to eq(badge.name)
|
2014-03-20 03:30:12 +08:00
|
|
|
end
|
|
|
|
end
|
2014-11-27 08:12:23 +08:00
|
|
|
|
|
|
|
describe "log_roll_up" do
|
2022-04-13 02:07:37 +08:00
|
|
|
let(:subnet) { "1.2.3.0/24" }
|
|
|
|
let(:ips) { %w[1.2.3.4 1.2.3.100] }
|
|
|
|
|
|
|
|
subject(:log_roll_up) { described_class.new(admin).log_roll_up(subnet, ips) }
|
2014-11-27 08:12:23 +08:00
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
2022-04-13 02:07:37 +08:00
|
|
|
log_record = logger.log_roll_up(subnet, ips)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(log_record).to be_valid
|
2022-04-13 02:07:37 +08:00
|
|
|
expect(log_record.details).to eq("#{subnet} from #{ips.join(", ")}")
|
2014-11-27 08:12:23 +08:00
|
|
|
end
|
|
|
|
end
|
2015-02-06 03:34:57 +08:00
|
|
|
|
|
|
|
describe "log_custom" do
|
|
|
|
it "raises an error when `custom_type` is missing" do
|
|
|
|
expect { logger.log_custom(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates the UserHistory record" do
|
|
|
|
logged =
|
|
|
|
logger.log_custom("clicked_something", evil: "trout", clicked_on: "thing", topic_id: 1234)
|
2015-04-25 23:18:35 +08:00
|
|
|
expect(logged).to be_valid
|
|
|
|
expect(logged.details).to eq("evil: trout\nclicked_on: thing")
|
|
|
|
expect(logged.action).to eq(UserHistory.actions[:custom_staff])
|
|
|
|
expect(logged.custom_type).to eq("clicked_something")
|
|
|
|
expect(logged.topic_id).to be === 1234
|
2015-02-06 03:34:57 +08:00
|
|
|
end
|
|
|
|
end
|
2015-09-17 15:51:32 +08:00
|
|
|
|
|
|
|
describe "log_category_settings_change" do
|
|
|
|
let(:category) { Fabricate(:category, name: "haha") }
|
|
|
|
let(:category_group) { Fabricate(:category_group, category: category, permission_type: 1) }
|
|
|
|
|
|
|
|
it "raises an error when category is missing" do
|
|
|
|
expect { logger.log_category_settings_change(nil, nil) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates new UserHistory records" do
|
|
|
|
attributes = { name: "new_name", permissions: { category_group.group_name => 2 } }
|
|
|
|
|
|
|
|
category.update!(attributes)
|
|
|
|
|
2021-09-17 18:37:56 +08:00
|
|
|
logger.log_category_settings_change(
|
|
|
|
category,
|
|
|
|
attributes,
|
|
|
|
old_permissions: {
|
|
|
|
category_group.group_name => category_group.permission_type,
|
|
|
|
},
|
2015-09-17 15:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(2)
|
|
|
|
|
|
|
|
permission_user_history = UserHistory.find_by_subject("permissions")
|
|
|
|
expect(permission_user_history.category_id).to eq(category.id)
|
|
|
|
expect(permission_user_history.previous_value).to eq(
|
|
|
|
{ category_group.group_name => 1 }.to_json,
|
|
|
|
)
|
|
|
|
expect(permission_user_history.new_value).to eq({ category_group.group_name => 2 }.to_json)
|
|
|
|
expect(permission_user_history.action).to eq(UserHistory.actions[:change_category_settings])
|
|
|
|
expect(permission_user_history.context).to eq(category.url)
|
|
|
|
|
|
|
|
name_user_history = UserHistory.find_by_subject("name")
|
|
|
|
expect(name_user_history.category).to eq(category)
|
|
|
|
expect(name_user_history.previous_value).to eq("haha")
|
|
|
|
expect(name_user_history.new_value).to eq("new_name")
|
|
|
|
end
|
2015-12-06 15:29:13 +08:00
|
|
|
|
|
|
|
it "does not log permissions changes for category visible to everyone" do
|
|
|
|
attributes = { name: "new_name" }
|
|
|
|
old_permission = category.permissions_params
|
|
|
|
category.update!(attributes)
|
|
|
|
|
2021-09-17 18:37:56 +08:00
|
|
|
logger.log_category_settings_change(
|
|
|
|
category,
|
|
|
|
attributes.merge(permissions: { "everyone" => 1 }),
|
|
|
|
old_permissions: old_permission,
|
|
|
|
)
|
2015-12-06 15:29:13 +08:00
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
expect(UserHistory.find_by_subject("name").category).to eq(category)
|
|
|
|
end
|
2022-05-11 16:21:51 +08:00
|
|
|
|
|
|
|
it "logs custom fields changes" do
|
|
|
|
attributes = { custom_fields: { "auto_populated" => "t" } }
|
|
|
|
category.update!(attributes)
|
|
|
|
|
|
|
|
logger.log_category_settings_change(
|
|
|
|
category,
|
|
|
|
attributes,
|
|
|
|
old_permissions: category.permissions_params,
|
|
|
|
old_custom_fields: {
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2022-05-11 16:21:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not log custom fields changes if value is unchanged" do
|
|
|
|
attributes = { custom_fields: { "auto_populated" => "t" } }
|
|
|
|
category.update!(attributes)
|
|
|
|
|
|
|
|
logger.log_category_settings_change(
|
|
|
|
category,
|
|
|
|
attributes,
|
|
|
|
old_permissions: category.permissions_params,
|
|
|
|
old_custom_fields: {
|
|
|
|
"auto_populated" => "t",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(0)
|
|
|
|
end
|
2015-09-17 15:51:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_category_deletion" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:parent_category) { Fabricate(:category) }
|
|
|
|
fab!(:category) { Fabricate(:category, parent_category: parent_category) }
|
2015-09-17 15:51:32 +08:00
|
|
|
|
|
|
|
it "raises an error when category is missing" do
|
|
|
|
expect { logger.log_category_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
logger.log_category_deletion(category)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.subject).to eq(nil)
|
|
|
|
expect(user_history.category).to eq(category)
|
|
|
|
expect(user_history.details).to include("parent_category: #{parent_category.name}")
|
|
|
|
expect(user_history.context).to eq(category.url)
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:delete_category])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_category_creation" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
2015-09-17 15:51:32 +08:00
|
|
|
|
|
|
|
it "raises an error when category is missing" do
|
|
|
|
expect { logger.log_category_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
logger.log_category_creation(category)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.category).to eq(category)
|
|
|
|
expect(user_history.context).to eq(category.url)
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:create_category])
|
|
|
|
end
|
|
|
|
end
|
2017-01-12 00:41:11 +08:00
|
|
|
|
|
|
|
describe "log_lock_trust_level" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-01-12 00:41:11 +08:00
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_lock_trust_level(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
2017-11-24 04:55:44 +08:00
|
|
|
user.manual_locked_trust_level = 3
|
2017-01-12 00:41:11 +08:00
|
|
|
expect { logger.log_lock_trust_level(user) }.to change { UserHistory.count }.by(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:lock_trust_level])
|
|
|
|
|
2017-11-24 04:55:44 +08:00
|
|
|
user.manual_locked_trust_level = nil
|
2017-01-12 00:41:11 +08:00
|
|
|
expect { logger.log_lock_trust_level(user) }.to change { UserHistory.count }.by(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:unlock_trust_level])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_user_activate" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-01-12 00:41:11 +08:00
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_user_activate(nil, nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
reason = "Staff activated from admin"
|
|
|
|
expect { logger.log_user_activate(user, reason) }.to change { UserHistory.count }.by(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:activate_user])
|
|
|
|
expect(user_history.details).to eq(reason)
|
2017-01-12 10:03:37 +08:00
|
|
|
end
|
2017-01-11 14:46:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#log_readonly_mode" do
|
|
|
|
it "creates a new record" do
|
|
|
|
expect { logger.log_change_readonly_mode(true) }.to change { UserHistory.count }.by(1)
|
|
|
|
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:change_readonly_mode])
|
|
|
|
expect(user_history.new_value).to eq("t")
|
|
|
|
expect(user_history.previous_value).to eq("f")
|
|
|
|
|
|
|
|
expect { logger.log_change_readonly_mode(false) }.to change { UserHistory.count }.by(1)
|
|
|
|
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:change_readonly_mode])
|
|
|
|
expect(user_history.new_value).to eq("f")
|
|
|
|
expect(user_history.previous_value).to eq("t")
|
2017-01-12 00:41:11 +08:00
|
|
|
end
|
|
|
|
end
|
2018-01-27 20:51:22 +08:00
|
|
|
|
|
|
|
describe "log_check_personal_message" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:personal_message) { Fabricate(:private_message_topic) }
|
2018-01-27 20:51:22 +08:00
|
|
|
|
|
|
|
subject(:log_check_personal_message) do
|
|
|
|
described_class.new(admin).log_check_personal_message(personal_message)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2018-01-27 20:51:22 +08:00
|
|
|
|
|
|
|
it "raises an error when topic is nil" do
|
|
|
|
expect { logger.log_check_personal_message(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when topic is not a Topic" do
|
|
|
|
expect { logger.log_check_personal_message(1) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_check_personal_message }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
2018-04-23 13:48:53 +08:00
|
|
|
|
|
|
|
describe "log_post_approved" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:approved_post) { Fabricate(:post) }
|
2018-04-23 13:48:53 +08:00
|
|
|
|
|
|
|
subject(:log_post_approved) { described_class.new(admin).log_post_approved(approved_post) }
|
|
|
|
|
|
|
|
it "raises an error when post is nil" do
|
|
|
|
expect { logger.log_post_approved(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when post is not a Post" do
|
|
|
|
expect { logger.log_post_approved(1) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_post_approved }.to change { UserHistory.count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
2018-06-01 22:12:31 +08:00
|
|
|
|
|
|
|
describe "log_post_rejected" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:reviewable) { Fabricate(:reviewable_queued_post) }
|
2018-06-01 22:12:31 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
subject(:log_post_rejected) do
|
|
|
|
described_class.new(admin).log_post_rejected(reviewable, DateTime.now)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2018-06-01 22:12:31 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
it "raises an error when reviewable not supplied" do
|
|
|
|
expect { logger.log_post_rejected(nil, DateTime.now) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
|
|
|
expect { logger.log_post_rejected(1, DateTime.now) }.to raise_error(
|
|
|
|
Discourse::InvalidParameters,
|
|
|
|
)
|
2018-06-01 22:12:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { log_post_rejected }.to change { UserHistory.count }.by(1)
|
2018-06-02 11:55:41 +08:00
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_rejected])
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(user_history.details).to include(reviewable.payload["raw"])
|
2018-06-01 22:12:31 +08:00
|
|
|
end
|
2019-04-03 23:40:32 +08:00
|
|
|
|
|
|
|
it "works if the user was destroyed" do
|
|
|
|
reviewable.created_by.destroy
|
|
|
|
reviewable.reload
|
|
|
|
|
|
|
|
expect { log_post_rejected }.to change { UserHistory.count }.by(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_rejected])
|
|
|
|
expect(user_history.details).to include(reviewable.payload["raw"])
|
|
|
|
end
|
2018-06-01 22:12:31 +08:00
|
|
|
end
|
2020-07-22 02:29:02 +08:00
|
|
|
|
|
|
|
describe "log_topic_closed" do
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_topic_closed(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { logger.log_topic_closed(topic, closed: true) }.to change {
|
|
|
|
UserHistory.where(action: UserHistory.actions[:topic_closed]).count
|
|
|
|
}.by(1)
|
|
|
|
expect { logger.log_topic_closed(topic, closed: false) }.to change {
|
|
|
|
UserHistory.where(action: UserHistory.actions[:topic_opened]).count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_topic_archived" do
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_topic_archived(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
expect { logger.log_topic_archived(topic, archived: true) }.to change {
|
|
|
|
UserHistory.where(action: UserHistory.actions[:topic_archived]).count
|
|
|
|
}.by(1)
|
|
|
|
expect { logger.log_topic_archived(topic, archived: false) }.to change {
|
|
|
|
UserHistory.where(action: UserHistory.actions[:topic_unarchived]).count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "log_post_staff_note" do
|
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_topic_archived(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
2020-11-11 20:49:53 +08:00
|
|
|
expect {
|
|
|
|
logger.log_post_staff_note(post, { new_value: "my note", old_value: nil })
|
|
|
|
}.to change { UserHistory.count }.by(1)
|
2020-07-22 02:29:02 +08:00
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_staff_note_create])
|
|
|
|
expect(user_history.new_value).to eq("my note")
|
|
|
|
expect(user_history.previous_value).to eq(nil)
|
|
|
|
|
2020-11-11 20:49:53 +08:00
|
|
|
expect {
|
|
|
|
logger.log_post_staff_note(post, { new_value: "", old_value: "my note" })
|
|
|
|
}.to change { UserHistory.count }.by(1)
|
2020-07-22 02:29:02 +08:00
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_staff_note_destroy])
|
|
|
|
expect(user_history.new_value).to eq(nil)
|
|
|
|
expect(user_history.previous_value).to eq("my note")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-23 21:50:00 +08:00
|
|
|
describe "log_post_staff_note" do
|
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
it "raises an error when argument is missing" do
|
|
|
|
expect { logger.log_topic_archived(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
2020-11-11 20:49:53 +08:00
|
|
|
expect {
|
|
|
|
logger.log_post_staff_note(post, { new_value: "my note", old_value: nil })
|
|
|
|
}.to change { UserHistory.count }.by(1)
|
2020-07-23 21:50:00 +08:00
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_staff_note_create])
|
|
|
|
expect(user_history.new_value).to eq("my note")
|
|
|
|
expect(user_history.previous_value).to eq(nil)
|
|
|
|
|
2020-11-11 20:49:53 +08:00
|
|
|
expect {
|
|
|
|
logger.log_post_staff_note(post, { new_value: nil, old_value: "my note" })
|
|
|
|
}.to change { UserHistory.count }.by(1)
|
2020-07-23 21:50:00 +08:00
|
|
|
user_history = UserHistory.last
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:post_staff_note_destroy])
|
|
|
|
expect(user_history.new_value).to eq(nil)
|
|
|
|
expect(user_history.previous_value).to eq("my note")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-30 13:52:46 +08:00
|
|
|
describe "#log_watched_words_creation" do
|
|
|
|
fab!(:watched_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
|
|
|
|
|
|
|
|
it "raises an error when watched_word is missing" do
|
|
|
|
expect { logger.log_watched_words_creation(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
logger.log_watched_words_creation(watched_word)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.subject).to eq(nil)
|
|
|
|
expect(user_history.details).to include(watched_word.word)
|
|
|
|
expect(user_history.context).to eq("block")
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:watched_word_create])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#log_watched_words_deletion" do
|
|
|
|
fab!(:watched_word) { Fabricate(:watched_word, action: WatchedWord.actions[:block]) }
|
|
|
|
|
|
|
|
it "raises an error when watched_word is missing" do
|
|
|
|
expect { logger.log_watched_words_deletion(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a new UserHistory record" do
|
|
|
|
logger.log_watched_words_deletion(watched_word)
|
|
|
|
|
|
|
|
expect(UserHistory.count).to eq(1)
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.subject).to eq(nil)
|
|
|
|
expect(user_history.details).to include(watched_word.word)
|
|
|
|
expect(user_history.context).to eq("block")
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:watched_word_destroy])
|
|
|
|
end
|
|
|
|
end
|
2013-07-24 05:58:26 +08:00
|
|
|
end
|