From cd97f4c6c717241bffcd3c6a6fd025102d90acfe Mon Sep 17 00:00:00 2001 From: "Pia B." Date: Wed, 1 Jul 2026 11:17:18 +0200 Subject: [PATCH] Add Json schema validation for action logs (#39670) --- app/controllers/admin/tags_controller.rb | 2 +- app/helpers/admin/action_logs_helper.rb | 1 + spec/requests/admin/tags_spec.rb | 12 +++++++++--- spec/support/schema/tags_format_1.0.json | 9 +++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 spec/support/schema/tags_format_1.0.json diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index f72a0325ab7..bc954bdbc9d 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -38,8 +38,8 @@ module Admin def log_action_from_change action_log = current_account.action_logs.new(action: 'update', target: @tag) - action_log.recorded_changes = @tag.saved_changes.slice('usable', 'trendable', 'listable').transform_values(&:last) + action_log.recorded_changes_format = 'tags_format_1.0' action_log.save end diff --git a/app/helpers/admin/action_logs_helper.rb b/app/helpers/admin/action_logs_helper.rb index 592bc356553..2f675aeb7da 100644 --- a/app/helpers/admin/action_logs_helper.rb +++ b/app/helpers/admin/action_logs_helper.rb @@ -55,6 +55,7 @@ module Admin::ActionLogsHelper end def permutation_of_key(log, key) + # we're utilizing the store_accessors here, to get the values from the jsonb like: log.listable => true return if log.public_send(key).nil? log.public_send(key) ? key : :"not_#{key}" diff --git a/spec/requests/admin/tags_spec.rb b/spec/requests/admin/tags_spec.rb index cca633d83f1..314024902c2 100644 --- a/spec/requests/admin/tags_spec.rb +++ b/spec/requests/admin/tags_spec.rb @@ -20,12 +20,18 @@ RSpec.describe 'Admin Tags' do let(:tag) { Fabricate :tag, name: '#supertag' } it 'redirects to tag page and saves update log action for all attribute statuses' do - put admin_tag_path(tag.id, params: { tag: { trendable: true, listable: false } }) + put admin_tag_path(tag.id, params: { tag: { trendable: true, listable: false, unallowed: true } }) expect(response).to have_http_status(302) - expect(Admin::ActionLog.last.human_identifier).to eq('#supertag') - expect(Admin::ActionLog.pluck(:action)).to eq(%w(update)) expect(Admin::ActionLog.pluck(:recorded_changes)).to eq([{ 'listable' => false, 'trendable' => true }]) + expect(Admin::ActionLog.last.recorded_changes).to match_json_schema('tags_format_1.0') + end + + it 'returns invalid when the schema does not match' do + put admin_tag_path(tag.id, params: { tag: { trendable: true } }) + + Admin::ActionLog.last.update(recorded_changes: { 'unallowed' => false }) + expect(Admin::ActionLog.last.recorded_changes).to_not match_json_schema('tags_format_1.0') end end end diff --git a/spec/support/schema/tags_format_1.0.json b/spec/support/schema/tags_format_1.0.json new file mode 100644 index 00000000000..d2541c3d722 --- /dev/null +++ b/spec/support/schema/tags_format_1.0.json @@ -0,0 +1,9 @@ +{ + "type": "object", + "properties": { + "usable": { "type": "boolean" }, + "trendable": { "type": "boolean" }, + "listable": { "type": "boolean" } + }, + "additionalProperties": false +}