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.
- checksums.yaml +4 -4
- data/README.md +82 -2049
- data/cybertrain/cli/attachment.rb +246 -164
- data/cybertrain/cli/authentication.rb +2405 -898
- data/cybertrain/cli/authentication_tests.rb +3175 -0
- data/cybertrain/cli/build.rb +93 -7
- data/cybertrain/cli/component.rb +118 -0
- data/cybertrain/cli/helper.rb +143 -0
- data/cybertrain/cli/job.rb +159 -0
- data/cybertrain/cli/line_diff.rb +216 -0
- data/cybertrain/cli/new_app.rb +17 -7
- data/cybertrain/cli/scaffold.rb +29 -1
- data/cybertrain/cli/templates.rb +405 -29
- data/cybertrain/cli/test_command.rb +301 -0
- data/cybertrain/cli/toolchain.rb +12 -12
- data/cybertrain/cli.rb +145 -24
- data/cybertrain/crypto.rb +136 -0
- data/cybertrain/ident.rb +9 -1
- data/cybertrain/shell.rb +13 -0
- data/cybertrain/test/args.rb +286 -0
- data/cybertrain/test_files.rb +48 -0
- data/cybertrain/version.rb +2 -2
- metadata +11 -2
|
@@ -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/
|
|
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
|
-
|
|
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
|
|
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
|
-
!
|
|
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/
|
|
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
|
|
373
|
-
puts "skip #{path} (:show is public, but show may check more than set_#{spec.singular}: a before_action of its own,
|
|
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
|
-
#
|
|
414
|
-
#
|
|
415
|
-
|
|
416
|
-
|
|
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
|
|
996
|
-
#
|
|
997
|
-
#
|
|
998
|
-
#
|
|
999
|
-
#
|
|
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 =
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1083
|
+
body += open_download ? TEST_PUBLIC_DOWNLOAD : TEST_AUTH_DOWNLOAD
|
|
1010
1084
|
end
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
text =
|
|
1015
|
-
text = text.sub("<<
|
|
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
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
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
|
-
"
|
|
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
|
-
|
|
1096
|
-
|
|
1097
|
-
#
|
|
1098
|
-
#
|
|
1099
|
-
#
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
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
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
record = <<Model>>.
|
|
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
|
-
|
|
1151
|
-
assert_response
|
|
1152
|
-
served =
|
|
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
|
|
1159
|
-
assert_equal "nosniff",
|
|
1160
|
-
assert
|
|
1161
|
-
assert_equal "default-src 'none'; sandbox",
|
|
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
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
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
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
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
|
-
|
|
1188
|
-
assert_response
|
|
1189
|
-
assert
|
|
1190
|
-
assert
|
|
1191
|
-
assert_equal "nosniff",
|
|
1192
|
-
assert_equal "default-src 'none'; sandbox",
|
|
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
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
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
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
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
|
-
|
|
1222
|
-
assert_includes
|
|
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
|
-
|
|
1227
|
-
|
|
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
|
-
|
|
1231
|
-
|
|
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
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
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
|
-
|
|
1251
|
-
|
|
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
|
-
|
|
1255
|
-
assert_response
|
|
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
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
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
|
-
|
|
1278
|
-
|
|
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
|
-
|
|
1282
|
-
|
|
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
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
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
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
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
|
-
|
|
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
|