2017-11-06 21:44:30 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
require Rails.root.join('db', 'post_migrate', '20171103140253_track_untracked_uploads')
|
|
|
|
|
|
|
|
describe TrackUntrackedUploads, :migration, :sidekiq do
|
2017-11-08 03:08:02 +00:00
|
|
|
class UnhashedUploadFile < ActiveRecord::Base
|
|
|
|
self.table_name = 'unhashed_upload_files'
|
|
|
|
end
|
|
|
|
|
2017-11-06 21:44:30 +00:00
|
|
|
matcher :be_scheduled_migration do
|
|
|
|
match do |migration|
|
|
|
|
BackgroundMigrationWorker.jobs.any? do |job|
|
|
|
|
job['args'] == [migration]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |migration|
|
|
|
|
"Migration `#{migration}` with args `#{expected.inspect}` not scheduled!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly schedules the follow-up background migration' do
|
|
|
|
Sidekiq::Testing.fake! do
|
|
|
|
migrate!
|
|
|
|
|
|
|
|
expect(described_class::MIGRATION).to be_scheduled_migration
|
|
|
|
expect(BackgroundMigrationWorker.jobs.size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures the unhashed_upload_files table exists' do
|
|
|
|
expect do
|
|
|
|
migrate!
|
|
|
|
end.to change { table_exists?(:unhashed_upload_files) }.from(false).to(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a path field long enough for really long paths' do
|
|
|
|
migrate!
|
|
|
|
|
|
|
|
max_length_namespace_path = max_length_project_path = max_length_filename = 'a' * 255
|
|
|
|
long_path = "./uploads#{("/#{max_length_namespace_path}") * Namespace::NUMBER_OF_ANCESTORS_ALLOWED}/#{max_length_project_path}/#{max_length_filename}"
|
|
|
|
unhashed_upload_file = UnhashedUploadFile.new(path: long_path)
|
|
|
|
unhashed_upload_file.save!
|
|
|
|
expect(UnhashedUploadFile.first.path.size).to eq(5641)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with tracked and untracked uploads' do
|
|
|
|
let(:user1) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
|
|
|
let(:project1) { create(:project) }
|
|
|
|
let(:project2) { create(:project) }
|
|
|
|
let(:appearance) { create(:appearance) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
fixture = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg')
|
|
|
|
|
|
|
|
# Tracked, by doing normal file upload
|
|
|
|
uploaded_file = fixture_file_upload(fixture)
|
|
|
|
user1.update(avatar: uploaded_file)
|
|
|
|
project1.update(avatar: uploaded_file)
|
2017-11-08 03:08:02 +00:00
|
|
|
upload_result = UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload
|
|
|
|
@project1_markdown_upload_path = upload_result[:url].sub(/\A\/uploads\//, '')
|
2017-11-06 21:44:30 +00:00
|
|
|
appearance.update(logo: uploaded_file)
|
|
|
|
|
|
|
|
# Untracked, by doing normal file upload then deleting records from DB
|
|
|
|
uploaded_file = fixture_file_upload(fixture)
|
|
|
|
user2.update(avatar: uploaded_file)
|
|
|
|
user2.uploads.delete_all
|
|
|
|
project2.update(avatar: uploaded_file)
|
2017-11-08 03:08:02 +00:00
|
|
|
upload_result = UploadService.new(project2, uploaded_file, FileUploader).execute # Markdown upload
|
|
|
|
@project2_markdown_upload_path = upload_result[:url].sub(/\A\/uploads\//, '')
|
2017-11-06 21:44:30 +00:00
|
|
|
project2.uploads.delete_all
|
|
|
|
appearance.update(header_logo: uploaded_file)
|
|
|
|
appearance.uploads.last.destroy
|
|
|
|
end
|
|
|
|
|
2017-11-08 03:08:02 +00:00
|
|
|
it 'tracks untracked migrations' do
|
2017-11-06 21:44:30 +00:00
|
|
|
Sidekiq::Testing.inline! do
|
|
|
|
migrate!
|
|
|
|
|
|
|
|
# Tracked uploads still exist
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(user1.reload.uploads.first.attributes).to include({
|
|
|
|
"path" => "uploads/-/system/user/avatar/#{user1.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AvatarUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(project1.reload.uploads.first.attributes).to include({
|
|
|
|
"path" => "uploads/-/system/project/avatar/#{project1.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AvatarUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(appearance.reload.uploads.first.attributes).to include({
|
|
|
|
"path" => "uploads/-/system/appearance/logo/#{appearance.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AttachmentUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(project1.uploads.last.attributes).to include({
|
|
|
|
"path" => @project1_markdown_upload_path,
|
|
|
|
"uploader" => "FileUploader"
|
|
|
|
})
|
2017-11-06 21:44:30 +00:00
|
|
|
|
|
|
|
# Untracked uploads are now tracked
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(user2.reload.uploads.first.attributes).to include({
|
|
|
|
"path" => "uploads/-/system/user/avatar/#{user2.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AvatarUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(project2.reload.uploads.first.attributes).to include({
|
|
|
|
"path" => "uploads/-/system/project/avatar/#{project2.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AvatarUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(appearance.reload.uploads.count).to eq(2)
|
2017-11-06 21:44:30 +00:00
|
|
|
expect(appearance.uploads.last.attributes).to include({
|
2017-11-08 03:08:02 +00:00
|
|
|
"path" => "uploads/-/system/appearance/header_logo/#{appearance.id}/rails_sample.jpg",
|
2017-11-06 21:44:30 +00:00
|
|
|
"uploader" => "AttachmentUploader"
|
|
|
|
})
|
2017-11-08 03:08:02 +00:00
|
|
|
expect(project2.uploads.last.attributes).to include({
|
|
|
|
"path" => @project2_markdown_upload_path,
|
|
|
|
"uploader" => "FileUploader"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'all UnhashedUploadFile records are marked as tracked' do
|
|
|
|
Sidekiq::Testing.inline! do
|
|
|
|
migrate!
|
|
|
|
|
|
|
|
expect(UnhashedUploadFile.count).to eq(8)
|
|
|
|
expect(UnhashedUploadFile.count).to eq(UnhashedUploadFile.where(tracked: true).count)
|
2017-11-06 21:44:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|