Skip to content

Commit

Permalink
Fix user settings not getting validated (mastodon#25508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Jun 19, 2023
1 parent 804488d commit dd07393
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/models/user_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def []=(key, value)

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)
Expand Down
10 changes: 10 additions & 0 deletions spec/models/user_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
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
Expand Down

0 comments on commit dd07393

Please sign in to comment.