Fix user settings not getting validated (#25508)

pull/25513/head
Claire 2 years ago committed by GitHub
parent 804488d38e
commit dd07393e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -72,7 +72,10 @@ class UserSettings
raise KeyError, "Undefined setting: #{key}" unless self.class.definition_for?(key)
typecast_value = self.class.definition_for(key).type_cast(value)
setting_definition = self.class.definition_for(key)
typecast_value = setting_definition.type_cast(value)
raise ArgumentError, "Invalid value for setting #{key}: #{typecast_value}" if setting_definition.in.present? && setting_definition.in.exclude?(typecast_value)
if typecast_value.nil?
@original_hash.delete(key)

@ -49,6 +49,16 @@ RSpec.describe UserSettings do
expect(subject[:always_send_emails]).to be true
end
end
context 'when the setting has a closed set of values' do
it 'updates the attribute when given a valid value' do
expect { subject[:'web.display_media'] = :show_all }.to change { subject[:'web.display_media'] }.from('default').to('show_all')
end
it 'raises an error when given an invalid value' do
expect { subject[:'web.display_media'] = 'invalid value' }.to raise_error ArgumentError
end
end
end
describe '#update' do

Loading…
Cancel
Save