2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Avatarable do
|
2018-04-23 12:59:53 -04:00
|
|
|
let(:project) { create(:project, :with_avatar) }
|
2017-11-09 10:40:41 -05:00
|
|
|
|
|
|
|
let(:gitlab_host) { "https://gitlab.example.com" }
|
|
|
|
let(:relative_url_root) { "/gitlab" }
|
2018-01-17 06:30:25 -05:00
|
|
|
let(:asset_host) { 'https://gitlab-assets.example.com' }
|
2017-11-09 10:40:41 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_config_setting(base_url: gitlab_host)
|
|
|
|
stub_config_setting(relative_url_root: relative_url_root)
|
|
|
|
end
|
|
|
|
|
2018-09-04 14:57:13 -04:00
|
|
|
describe '#update' do
|
2020-03-02 13:07:42 -05:00
|
|
|
let(:validator) { project.class.validators_on(:avatar).find { |v| v.is_a?(FileSizeValidator) } }
|
2018-09-04 14:57:13 -04:00
|
|
|
|
|
|
|
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')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
describe '#avatar_path' do
|
2018-12-19 18:18:20 -05:00
|
|
|
context 'with caching enabled', :request_store do
|
|
|
|
let!(:avatar_path) { [relative_url_root, project.avatar.local_url].join }
|
|
|
|
let!(:avatar_url) { [gitlab_host, relative_url_root, project.avatar.local_url].join }
|
|
|
|
|
|
|
|
it 'only calls local_url once' do
|
|
|
|
expect(project.avatar).to receive(:local_url).once.and_call_original
|
|
|
|
|
|
|
|
2.times do
|
|
|
|
expect(project.avatar_path).to eq(avatar_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls local_url twice for path and URLs' do
|
2020-02-06 16:08:48 -05:00
|
|
|
expect(project.avatar).to receive(:local_url).twice.and_call_original
|
2018-12-19 18:18:20 -05:00
|
|
|
|
|
|
|
expect(project.avatar_path(only_path: true)).to eq(avatar_path)
|
|
|
|
expect(project.avatar_path(only_path: false)).to eq(avatar_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls local_url twice for different sizes' do
|
2020-02-06 16:08:48 -05:00
|
|
|
expect(project.avatar).to receive(:local_url).twice.and_call_original
|
2018-12-19 18:18:20 -05:00
|
|
|
|
|
|
|
expect(project.avatar_path).to eq(avatar_path)
|
|
|
|
expect(project.avatar_path(size: 40)).to eq(avatar_path + "?width=40")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles unpersisted objects' do
|
|
|
|
new_project = build(:project, :with_avatar)
|
|
|
|
path = [relative_url_root, new_project.avatar.local_url].join
|
2020-02-06 16:08:48 -05:00
|
|
|
expect(new_project.avatar).to receive(:local_url).twice.and_call_original
|
2018-12-19 18:18:20 -05:00
|
|
|
|
|
|
|
2.times do
|
|
|
|
expect(new_project.avatar_path).to eq(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2018-01-17 06:30:25 -05:00
|
|
|
where(:has_asset_host, :visibility_level, :only_path, :avatar_path_prefix) do
|
|
|
|
true | Project::PRIVATE | true | [gitlab_host, relative_url_root]
|
|
|
|
true | Project::PRIVATE | false | [gitlab_host, relative_url_root]
|
|
|
|
true | Project::INTERNAL | true | [gitlab_host, relative_url_root]
|
|
|
|
true | Project::INTERNAL | false | [gitlab_host, relative_url_root]
|
|
|
|
true | Project::PUBLIC | true | []
|
|
|
|
true | Project::PUBLIC | false | [asset_host]
|
|
|
|
false | Project::PRIVATE | true | [relative_url_root]
|
|
|
|
false | Project::PRIVATE | false | [gitlab_host, relative_url_root]
|
|
|
|
false | Project::INTERNAL | true | [relative_url_root]
|
|
|
|
false | Project::INTERNAL | false | [gitlab_host, relative_url_root]
|
|
|
|
false | Project::PUBLIC | true | [relative_url_root]
|
|
|
|
false | Project::PUBLIC | false | [gitlab_host, relative_url_root]
|
2017-11-09 10:40:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
2018-01-17 06:30:25 -05:00
|
|
|
allow(ActionController::Base).to receive(:asset_host) { has_asset_host && asset_host }
|
|
|
|
|
|
|
|
project.visibility_level = visibility_level
|
2017-11-09 10:40:41 -05:00
|
|
|
end
|
|
|
|
|
2018-04-23 12:59:53 -04:00
|
|
|
let(:avatar_path) { (avatar_path_prefix + [project.avatar.local_url]).join }
|
2018-01-17 06:30:25 -05:00
|
|
|
|
2017-11-09 10:40:41 -05:00
|
|
|
it 'returns the expected avatar path' do
|
2018-01-17 06:30:25 -05:00
|
|
|
expect(project.avatar_path(only_path: only_path)).to eq(avatar_path)
|
2017-11-09 10:40:41 -05:00
|
|
|
end
|
2018-04-23 12:59:53 -04:00
|
|
|
|
2018-08-07 02:04:33 -04:00
|
|
|
it 'returns the expected avatar path with width parameter' do
|
|
|
|
expect(project.avatar_path(only_path: only_path, size: 128)).to eq(avatar_path + "?width=128")
|
|
|
|
end
|
|
|
|
|
2018-04-23 12:59:53 -04:00
|
|
|
context "when avatar is stored remotely" do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(AvatarUploader)
|
|
|
|
|
|
|
|
project.avatar.migrate!(ObjectStorage::Store::REMOTE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected avatar path' do
|
|
|
|
expect(project.avatar_url(only_path: only_path)).to eq(avatar_path)
|
|
|
|
end
|
|
|
|
end
|
2017-11-09 10:40:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|