-
Notifications
You must be signed in to change notification settings - Fork 21.3k
/
attachment_test.rb
171 lines (128 loc) · 5.25 KB
/
attachment_test.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
# frozen_string_literal: true
require "test_helper"
require "database/setup"
require "active_support/testing/method_call_assertions"
class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
setup do
@user = User.create!(name: "Josh")
end
teardown { ActiveStorage::Blob.all.each(&:delete) }
test "analyzing a directly-uploaded blob after attaching it" do
blob = directly_upload_file_blob(filename: "racecar.jpg")
assert_not blob.analyzed?
perform_enqueued_jobs do
@user.highlights.attach(blob)
end
assert blob.reload.analyzed?
assert_equal 4104, blob.metadata[:width]
assert_equal 2736, blob.metadata[:height]
end
test "attaching a un-analyzable blob" do
blob = create_blob(filename: "blank.txt")
assert_not_predicate blob, :analyzed?
assert_no_enqueued_jobs do
@user.highlights.attach(blob)
end
assert_predicate blob.reload, :analyzed?
end
test "mirroring a directly-uploaded blob after attaching it" do
with_service("mirror") do
blob = directly_upload_file_blob
assert_not ActiveStorage::Blob.service.mirrors.second.exist?(blob.key)
perform_enqueued_jobs do
@user.highlights.attach(blob)
end
assert ActiveStorage::Blob.service.mirrors.second.exist?(blob.key)
end
end
test "directly-uploaded blob identification for one attached occurs before validation" do
blob = directly_upload_file_blob(filename: "racecar.jpg", content_type: "application/octet-stream")
assert_blob_identified_before_owner_validated(@user, blob, "image/jpeg") do
@user.avatar.attach(blob)
end
end
test "directly-uploaded blob identification for many attached occurs before validation" do
blob = directly_upload_file_blob(filename: "racecar.jpg", content_type: "application/octet-stream")
assert_blob_identified_before_owner_validated(@user, blob, "image/jpeg") do
@user.highlights.attach(blob)
end
end
test "directly-uploaded blob identification for one attached occurs outside transaction" do
blob = directly_upload_file_blob(filename: "racecar.jpg")
assert_blob_identified_outside_transaction(blob) do
@user.avatar.attach(blob)
end
end
test "directly-uploaded blob identification for many attached occurs outside transaction" do
blob = directly_upload_file_blob(filename: "racecar.jpg")
assert_blob_identified_outside_transaction(blob) do
@user.highlights.attach(blob)
end
end
test "getting a signed blob ID from an attachment" do
blob = create_blob
@user.avatar.attach(blob)
signed_id = @user.avatar.signed_id
assert_equal blob, ActiveStorage::Blob.find_signed!(signed_id)
assert_equal blob, ActiveStorage::Blob.find_signed(signed_id)
end
test "getting a signed blob ID from an attachment with a custom purpose" do
blob = create_blob
@user.avatar.attach(blob)
signed_id = @user.avatar.signed_id(purpose: :custom_purpose)
assert_equal blob, ActiveStorage::Blob.find_signed!(signed_id, purpose: :custom_purpose)
end
test "getting a signed blob ID from an attachment with a expires_in" do
blob = create_blob
@user.avatar.attach(blob)
signed_id = @user.avatar.signed_id(expires_in: 1.minute)
assert_equal blob, ActiveStorage::Blob.find_signed!(signed_id)
end
test "fail to find blob within expiration date" do
blob = create_blob
@user.avatar.attach(blob)
signed_id = @user.avatar.signed_id(expires_in: 1.minute)
travel 2.minutes
assert_nil ActiveStorage::Blob.find_signed(signed_id)
end
test "signed blob ID backwards compatibility" do
blob = create_blob
@user.avatar.attach(blob)
signed_id_generated_old_way = ActiveStorage.verifier.generate(@user.avatar.blob.id, purpose: :blob_id)
assert_equal blob, ActiveStorage::Blob.find_signed!(signed_id_generated_old_way)
end
test "attaching with strict_loading and getting a signed blob ID from an attachment" do
blob = create_blob
@user.strict_loading!(true)
@user.avatar.attach(blob)
signed_id = @user.avatar.signed_id
assert_equal blob, ActiveStorage::Blob.find_signed(signed_id)
end
test "can destroy attachment without existing relation" do
blob = create_blob
@user.highlights.attach(blob)
attachment = @user.highlights.find_by(blob_id: blob.id)
attachment.update_attribute(:name, "old_highlights")
assert_nothing_raised { attachment.destroy }
end
private
def assert_blob_identified_before_owner_validated(owner, blob, content_type)
validated_content_type = nil
owner.class.validate do
validated_content_type ||= blob.content_type
end
yield
assert_equal content_type, validated_content_type
assert_equal content_type, blob.reload.content_type
end
def assert_blob_identified_outside_transaction(blob, &block)
baseline_transaction_depth = ActiveRecord::Base.connection.open_transactions
max_transaction_depth = -1
track_transaction_depth = ->(*) do
max_transaction_depth = [ActiveRecord::Base.connection.open_transactions, max_transaction_depth].max
end
blob.stub(:identify_without_saving, track_transaction_depth, &block)
assert_equal 0, (max_transaction_depth - baseline_transaction_depth)
end
end