Add Geo avatar downloader

parent 98cd66ee
module Gitlab
module Geo
class AvatarDownloader < FileDownloader
def execute
upload = Upload.find_by_id(object_db_id)
return unless upload.present?
transfer = ::Gitlab::Geo::AvatarTransfer.new(upload)
transfer.download_from_primary
end
end
end
end
require 'spec_helper'
describe Gitlab::Geo::AvatarDownloader do
let(:avatar) { fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png') }
let(:user) { create(:user, avatar: avatar) }
let(:upload) { Upload.find_by(model: user, uploader: 'AvatarUploader') }
context '#download_from_primary' do
it 'downlods the avatar' do
allow_any_instance_of(Gitlab::Geo::AvatarTransfer)
.to receive(:download_from_primary).and_return(100)
downloader = described_class.new(upload.id)
expect(downloader.execute).to eq(100)
end
it 'returns nil with unknown avatar' do
downloader = described_class.new(10000)
expect(downloader).not_to receive(:download_from_primary)
expect(downloader.execute).to be_nil
end
end
end
require 'spec_helper'
describe Geo::FileDownloadService, services: true do
let(:lfs_object) { create(:lfs_object) }
let!(:primary) { create(:geo_node, :primary) }
let(:secondary) { create(:geo_node) }
subject { Geo::FileDownloadService.new(:lfs, lfs_object.id) }
before do
create(:geo_node, :primary)
allow(described_class).to receive(:current_node) { secondary }
end
describe '#execute' do
context 'user avatar' do
let(:user) { create(:user, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
let(:upload) { Upload.find_by(model: user, uploader: 'AvatarUploader') }
subject { described_class.new(:avatar, upload.id) }
it 'downloads an user avatar' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::Geo::AvatarTransfer)
.to receive(:download_from_primary).and_return(100)
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
context 'group avatar' do
let(:group) { create(:group, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
let(:upload) { Upload.find_by(model: group, uploader: 'AvatarUploader') }
subject { described_class.new(:avatar, upload.id) }
it 'downloads a group avatar' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::Geo::AvatarTransfer)
.to receive(:download_from_primary).and_return(100)
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
context 'project avatar' do
let(:project) { create(:empty_project, avatar: fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'image/png')) }
let(:upload) { Upload.find_by(model: project, uploader: 'AvatarUploader') }
subject { described_class.new(:avatar, upload.id) }
it 'downloads a project avatar' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
allow_any_instance_of(Gitlab::Geo::AvatarTransfer)
.to receive(:download_from_primary).and_return(100)
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
context 'LFS object' do
let(:lfs_object) { create(:lfs_object) }
subject { described_class.new(:lfs, lfs_object.id) }
it 'downloads an LFS object' do
allow_any_instance_of(Gitlab::ExclusiveLease)
.to receive(:try_obtain).and_return(true)
......@@ -20,9 +70,10 @@ describe Geo::FileDownloadService, services: true do
expect{ subject.execute }.to change { Geo::FileRegistry.count }.by(1)
end
end
it 'bad object type' do
expect{ described_class.new(:bad, lfs_object.id).execute }.to raise_error(NameError)
it 'raises an error with bad object type' do
expect{ described_class.new(:bad, 1).execute }.to raise_error(NameError)
end
end
end
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