Add Json schema validation for action logs (#39670)

This commit is contained in:
Pia B.
2026-07-01 11:17:18 +02:00
committed by GitHub
parent 16baa14ee1
commit cd97f4c6c7
4 changed files with 20 additions and 4 deletions

View File

@@ -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

View File

@@ -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}"

View File

@@ -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

View File

@@ -0,0 +1,9 @@
{
"type": "object",
"properties": {
"usable": { "type": "boolean" },
"trendable": { "type": "boolean" },
"listable": { "type": "boolean" }
},
"additionalProperties": false
}