inline_forms 8.1.75 → 8.1.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -1
- data/README.rdoc +1 -1
- data/inline_forms.gemspec +0 -1
- data/lib/generators/USAGE +1 -1
- data/lib/generators/inline_forms_addto/USAGE +1 -1
- data/lib/generators/inline_forms_addto_generator.rb +30 -139
- data/lib/generators/inline_forms_attribute_list_editor.rb +211 -0
- data/lib/generators/inline_forms_attribute_overrides.rb +9 -0
- data/lib/generators/inline_forms_generator.rb +16 -35
- data/lib/inline_forms/gem_files.rb +20 -17
- data/lib/inline_forms/version.rb +1 -1
- metadata +3 -142
- data/.document +0 -5
- data/.forgejo/brakeman-engine.sh +0 -51
- data/.forgejo/brakeman.ignore +0 -211
- data/.forgejo/workflows/ci.yml +0 -74
- data/.forgejo/workflows/full-gate.yml +0 -104
- data/.gitignore +0 -39
- data/.rubocop.yml +0 -31
- data/.rubocop_todo.yml +0 -151
- data/.ruby-version +0 -1
- data/Gemfile +0 -28
- data/Rakefile +0 -103
- data/script/migrate_form_elements.rb +0 -62
- data/test/archived_form_elements_test.rb +0 -101
- data/test/attribute_list_test.rb +0 -165
- data/test/dummy/app/controllers/application_controller.rb +0 -32
- data/test/dummy/app/controllers/daily_reports_controller.rb +0 -4
- data/test/dummy/app/controllers/documents_controller.rb +0 -4
- data/test/dummy/app/controllers/dossiers_controller.rb +0 -4
- data/test/dummy/app/controllers/gizmos_controller.rb +0 -4
- data/test/dummy/app/controllers/machines_controller.rb +0 -4
- data/test/dummy/app/controllers/parts_controller.rb +0 -4
- data/test/dummy/app/controllers/stats_controller.rb +0 -10
- data/test/dummy/app/controllers/widgets_controller.rb +0 -4
- data/test/dummy/app/helpers/application_helper.rb +0 -13
- data/test/dummy/app/models/ability.rb +0 -15
- data/test/dummy/app/models/application_record.rb +0 -27
- data/test/dummy/app/models/daily_report.rb +0 -21
- data/test/dummy/app/models/document.rb +0 -33
- data/test/dummy/app/models/dossier.rb +0 -38
- data/test/dummy/app/models/gizmo.rb +0 -25
- data/test/dummy/app/models/kind.rb +0 -8
- data/test/dummy/app/models/machine.rb +0 -24
- data/test/dummy/app/models/part.rb +0 -17
- data/test/dummy/app/models/phase.rb +0 -9
- data/test/dummy/app/models/widget.rb +0 -40
- data/test/dummy/app/views/_gizmos_search.html.erb +0 -3
- data/test/dummy/app/views/stats/show.html.erb +0 -1
- data/test/dummy/config/application.rb +0 -49
- data/test/dummy/config/database.yml +0 -3
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/initializers/inline_forms.rb +0 -6
- data/test/dummy/config/initializers/paper_trail_yaml_safe_load.rb +0 -28
- data/test/dummy/config/routes.rb +0 -51
- data/test/form_element_from_callee_test.rb +0 -73
- data/test/gem_package_modes_test.rb +0 -86
- data/test/inline_edit_polymorphic_path_test.rb +0 -81
- data/test/inline_forms_addto_generator_test.rb +0 -584
- data/test/inline_forms_generator_test.rb +0 -301
- data/test/integration/csp_nonce_test.rb +0 -50
- data/test/integration/dropdown_test.rb +0 -186
- data/test/integration/field_authorization_test.rb +0 -131
- data/test/integration/file_slots_test.rb +0 -665
- data/test/integration/html_refusal_test.rb +0 -152
- data/test/integration/inline_forms_crud_test.rb +0 -165
- data/test/integration/layout_title_test.rb +0 -56
- data/test/integration/legacy_elements_test.rb +0 -99
- data/test/integration/list_params_test.rb +0 -200
- data/test/integration/locale_test.rb +0 -234
- data/test/integration/model_top_bar_test.rb +0 -85
- data/test/integration/native_inputs_test.rb +0 -138
- data/test/integration/open_after_create_test.rb +0 -85
- data/test/integration/pending_migration_gate_test.rb +0 -75
- data/test/integration/presentation_escaping_test.rb +0 -80
- data/test/integration/revert_skipped_columns_test.rb +0 -59
- data/test/integration/schema_batch_pipeline_test.rb +0 -189
- data/test/integration/schema_edit_test.rb +0 -141
- data/test/integration/schema_preview_test.rb +0 -65
- data/test/integration/turbo_menu_test.rb +0 -60
- data/test/integration_test_helper.rb +0 -203
- data/test/plain_text_configuration_test.rb +0 -90
- data/test/schema_apply_test.rb +0 -73
- data/test/schema_intent_test.rb +0 -53
- data/test/schema_label_test.rb +0 -61
- data/test/tabs_test.rb +0 -98
- data/test/test_helper.rb +0 -10
- data/test/user_model_config_test.rb +0 -44
|
@@ -1,665 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "../integration_test_helper"
|
|
4
|
-
|
|
5
|
-
# Declared file slots (InlineForms::StoredFiles) and the file trash:
|
|
6
|
-
# upload / replace / remove / restore / purge, permissions per action, the
|
|
7
|
-
# per-record panel, versions history, revert interplay, the retention sweep
|
|
8
|
-
# and the global trash page. Dummy model: Document (test/dummy).
|
|
9
|
-
class FileSlotsTest < InlineFormsIntegrationTestCase
|
|
10
|
-
PLAN = "%PDF-1.4 the plan \x00\x01".b
|
|
11
|
-
OTHER = "%PDF-1.4 another file \x02\x03".b
|
|
12
|
-
|
|
13
|
-
setup do
|
|
14
|
-
@doc = Document.create!(title: "Dossier A")
|
|
15
|
-
@frame = "document_#{@doc.id}_filename"
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def upload(bytes, name = "plan.pdf", type = "application/pdf")
|
|
19
|
-
Rack::Test::UploadedFile.new(StringIO.new(bytes), type, original_filename: name)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def stream_headers(frame)
|
|
23
|
-
frame_headers(frame).merge("Accept" => "text/vnd.turbo-stream.html")
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def put_file(doc, attribute, value)
|
|
27
|
-
frame = "document_#{doc.id}_#{attribute}"
|
|
28
|
-
patch document_path(doc, attribute: attribute, form_element: "simple_file_field", update: frame),
|
|
29
|
-
params: { attribute => value }, headers: frame_headers(frame)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def with_file(bytes = PLAN, name = "plan.pdf")
|
|
33
|
-
@doc.update!(filename: upload(bytes, name))
|
|
34
|
-
@doc.reload
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def trash
|
|
38
|
-
InlineForms::TrashedFile.trash_entries
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# ---- declaration ------------------------------------------------------
|
|
42
|
-
|
|
43
|
-
test "slot columns are skipped by PaperTrail on the model only" do
|
|
44
|
-
skip_list = Document.paper_trail_options[:skip]
|
|
45
|
-
%w[filename data content_type plan_filename plan_data plan_content_type].each do |column|
|
|
46
|
-
assert_includes skip_list, column
|
|
47
|
-
end
|
|
48
|
-
refute_includes Widget.paper_trail_options[:skip], "data",
|
|
49
|
-
"the inherited ApplicationRecord options must not be mutated"
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
test "default column names follow the attribute" do
|
|
53
|
-
assert_equal({ data: "data", content_type: "content_type" }, Document.inline_forms_file_slot(:filename))
|
|
54
|
-
assert_equal({ data: "plan_data", content_type: "plan_content_type" }, Document.inline_forms_file_slot(:plan_filename))
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
test "a hand-written setter for a declared slot raises outside production" do
|
|
58
|
-
assert_raises(InlineForms::StoredFiles::HandWrittenSetterError) do
|
|
59
|
-
Class.new(ApplicationRecord) do
|
|
60
|
-
self.table_name = "documents"
|
|
61
|
-
include InlineForms::StoredFiles
|
|
62
|
-
inline_forms_file :filename
|
|
63
|
-
def filename=(value); end
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
test "has_paper_trail after the macro still skips the slot columns" do
|
|
69
|
-
klass = Class.new(ActiveRecord::Base) do
|
|
70
|
-
self.table_name = "documents"
|
|
71
|
-
include InlineForms::StoredFiles
|
|
72
|
-
inline_forms_file :filename
|
|
73
|
-
has_paper_trail
|
|
74
|
-
end
|
|
75
|
-
assert_includes klass.paper_trail_options[:skip], "data"
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
# ---- upload / replace ---------------------------------------------------
|
|
79
|
-
|
|
80
|
-
test "uploading into an empty slot stores bytes and logs an upload event" do
|
|
81
|
-
put_file @doc, "filename", upload(PLAN, "my plan (v2).pdf")
|
|
82
|
-
|
|
83
|
-
assert_response :success
|
|
84
|
-
@doc.reload
|
|
85
|
-
assert_equal PLAN, @doc.data
|
|
86
|
-
assert_equal "my_plan__v2_.pdf", @doc.filename
|
|
87
|
-
assert_equal "application/pdf", @doc.content_type
|
|
88
|
-
event = InlineForms::TrashedFile.events.sole
|
|
89
|
-
assert_equal "filename", event.attribute_name
|
|
90
|
-
assert_nil event.data
|
|
91
|
-
assert_equal 1, event.trashed_by_id
|
|
92
|
-
assert_empty trash
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
test "replacing moves the old bytes to the trash" do
|
|
96
|
-
with_file
|
|
97
|
-
put_file @doc, "filename", upload(OTHER, "other.pdf")
|
|
98
|
-
|
|
99
|
-
assert_response :success
|
|
100
|
-
assert_equal OTHER, @doc.reload.data
|
|
101
|
-
entry = trash.sole
|
|
102
|
-
assert_equal "replaced", entry.reason
|
|
103
|
-
assert_equal PLAN, entry.file_data
|
|
104
|
-
assert_equal "plan.pdf", entry.filename
|
|
105
|
-
assert_equal Digest::SHA256.hexdigest(PLAN), entry.checksum
|
|
106
|
-
assert_in_delta 30.days.from_now, entry.purge_after, 5
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
test "re-uploading identical bytes trashes nothing" do
|
|
110
|
-
with_file
|
|
111
|
-
put_file @doc, "filename", upload(PLAN, "plan.pdf")
|
|
112
|
-
|
|
113
|
-
assert_response :success
|
|
114
|
-
assert_empty trash
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
test "replace needs :replace_file; an empty slot only needs :update" do
|
|
118
|
-
Ability.restrictions = -> { cannot :replace_file, Document }
|
|
119
|
-
put_file @doc, "plan_filename", upload(PLAN)
|
|
120
|
-
assert_response :success
|
|
121
|
-
|
|
122
|
-
with_file
|
|
123
|
-
put_file @doc, "filename", upload(OTHER, "other.pdf")
|
|
124
|
-
assert_response :forbidden
|
|
125
|
-
assert_equal PLAN, @doc.reload.data
|
|
126
|
-
assert_empty trash
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
test "edit of an occupied slot needs :replace_file too" do
|
|
130
|
-
with_file
|
|
131
|
-
Ability.restrictions = -> { cannot :replace_file, Document }
|
|
132
|
-
get edit_document_path(@doc, attribute: "filename", form_element: "simple_file_field", update: @frame),
|
|
133
|
-
headers: frame_headers(@frame)
|
|
134
|
-
assert_response :forbidden
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
test "a crafted string for a slot is refused as 'no file chosen'" do
|
|
138
|
-
with_file
|
|
139
|
-
put_file @doc, "filename", "evil.exe"
|
|
140
|
-
|
|
141
|
-
assert_response :success
|
|
142
|
-
assert_includes response.body, "no file chosen"
|
|
143
|
-
@doc.reload
|
|
144
|
-
assert_equal "plan.pdf", @doc.filename
|
|
145
|
-
assert_equal PLAN, @doc.data
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
test "an upload above max_file_size is a validation error" do
|
|
149
|
-
InlineForms.max_file_size = 10
|
|
150
|
-
put_file @doc, "filename", upload(PLAN)
|
|
151
|
-
|
|
152
|
-
assert_includes response.body, "is too large"
|
|
153
|
-
assert_nil @doc.reload.data
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
test "a failed save does not leave a trash row behind" do
|
|
157
|
-
with_file
|
|
158
|
-
Document.where(id: @doc.id).update_all(title: nil) # an unrelated invalid column
|
|
159
|
-
put_file @doc, "filename", upload(OTHER, "other.pdf")
|
|
160
|
-
|
|
161
|
-
assert_includes response.body, "can"
|
|
162
|
-
assert_equal PLAN, @doc.reload.data
|
|
163
|
-
assert_empty trash
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
# ---- show ---------------------------------------------------------------
|
|
167
|
-
|
|
168
|
-
test "a present slot renders download, replace and remove; an empty one the plus link" do
|
|
169
|
-
with_file
|
|
170
|
-
get document_path(@doc, attribute: "filename", form_element: "simple_file_field", update: @frame),
|
|
171
|
-
headers: frame_headers(@frame)
|
|
172
|
-
|
|
173
|
-
assert_response :success
|
|
174
|
-
assert_includes response.body, download_file_document_path(@doc, attribute: "filename")
|
|
175
|
-
assert_includes response.body, remove_file_document_path(@doc, attribute: "filename")
|
|
176
|
-
assert_includes response.body, "fi-pencil"
|
|
177
|
-
assert_includes response.body, "data-turbo-confirm"
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
test "remove and replace icons follow the permissions" do
|
|
181
|
-
with_file
|
|
182
|
-
Ability.restrictions = -> { cannot [ :remove_file, :replace_file ], Document }
|
|
183
|
-
get document_path(@doc, attribute: "filename", form_element: "simple_file_field", update: @frame),
|
|
184
|
-
headers: frame_headers(@frame)
|
|
185
|
-
|
|
186
|
-
refute_includes response.body, "remove_file"
|
|
187
|
-
refute_includes response.body, "fi-pencil"
|
|
188
|
-
assert_includes response.body, "download_file"
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
test "the record shows a trash panel frame and file events in versions" do
|
|
192
|
-
with_file
|
|
193
|
-
@doc.inline_forms_remove_file!(:filename, by: 1)
|
|
194
|
-
row = "document_#{@doc.id}"
|
|
195
|
-
get document_path(@doc, update: row), headers: frame_headers(row)
|
|
196
|
-
|
|
197
|
-
assert_response :success
|
|
198
|
-
assert_includes response.body, %(<turbo-frame id="document_#{@doc.id}_file_trash">)
|
|
199
|
-
assert_includes response.body, "Removed files (1)"
|
|
200
|
-
assert_includes response.body, "Versions (3)", "create version + upload event + removal event"
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
# ---- remove / restore / purge -----------------------------------------
|
|
204
|
-
|
|
205
|
-
test "remove moves the file to the trash and offers undo" do
|
|
206
|
-
with_file
|
|
207
|
-
post remove_file_document_path(@doc, attribute: "filename", update: @frame), headers: stream_headers(@frame)
|
|
208
|
-
|
|
209
|
-
assert_response :success
|
|
210
|
-
@doc.reload
|
|
211
|
-
assert_nil @doc.filename
|
|
212
|
-
assert_nil @doc.data
|
|
213
|
-
entry = trash.sole
|
|
214
|
-
assert_equal "removed", entry.reason
|
|
215
|
-
assert_equal PLAN, entry.file_data
|
|
216
|
-
assert_includes response.body, "plan.pdf removed"
|
|
217
|
-
assert_includes response.body, "plan.pdf removed"
|
|
218
|
-
assert_equal 1, response.body.scan("trash_id=#{entry.id}").size, "one undo link: the notice's, not also the plus link's"
|
|
219
|
-
assert_includes response.body, %(target="document_#{@doc.id}_file_trash")
|
|
220
|
-
assert_includes response.body, %(target="document_#{@doc.id}_versions")
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
test "removing an empty slot twice creates no empty trash row" do
|
|
224
|
-
post remove_file_document_path(@doc, attribute: "filename", update: @frame), headers: stream_headers(@frame)
|
|
225
|
-
assert_response :success
|
|
226
|
-
assert_empty trash
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
test "remove works on a record that fails unrelated validations" do
|
|
230
|
-
with_file
|
|
231
|
-
Document.where(id: @doc.id).update_all(title: nil)
|
|
232
|
-
post remove_file_document_path(@doc, attribute: "filename", update: @frame), headers: stream_headers(@frame)
|
|
233
|
-
|
|
234
|
-
assert_response :success
|
|
235
|
-
assert_nil @doc.reload.data
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
test "remove needs :remove_file" do
|
|
239
|
-
with_file
|
|
240
|
-
Ability.restrictions = -> { cannot :remove_file, Document }
|
|
241
|
-
post remove_file_document_path(@doc, attribute: "filename", update: @frame), headers: stream_headers(@frame)
|
|
242
|
-
|
|
243
|
-
assert_response :forbidden
|
|
244
|
-
assert_equal PLAN, @doc.reload.data
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
test "an undeclared attribute is refused" do
|
|
248
|
-
post remove_file_document_path(@doc, attribute: "title", update: @frame), headers: stream_headers(@frame)
|
|
249
|
-
assert_response :bad_request
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
test "restore puts the bytes back and empties the trash row" do
|
|
253
|
-
with_file
|
|
254
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
255
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
256
|
-
headers: stream_headers(@frame)
|
|
257
|
-
|
|
258
|
-
assert_response :success
|
|
259
|
-
@doc.reload
|
|
260
|
-
assert_equal PLAN, @doc.data
|
|
261
|
-
assert_equal "plan.pdf", @doc.filename
|
|
262
|
-
entry.reload
|
|
263
|
-
assert entry.restored?
|
|
264
|
-
assert_nil entry.file_data
|
|
265
|
-
end
|
|
266
|
-
|
|
267
|
-
test "restore into an occupied slot swaps: the current file goes to the trash" do
|
|
268
|
-
with_file
|
|
269
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
270
|
-
with_file(OTHER, "other.pdf")
|
|
271
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
272
|
-
headers: stream_headers(@frame)
|
|
273
|
-
|
|
274
|
-
assert_equal PLAN, @doc.reload.data
|
|
275
|
-
swapped = trash.where(reason: "replaced").sole
|
|
276
|
-
assert_equal OTHER, swapped.file_data
|
|
277
|
-
end
|
|
278
|
-
|
|
279
|
-
test "restore twice is a no-op the second time" do
|
|
280
|
-
with_file
|
|
281
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
282
|
-
2.times do
|
|
283
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
284
|
-
headers: stream_headers(@frame)
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
assert_includes response.body, "can no longer be restored"
|
|
288
|
-
assert_equal 1, trash.count
|
|
289
|
-
end
|
|
290
|
-
|
|
291
|
-
test "restore needs :restore_file, and :update for an empty slot" do
|
|
292
|
-
with_file
|
|
293
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
294
|
-
Ability.restrictions = -> { cannot :update, Document, [ :filename ] }
|
|
295
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
296
|
-
headers: stream_headers(@frame)
|
|
297
|
-
|
|
298
|
-
assert_response :forbidden
|
|
299
|
-
assert_nil @doc.reload.data
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
# The stored SHA-256 is checked before bytes go back into a slot: a
|
|
303
|
-
# tampered (or corrupted) trash row is refused, nothing is written, and the
|
|
304
|
-
# row stays in the trash.
|
|
305
|
-
test "restore refuses a trash row whose bytes do not match its checksum" do
|
|
306
|
-
with_file
|
|
307
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
308
|
-
InlineForms::TrashedFile.where(id: entry.id).update_all(data: OTHER)
|
|
309
|
-
|
|
310
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
311
|
-
headers: stream_headers(@frame)
|
|
312
|
-
|
|
313
|
-
assert_response :success
|
|
314
|
-
assert_includes response.body, "can no longer be restored"
|
|
315
|
-
@doc.reload
|
|
316
|
-
assert_nil @doc.filename
|
|
317
|
-
assert_nil @doc.data
|
|
318
|
-
assert entry.reload.trashed?, "not marked restored"
|
|
319
|
-
assert_equal OTHER, entry.file_data, "the row is left as it is, for a look"
|
|
320
|
-
end
|
|
321
|
-
|
|
322
|
-
test "restore! raises and logs on a checksum mismatch, writing nothing" do
|
|
323
|
-
with_file
|
|
324
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
325
|
-
InlineForms::TrashedFile.where(id: entry.id).update_all(checksum: Digest::SHA256.hexdigest("something else"))
|
|
326
|
-
log = StringIO.new
|
|
327
|
-
original_logger = Rails.logger
|
|
328
|
-
Rails.logger = ActiveSupport::Logger.new(log)
|
|
329
|
-
|
|
330
|
-
assert_raises(InlineForms::TrashedFile::NotRestorable) { entry.reload.restore!(by: 1) }
|
|
331
|
-
|
|
332
|
-
assert_includes log.string, "checksum mismatch entry=#{entry.id}"
|
|
333
|
-
assert_nil @doc.reload.data
|
|
334
|
-
assert entry.reload.trashed?
|
|
335
|
-
assert_equal 0, trash.where(reason: "replaced").count
|
|
336
|
-
ensure
|
|
337
|
-
Rails.logger = original_logger
|
|
338
|
-
end
|
|
339
|
-
|
|
340
|
-
test "a trash entry of another record is not found" do
|
|
341
|
-
other = Document.create!(title: "Dossier B", filename: upload(PLAN))
|
|
342
|
-
entry = other.inline_forms_remove_file!(:filename, by: 1)
|
|
343
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
344
|
-
headers: stream_headers(@frame)
|
|
345
|
-
|
|
346
|
-
assert_response :not_found
|
|
347
|
-
assert entry.reload.trashed?
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
test "purge deletes the bytes and keeps the tombstone" do
|
|
351
|
-
with_file
|
|
352
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
353
|
-
post purge_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
354
|
-
headers: stream_headers(@frame)
|
|
355
|
-
|
|
356
|
-
assert_response :success
|
|
357
|
-
entry.reload
|
|
358
|
-
assert entry.purged?
|
|
359
|
-
assert_nil entry.file_data
|
|
360
|
-
assert_equal "plan.pdf", entry.filename, "no redaction by default"
|
|
361
|
-
assert_equal Digest::SHA256.hexdigest(PLAN), entry.checksum
|
|
362
|
-
end
|
|
363
|
-
|
|
364
|
-
test "purge with redaction blanks the filename" do
|
|
365
|
-
InlineForms.file_trash_redact_on_purge = true
|
|
366
|
-
with_file
|
|
367
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
368
|
-
assert entry.purge!(by: 1)
|
|
369
|
-
|
|
370
|
-
assert_nil entry.reload.filename
|
|
371
|
-
assert entry.redacted?
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
test "purge needs :purge_file" do
|
|
375
|
-
with_file
|
|
376
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
377
|
-
Ability.restrictions = -> { cannot :purge_file, Document }
|
|
378
|
-
post purge_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame),
|
|
379
|
-
headers: stream_headers(@frame)
|
|
380
|
-
|
|
381
|
-
assert_response :forbidden
|
|
382
|
-
assert entry.reload.trashed?
|
|
383
|
-
end
|
|
384
|
-
|
|
385
|
-
test "a purged entry can be neither restored nor purged again" do
|
|
386
|
-
with_file
|
|
387
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
388
|
-
assert entry.purge!(by: 1)
|
|
389
|
-
|
|
390
|
-
refute entry.purge!(by: 1)
|
|
391
|
-
assert_raises(InlineForms::TrashedFile::NotRestorable) { entry.restore!(by: 1) }
|
|
392
|
-
end
|
|
393
|
-
|
|
394
|
-
# ---- downloads --------------------------------------------------------
|
|
395
|
-
|
|
396
|
-
test "download_file sends the bytes as an attachment, never cached" do
|
|
397
|
-
with_file
|
|
398
|
-
get download_file_document_path(@doc, attribute: "filename")
|
|
399
|
-
|
|
400
|
-
assert_response :success
|
|
401
|
-
assert_equal PLAN, response.body.b
|
|
402
|
-
assert_match(/attachment/, response.headers["Content-Disposition"])
|
|
403
|
-
assert_equal "no-store", response.headers["Cache-Control"]
|
|
404
|
-
end
|
|
405
|
-
|
|
406
|
-
test "download_file of an empty slot is a 404, not an empty file" do
|
|
407
|
-
get download_file_document_path(@doc, attribute: "filename")
|
|
408
|
-
assert_response :not_found
|
|
409
|
-
end
|
|
410
|
-
|
|
411
|
-
test "download_file needs :read on the attribute" do
|
|
412
|
-
with_file
|
|
413
|
-
Ability.restrictions = -> { cannot :read, Document, [ :filename ] }
|
|
414
|
-
get download_file_document_path(@doc, attribute: "filename")
|
|
415
|
-
assert_response :forbidden
|
|
416
|
-
end
|
|
417
|
-
|
|
418
|
-
test "a trashed file downloads as octet-stream attachment until purged" do
|
|
419
|
-
with_file(PLAN, "page.html")
|
|
420
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
421
|
-
get trashed_file_document_path(@doc, attribute: "filename", trash_id: entry.id)
|
|
422
|
-
|
|
423
|
-
assert_response :success
|
|
424
|
-
assert_equal PLAN, response.body.b
|
|
425
|
-
assert_equal "application/octet-stream", response.media_type
|
|
426
|
-
assert_match(/attachment/, response.headers["Content-Disposition"])
|
|
427
|
-
|
|
428
|
-
entry.purge!(by: 1)
|
|
429
|
-
get trashed_file_document_path(@doc, attribute: "filename", trash_id: entry.id)
|
|
430
|
-
assert_response :not_found
|
|
431
|
-
end
|
|
432
|
-
|
|
433
|
-
test "downloading a trashed file needs :download_trashed_file" do
|
|
434
|
-
with_file
|
|
435
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
436
|
-
Ability.restrictions = -> { cannot :download_trashed_file, Document }
|
|
437
|
-
get trashed_file_document_path(@doc, attribute: "filename", trash_id: entry.id)
|
|
438
|
-
assert_response :forbidden
|
|
439
|
-
end
|
|
440
|
-
|
|
441
|
-
# ---- panel --------------------------------------------------------------
|
|
442
|
-
|
|
443
|
-
test "the open panel lists entries with actions; closed shows the count" do
|
|
444
|
-
with_file
|
|
445
|
-
@doc.inline_forms_remove_file!(:filename, by: 1)
|
|
446
|
-
panel = "document_#{@doc.id}_file_trash"
|
|
447
|
-
get file_trash_document_path(@doc, update: panel, slots: "filename,plan_filename"), headers: frame_headers(panel)
|
|
448
|
-
|
|
449
|
-
assert_response :success
|
|
450
|
-
assert_includes response.body, "plan.pdf"
|
|
451
|
-
assert_includes response.body, "delete permanently"
|
|
452
|
-
assert_includes response.body, "restorable until"
|
|
453
|
-
end
|
|
454
|
-
|
|
455
|
-
# FileSlotsConcern has no per-tab attribute list, so the field it
|
|
456
|
-
# re-renders used to build its links' slots= from the model's full list:
|
|
457
|
-
# after a restore on one tab, the next action's panel (and the versions
|
|
458
|
-
# panel) listed the other tabs' slots. The page's own slots param now
|
|
459
|
-
# carries through.
|
|
460
|
-
test "a file action re-renders with the page's slots only" do
|
|
461
|
-
with_file
|
|
462
|
-
@doc.update!(plan_filename: upload(OTHER, "other-tab.pdf"))
|
|
463
|
-
@doc.inline_forms_remove_file!(:plan_filename, by: 1)
|
|
464
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
465
|
-
|
|
466
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame,
|
|
467
|
-
slots: "filename", panel: "open"),
|
|
468
|
-
headers: stream_headers(@frame)
|
|
469
|
-
|
|
470
|
-
assert_response :success
|
|
471
|
-
assert_equal PLAN, @doc.reload.data
|
|
472
|
-
remove_link = response.body[%r{href="([^"]*/remove_file\?[^"]*)"}, 1]
|
|
473
|
-
assert remove_link, "the restored field offers remove"
|
|
474
|
-
assert_equal "filename", Rack::Utils.parse_query(URI.parse(CGI.unescapeHTML(remove_link)).query)["slots"]
|
|
475
|
-
refute_includes response.body, "other-tab.pdf", "neither the trash panel nor the versions panel lists the other slot"
|
|
476
|
-
end
|
|
477
|
-
|
|
478
|
-
test "a file action's slots param is reduced to declared, listed slots" do
|
|
479
|
-
with_file
|
|
480
|
-
@doc.update!(plan_filename: upload(OTHER, "other-tab.pdf"))
|
|
481
|
-
@doc.inline_forms_remove_file!(:plan_filename, by: 1)
|
|
482
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
483
|
-
|
|
484
|
-
post restore_file_document_path(@doc, attribute: "filename", trash_id: entry.id, update: @frame,
|
|
485
|
-
slots: "filename,title,data", panel: "open"),
|
|
486
|
-
headers: stream_headers(@frame)
|
|
487
|
-
|
|
488
|
-
remove_link = response.body[%r{href="([^"]*/remove_file\?[^"]*)"}, 1]
|
|
489
|
-
assert_equal "filename", Rack::Utils.parse_query(URI.parse(CGI.unescapeHTML(remove_link)).query)["slots"]
|
|
490
|
-
end
|
|
491
|
-
|
|
492
|
-
test "the panel needs :read_file_trash" do
|
|
493
|
-
Ability.restrictions = -> { cannot :read_file_trash, Document }
|
|
494
|
-
panel = "document_#{@doc.id}_file_trash"
|
|
495
|
-
get file_trash_document_path(@doc, update: panel), headers: frame_headers(panel)
|
|
496
|
-
assert_response :forbidden
|
|
497
|
-
end
|
|
498
|
-
|
|
499
|
-
# ---- destroy / revert --------------------------------------------------
|
|
500
|
-
|
|
501
|
-
test "destroying a record trashes its files; undoing the destroy brings them back" do
|
|
502
|
-
Document.require_filename = true
|
|
503
|
-
with_file
|
|
504
|
-
@doc.update!(plan_filename: upload(OTHER, "plan2.pdf"))
|
|
505
|
-
row = "document_#{@doc.id}"
|
|
506
|
-
delete document_path(@doc, update: row), headers: frame_headers(row)
|
|
507
|
-
assert_equal 2, trash.where(reason: "record_destroyed").count
|
|
508
|
-
|
|
509
|
-
version = PaperTrail::Version.where(item_type: "Document", item_id: @doc.id, event: "destroy").last
|
|
510
|
-
post revert_document_path(version.id, update: "documents_list"), headers: stream_headers("documents_list")
|
|
511
|
-
|
|
512
|
-
assert_response :success
|
|
513
|
-
restored = Document.find(@doc.id)
|
|
514
|
-
assert_equal PLAN, restored.data
|
|
515
|
-
assert_equal "plan.pdf", restored.filename
|
|
516
|
-
assert_equal OTHER, restored.plan_data
|
|
517
|
-
assert trash.where(reason: "record_destroyed").all?(&:restored?)
|
|
518
|
-
end
|
|
519
|
-
|
|
520
|
-
test "undoing a destroy after a purge brings the record back without that file" do
|
|
521
|
-
with_file
|
|
522
|
-
row = "document_#{@doc.id}"
|
|
523
|
-
delete document_path(@doc, update: row), headers: frame_headers(row)
|
|
524
|
-
trash.sole.purge!(by: 1)
|
|
525
|
-
|
|
526
|
-
version = PaperTrail::Version.where(item_type: "Document", item_id: @doc.id, event: "destroy").last
|
|
527
|
-
post revert_document_path(version.id, update: "documents_list"), headers: stream_headers("documents_list")
|
|
528
|
-
|
|
529
|
-
restored = Document.find(@doc.id)
|
|
530
|
-
assert_nil restored.filename
|
|
531
|
-
assert_nil restored.data
|
|
532
|
-
end
|
|
533
|
-
|
|
534
|
-
test "undoing a destroy refuses a tampered trash row: nothing is restored" do
|
|
535
|
-
with_file
|
|
536
|
-
row = "document_#{@doc.id}"
|
|
537
|
-
delete document_path(@doc, update: row), headers: frame_headers(row)
|
|
538
|
-
entry = trash.where(reason: "record_destroyed").sole
|
|
539
|
-
InlineForms::TrashedFile.where(id: entry.id).update_all(data: OTHER)
|
|
540
|
-
|
|
541
|
-
version = PaperTrail::Version.where(item_type: "Document", item_id: @doc.id, event: "destroy").last
|
|
542
|
-
post revert_document_path(version.id, update: "documents_list"), headers: stream_headers("documents_list")
|
|
543
|
-
|
|
544
|
-
assert_response :unprocessable_entity
|
|
545
|
-
refute Document.exists?(@doc.id), "the revert rolled back"
|
|
546
|
-
assert entry.reload.trashed?
|
|
547
|
-
assert_equal OTHER, entry.file_data
|
|
548
|
-
end
|
|
549
|
-
|
|
550
|
-
test "reverting an update never touches a slot, even from an old version carrying a filename" do
|
|
551
|
-
with_file
|
|
552
|
-
@doc.update!(title: "Renamed")
|
|
553
|
-
version = PaperTrail::Version.where(item_type: "Document", item_id: @doc.id, event: "update").last
|
|
554
|
-
# A pre-adoption version row still has the slot columns in it.
|
|
555
|
-
version.update_columns(object: version.object.sub("title: Dossier A", "title: Dossier A\nfilename: old.pdf"))
|
|
556
|
-
|
|
557
|
-
post revert_document_path(version.id, update: "document_#{@doc.id}"), headers: stream_headers("document_#{@doc.id}")
|
|
558
|
-
|
|
559
|
-
assert_response :success
|
|
560
|
-
@doc.reload
|
|
561
|
-
assert_equal "Dossier A", @doc.title
|
|
562
|
-
assert_equal "plan.pdf", @doc.filename
|
|
563
|
-
assert_equal PLAN, @doc.data
|
|
564
|
-
end
|
|
565
|
-
|
|
566
|
-
# ---- retention ----------------------------------------------------------
|
|
567
|
-
|
|
568
|
-
test "an expired entry is no longer restorable and the sweep purges it" do
|
|
569
|
-
with_file
|
|
570
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
571
|
-
entry.update_columns(purge_after: 1.minute.ago)
|
|
572
|
-
|
|
573
|
-
assert_raises(InlineForms::TrashedFile::NotRestorable) { entry.reload.restore!(by: 1) }
|
|
574
|
-
assert_equal 1, InlineForms::FileTrashSweep.run!
|
|
575
|
-
assert entry.reload.purged?
|
|
576
|
-
assert_nil entry.purged_by_id
|
|
577
|
-
refute InlineForms::FileTrashSweep.overdue?
|
|
578
|
-
end
|
|
579
|
-
|
|
580
|
-
test "retention nil never expires" do
|
|
581
|
-
InlineForms.file_trash_retention = nil
|
|
582
|
-
with_file
|
|
583
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
584
|
-
assert_nil entry.purge_after
|
|
585
|
-
assert_equal 0, InlineForms::TrashedFile.purge_expired!
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
test "the sweep is overdue when it never ran" do
|
|
589
|
-
assert InlineForms::FileTrashSweep.overdue?
|
|
590
|
-
end
|
|
591
|
-
|
|
592
|
-
# ---- global page ----------------------------------------------------------
|
|
593
|
-
|
|
594
|
-
test "the global page lists trashed files and bulk-purges selected ones" do
|
|
595
|
-
with_file
|
|
596
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
597
|
-
get inline_forms_file_trash_path
|
|
598
|
-
|
|
599
|
-
assert_response :success
|
|
600
|
-
assert_includes response.body, "plan.pdf"
|
|
601
|
-
|
|
602
|
-
post inline_forms_file_trash_purge_path, params: { ids: [ entry.id ] }
|
|
603
|
-
assert_response :redirect
|
|
604
|
-
assert entry.reload.purged?
|
|
605
|
-
end
|
|
606
|
-
|
|
607
|
-
test "the global page offers bulk purge only when a listed row is purgeable" do
|
|
608
|
-
with_file
|
|
609
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
610
|
-
get inline_forms_file_trash_path(state: "trashed")
|
|
611
|
-
assert_includes response.body, %(id="trash_entry_#{entry.id}")
|
|
612
|
-
assert_includes response.body, I18n.t("inline_forms.files.global.purge_selected")
|
|
613
|
-
|
|
614
|
-
entry.restore!(by: 1)
|
|
615
|
-
get inline_forms_file_trash_path(state: "restored")
|
|
616
|
-
assert_includes response.body, "plan.pdf"
|
|
617
|
-
refute_includes response.body, %(name="ids[]")
|
|
618
|
-
refute_includes response.body, I18n.t("inline_forms.files.global.purge_selected")
|
|
619
|
-
end
|
|
620
|
-
|
|
621
|
-
test "the global page restores and downloads" do
|
|
622
|
-
with_file
|
|
623
|
-
entry = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
624
|
-
get inline_forms_file_trash_download_path(entry)
|
|
625
|
-
assert_equal PLAN, response.body.b
|
|
626
|
-
|
|
627
|
-
post inline_forms_file_trash_restore_path(entry)
|
|
628
|
-
assert_response :redirect
|
|
629
|
-
assert_equal PLAN, @doc.reload.data
|
|
630
|
-
end
|
|
631
|
-
|
|
632
|
-
test "the global page purges files of deleted records" do
|
|
633
|
-
with_file
|
|
634
|
-
row = "document_#{@doc.id}"
|
|
635
|
-
delete document_path(@doc, update: row), headers: frame_headers(row)
|
|
636
|
-
entry = trash.sole
|
|
637
|
-
|
|
638
|
-
post inline_forms_file_trash_purge_path, params: { ids: [ entry.id ] }
|
|
639
|
-
assert entry.reload.purged?
|
|
640
|
-
end
|
|
641
|
-
|
|
642
|
-
test "the global page needs :read_file_trash on :all" do
|
|
643
|
-
Ability.restrictions = lambda do
|
|
644
|
-
cannot :manage, :all
|
|
645
|
-
can :read, :all
|
|
646
|
-
can :read_file_trash, Document
|
|
647
|
-
end
|
|
648
|
-
get inline_forms_file_trash_path
|
|
649
|
-
assert_response :forbidden
|
|
650
|
-
end
|
|
651
|
-
|
|
652
|
-
test "a bulk purge is all or nothing when one row is not allowed" do
|
|
653
|
-
with_file
|
|
654
|
-
mine = @doc.inline_forms_remove_file!(:filename, by: 1)
|
|
655
|
-
@doc.update!(plan_filename: upload(OTHER, "x.pdf"))
|
|
656
|
-
theirs = @doc.inline_forms_remove_file!(:plan_filename, by: 1)
|
|
657
|
-
Ability.restrictions = -> { cannot :purge_file, Document, [ :plan_filename ] }
|
|
658
|
-
|
|
659
|
-
post inline_forms_file_trash_purge_path, params: { ids: [ mine.id, theirs.id ] }
|
|
660
|
-
|
|
661
|
-
assert_response :forbidden
|
|
662
|
-
assert mine.reload.trashed?
|
|
663
|
-
assert theirs.reload.trashed?
|
|
664
|
-
end
|
|
665
|
-
end
|