forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_up_uploads_spec.rb
407 lines (307 loc) · 13 KB
/
clean_up_uploads_spec.rb
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# frozen_string_literal: true
RSpec.describe Jobs::CleanUpUploads do
def fabricate_upload(attributes = {})
Fabricate(:upload, { created_at: 2.hours.ago }.merge(attributes))
end
fab! :expired_upload do
fabricate_upload
end
before do
SiteSetting.clean_up_uploads = true
SiteSetting.clean_orphan_uploads_grace_period_hours = 1
# pre-fabrication resets created_at, so re-expire the upload
expired_upload
freeze_time 2.hours.from_now
Jobs::CleanUpUploads.new.reset_last_cleanup!
end
it "only runs upload cleanup every grace period / 2 time" do
SiteSetting.clean_orphan_uploads_grace_period_hours = 48
expired = fabricate_upload(created_at: 49.hours.ago)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired.id)).to eq(false)
upload = fabricate_upload(created_at: 72.hours.ago)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: upload.id)).to eq(true)
freeze_time 25.hours.from_now
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: upload.id)).to eq(false)
end
it "deletes orphan uploads" do
expect do Jobs::CleanUpUploads.new.execute(nil) end.to change { Upload.count }.by(-1)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
end
describe "unused callbacks" do
before { Upload.add_unused_callback { |uploads| uploads.where.not(id: expired_upload.id) } }
after { Upload.reset_unused_callbacks }
it "does not delete uploads skipped by an unused callback" do
expect do Jobs::CleanUpUploads.new.execute(nil) end.not_to change { Upload.count }
expect(Upload.exists?(id: expired_upload.id)).to eq(true)
end
it "deletes other uploads not skipped by an unused callback" do
expired_upload2 = fabricate_upload
upload = fabricate_upload
UploadReference.create(target: Fabricate(:post), upload: upload)
expect do Jobs::CleanUpUploads.new.execute(nil) end.to change { Upload.count }.by(-1)
expect(Upload.exists?(id: expired_upload.id)).to eq(true)
expect(Upload.exists?(id: expired_upload2.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
end
describe "in use callbacks" do
before { Upload.add_in_use_callback { |upload| expired_upload.id == upload.id } }
after { Upload.reset_in_use_callbacks }
it "does not delete uploads that are in use by callback" do
expect do Jobs::CleanUpUploads.new.execute(nil) end.not_to change { Upload.count }
expect(Upload.exists?(id: expired_upload.id)).to eq(true)
end
it "deletes other uploads that are not in use by callback" do
expired_upload2 = fabricate_upload
upload = fabricate_upload
UploadReference.create(target: Fabricate(:post), upload: upload)
expect do Jobs::CleanUpUploads.new.execute(nil) end.to change { Upload.count }.by(-1)
expect(Upload.exists?(id: expired_upload.id)).to eq(true)
expect(Upload.exists?(id: expired_upload2.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
end
describe "when clean_up_uploads is disabled" do
before { SiteSetting.clean_up_uploads = false }
it "should still delete invalid upload records" do
upload2 = fabricate_upload(url: "", retain_hours: nil)
expect do Jobs::CleanUpUploads.new.execute(nil) end.to change { Upload.count }.by(-1)
expect(Upload.exists?(id: expired_upload.id)).to eq(true)
expect(Upload.exists?(id: upload2.id)).to eq(false)
end
end
it "does not clean up upload site settings" do
begin
original_provider = SiteSetting.provider
SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting)
SiteSetting.clean_orphan_uploads_grace_period_hours = 1
system_upload = fabricate_upload(id: -999)
logo_upload = fabricate_upload
logo_small_upload = fabricate_upload
digest_logo_upload = fabricate_upload
mobile_logo_upload = fabricate_upload
large_icon_upload = fabricate_upload
opengraph_image_upload = fabricate_upload
twitter_summary_large_image_upload = fabricate_upload
favicon_upload = fabricate_upload
apple_touch_icon_upload = fabricate_upload
SiteSetting.logo = logo_upload
SiteSetting.logo_small = logo_small_upload
SiteSetting.digest_logo = digest_logo_upload
SiteSetting.mobile_logo = mobile_logo_upload
SiteSetting.large_icon = large_icon_upload
SiteSetting.opengraph_image = opengraph_image_upload
SiteSetting.twitter_summary_large_image = twitter_summary_large_image_upload
SiteSetting.favicon = favicon_upload
SiteSetting.apple_touch_icon = apple_touch_icon_upload
Jobs::CleanUpUploads.new.execute(nil)
[
logo_upload,
logo_small_upload,
digest_logo_upload,
mobile_logo_upload,
large_icon_upload,
opengraph_image_upload,
twitter_summary_large_image_upload,
favicon_upload,
apple_touch_icon_upload,
system_upload,
].each { |record| expect(Upload.exists?(id: record.id)).to eq(true) }
fabricate_upload
SiteSetting.opengraph_image = ""
Jobs::CleanUpUploads.new.execute(nil)
ensure
SiteSetting.delete_all
SiteSetting.provider = original_provider
end
end
it "does not clean up selectable avatars" do
original_provider = SiteSetting.provider
SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting)
SiteSetting.clean_orphan_uploads_grace_period_hours = 1
avatar1_upload = fabricate_upload
avatar2_upload = fabricate_upload
SiteSetting.selectable_avatars = [avatar1_upload, avatar2_upload]
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: avatar1_upload.id)).to eq(true)
expect(Upload.exists?(id: avatar2_upload.id)).to eq(true)
ensure
SiteSetting.delete_all
SiteSetting.provider = original_provider
end
it "does not delete profile background uploads" do
profile_background_upload = fabricate_upload
UserProfile.last.upload_profile_background(profile_background_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: profile_background_upload.id)).to eq(true)
end
it "does not delete card background uploads" do
card_background_upload = fabricate_upload
UserProfile.last.upload_card_background(card_background_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: card_background_upload.id)).to eq(true)
end
it "does not delete category logo uploads" do
category_logo_upload = fabricate_upload
Fabricate(:category, uploaded_logo: category_logo_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: category_logo_upload.id)).to eq(true)
end
it "does not delete category dark logo uploads" do
category_logo_dark_upload = fabricate_upload
Fabricate(:category, uploaded_logo_dark: category_logo_dark_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: category_logo_dark_upload.id)).to eq(true)
end
it "does not delete category background uploads" do
category_background_upload = fabricate_upload
Fabricate(:category, uploaded_background: category_background_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: category_background_upload.id)).to eq(true)
end
it "does not delete category dark background uploads" do
category_background_dark_upload = fabricate_upload
Fabricate(:category, uploaded_background_dark: category_background_dark_upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: category_background_dark_upload.id)).to eq(true)
end
it "does not delete post uploads" do
upload = fabricate_upload
Fabricate(:post, uploads: [upload])
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete user uploaded avatar" do
upload = fabricate_upload
Fabricate(:user, uploaded_avatar: upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete user gravatar" do
upload = fabricate_upload
Fabricate(:user, user_avatar: Fabricate(:user_avatar, gravatar_upload: upload))
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete user custom upload" do
upload = fabricate_upload
Fabricate(:user, user_avatar: Fabricate(:user_avatar, custom_upload: upload))
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete uploads in a queued post" do
upload = fabricate_upload
upload2 = fabricate_upload
upload3 = fabricate_upload
Fabricate(
:reviewable_queued_post_topic,
payload: {
raw: "#{upload.short_url}\n#{upload2.short_url}",
},
status: :pending,
)
Fabricate(
:reviewable_queued_post_topic,
payload: {
raw: "#{upload3.short_url}",
},
status: :rejected,
)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
expect(Upload.exists?(id: upload2.id)).to eq(true)
expect(Upload.exists?(id: upload3.id)).to eq(false)
end
it "does not delete uploads in a draft" do
upload = fabricate_upload
upload2 = fabricate_upload
Draft.set(Fabricate(:user), "test", 0, "upload://#{upload.sha1}\n#{upload2.short_url}")
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
expect(Upload.exists?(id: upload2.id)).to eq(true)
end
it "does not delete uploads with an access control post ID (secure uploads)" do
upload = fabricate_upload(access_control_post_id: Fabricate(:post).id, secure: true)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete custom emojis" do
upload = fabricate_upload
CustomEmoji.create!(name: "test", upload: upload)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: upload.id)).to eq(true)
end
it "does not delete user exported csv uploads" do
csv_file = fabricate_upload
UserExport.create(file_name: "export.csv", user_id: Fabricate(:user).id, upload_id: csv_file.id)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: csv_file.id)).to eq(true)
end
it "does not delete theme setting uploads" do
theme = Fabricate(:theme)
theme_upload = fabricate_upload
ThemeSetting.create!(
theme: theme,
data_type: ThemeSetting.types[:upload],
value: theme_upload.id.to_s,
name: "my_setting_name",
)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: theme_upload.id)).to eq(true)
end
it "does not delete badges uploads" do
badge_image = fabricate_upload
badge = Fabricate(:badge, image_upload_id: badge_image.id)
Jobs::CleanUpUploads.new.execute(nil)
expect(Upload.exists?(id: expired_upload.id)).to eq(false)
expect(Upload.exists?(id: badge_image.id)).to eq(true)
end
it "deletes external upload stubs that have expired" do
external_stub1 =
Fabricate(
:external_upload_stub,
status: ExternalUploadStub.statuses[:created],
created_at: 10.minutes.ago,
)
external_stub2 =
Fabricate(
:external_upload_stub,
status: ExternalUploadStub.statuses[:created],
created_at: (ExternalUploadStub::CREATED_EXPIRY_HOURS.hours + 10.minutes).ago,
)
external_stub3 =
Fabricate(
:external_upload_stub,
status: ExternalUploadStub.statuses[:uploaded],
created_at: 10.minutes.ago,
)
external_stub4 =
Fabricate(
:external_upload_stub,
status: ExternalUploadStub.statuses[:uploaded],
created_at: (ExternalUploadStub::UPLOADED_EXPIRY_HOURS.hours + 10.minutes).ago,
)
Jobs::CleanUpUploads.new.execute(nil)
expect(ExternalUploadStub.pluck(:id)).to contain_exactly(external_stub1.id, external_stub3.id)
end
end