2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Admin::StaffActionLogsController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2022-11-02 23:42:44 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2018-06-11 00:37:06 -04:00
|
|
|
describe '#index' do
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "staff action logs accessible" do
|
|
|
|
it 'returns logs' do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
StaffActionLogger.new(Discourse.system_user).log_topic_delete_recover(topic, "delete_topic")
|
2017-05-30 11:25:42 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
get "/admin/logs/staff_action_logs.json", params: { action_id: UserHistory.actions[:delete_topic] }
|
2017-05-30 11:25:42 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
json = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
2017-05-30 11:25:42 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(json["staff_action_logs"].length).to eq(1)
|
|
|
|
expect(json["staff_action_logs"][0]["action_name"]).to eq("delete_topic")
|
2017-05-30 11:25:42 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(json["extras"]["user_history_actions"]).to include(
|
|
|
|
"id" => 'delete_topic', "action_id" => UserHistory.actions[:delete_topic]
|
|
|
|
)
|
|
|
|
end
|
2019-04-10 08:53:17 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
include_examples "staff action logs accessible"
|
|
|
|
|
|
|
|
it 'generates logs with pages' do
|
|
|
|
1.upto(4).each do |idx|
|
|
|
|
StaffActionLogger.new(Discourse.system_user).log_site_setting_change("title", "value #{idx - 1}", "value #{idx}")
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/admin/logs/staff_action_logs.json", params: { limit: 3 }
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json["staff_action_logs"].length).to eq(3)
|
|
|
|
expect(json["staff_action_logs"][0]["new_value"]).to eq("value 4")
|
|
|
|
|
|
|
|
get "/admin/logs/staff_action_logs.json", params: { limit: 3, page: 1 }
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json["staff_action_logs"].length).to eq(1)
|
|
|
|
expect(json["staff_action_logs"][0]["new_value"]).to eq("value 1")
|
2019-06-05 23:02:53 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context 'when staff actions are extended' do
|
|
|
|
let(:plugin_extended_action) { :confirmed_ham }
|
|
|
|
before { UserHistory.stubs(:staff_actions).returns([plugin_extended_action]) }
|
|
|
|
after { UserHistory.unstub(:staff_actions) }
|
2019-06-05 23:02:53 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
it 'Uses the custom_staff id' do
|
|
|
|
get "/admin/logs/staff_action_logs.json", params: {}
|
2019-06-05 23:02:53 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
json = response.parsed_body
|
|
|
|
action = json['extras']['user_history_actions'].first
|
2019-06-05 23:02:53 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(action['id']).to eq plugin_extended_action.to_s
|
|
|
|
expect(action['action_id']).to eq UserHistory.actions[:custom_staff]
|
|
|
|
end
|
|
|
|
end
|
2019-06-05 23:02:53 -04:00
|
|
|
end
|
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2019-04-10 08:53:17 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "staff action logs accessible"
|
|
|
|
end
|
2019-04-10 08:53:17 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/logs/staff_action_logs.json", params: { action_id: UserHistory.actions[:delete_topic] }
|
2019-04-10 08:53:17 -04:00
|
|
|
|
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"))
|
2019-04-10 08:53:17 -04:00
|
|
|
end
|
2013-08-07 16:04:12 -04:00
|
|
|
end
|
2017-04-12 10:52:52 -04:00
|
|
|
end
|
|
|
|
|
2018-06-11 00:37:06 -04:00
|
|
|
describe '#diff' do
|
2022-11-02 23:42:44 -04:00
|
|
|
shared_examples "theme diffs accessible" do
|
|
|
|
it 'generates diffs for theme changes' do
|
|
|
|
theme = Fabricate(:theme)
|
|
|
|
theme.set_field(target: :mobile, name: :scss, value: 'body {.up}')
|
|
|
|
theme.set_field(target: :common, name: :scss, value: 'omit-dupe')
|
|
|
|
|
|
|
|
original_json = ThemeSerializer.new(theme, root: false).to_json
|
|
|
|
|
|
|
|
theme.set_field(target: :mobile, name: :scss, value: 'body {.down}')
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
record = StaffActionLogger.new(Discourse.system_user)
|
|
|
|
.log_theme_change(original_json, theme)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
get "/admin/logs/staff_action_logs/#{record.id}/diff.json"
|
|
|
|
expect(response.status).to eq(200)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
parsed = response.parsed_body
|
|
|
|
expect(parsed["side_by_side"]).to include("up")
|
|
|
|
expect(parsed["side_by_side"]).to include("down")
|
2017-04-12 10:52:52 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
expect(parsed["side_by_side"]).not_to include("omit-dupe")
|
|
|
|
end
|
|
|
|
end
|
2013-08-07 16:04:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2013-08-07 16:04:12 -04:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
include_examples "theme diffs accessible"
|
|
|
|
|
|
|
|
it 'is not erroring when current value is empty' do
|
|
|
|
theme = Fabricate(:theme)
|
|
|
|
StaffActionLogger.new(admin).log_theme_destroy(theme)
|
|
|
|
get "/admin/logs/staff_action_logs/#{UserHistory.last.id}/diff.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2013-08-07 16:04:12 -04:00
|
|
|
end
|
2019-11-25 17:17:14 -05:00
|
|
|
|
2022-11-02 23:42:44 -04:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "theme diffs accessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
theme = Fabricate(:theme)
|
|
|
|
StaffActionLogger.new(admin).log_theme_destroy(theme)
|
|
|
|
|
|
|
|
get "/admin/logs/staff_action_logs/#{UserHistory.last.id}/diff.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2019-11-25 17:17:14 -05:00
|
|
|
end
|
2013-08-07 16:04:12 -04:00
|
|
|
end
|
|
|
|
end
|