gitlab-org--gitlab-foss/spec/models/appearance_spec.rb
Stan Hu 19f9d99870 Fix 500 errors with legacy appearance logos
Prior to GitLab 9.0, attachments were not tracked the `uploads` table,
so it was possible that the appearance logos were just stored in the
database as a string and mounted via CarrierWave.

https://gitlab.com/gitlab-org/gitlab-ce/issues/29240 implemented in
GitLab 10.3 was supposed to cover populating the `uploads` table for all
attachments, including all the logos from appearances. However, it's
possible that didn't work for logos or the `uploads` entry was orphaned.

GitLab instances that had a customized logo with no associated `uploads`
entry would see Error 500s. The only way to fix this is to delete the
`logo` column from the `appearances` table and re-upload the attachment.

This change makes things more robust by falling back to the original
behavior if the upload is not available.

This is a CE backport of
https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/9277.

Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/9357
2019-01-23 11:18:00 -08:00

66 lines
1.9 KiB
Ruby

require 'rails_helper'
describe Appearance do
subject { build(:appearance) }
it { include(CacheableAttributes) }
it { expect(described_class.current_without_cache).to eq(described_class.first) }
it { is_expected.to have_many(:uploads) }
describe '#single_appearance_row' do
it 'adds an error when more than 1 row exists' do
create(:appearance)
new_row = build(:appearance)
new_row.save
expect(new_row.valid?).to eq(false)
end
end
context 'with uploads' do
it_behaves_like 'model with uploads', false do
let(:model_object) { create(:appearance, :with_logo) }
let(:upload_attribute) { :logo }
let(:uploader_class) { AttachmentUploader }
end
end
shared_examples 'logo paths' do |logo_type|
let(:appearance) { create(:appearance, "with_#{logo_type}".to_sym) }
let(:filename) { 'dk.png' }
let(:expected_path) { "/uploads/-/system/appearance/#{logo_type}/#{appearance.id}/#{filename}" }
it 'returns nil when there is no upload' do
expect(subject.send("#{logo_type}_path")).to be_nil
end
it 'returns the path when the upload has been orphaned' do
appearance.send(logo_type).upload.destroy
appearance.reload
expect(appearance.send("#{logo_type}_path")).to eq(expected_path)
end
it 'returns a local path using the system route' do
expect(appearance.send("#{logo_type}_path")).to eq(expected_path)
end
describe 'with asset host configured' do
let(:asset_host) { 'https://gitlab-assets.example.com' }
before do
allow(ActionController::Base).to receive(:asset_host) { asset_host }
end
it 'returns a full URL with the system path' do
expect(appearance.send("#{logo_type}_path")).to eq("#{asset_host}#{expected_path}")
end
end
end
%i(logo header_logo favicon).each do |logo_type|
it_behaves_like 'logo paths', logo_type
end
end