summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2016-02-29 17:17:14 -0500
committerRobert Speicher <rspeicher@gmail.com>2016-02-29 17:19:53 -0500
commit58a8d6e9ba783d26ebdc6d79b2c457e2721988e8 (patch)
tree986f0dbeaa8708b8325ea0503f6e1e299df30505
parent128757a7f97b3dc0bf2efd4fac49dc0eaa0af04c (diff)
downloadgitlab-ce-58a8d6e9ba783d26ebdc6d79b2c457e2721988e8.tar.gz
Only validate avatar cropping values when avatar is present AND changedrs-avatar-validation
Closes #13905
-rw-r--r--app/models/user.rb2
-rw-r--r--spec/models/user_spec.rb16
2 files changed, 16 insertions, 2 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 913a32854ac..c5a9fec822a 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -169,7 +169,7 @@ class User < ActiveRecord::Base
validates :avatar_crop_x, :avatar_crop_y, :avatar_crop_size,
numericality: { only_integer: true },
presence: true,
- if: ->(user) { user.avatar? }
+ if: ->(user) { user.avatar? && user.avatar_changed? }
before_validation :generate_password, on: :create
before_validation :restricted_signup_domains, on: :create
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 88821dd0dad..b2a398348b8 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -176,7 +176,7 @@ describe User, models: true do
end
describe 'avatar' do
- it 'only validates when avatar is present' do
+ it 'only validates when avatar is present and changed' do
user = build(:user, :with_avatar)
user.avatar_crop_x = nil
@@ -184,6 +184,20 @@ describe User, models: true do
user.avatar_crop_size = nil
expect(user).not_to be_valid
+ expect(user.errors.keys).
+ to match_array %i(avatar_crop_x avatar_crop_y avatar_crop_size)
+ end
+
+ it 'does not validate when avatar has not changed' do
+ user = create(:user, :with_avatar)
+
+ expect { user.avatar_crop_x = nil }.not_to change(user, :valid?)
+ end
+
+ it 'does not validate when avatar is not present' do
+ user = create(:user)
+
+ expect { user.avatar_crop_y = nil }.not_to change(user, :valid?)
end
end
end