aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/test/models/attachments_test.rb
blob: 25e0352eca1a7113c24a00d6dc7e1b6d6b361d4b (plain) (blame)
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
# frozen_string_literal: true

require "test_helper"
require "database/setup"

class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
  include ActiveJob::TestHelper

  setup { @user = User.create!(name: "DHH") }

  teardown { ActiveStorage::Blob.all.each(&:purge) }

  test "attach existing blob" do
    @user.avatar.attach create_blob(filename: "funky.jpg")
    assert_equal "funky.jpg", @user.avatar.filename.to_s
  end

  test "attach existing blob from a signed ID" do
    @user.avatar.attach create_blob(filename: "funky.jpg").signed_id
    assert_equal "funky.jpg", @user.avatar.filename.to_s
  end

  test "attach new blob from a Hash" do
    @user.avatar.attach io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg"
    assert_equal "town.jpg", @user.avatar.filename.to_s
  end

  test "attach new blob from an UploadedFile" do
    file = file_fixture "racecar.jpg"
    @user.avatar.attach Rack::Test::UploadedFile.new file.to_s
    assert_equal "racecar.jpg", @user.avatar.filename.to_s
  end

  test "replace attached blob" do
    @user.avatar.attach create_blob(filename: "funky.jpg")

    perform_enqueued_jobs do
      assert_no_difference -> { ActiveStorage::Blob.count } do
        @user.avatar.attach create_blob(filename: "town.jpg")
      end
    end

    assert_equal "town.jpg", @user.avatar.filename.to_s
  end

  test "replace attached blob unsuccessfully" do
    @user.avatar.attach create_blob(filename: "funky.jpg")

    perform_enqueued_jobs do
      assert_raises do
        @user.avatar.attach nil
      end
    end

    assert_equal "funky.jpg", @user.reload.avatar.filename.to_s
    assert ActiveStorage::Blob.service.exist?(@user.avatar.key)
  end

  test "replace attached blob with itself" do
    @user.avatar.attach create_blob(filename: "funky.jpg")

    assert_no_changes -> { @user.reload.avatar.blob } do
      assert_no_changes -> { @user.reload.avatar.attachment } do
        assert_no_enqueued_jobs do
          @user.avatar.attach @user.avatar.blob
        end
      end
    end
  end

  test "replaced attached blob with itself by signed ID" do
    @user.avatar.attach create_blob(filename: "funky.jpg")

    assert_no_changes -> { @user.reload.avatar.blob } do
      assert_no_changes -> { @user.reload.avatar.attachment } do
        assert_no_enqueued_jobs do
          @user.avatar.attach @user.avatar.blob.signed_id
        end
      end
    end
  end

  test "attach blob to new record" do
    user = User.new(name: "Jason")

    assert_no_changes -> { user.new_record? } do
      assert_no_difference -> { ActiveStorage::Attachment.count } do
        user.avatar.attach create_blob(filename: "funky.jpg")
      end
    end

    assert_predicate user.avatar, :attached?
    assert_equal "funky.jpg", user.avatar.filename.to_s

    assert_difference -> { ActiveStorage::Attachment.count }, +1 do
      user.save!
    end

    assert_predicate user.reload.avatar, :attached?
    assert_equal "funky.jpg", user.avatar.filename.to_s
  end

  test "build new record with attached blob" do
    assert_no_difference -> { ActiveStorage::Attachment.count } do
      @user = User.new(name: "Jason", avatar: { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" })
    end

    assert_predicate @user, :new_record?
    assert_predicate @user.avatar, :attached?
    assert_equal "town.jpg", @user.avatar.filename.to_s

    @user.save!
    assert_predicate @user.reload.avatar, :attached?
    assert_equal "town.jpg", @user.avatar.filename.to_s
  end

  test "access underlying associations of new blob" do
    @user.avatar.attach create_blob(filename: "funky.jpg")
    assert_equal @user, @user.avatar_attachment.record
    assert_equal @user.avatar_attachment.blob, @user.avatar_blob
    assert_equal "funky.jpg", @user.avatar_attachment.blob.filename.to_s
  end

  test "identify newly-attached, directly-uploaded blob" do
    # Simulate a direct upload.
    blob = create_blob_before_direct_upload(filename: "racecar.jpg", content_type: "application/octet-stream", byte_size: 1124062, checksum: "7GjDDNEQb4mzMzsW+MS0JQ==")
    ActiveStorage::Blob.service.upload(blob.key, file_fixture("racecar.jpg").open)

    stub_request(:get, %r{localhost:3000/rails/active_storage/disk/.*}).to_return(body: file_fixture("racecar.jpg"))
    @user.avatar.attach(blob)

    assert_equal "image/jpeg", @user.avatar.reload.content_type
    assert_predicate @user.avatar, :identified?
  end

  test "identify newly-attached blob only once" do
    blob = create_file_blob
    assert_predicate blob, :identified?

    # The blob's backing file is a PNG image. Fudge its content type so we can tell if it's identified when we attach it.
    blob.update! content_type: "application/octet-stream"

    @user.avatar.attach blob
    assert_equal "application/octet-stream", blob.content_type
  end

  test "analyze newly-attached blob" do
    perform_enqueued_jobs do
      @user.avatar.attach create_file_blob
    end

    assert_equal 4104, @user.avatar.reload.metadata[:width]
    assert_equal 2736, @user.avatar.metadata[:height]
  end

  test "analyze attached blob only once" do
    blob = create_file_blob

    perform_enqueued_jobs do
      @user.avatar.attach blob
    end

    assert_predicate blob.reload, :analyzed?

    @user.avatar.detach

    assert_no_enqueued_jobs do
      @user.reload.avatar.attach blob
    end
  end

  test "preserve existing metadata when analyzing a newly-attached blob" do
    blob = create_file_blob(metadata: { foo: "bar" })

    perform_enqueued_jobs do
      @user.avatar.attach blob
    end

    assert_equal "bar", blob.reload.metadata[:foo]
  end

  test "detach blob" do
    @user.avatar.attach create_blob(filename: "funky.jpg")
    avatar_blob_id = @user.avatar.blob.id
    avatar_key = @user.avatar.key

    @user.avatar.detach
    assert_not_predicate @user.avatar, :attached?
    assert ActiveStorage::Blob.exists?(avatar_blob_id)
    assert ActiveStorage::Blob.service.exist?(avatar_key)
  end

  test "purge attached blob" do
    @user.avatar.attach create_blob(filename: "funky.jpg")
    avatar_key = @user.avatar.key

    @user.avatar.purge
    assert_not_predicate @user.avatar, :attached?
    assert_not ActiveStorage::Blob.service.exist?(avatar_key)
  end

  test "purge attached blob later when the record is destroyed" do
    @user.avatar.attach create_blob(filename: "funky.jpg")
    avatar_key = @user.avatar.key

    perform_enqueued_jobs do
      @user.destroy

      assert_nil ActiveStorage::Blob.find_by(key: avatar_key)
      assert_not ActiveStorage::Blob.service.exist?(avatar_key)
    end
  end

  test "find with attached blob" do
    records = %w[alice bob].map do |name|
      User.create!(name: name).tap do |user|
        user.avatar.attach create_blob(filename: "#{name}.jpg")
      end
    end

    users = User.where(id: records.map(&:id)).with_attached_avatar.all

    assert_equal "alice.jpg", users.first.avatar.filename.to_s
    assert_equal "bob.jpg", users.second.avatar.filename.to_s
  end


  test "attach existing blobs" do
    @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")

    assert_equal "funky.jpg", @user.highlights.first.filename.to_s
    assert_equal "wonky.jpg", @user.highlights.second.filename.to_s
  end

  test "attach new blobs" do
    @user.highlights.attach(
      { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
      { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" })

    assert_equal "town.jpg", @user.highlights.first.filename.to_s
    assert_equal "country.jpg", @user.highlights.second.filename.to_s
  end

  test "attach blobs to new record" do
    user = User.new(name: "Jason")

    assert_no_changes -> { user.new_record? } do
      assert_no_difference -> { ActiveStorage::Attachment.count } do
        user.highlights.attach(
          { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
          { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" })
      end
    end

    assert_predicate user.highlights, :attached?
    assert_equal "town.jpg", user.highlights.first.filename.to_s
    assert_equal "country.jpg", user.highlights.second.filename.to_s

    assert_difference -> { ActiveStorage::Attachment.count }, +2 do
      user.save!
    end

    assert_predicate user.reload.highlights, :attached?
    assert_equal "town.jpg", user.highlights.first.filename.to_s
    assert_equal "country.jpg", user.highlights.second.filename.to_s
  end

  test "build new record with attached blobs" do
    assert_no_difference -> { ActiveStorage::Attachment.count } do
      @user = User.new(name: "Jason", highlights: [
        { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
        { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" }])
    end

    assert_predicate @user, :new_record?
    assert_predicate @user.highlights, :attached?
    assert_equal "town.jpg", @user.highlights.first.filename.to_s
    assert_equal "country.jpg", @user.highlights.second.filename.to_s

    @user.save!
    assert_predicate @user.reload.highlights, :attached?
    assert_equal "town.jpg", @user.highlights.first.filename.to_s
    assert_equal "country.jpg", @user.highlights.second.filename.to_s
  end

  test "find attached blobs" do
    @user.highlights.attach(
      { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
      { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" })

    highlights = User.where(id: @user.id).with_attached_highlights.first.highlights

    assert_equal "town.jpg", highlights.first.filename.to_s
    assert_equal "country.jpg", highlights.second.filename.to_s
  end

  test "access underlying associations of new blobs" do
    @user.highlights.attach(
      { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
      { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" })

    assert_equal @user, @user.highlights_attachments.first.record
    assert_equal @user.highlights_attachments.collect(&:blob).sort, @user.highlights_blobs.sort
    assert_equal "town.jpg", @user.highlights_attachments.first.blob.filename.to_s
  end

  test "analyze newly-attached blobs" do
    perform_enqueued_jobs do
      @user.highlights.attach(
        create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg"),
        create_file_blob(filename: "video.mp4", content_type: "video/mp4"))
    end

    assert_equal 4104, @user.highlights.first.metadata[:width]
    assert_equal 2736, @user.highlights.first.metadata[:height]

    assert_equal 640, @user.highlights.second.metadata[:width]
    assert_equal 480, @user.highlights.second.metadata[:height]
  end

  test "analyze attached blobs only once" do
    blobs = [
      create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg"),
      create_file_blob(filename: "video.mp4", content_type: "video/mp4")
    ]

    perform_enqueued_jobs do
      @user.highlights.attach(blobs)
    end

    assert blobs.each(&:reload).all?(&:analyzed?)

    @user.highlights.attachments.destroy_all

    assert_no_enqueued_jobs do
      @user.highlights.attach(blobs)
    end
  end

  test "preserve existing metadata when analyzing newly-attached blobs" do
    blobs = [
      create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", metadata: { foo: "bar" }),
      create_file_blob(filename: "video.mp4", content_type: "video/mp4", metadata: { foo: "bar" })
    ]

    perform_enqueued_jobs do
      @user.highlights.attach(blobs)
    end

    blobs.each do |blob|
      assert_equal "bar", blob.reload.metadata[:foo]
    end
  end

  test "detach blobs" do
    @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
    highlight_blob_ids = @user.highlights.collect { |highlight| highlight.blob.id }
    highlight_keys = @user.highlights.collect(&:key)

    @user.highlights.detach
    assert_not_predicate @user.highlights, :attached?

    assert ActiveStorage::Blob.exists?(highlight_blob_ids.first)
    assert ActiveStorage::Blob.exists?(highlight_blob_ids.second)

    assert ActiveStorage::Blob.service.exist?(highlight_keys.first)
    assert ActiveStorage::Blob.service.exist?(highlight_keys.second)
  end

  test "purge attached blobs" do
    @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
    highlight_keys = @user.highlights.collect(&:key)

    @user.highlights.purge
    assert_not_predicate @user.highlights, :attached?
    assert_not ActiveStorage::Blob.service.exist?(highlight_keys.first)
    assert_not ActiveStorage::Blob.service.exist?(highlight_keys.second)
  end

  test "purge attached blobs later when the record is destroyed" do
    @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg")
    highlight_keys = @user.highlights.collect(&:key)

    perform_enqueued_jobs do
      @user.destroy

      assert_nil ActiveStorage::Blob.find_by(key: highlight_keys.first)
      assert_not ActiveStorage::Blob.service.exist?(highlight_keys.first)

      assert_nil ActiveStorage::Blob.find_by(key: highlight_keys.second)
      assert_not ActiveStorage::Blob.service.exist?(highlight_keys.second)
    end
  end
end