From bad3b7a2be86f9cdbce2b8e3ebcb244c40f28951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 23 May 2024 00:23:42 +0200 Subject: [PATCH] Use dynamic list for allowed user preference params --- app/controllers/settings_controller.rb | 8 +++----- app/models/user_preferences.rb | 4 ++++ spec/models/user_preferences_spec.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index cb7de77..8e307ca 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -147,11 +147,9 @@ class SettingsController < ApplicationController end def user_params - params.require(:user).permit(:display_name, :avatar, preferences: [ - :lightning_notify_sats_received, - :remotestorage_notify_auth_created, - :xmpp_exchange_contacts_with_invitees - ]) + params.require(:user).permit( + :display_name, :avatar, preferences: UserPreferences.pref_keys + ) end def email_params diff --git a/app/models/user_preferences.rb b/app/models/user_preferences.rb index ffdf7f6..0eba822 100644 --- a/app/models/user_preferences.rb +++ b/app/models/user_preferences.rb @@ -26,4 +26,8 @@ class UserPreferences end hash.stringify_keys!.to_h end + + def self.pref_keys + DEFAULT_PREFS.keys.map(&:to_sym) + end end diff --git a/spec/models/user_preferences_spec.rb b/spec/models/user_preferences_spec.rb index 4bb7b42..22e3e29 100644 --- a/spec/models/user_preferences_spec.rb +++ b/spec/models/user_preferences_spec.rb @@ -38,4 +38,15 @@ RSpec.describe UserPreferences, type: :model do expect(res['lightning_notify_sats_received_threshold']).to eq(1000) end end + + describe ".pref_keys" do + let(:default_prefs) { YAML.load_file("#{Rails.root}/config/default_preferences.yml") } + + it "returns the keys of all default preferences as an array of symbols" do + expect(UserPreferences.pref_keys).to be_a(Array) + expect(UserPreferences.pref_keys).to include(:lightning_notify_sats_received) + expect(UserPreferences.pref_keys).to include(:xmpp_exchange_contacts_with_invitees) + expect(UserPreferences.pref_keys.length).to eq(default_prefs.keys.length) + end + end end