Commit d4bdcfbf authored by Stan Hu's avatar Stan Hu

Disable project avatar validation if avatar has not changed

Every time a column in the projects table is changed, the Avatarable concern
would validate that the avatar file size was under 200K.  This not only delays
the database changes, but it also can lead to unrelated failures if the HTTP
request fails for some reason.

Closes #51053
parent 8ff02cf7
......@@ -9,7 +9,7 @@ module Avatarable
include Gitlab::Utils::StrongMemoize
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }, if: :avatar_changed?
mount_uploader :avatar, AvatarUploader
title: Disable project avatar validation if avatar has not changed
type: performance
......@@ -12,6 +12,26 @@ describe Avatarable do
stub_config_setting(relative_url_root: relative_url_root)
describe '#update' do
let(:validator) { project._validators[:avatar].detect { |v| v.is_a?(FileSizeValidator) } }
context 'when avatar changed' do
it 'validates the file size' do
expect(validator).to receive(:validate_each).and_call_original
project.update(avatar: 'uploads/avatar.png')
context 'when avatar was not changed' do
it 'skips validation of file size' do
expect(validator).not_to receive(:validate_each)
project.update(name: 'Hello world')
describe '#avatar_path' do
using RSpec::Parameterized::TableSyntax
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment