cybertrain 0.3.1 → 0.4.1

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.
@@ -5,7 +5,7 @@
5
5
  # Controller#send_blob); this writes the rest:
6
6
  #
7
7
  # db/migrate/*_add_cover_to_posts.rb five metadata columns
8
- # test/attachment_post_cover.rb, test/support/app_test.rb
8
+ # test/integration/post_cover_attachment_test.rb the uploads, end to end
9
9
  #
10
10
  # and one-time edits, each guarded by a marker so that running the
11
11
  # generator again changes nothing:
@@ -128,20 +128,44 @@ module Cybertrain
128
128
  # the columns and views itself. Appends the paths it created.
129
129
  def self.install_code(root, spec, created)
130
130
  add_model_block(root, spec)
131
- add_action(root, spec)
131
+ added = add_action(root, spec)
132
132
  add_public_download(root, spec)
133
+ warn_show_body(root, spec) if added
133
134
  add_assignments(root, spec)
134
135
  add_route(root, spec)
135
136
  add_gitignore(root)
136
- record(created, root, "test/support/app_test.rb", Authentication.app_test_support)
137
+ record_bytes(created, root, fixture_upload_file(spec), TEST_PNG_FILE)
137
138
  record(created, root, test_file(spec), attachment_test(root, spec))
138
139
  nil
139
140
  end
140
141
 
142
+ # Writes `bytes` (binary) at root/path unless the file is there: a
143
+ # file with the same bytes is "identical", other bytes are kept
144
+ # ("exist"), as Templates.write does for text. Appends a created path.
145
+ def self.record_bytes(created, root, path, bytes)
146
+ full = File.join(root, path)
147
+ status = "create"
148
+ if File.exist?(full)
149
+ status = File.binread(full) == bytes ? "identical" : "exist"
150
+ else
151
+ Templates.mkdir_p(File.dirname(full))
152
+ f = File.open(full, File::WRONLY | File::CREAT | File::TRUNC | File::BINARY)
153
+ f.write(bytes)
154
+ f.close
155
+ created << path
156
+ end
157
+ puts "#{status} #{path}"
158
+ nil
159
+ end
160
+
141
161
  # Check before writing anything, including when invoked by scaffold.
142
162
  # Authentication alone cannot replace a show-only authorization check.
143
163
  # Only the shared record callback is extended automatically; any other
144
- # policy must explicitly cover both show and the download already.
164
+ # policy a callback declares must explicitly cover both show and the
165
+ # download already. Code inside def show is not refused: it cannot be
166
+ # read for what it checks, so the download is generated, never made
167
+ # public with show (add_public_download), and warned about
168
+ # (warn_show_body).
145
169
  def self.check_download_policy!(root, spec, source)
146
170
  coverage = set_record_coverage(source, spec, spec.field)
147
171
  new_action = line_start(source, "def #{spec.field}", "exact") < 0
@@ -149,7 +173,7 @@ module Cybertrain
149
173
  safe = line_start(source, controller_line(spec), "exact") >= 0 &&
150
174
  set_record_coverage(source, spec, "show") == "yes" &&
151
175
  set_record_coverage(source, spec, spec.field) == "yes" &&
152
- !show_checks_more?(source, spec)
176
+ !callback_checks_more?(source, spec)
153
177
 
154
178
  # Read the actual ancestry rather than inferring policy from the child.
155
179
  parent = "ApplicationController"
@@ -161,9 +185,16 @@ module Cybertrain
161
185
  else
162
186
  seen << parent
163
187
  inherited = File.read(path)
188
+ # The authentication generator's include (the line
189
+ # Authentication.include_line? recognises, as that generator
190
+ # does): its callbacks are read from the concern itself.
191
+ if Authentication.included?(inherited)
192
+ safe = !authentication_concern_checks_more?(root, spec)
193
+ inherited = without_authentication_include(inherited)
194
+ end
164
195
  head = "class #{parent} < "
165
196
  at = line_start(inherited, head, "start")
166
- safe = at >= 0 && !callback_checks_more?(inherited, spec)
197
+ safe = safe && at >= 0 && !callback_checks_more?(inherited, spec)
167
198
  parent = inherited[at, line_end(inherited, at) - at].to_s.strip.delete_prefix(head) if safe
168
199
  end
169
200
  end
@@ -192,7 +223,13 @@ module Cybertrain
192
223
  end
193
224
 
194
225
  def self.test_file(spec)
195
- "test/attachment_#{spec.singular}_#{spec.field}.rb"
226
+ "test/integration/#{spec.singular}_#{spec.field}_attachment_test.rb"
227
+ end
228
+
229
+ # The image the test uploads with fixture_file_upload, where Rails
230
+ # keeps such files: test/fixtures/files/cover.png.
231
+ def self.fixture_upload_file(spec)
232
+ "test/fixtures/files/#{spec.field}.png"
196
233
  end
197
234
 
198
235
  def self.route_line(spec)
@@ -369,8 +406,12 @@ module Cybertrain
369
406
  puts "skip #{path} (:show is public, but def #{spec.field} does not load the #{spec.singular} through before_action :set_#{spec.singular} as show does): add :#{spec.field} to allow_unauthenticated_access yourself if visitors may download it"
370
407
  return false
371
408
  end
372
- if show_checks_more?(source, spec)
373
- puts "skip #{path} (:show is public, but show may check more than set_#{spec.singular}: a before_action of its own, code in def show, or a line this cannot read): add :#{spec.field} to allow_unauthenticated_access yourself once def #{spec.field} checks the same"
409
+ if callback_checks_more?(source, spec)
410
+ puts "skip #{path} (:show is public, but show may check more than set_#{spec.singular}: a before_action of its own, or a line this cannot read): add :#{spec.field} to allow_unauthenticated_access yourself once def #{spec.field} checks the same"
411
+ return false
412
+ end
413
+ if show_has_body?(source)
414
+ puts "skip #{path} (:show is public, but def show has code of its own, which def #{spec.field} does not run): the download needs a sign-in; add :#{spec.field} to allow_unauthenticated_access yourself once def #{spec.field} checks whatever that code checks"
374
415
  return false
375
416
  end
376
417
 
@@ -379,6 +420,24 @@ module Cybertrain
379
420
  true
380
421
  end
381
422
 
423
+ # source without its `include Authentication` lines.
424
+ def self.without_authentication_include(source)
425
+ kept = +""
426
+ source.each_line { |line| kept << line unless Authentication.include_line?(line) }
427
+ kept
428
+ end
429
+
430
+ # Whether app/controllers/concerns/authentication.rb, which
431
+ # `include Authentication` brings in, checks more for show than for the
432
+ # download (callback_checks_more?, its `extend ActiveSupport::Concern`
433
+ # aside); true when the file is not there to read.
434
+ def self.authentication_concern_checks_more?(root, spec)
435
+ path = File.join(root, "app/controllers/concerns/authentication.rb")
436
+ return true unless File.file?(path)
437
+
438
+ callback_checks_more?(File.read(path).sub("\n extend ActiveSupport::Concern\n", "\n"), spec)
439
+ end
440
+
382
441
  # Unshared callbacks, skips or mixins cannot establish a shared policy.
383
442
  # Restrict callback declarations to literal, single-line symbol lists;
384
443
  # conditional/dynamic declarations and blocks need application review.
@@ -410,11 +469,11 @@ module Cybertrain
410
469
  false
411
470
  end
412
471
 
413
- # The scaffold's show is empty. Inline checks cannot safely be copied
414
- # into a different action; the application must share an explicit policy.
415
- def self.show_checks_more?(source, spec)
416
- return true if callback_checks_more?(source, spec)
417
-
472
+ # Whether def show is anything but the scaffold's empty action. Code
473
+ # there cannot be read for what it checks (`@comment = Comment.new`
474
+ # and `head 404 if @post.draft?` are both a line), so any line counts,
475
+ # and so does a show this cannot find (`def show; end`, no show).
476
+ def self.show_has_body?(source)
418
477
  at = line_start(source, "def show", "exact")
419
478
  return true if at < 0
420
479
 
@@ -434,6 +493,17 @@ module Cybertrain
434
493
  body
435
494
  end
436
495
 
496
+ # Said once, when the download action is added: the action runs what
497
+ # the callbacks run and nothing of def show. Without authentication
498
+ # this line is all that stands between a check in show and the file.
499
+ def self.warn_show_body(root, spec)
500
+ path = controller_file(spec)
501
+ return nil unless show_has_body?(File.read(File.join(root, path)))
502
+
503
+ puts "warning: #{path}: def show has code of its own, which def #{spec.field} does not run; if that code limits who may see #{spec.plural}, check the same in def #{spec.field}"
504
+ nil
505
+ end
506
+
437
507
  # Who may download: "public" when allow_unauthenticated_access already
438
508
  # covers the action (no list, an only: list with it, an except: list
439
509
  # without it), "add" when its one-line only: list makes show public but
@@ -992,30 +1062,41 @@ module Cybertrain
992
1062
 
993
1063
  # ---- the generated test ---------------------------------------------
994
1064
 
995
- # The app test for this attachment: written as test/attachment_post_cover.rb.
996
- # It signs in first when the app has authentication (app/models/user.rb;
997
- # a later `generate authentication` adds the sign-in to it instead),
998
- # fills the form fields the model validates for presence, and checks
999
- # the security-relevant behaviour (type, filename, CSRF, cleanup).
1065
+ # The app test for this attachment, in the class form: written as
1066
+ # test/integration/post_cover_attachment_test.rb, an
1067
+ # ActionDispatch::IntegrationTest. It signs in first when the app has
1068
+ # authentication (Templates.signs_in_tests?: the authentication
1069
+ # generator's test helper, not app/models/user.rb, which a scaffold
1070
+ # of a `user` model writes too; a later `generate authentication`
1071
+ # adds the sign-in to it instead), fills the form
1072
+ # fields the model validates for presence, and checks the
1073
+ # security-relevant behaviour (type, filename, CSRF, cleanup).
1000
1074
  def self.attachment_test(root, spec)
1001
- auth = File.exist?(File.join(root, "app/models/user.rb"))
1002
- text = TEST_HEAD
1003
- text += spec.any ? TEST_PNG_ROUNDTRIP + TEST_ANY_TYPE : TEST_PNG_ROUNDTRIP + TEST_REJECT_TYPES
1004
- text += TEST_COMMON
1075
+ auth = Templates.signs_in_tests?(root)
1076
+ # The class body after setup is written flush and indented here.
1077
+ body = spec.any ? TEST_PNG_ROUNDTRIP + TEST_ANY_TYPE : TEST_PNG_ROUNDTRIP + TEST_REJECT_TYPES
1078
+ body += TEST_COMMON
1005
1079
  fields = required_fields(root, spec)
1006
- text += TEST_KEEP
1080
+ body += TEST_KEEP
1007
1081
  if auth
1008
1082
  open_download = download_access(File.read(File.join(root, controller_file(spec))), spec) == "public"
1009
- text += open_download ? TEST_PUBLIC_DOWNLOAD : TEST_AUTH_DOWNLOAD
1083
+ body += open_download ? TEST_PUBLIC_DOWNLOAD : TEST_AUTH_DOWNLOAD
1010
1084
  end
1011
- text += TEST_TAIL
1012
- text = text.sub(" <<reset_auth>>\n", auth ? Authentication::TEST_RESET_LINES : "")
1013
- text = text.sub(" <<sign_in>>\n", auth ? Authentication::TEST_SIGN_IN_LINES : "")
1014
- text = text.sub("<<fields_lines>>", field_lines(root, spec, fields))
1015
- text = text.sub("<<show_check>>", show_check(root, spec))
1085
+ body += TEST_TAIL
1086
+ body = body.sub("<<show_check>>", show_check(root, spec))
1087
+ body = body.sub("<<fields_hash>>", fields_hash(root, spec, fields))
1088
+ text = TEST_HEAD.sub("<<sign_in>>", auth ? Authentication.test_sign_in_lines : "")
1089
+ text = text.sub("<<TestClass>>", test_class(spec))
1090
+ text += Authentication.margin(body, 2)
1091
+ text += "end\n"
1016
1092
  spec.expand(text)
1017
1093
  end
1018
1094
 
1095
+ # PostCoverAttachmentTest for post's cover.
1096
+ def self.test_class(spec)
1097
+ Inflector.camelize("#{spec.singular}_#{spec.field}_attachment_test")
1098
+ end
1099
+
1019
1100
  # The attributes the model validates for presence or a minimum length
1020
1101
  # (`validates :title, presence: true`), which a valid record needs in
1021
1102
  # the form.
@@ -1039,10 +1120,12 @@ module Cybertrain
1039
1120
  names
1040
1121
  end
1041
1122
 
1042
- def self.field_lines(root, spec, names)
1043
- lines = +""
1044
- names.each { |name| lines << " fields[\"#{spec.singular}[#{name}]\"] = \"#{field_value(root, spec, name)}\"\n" }
1045
- lines
1123
+ # The form's fields as the params Hash: the attributes the model
1124
+ # requires, then the file, `{ title: "A post", cover: file }`.
1125
+ def self.fields_hash(root, spec, names)
1126
+ pairs = names.map { |name| "#{name}: \"#{field_value(root, spec, name)}\"" }
1127
+ pairs << "#{spec.field}: file"
1128
+ "{ #{pairs.join(", ")} }"
1046
1129
  end
1047
1130
 
1048
1131
  # "A post", stretched to the field's `length: { minimum: N }` and cut
@@ -1088,90 +1171,73 @@ module Cybertrain
1088
1171
  path = File.join(root, "app/views/#{spec.plural}/show.html.erb")
1089
1172
  return "" unless File.exist?(path) && name_present?(File.read(path), "@#{spec.singular}.#{spec.field}_url")
1090
1173
 
1091
- " res = client.get(\"/#{spec.plural}/\#{record.id}\")\n assert_includes res.body, \"/#{spec.plural}/\#{record.id}/#{spec.field}?v=\"\n"
1174
+ " get \"/#{spec.plural}/\#{record.id}\"\n assert_includes response.body, \"/#{spec.plural}/\#{record.id}/#{spec.field}?v=\"\n"
1092
1175
  end
1093
1176
 
1094
1177
  TEST_HEAD = <<~'RUBY'
1095
- # Uploads of <<Model>>#<<field>> through the full stack: the multipart parser, CSRF,
1096
- # method override, the model's callbacks and the download action. Written
1097
- # by `cybertrain generate attachment <<model>> <<field>>`. Run with
1098
- # `cybertrain spin test`; its snapshot comes from the compiled binary (the
1099
- # database is FFI): `script/regen-snapshot`-style, see README "Testing an app".
1100
- require_relative "support/app_test"
1101
-
1102
- # Uploaded files land here, not in storage/uploads.
1103
- Cybertrain::BlobStore.root = "tmp/test_uploads"
1104
-
1105
- # A PNG signature, then NUL, 0xFF and CR LF bytes: the server sniffs the
1106
- # signature only, the rest shows that every byte survives the round trip.
1107
- PNG_BYTES = Cybertrain::Test.binary([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0xFF, 0x0D, 0x0A, 0x00, 0x01])
1108
- HTML_BYTES = "<html><script>alert(document.cookie)</script></html>"
1109
- SVG_BYTES = '<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"></svg>'
1110
-
1111
- # The text fields of the form besides the file, and the CSRF token. Add the
1112
- # other attributes your <<Model>> requires.
1113
- def <<model>>_fields(token)
1114
- fields = { "authenticity_token" => token }
1115
- <<fields_lines>> fields
1116
- end
1117
-
1118
- def new_client
1119
- <<Model>>.all.delete_all
1120
- <<reset_auth>>
1121
- Dir.glob("tmp/test_uploads/*/*").each { |path| File.delete(path) }
1122
- client = Cybertrain::Test::Client.new(APP)
1123
- <<sign_in>>
1124
- client
1125
- end
1126
-
1127
- # POST /<<plural>> with one file in <<model>>[<<field>>], as the new form sends it.
1128
- def create_<<model>>(client, filename, type, data)
1129
- client.get("/<<plural>>/new")
1130
- token = AppTest.form_token(client)
1131
- field = "<<model>>[<<field>>]"
1132
- upload = Cybertrain::Test::Upload.new(field, filename, type, data)
1133
- client.post_multipart("/<<plural>>", <<model>>_fields(token), [upload])
1134
- end
1178
+ require "test_helper"
1179
+
1180
+ # Uploads of <<Model>>#<<field>> through the full stack: the multipart parser,
1181
+ # the model's callbacks and the download action. Written by `cybertrain
1182
+ # generate attachment <<model>> <<field>>`; run with `cybertrain test`.
1183
+ class <<TestClass>> < ActionDispatch::IntegrationTest
1184
+ # test/fixtures/files/<<field>>.png: a 1x1 PNG, whose bytes hold NUL, 0xFF and
1185
+ # CR LF; the round trip shows that every byte survives. Read from the
1186
+ # file, so a picture of your own put there is what the tests send.
1187
+ PNG_BYTES = File.binread("test/fixtures/files/<<field>>.png")
1188
+ HTML_BYTES = "<html><script>alert(document.cookie)</script></html>"
1189
+ SVG_BYTES = '<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"></svg>'
1190
+
1191
+ setup do
1192
+ <<sign_in>> # Uploaded files land here, not in storage/uploads.
1193
+ Cybertrain::BlobStore.root = "tmp/test_uploads"
1194
+ Dir.glob("tmp/test_uploads/*/*").each { |path| File.delete(path) }
1195
+ end
1196
+
1197
+ # The configured upload directory again, for the tests after this one.
1198
+ teardown do
1199
+ Cybertrain::BlobStore.root = Cybertrain.config.upload_root
1200
+ end
1135
1201
 
1136
1202
  RUBY
1137
1203
 
1138
1204
  TEST_PNG_ROUNDTRIP = <<~'RUBY'
1139
1205
  test "a PNG upload is stored on disk and served back with safe headers" do
1140
- client = new_client
1141
- res = create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1142
- assert_response res, :see_other
1143
- assert_equal 1, <<Model>>.count
1144
- record = <<Model>>.first
1206
+ assert_difference(-> { <<Model>>.count }, 1) do
1207
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1208
+ end
1209
+ assert_response :see_other
1210
+ record = <<Model>>.last
1145
1211
  assert record.<<field>>_attached?
1146
1212
  assert_equal "<<field>>.png", record.<<field>>_filename
1147
1213
  assert_equal "image/png", record.<<field>>_content_type
1148
1214
  assert_equal PNG_BYTES.bytesize, record.<<field>>_byte_size
1149
1215
  assert Cybertrain::BlobStore.exist?(record.<<field>>_key)
1150
- res = client.get("/<<plural>>/#{record.id}/<<field>>")
1151
- assert_response res, :ok
1152
- served = res.body
1216
+ get "/<<plural>>/#{record.id}/<<field>>"
1217
+ assert_response :ok
1218
+ served = response.body
1153
1219
  assert_equal PNG_BYTES.bytesize, served.bytesize
1154
1220
  name = "copy.png"
1155
1221
  data = served.b
1156
1222
  copy = Cybertrain::UploadedFile.new(name, "", data)
1157
1223
  assert_equal record.<<field>>_checksum, copy.sha256_hex
1158
- assert res.header("content-type").to_s.start_with?("image/png"), "content-type"
1159
- assert_equal "nosniff", res.header("x-content-type-options")
1160
- assert res.header("content-disposition").to_s.start_with?("inline"), "content-disposition"
1161
- assert_equal "default-src 'none'; sandbox", res.header("content-security-policy")
1224
+ assert response.header("content-type").to_s.start_with?("image/png"), "content-type"
1225
+ assert_equal "nosniff", response.header("x-content-type-options")
1226
+ assert response.header("content-disposition").to_s.start_with?("inline"), "content-disposition"
1227
+ assert_equal "default-src 'none'; sandbox", response.header("content-security-policy")
1162
1228
  <<show_check>>end
1163
1229
 
1164
1230
  RUBY
1165
1231
 
1166
1232
  TEST_REJECT_TYPES = <<~'RUBY'
1167
1233
  test "HTML and SVG bytes are refused even when named .png and declared image/png" do
1168
- client = new_client
1169
- res = create_<<model>>(client, "x.png", "image/png", HTML_BYTES)
1170
- assert_response res, :unprocessable_entity
1171
- assert_includes res.body, "<<Field>> must be a PNG, JPEG, GIF or WebP image"
1172
- res = create_<<model>>(client, "logo.png", "image/png", SVG_BYTES)
1173
- assert_response res, :unprocessable_entity
1174
- assert_equal 0, <<Model>>.count
1234
+ assert_no_difference(-> { <<Model>>.count }) do
1235
+ create_<<model>>(file_named("x.png", "image/png", HTML_BYTES))
1236
+ assert_response :unprocessable_entity
1237
+ assert_includes response.body, "<<Field>> must be a PNG, JPEG, GIF or WebP image"
1238
+ create_<<model>>(file_named("logo.png", "image/png", SVG_BYTES))
1239
+ assert_response :unprocessable_entity
1240
+ end
1175
1241
  assert_equal 0, Dir.glob("tmp/test_uploads/*/*").size
1176
1242
  end
1177
1243
 
@@ -1179,38 +1245,36 @@ module Cybertrain
1179
1245
 
1180
1246
  TEST_ANY_TYPE = <<~'RUBY'
1181
1247
  test "any file is accepted, stored as a download and never served inline" do
1182
- client = new_client
1183
- res = create_<<model>>(client, "page.html", "text/html", HTML_BYTES)
1184
- assert_response res, :see_other
1185
- record = <<Model>>.first
1248
+ create_<<model>>(file_named("page.html", "text/html", HTML_BYTES))
1249
+ assert_response :see_other
1250
+ record = <<Model>>.last
1186
1251
  assert_equal "application/octet-stream", record.<<field>>_content_type
1187
- res = client.get("/<<plural>>/#{record.id}/<<field>>")
1188
- assert_response res, :ok
1189
- assert res.header("content-type").to_s.start_with?("application/octet-stream"), "content-type"
1190
- assert res.header("content-disposition").to_s.start_with?("attachment"), "content-disposition"
1191
- assert_equal "nosniff", res.header("x-content-type-options")
1192
- assert_equal "default-src 'none'; sandbox", res.header("content-security-policy")
1252
+ get "/<<plural>>/#{record.id}/<<field>>"
1253
+ assert_response :ok
1254
+ assert response.header("content-type").to_s.start_with?("application/octet-stream"), "content-type"
1255
+ assert response.header("content-disposition").to_s.start_with?("attachment"), "content-disposition"
1256
+ assert_equal "nosniff", response.header("x-content-type-options")
1257
+ assert_equal "default-src 'none'; sandbox", response.header("content-security-policy")
1193
1258
  end
1194
1259
 
1195
1260
  RUBY
1196
1261
 
1197
1262
  TEST_COMMON = <<~'RUBY'
1198
1263
  test "a file over the model's limit is a 422 with a message" do
1199
- client = new_client
1200
1264
  big = +""
1201
1265
  big << PNG_BYTES
1202
1266
  big << ("a" * (<<Model>>::<<FIELD>>_MAX_BYTES + 1))
1203
- res = create_<<model>>(client, "big.png", "image/png", big)
1204
- assert_response res, :unprocessable_entity
1205
- assert_includes res.body, "<<Field>> is too large (maximum is 2 MB)"
1206
- assert_equal 0, <<Model>>.count
1267
+ assert_no_difference(-> { <<Model>>.count }) do
1268
+ create_<<model>>(file_named("big.png", "image/png", big))
1269
+ end
1270
+ assert_response :unprocessable_entity
1271
+ assert_includes response.body, "<<Field>> is too large (maximum is 2 MB)"
1207
1272
  end
1208
1273
 
1209
1274
  test "the client's filename is a display name: it never reaches the disk path" do
1210
- client = new_client
1211
- res = create_<<model>>(client, "../../evil.png", "image/png", PNG_BYTES)
1212
- assert_response res, :see_other
1213
- record = <<Model>>.first
1275
+ create_<<model>>(file_named("../../evil.png", "image/png", PNG_BYTES))
1276
+ assert_response :see_other
1277
+ record = <<Model>>.last
1214
1278
  assert_equal "evil.png", record.<<field>>_filename
1215
1279
  key = record.<<field>>_key
1216
1280
  assert_equal 32, key.bytesize
@@ -1218,53 +1282,46 @@ module Cybertrain
1218
1282
  path = Cybertrain::BlobStore.path_for(key)
1219
1283
  refute path.include?("evil"), path
1220
1284
  assert path.start_with?("tmp/test_uploads/"), path
1221
- res = client.get("/<<plural>>/#{record.id}/<<field>>")
1222
- assert_includes res.header("content-disposition").to_s, "filename=\"evil.png\""
1285
+ get "/<<plural>>/#{record.id}/<<field>>"
1286
+ assert_includes response.header("content-disposition").to_s, "filename=\"evil.png\""
1223
1287
  end
1224
1288
 
1225
1289
  test "replacing the file deletes the old blob; destroying the record deletes the current one" do
1226
- client = new_client
1227
- create_<<model>>(client, "one.png", "image/png", PNG_BYTES)
1228
- record = <<Model>>.first
1290
+ create_<<model>>(file_named("one.png", "image/png", PNG_BYTES))
1291
+ record = <<Model>>.last
1229
1292
  old_key = record.<<field>>_key
1230
- client.get("/<<plural>>/#{record.id}/edit")
1231
- token = AppTest.form_token(client)
1232
- field = "<<model>>[<<field>>]"
1233
- upload = Cybertrain::Test::Upload.new(field, "two.png", "image/png", PNG_BYTES)
1234
- res = client.patch_multipart("/<<plural>>/#{record.id}", <<model>>_fields(token), [upload])
1235
- assert_response res, :see_other
1293
+ update_<<model>>(record, file_named("two.png", "image/png", PNG_BYTES))
1294
+ assert_response :see_other
1236
1295
  record = <<Model>>.find(record.id)
1237
1296
  new_key = record.<<field>>_key
1238
1297
  refute new_key == old_key, "the key changes with the file"
1239
1298
  assert_equal "two.png", record.<<field>>_filename
1240
1299
  refute Cybertrain::BlobStore.exist?(old_key), "the replaced blob is deleted"
1241
1300
  assert Cybertrain::BlobStore.exist?(new_key)
1242
- client.get("/<<plural>>/#{record.id}")
1243
- res = client.post("/<<plural>>/#{record.id}", { "_method" => "delete", "authenticity_token" => AppTest.form_token(client) })
1244
- assert_response res, :see_other
1245
- assert_equal 0, <<Model>>.count
1301
+ assert_difference(-> { <<Model>>.count }, -1) do
1302
+ delete "/<<plural>>/#{record.id}"
1303
+ end
1304
+ assert_response :see_other
1246
1305
  refute Cybertrain::BlobStore.exist?(new_key), "the destroyed record's blob is deleted"
1247
1306
  end
1248
1307
 
1249
1308
  test "a record whose blob is gone answers 404" do
1250
- client = new_client
1251
- create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1252
- record = <<Model>>.first
1309
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1310
+ record = <<Model>>.last
1253
1311
  File.delete(Cybertrain::BlobStore.path_for(record.<<field>>_key))
1254
- res = client.get("/<<plural>>/#{record.id}/<<field>>")
1255
- assert_response res, :not_found
1312
+ get "/<<plural>>/#{record.id}/<<field>>"
1313
+ assert_response :not_found
1256
1314
  end
1257
1315
 
1316
+ # Forgery protection is off in tests unless config/app.rb sets csrf,
1317
+ # as in Rails; the block turns it on for its requests.
1258
1318
  test "a multipart POST without the CSRF token is refused" do
1259
- client = new_client
1260
- client.get("/<<plural>>/new")
1261
- field = "<<model>>[<<field>>]"
1262
- upload = Cybertrain::Test::Upload.new(field, "<<field>>.png", "image/png", PNG_BYTES)
1263
- fields = <<model>>_fields("unused")
1264
- fields.delete("authenticity_token")
1265
- res = client.post_multipart("/<<plural>>", fields, [upload])
1266
- assert_response res, :forbidden
1267
- assert_equal 0, <<Model>>.count
1319
+ assert_no_difference(-> { <<Model>>.count }) do
1320
+ with_forgery_protection do
1321
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1322
+ assert_response :forbidden
1323
+ end
1324
+ end
1268
1325
  end
1269
1326
 
1270
1327
  RUBY
@@ -1274,15 +1331,11 @@ module Cybertrain
1274
1331
  # bytes. The parser drops it, so a form whose only field is the file
1275
1332
  # arrives without any <<model>>[...] key at all.
1276
1333
  test "an update without a file keeps the stored one" do
1277
- client = new_client
1278
- create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1279
- record = <<Model>>.first
1334
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1335
+ record = <<Model>>.last
1280
1336
  key = record.<<field>>_key
1281
- client.get("/<<plural>>/#{record.id}/edit")
1282
- field = "<<model>>[<<field>>]"
1283
- empty = Cybertrain::Test::Upload.new(field, "", "application/octet-stream", "")
1284
- res = client.patch_multipart("/<<plural>>/#{record.id}", <<model>>_fields(AppTest.form_token(client)), [empty])
1285
- assert_response res, :see_other
1337
+ update_<<model>>(record, file_named("", "application/octet-stream", ""))
1338
+ assert_response :see_other
1286
1339
  record = <<Model>>.find(record.id)
1287
1340
  assert_equal key, record.<<field>>_key
1288
1341
  assert Cybertrain::BlobStore.exist?(key)
@@ -1292,32 +1345,61 @@ module Cybertrain
1292
1345
 
1293
1346
  TEST_AUTH_DOWNLOAD = <<~'RUBY'
1294
1347
  test "the download needs a signed-in user, like every other action of the controller" do
1295
- client = new_client
1296
- create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1297
- record = <<Model>>.first
1298
- anonymous = Cybertrain::Test::Client.new(APP)
1299
- res = anonymous.get("/<<plural>>/#{record.id}/<<field>>")
1300
- assert_redirected_to res, "/session/new"
1348
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1349
+ record = <<Model>>.last
1350
+ delete "/session"
1351
+ get "/<<plural>>/#{record.id}/<<field>>"
1352
+ assert_redirected_to "/session/new"
1301
1353
  end
1302
1354
 
1303
1355
  RUBY
1304
1356
 
1305
1357
  TEST_PUBLIC_DOWNLOAD = <<~'RUBY'
1306
1358
  test "the download is public like show: a visitor gets the file" do
1307
- client = new_client
1308
- create_<<model>>(client, "<<field>>.png", "image/png", PNG_BYTES)
1309
- record = <<Model>>.first
1310
- anonymous = Cybertrain::Test::Client.new(APP)
1311
- res = anonymous.get("/<<plural>>/#{record.id}/<<field>>")
1312
- assert_response res, :ok
1313
- assert_equal PNG_BYTES.bytesize, res.body.bytesize
1359
+ create_<<model>>(fixture_file_upload("<<field>>.png", "image/png"))
1360
+ record = <<Model>>.last
1361
+ delete "/session"
1362
+ get "/<<plural>>/#{record.id}/<<field>>"
1363
+ assert_response :ok
1364
+ assert_equal PNG_BYTES.bytesize, response.body.bytesize
1314
1365
  end
1315
1366
 
1316
1367
  RUBY
1317
1368
 
1318
1369
  TEST_TAIL = <<~'RUBY'
1319
- Cybertrain::Test.run!
1370
+ private
1371
+
1372
+ # The form's fields: the file, and the other attributes your <<Model>>
1373
+ # requires.
1374
+ def <<model>>_params(file)
1375
+ <<fields_hash>>
1376
+ end
1377
+
1378
+ # POST /<<plural>> as the new form sends it: the file in <<model>>[<<field>>]
1379
+ # (multipart/form-data, since a params value is a file).
1380
+ def create_<<model>>(file)
1381
+ post "/<<plural>>", params: { <<model>>: <<model>>_params(file) }
1382
+ end
1383
+
1384
+ # PATCH /<<plural>>/:id as the edit form sends it.
1385
+ def update_<<model>>(record, file)
1386
+ patch "/<<plural>>/#{record.id}", params: { <<model>>: <<model>>_params(file) }
1387
+ end
1388
+
1389
+ # A file with this name, type and bytes, for what no fixture file
1390
+ # holds (a disguised type, a hostile name, a file input left empty).
1391
+ def file_named(filename, type, data)
1392
+ Cybertrain::Test::Upload.new("", filename, type, data)
1393
+ end
1320
1394
  RUBY
1395
+
1396
+ # test/fixtures/files/<field>.png, the PNG_BYTES of the test above: a
1397
+ # real 1x1 PNG (8-bit RGBA, the pixel FF 00 0D 0A), its IHDR, IDAT and
1398
+ # IEND chunks with their CRC-32s.
1399
+ TEST_PNG_FILE = ("\x89PNG\r\n\x1A\n" \
1400
+ "\x00\x00\x00\x0DIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1F\x15\xC4\x89" \
1401
+ "\x00\x00\x00\x0DIDATx\xDAc\xF8\xCF\xC0\xCB\x05\x00\x04%\x01\x17v\x05v|" \
1402
+ "\x00\x00\x00\x00IEND\xAEB`\x82").b
1321
1403
  end
1322
1404
  end
1323
1405
  end