FEATURE: Check if selectable avatars exist before enabling them (#10032)

This commit is contained in:
Bianca Nenciu 2020-06-22 16:58:26 +03:00 committed by GitHub
parent 685646540a
commit 68f767a557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 7 deletions

View File

@ -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'."

View File

@ -1241,6 +1241,7 @@ files:
selectable_avatars_enabled:
default: false
client: true
validator: "SelectableAvatarsEnabledValidator"
selectable_avatars:
default: ""
type: uploaded_image_list

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)