From 68f767a557b57e10133e2e4872b2d514ab3a22d2 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Mon, 22 Jun 2020 16:58:26 +0300 Subject: [PATCH] FEATURE: Check if selectable avatars exist before enabling them (#10032) --- config/locales/server.en.yml | 1 + config/site_settings.yml | 1 + .../selectable_avatars_enabled_validator.rb | 15 ++++++++++ ...lectable_avatars_enabled_validator_spec.rb | 30 +++++++++++++++++++ spec/models/user_spec.rb | 2 +- spec/requests/users_controller_spec.rb | 11 ++++--- 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 lib/validators/selectable_avatars_enabled_validator.rb create mode 100644 spec/components/validators/selectable_avatars_enabled_validator_spec.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index eb5ae6820aa..03d621d3de9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2255,6 +2255,7 @@ en: max_username_length_exists: "You cannot set the maximum username length below the longest username (%{username})." max_username_length_range: "You cannot set the maximum below the minimum." invalid_hex_value: "Color values have to be 6-digit hexadecimal codes." + empty_selectable_avatars: "You must first upload at least two selectable avatars before enabling this setting." category_search_priority: very_low_weight_invalid: "You cannot set the weight to be greater than 'category_search_priority_low_weight'." low_weight_invalid: "You cannot set the weight to be greater or equal to 1 or smaller than 'category_search_priority_very_low_weight'." diff --git a/config/site_settings.yml b/config/site_settings.yml index 06549cd6734..e8cd6f7d7aa 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1241,6 +1241,7 @@ files: selectable_avatars_enabled: default: false client: true + validator: "SelectableAvatarsEnabledValidator" selectable_avatars: default: "" type: uploaded_image_list diff --git a/lib/validators/selectable_avatars_enabled_validator.rb b/lib/validators/selectable_avatars_enabled_validator.rb new file mode 100644 index 00000000000..065c5bcf981 --- /dev/null +++ b/lib/validators/selectable_avatars_enabled_validator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class SelectableAvatarsEnabledValidator + def initialize(opts = {}) + @opts = opts + end + + def valid_value?(value) + value == "f" || SiteSetting.selectable_avatars.split("\n").size > 1 + end + + def error_message + I18n.t('site_settings.errors.empty_selectable_avatars') + end +end diff --git a/spec/components/validators/selectable_avatars_enabled_validator_spec.rb b/spec/components/validators/selectable_avatars_enabled_validator_spec.rb new file mode 100644 index 00000000000..2635e2e3740 --- /dev/null +++ b/spec/components/validators/selectable_avatars_enabled_validator_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe SelectableAvatarsEnabledValidator do + describe '#valid_value?' do + subject(:validator) { described_class.new } + + it "returns true when disabling" do + SiteSetting.selectable_avatars = "" + expect(validator.valid_value?("f")).to eq(true) + + SiteSetting.selectable_avatars = [Fabricate(:image_upload).url, Fabricate(:image_upload).url].join("\n") + expect(validator.valid_value?("f")).to eq(true) + end + + it "returns true when there are at least two selectable avatars" do + SiteSetting.selectable_avatars = [Fabricate(:image_upload).url, Fabricate(:image_upload).url].join("\n") + expect(validator.valid_value?("t")).to eq(true) + end + + it "returns false when selectable avatars is blank or has one avatar" do + SiteSetting.selectable_avatars = "" + expect(validator.valid_value?("t")).to eq(false) + + SiteSetting.selectable_avatars = Fabricate(:image_upload).url + expect(validator.valid_value?("t")).to eq(false) + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index cc11a35c37a..791153b43ff 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2033,8 +2033,8 @@ describe User do it "sets a random avatar when selectable avatars is enabled" do avatar1 = Fabricate(:upload) avatar2 = Fabricate(:upload) - SiteSetting.selectable_avatars_enabled = true SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n") + SiteSetting.selectable_avatars_enabled = true user = Fabricate(:user) expect(user.uploaded_avatar_id).not_to be(nil) diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index add1d7af01a..b5a13429be8 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -2290,19 +2290,18 @@ describe UsersController do context 'selectable avatars is enabled' do - before { SiteSetting.selectable_avatars_enabled = true } + before do + SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n") + SiteSetting.selectable_avatars_enabled = true + end it 'raises an error when selectable avatars is empty' do + SiteSetting.selectable_avatars = "" put "/u/#{user.username}/preferences/avatar/select.json", params: { url: url } expect(response.status).to eq(422) end context 'selectable avatars is properly setup' do - - before do - SiteSetting.selectable_avatars = [avatar1.url, avatar2.url].join("\n") - end - it 'raises an error when url is not in selectable avatars list' do put "/u/#{user.username}/preferences/avatar/select.json", params: { url: url } expect(response.status).to eq(422)