Commit d5300856 authored by Michael Kozono's avatar Michael Kozono

Refactor specs

parent c25b7c0e
require 'spec_helper' require 'spec_helper'
describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :migration, :sidekiq, schema: 20171103140253 do describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :migration, :sidekiq, schema: 20171103140253 do
let!(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) } include TrackUntrackedUploadsHelpers
let(:user1) { create(:user) } let!(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) }
let(:user2) { create(:user) }
let(:project1) { create(:project) }
let(:project2) { create(:project) }
let(:appearance) { create(:appearance) }
matcher :be_scheduled_migration do |*expected| matcher :be_scheduled_migration do |*expected|
match do |migration| match do |migration|
...@@ -22,20 +18,18 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :migration, :side ...@@ -22,20 +18,18 @@ describe Gitlab::BackgroundMigration::PrepareUntrackedUploads, :migration, :side
end end
context 'when files were uploaded before and after hashed storage was enabled' do context 'when files were uploaded before and after hashed storage was enabled' do
before do let!(:appearance) { create(:appearance, logo: uploaded_file, header_logo: uploaded_file) }
fixture = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg') let!(:user) { create(:user, :with_avatar) }
uploaded_file = fixture_file_upload(fixture) let!(:project1) { create(:project, :with_avatar) }
let(:project2) { create(:project) } # instantiate after enabling hashed_storage
user1.update(avatar: uploaded_file) before do
project1.update(avatar: uploaded_file) # Markdown upload before enabling hashed_storage
appearance.update(logo: uploaded_file, header_logo: uploaded_file) UploadService.new(project1, uploaded_file, FileUploader).execute
uploaded_file = fixture_file_upload(fixture)
UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload
stub_application_setting(hashed_storage_enabled: true) stub_application_setting(hashed_storage_enabled: true)
# Hashed files # Markdown upload after enabling hashed_storage
uploaded_file = fixture_file_upload(fixture)
UploadService.new(project2, uploaded_file, FileUploader).execute UploadService.new(project2, uploaded_file, FileUploader).execute
end end
......
...@@ -4,9 +4,7 @@ require Rails.root.join('db', 'post_migrate', '20171103140253_track_untracked_up ...@@ -4,9 +4,7 @@ require Rails.root.join('db', 'post_migrate', '20171103140253_track_untracked_up
describe TrackUntrackedUploads, :migration, :sidekiq do describe TrackUntrackedUploads, :migration, :sidekiq do
include TrackUntrackedUploadsHelpers include TrackUntrackedUploadsHelpers
class UntrackedFile < ActiveRecord::Base let(:untracked_files_for_uploads) { table(:untracked_files_for_uploads) }
self.table_name = 'untracked_files_for_uploads'
end
matcher :be_scheduled_migration do matcher :be_scheduled_migration do
match do |migration| match do |migration|
...@@ -46,39 +44,36 @@ describe TrackUntrackedUploads, :migration, :sidekiq do ...@@ -46,39 +44,36 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
component # filename component # filename
].flatten.join('/') ].flatten.join('/')
record = UntrackedFile.create!(path: long_path) record = untracked_files_for_uploads.create!(path: long_path)
expect(record.reload.path.size).to eq(519) expect(record.reload.path.size).to eq(519)
end end
context 'with tracked and untracked uploads' do context 'with tracked and untracked uploads' do
let(:user1) { create(:user) } let!(:appearance) { create(:appearance, logo: uploaded_file, header_logo: uploaded_file) }
let(:user2) { create(:user) } let!(:user1) { create(:user, :with_avatar) }
let(:project1) { create(:project) } let!(:user2) { create(:user, :with_avatar) }
let(:project2) { create(:project) } let!(:project1) { create(:project, :with_avatar) }
let(:appearance) { create(:appearance) } let!(:project2) { create(:project, :with_avatar) }
let(:uploads) { table(:uploads) } let(:uploads) { table(:uploads) }
before do before do
fixture = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg') UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload
UploadService.new(project2, uploaded_file, FileUploader).execute # Markdown upload
# Tracked, by doing normal file upload
uploaded_file = fixture_file_upload(fixture) # Save expected Upload attributes
user1.update(avatar: uploaded_file) @appearance_logo_attributes = appearance.uploads.where("path like '%/logo/%'").first.attributes.slice('path', 'uploader', 'size', 'checksum')
project1.update(avatar: uploaded_file) @appearance_header_logo_attributes = appearance.uploads.where("path like '%/header_logo/%'").first.attributes.slice('path', 'uploader', 'size', 'checksum')
upload_result = UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload @user1_avatar_attributes = user1.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
@project1_markdown_upload_path = upload_result[:url].sub(%r{\A/uploads/}, '') @user2_avatar_attributes = user2.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
appearance.update(logo: uploaded_file) @project1_avatar_attributes = project1.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
@project2_avatar_attributes = project2.uploads.first.attributes.slice('path', 'uploader', 'size', 'checksum')
# Untracked, by doing normal file upload then deleting records from DB @project1_markdown_attributes = project1.uploads.last.attributes.slice('path', 'uploader', 'size', 'checksum')
uploaded_file = fixture_file_upload(fixture) @project2_markdown_attributes = project2.uploads.last.attributes.slice('path', 'uploader', 'size', 'checksum')
user2.update(avatar: uploaded_file)
# Untrack 4 files
user2.uploads.delete_all user2.uploads.delete_all
project2.update(avatar: uploaded_file) project2.uploads.delete_all # 2 files: avatar and a Markdown upload
upload_result = UploadService.new(project2, uploaded_file, FileUploader).execute # Markdown upload appearance.uploads.where("path like '%header_logo%'").delete_all
@project2_markdown_upload_path = upload_result[:url].sub(%r{\A/uploads/}, '')
project2.uploads.delete_all
appearance.update(header_logo: uploaded_file)
appearance.uploads.last.destroy
end end
it 'tracks untracked uploads' do it 'tracks untracked uploads' do
...@@ -87,23 +82,10 @@ describe TrackUntrackedUploads, :migration, :sidekiq do ...@@ -87,23 +82,10 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
migrate! migrate!
end.to change { uploads.count }.from(4).to(8) end.to change { uploads.count }.from(4).to(8)
expect(user2.reload.uploads.first.attributes).to include({ expect(appearance.reload.uploads.where("path like '%/header_logo/%'").first.attributes).to include(@appearance_header_logo_attributes)
"path" => "uploads/-/system/user/avatar/#{user2.id}/rails_sample.jpg", expect(user2.reload.uploads.first.attributes).to include(@user2_avatar_attributes)
"uploader" => "AvatarUploader" expect(project2.reload.uploads.first.attributes).to include(@project2_avatar_attributes)
}.merge(rails_sample_jpg_attrs)) expect(project2.uploads.last.attributes).to include(@project2_markdown_attributes)
expect(project2.reload.uploads.first.attributes).to include({
"path" => "uploads/-/system/project/avatar/#{project2.id}/rails_sample.jpg",
"uploader" => "AvatarUploader"
}.merge(rails_sample_jpg_attrs))
expect(appearance.reload.uploads.count).to eq(2)
expect(appearance.uploads.last.attributes).to include({
"path" => "uploads/-/system/appearance/header_logo/#{appearance.id}/rails_sample.jpg",
"uploader" => "AttachmentUploader"
}.merge(rails_sample_jpg_attrs))
expect(project2.uploads.last.attributes).to include({
"path" => @project2_markdown_upload_path,
"uploader" => "FileUploader"
}.merge(rails_sample_jpg_attrs))
end end
end end
...@@ -111,31 +93,19 @@ describe TrackUntrackedUploads, :migration, :sidekiq do ...@@ -111,31 +93,19 @@ describe TrackUntrackedUploads, :migration, :sidekiq do
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
migrate! migrate!
expect(user1.reload.uploads.first.attributes).to include({ expect(appearance.reload.uploads.where("path like '%/logo/%'").first.attributes).to include(@appearance_logo_attributes)
"path" => "uploads/-/system/user/avatar/#{user1.id}/rails_sample.jpg", expect(user1.reload.uploads.first.attributes).to include(@user1_avatar_attributes)
"uploader" => "AvatarUploader" expect(project1.reload.uploads.first.attributes).to include(@project1_avatar_attributes)
}.merge(rails_sample_jpg_attrs)) expect(project1.uploads.last.attributes).to include(@project1_markdown_attributes)
expect(project1.reload.uploads.first.attributes).to include({ end
"path" => "uploads/-/system/project/avatar/#{project1.id}/rails_sample.jpg", end
"uploader" => "AvatarUploader"
}.merge(rails_sample_jpg_attrs)) it 'all untracked_files_for_uploads records are marked as tracked' do
expect(appearance.reload.uploads.first.attributes).to include({
"path" => "uploads/-/system/appearance/logo/#{appearance.id}/rails_sample.jpg",
"uploader" => "AttachmentUploader"
}.merge(rails_sample_jpg_attrs))
expect(project1.uploads.last.attributes).to include({
"path" => @project1_markdown_upload_path,
"uploader" => "FileUploader"
}.merge(rails_sample_jpg_attrs))
end
end
it 'all UntrackedFile records are marked as tracked' do
Sidekiq::Testing.inline! do Sidekiq::Testing.inline! do
migrate! migrate!
expect(UntrackedFile.count).to eq(8) expect(untracked_files_for_uploads.count).to eq(8)
expect(UntrackedFile.count).to eq(UntrackedFile.where(tracked: true).count) expect(untracked_files_for_uploads.count).to eq(untracked_files_for_uploads.where(tracked: true).count)
end end
end end
end end
......
module TrackUntrackedUploadsHelpers module TrackUntrackedUploadsHelpers
def rails_sample_jpg_attrs def uploaded_file
@rails_sample_jpg_attrs ||= { fixture_path = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg')
"size" => File.size(rails_sample_file_path), fixture_file_upload(fixture_path)
"checksum" => Digest::SHA256.file(rails_sample_file_path).hexdigest
}
end
def rails_sample_file_path
Rails.root.join('spec', 'fixtures', 'rails_sample.jpg')
end 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