Browse Source

Improve allowed language handling (#2897)

* Dont allow empty value in user allowed languages

* Sanitize language input to reject blank values in array
master
Matt Jankowski 7 years ago
committed by Eugen Rochko
parent
commit
bba537a7be
4 changed files with 27 additions and 1 deletions
  1. +8
    -0
      app/models/user.rb
  2. +1
    -1
      spec/controllers/settings/preferences_controller_spec.rb
  3. +12
    -0
      spec/models/status_spec.rb
  4. +6
    -0
      spec/models/user_spec.rb

+ 8
- 0
app/models/user.rb View File

@ -52,6 +52,8 @@ class User < ApplicationRecord
scope :admins, -> { where(admin: true) } scope :admins, -> { where(admin: true) }
scope :confirmed, -> { where.not(confirmed_at: nil) } scope :confirmed, -> { where.not(confirmed_at: nil) }
before_validation :sanitize_languages
def confirmed? def confirmed?
confirmed_at.present? confirmed_at.present?
end end
@ -77,4 +79,10 @@ class User < ApplicationRecord
def setting_auto_play_gif def setting_auto_play_gif
settings.auto_play_gif settings.auto_play_gif
end end
private
def sanitize_languages
allowed_languages.reject!(&:blank?)
end
end end

+ 1
- 1
spec/controllers/settings/preferences_controller_spec.rb View File

@ -18,7 +18,7 @@ describe Settings::PreferencesController do
describe 'PUT #update' do describe 'PUT #update' do
it 'updates the user record' do it 'updates the user record' do
put :update, params: { user: { locale: 'en', allowed_languages: ['es', 'fr'] } }
put :update, params: { user: { locale: 'en', allowed_languages: ['es', 'fr', ''] } }
expect(response).to redirect_to(settings_preferences_path) expect(response).to redirect_to(settings_preferences_path)
user.reload user.reload

+ 12
- 0
spec/models/status_spec.rb View File

@ -265,6 +265,18 @@ RSpec.describe Status, type: :model do
expect(results).not_to include(fr_status) expect(results).not_to include(fr_status)
end end
it 'includes all languages when user does not have a setting' do
user = Fabricate(:user, allowed_languages: [])
@account.update(user: user)
en_status = Fabricate(:status, language: 'en')
es_status = Fabricate(:status, language: 'es')
results = Status.as_public_timeline(@account)
expect(results).to include(en_status)
expect(results).to include(es_status)
end
it 'includes all languages when account does not have a user' do it 'includes all languages when account does not have a user' do
expect(@account.user).to be_nil expect(@account.user).to be_nil
en_status = Fabricate(:status, language: 'en') en_status = Fabricate(:status, language: 'en')

+ 6
- 0
spec/models/user_spec.rb View File

@ -22,6 +22,12 @@ RSpec.describe User, type: :model do
user.valid? user.valid?
expect(user).to model_have_error_on_field(:email) expect(user).to model_have_error_on_field(:email)
end end
it 'cleans out empty string from languages' do
user = Fabricate.build(:user, allowed_languages: [''])
user.valid?
expect(user.allowed_languages).to eq []
end
end end
describe 'settings' do describe 'settings' do

Loading…
Cancel
Save