summaryrefslogtreecommitdiff
path: root/spec/migrations/track_untracked_uploads_spec.rb
blob: 308d8924f19c71451a2e498a89a8dc70730ae913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20171103140253_track_untracked_uploads')

describe TrackUntrackedUploads, :migration, :sidekiq do
  class UnhashedUploadFile < ActiveRecord::Base
    self.table_name = 'unhashed_upload_files'
  end

  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) }
    let(:uploads) { table(:uploads) }

    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)
      upload_result = UploadService.new(project1, uploaded_file, FileUploader).execute # Markdown upload
      @project1_markdown_upload_path = upload_result[:url].sub(%r{\A/uploads/}, '')
      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)
      upload_result = UploadService.new(project2, uploaded_file, FileUploader).execute # Markdown upload
      @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

    it 'tracks untracked uploads' do
      Sidekiq::Testing.inline! do
        expect do
          migrate!
        end.to change { uploads.count }.from(4).to(8)

        expect(user2.reload.uploads.first.attributes).to include({
          "path" => "uploads/-/system/user/avatar/#{user2.id}/rails_sample.jpg",
          "uploader" => "AvatarUploader"
        }.merge(rails_sample_jpg_attrs))
        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

    it 'ignores already-tracked uploads' do
      Sidekiq::Testing.inline! do
        migrate!

        expect(user1.reload.uploads.first.attributes).to include({
          "path" => "uploads/-/system/user/avatar/#{user1.id}/rails_sample.jpg",
          "uploader" => "AvatarUploader"
        }.merge(rails_sample_jpg_attrs))
        expect(project1.reload.uploads.first.attributes).to include({
          "path" => "uploads/-/system/project/avatar/#{project1.id}/rails_sample.jpg",
          "uploader" => "AvatarUploader"
        }.merge(rails_sample_jpg_attrs))
        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 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)
      end
    end
  end

  def rails_sample_jpg_attrs
    {
      "size"       => 35255,
      "checksum"   => 'f2d1fd9d8d8a3368d468fa067888605d74a66f41c16f55979ceaf2af77375844'
    }
  end
end