3889: minor test assertion updates
[arvados.git] / services / api / test / functional / arvados / v1 / collections_controller_test.rb
1 require 'test_helper'
2
3 class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
4
5   setup do
6     # Unless otherwise specified in the test, we want normal/secure behavior.
7     permit_unsigned_manifests false
8   end
9
10   teardown do
11     # Reset to secure behavior after each test.
12     permit_unsigned_manifests false
13   end
14
15   def permit_unsigned_manifests isok=true
16     # Set security model for the life of a test.
17     Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
18   end
19
20   test "should get index" do
21     authorize_with :active
22     get :index
23     assert_response :success
24     assert(assigns(:objects).andand.any?, "no Collections returned in index")
25     refute(json_response["items"].any? { |c| c.has_key?("manifest_text") },
26            "basic Collections index included manifest_text")
27   end
28
29   test "index with manifest_text selected returns signed locators" do
30     columns = %w(uuid owner_uuid manifest_text)
31     authorize_with :active
32     get :index, select: columns
33     assert_response :success
34     assert(assigns(:objects).andand.any?,
35            "no Collections returned for index with columns selected")
36     json_response["items"].each do |coll|
37       assert_equal(columns, columns & coll.keys,
38                    "Collections index did not respect selected columns")
39       loc_regexp = / [[:xdigit:]]{32}\+\d+\S+/
40       pos = 0
41       while match = loc_regexp.match(coll["manifest_text"], pos)
42         assert_match(/\+A[[:xdigit:]]+@[[:xdigit:]]{8}\b/, match.to_s,
43                      "Locator in manifest_text was not signed")
44         pos = match.end(0)
45       end
46     end
47   end
48
49   [0,1,2].each do |limit|
50     test "get index with limit=#{limit}" do
51       authorize_with :active
52       get :index, limit: limit
53       assert_response :success
54       assert_equal limit, assigns(:objects).count
55       resp = JSON.parse(@response.body)
56       assert_equal limit, resp['limit']
57     end
58   end
59
60   test "items.count == items_available" do
61     authorize_with :active
62     get :index, limit: 100000
63     assert_response :success
64     resp = JSON.parse(@response.body)
65     assert_equal resp['items_available'], assigns(:objects).length
66     assert_equal resp['items_available'], resp['items'].count
67     unique_uuids = resp['items'].collect { |i| i['uuid'] }.compact.uniq
68     assert_equal unique_uuids.count, resp['items'].count
69   end
70
71   test "get index with limit=2 offset=99999" do
72     # Assume there are not that many test fixtures.
73     authorize_with :active
74     get :index, limit: 2, offset: 99999
75     assert_response :success
76     assert_equal 0, assigns(:objects).count
77     resp = JSON.parse(@response.body)
78     assert_equal 2, resp['limit']
79     assert_equal 99999, resp['offset']
80   end
81
82   test "admin can create collection with unsigned manifest" do
83     authorize_with :admin
84     test_collection = {
85       manifest_text: <<-EOS
86 . d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo.txt
87 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
88 . acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
89 ./baz acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:bar.txt
90 EOS
91     }
92     test_collection[:portable_data_hash] =
93       Digest::MD5.hexdigest(test_collection[:manifest_text]) +
94       '+' +
95       test_collection[:manifest_text].length.to_s
96
97     # post :create will modify test_collection in place, so we save a copy first.
98     # Hash.deep_dup is not sufficient as it preserves references of strings (??!?)
99     post_collection = Marshal.load(Marshal.dump(test_collection))
100     post :create, {
101       collection: post_collection
102     }
103
104     assert_response :success
105     assert_nil assigns(:objects)
106
107     response_collection = assigns(:object)
108
109     stored_collection = Collection.select([:uuid, :portable_data_hash, :manifest_text]).
110       where(portable_data_hash: response_collection['portable_data_hash']).first
111
112     assert_equal test_collection[:portable_data_hash], stored_collection['portable_data_hash']
113
114     # The manifest in the response will have had permission hints added.
115     # Remove any permission hints in the response before comparing it to the source.
116     stripped_manifest = stored_collection['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
117     assert_equal test_collection[:manifest_text], stripped_manifest
118
119     # TBD: create action should add permission signatures to manifest_text in the response,
120     # and we need to check those permission signatures here.
121   end
122
123   [:admin, :active].each do |user|
124     test "#{user} can get collection using portable data hash" do
125       authorize_with user
126
127       foo_collection = collections(:foo_file)
128
129       # Get foo_file using it's portable data has
130       get :show, {
131         id: foo_collection[:portable_data_hash]
132       }
133       assert_response :success
134       assert_not_nil assigns(:object)
135       resp = assigns(:object)
136       assert_equal foo_collection[:portable_data_hash], resp['portable_data_hash']
137
138       # The manifest in the response will have had permission hints added.
139       # Remove any permission hints in the response before comparing it to the source.
140       stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
141       assert_equal foo_collection[:manifest_text], stripped_manifest
142     end
143   end
144
145   test "create with owner_uuid set to owned group" do
146     permit_unsigned_manifests
147     authorize_with :active
148     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
149     post :create, {
150       collection: {
151         owner_uuid: 'zzzzz-j7d0g-rew6elm53kancon',
152         manifest_text: manifest_text,
153         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
154       }
155     }
156     assert_response :success
157     resp = JSON.parse(@response.body)
158     assert_equal 'zzzzz-j7d0g-rew6elm53kancon', resp['owner_uuid']
159   end
160
161   test "create fails with duplicate name" do
162     permit_unsigned_manifests
163     authorize_with :admin
164     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
165     post :create, {
166       collection: {
167         owner_uuid: 'zzzzz-tpzed-000000000000000',
168         manifest_text: manifest_text,
169         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
170         name: "foo_file"
171       }
172     }
173     assert_response 422
174   end
175
176   test "create succeeds with with duplicate name with ensure_unique_name" do
177     permit_unsigned_manifests
178     authorize_with :admin
179     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
180     post :create, {
181       collection: {
182         owner_uuid: 'zzzzz-tpzed-000000000000000',
183         manifest_text: manifest_text,
184         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47",
185         name: "foo_file"
186       },
187       ensure_unique_name: true
188     }
189     assert_response :success
190     resp = JSON.parse(@response.body)
191     assert_equal 'foo_file (2)', resp['name']
192   end
193
194   test "create with owner_uuid set to group i can_manage" do
195     permit_unsigned_manifests
196     authorize_with :active
197     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
198     post :create, {
199       collection: {
200         owner_uuid: groups(:active_user_has_can_manage).uuid,
201         manifest_text: manifest_text,
202         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
203       }
204     }
205     assert_response :success
206     resp = JSON.parse(@response.body)
207     assert_equal groups(:active_user_has_can_manage).uuid, resp['owner_uuid']
208   end
209
210   test "create with owner_uuid fails on group with only can_read permission" do
211     permit_unsigned_manifests
212     authorize_with :active
213     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
214     post :create, {
215       collection: {
216         owner_uuid: groups(:all_users).uuid,
217         manifest_text: manifest_text,
218         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
219       }
220     }
221     assert_response 403
222   end
223
224   test "create with owner_uuid fails on group with no permission" do
225     permit_unsigned_manifests
226     authorize_with :active
227     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
228     post :create, {
229       collection: {
230         owner_uuid: groups(:public).uuid,
231         manifest_text: manifest_text,
232         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
233       }
234     }
235     assert_response 422
236   end
237
238   test "admin create with owner_uuid set to group with no permission" do
239     permit_unsigned_manifests
240     authorize_with :admin
241     manifest_text = ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n"
242     post :create, {
243       collection: {
244         owner_uuid: 'zzzzz-j7d0g-it30l961gq3t0oi',
245         manifest_text: manifest_text,
246         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47"
247       }
248     }
249     assert_response :success
250   end
251
252   test "should create with collection passed as json" do
253     permit_unsigned_manifests
254     authorize_with :active
255     post :create, {
256       collection: <<-EOS
257       {
258         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",\
259         "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
260       }
261       EOS
262     }
263     assert_response :success
264   end
265
266   test "should fail to create with checksum mismatch" do
267     permit_unsigned_manifests
268     authorize_with :active
269     post :create, {
270       collection: <<-EOS
271       {
272         "manifest_text":". d41d8cd98f00b204e9800998ecf8427e 0:0:bar.txt\n",\
273         "portable_data_hash":"d30fe8ae534397864cb96c544f4cf102+47"\
274       }
275       EOS
276     }
277     assert_response 422
278   end
279
280   test "collection UUID is normalized when created" do
281     permit_unsigned_manifests
282     authorize_with :active
283     post :create, {
284       collection: {
285         manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:0:foo.txt\n",
286         portable_data_hash: "d30fe8ae534397864cb96c544f4cf102+47+Khint+Xhint+Zhint"
287       }
288     }
289     assert_response :success
290     assert_not_nil assigns(:object)
291     resp = JSON.parse(@response.body)
292     assert_equal "d30fe8ae534397864cb96c544f4cf102+47", resp['portable_data_hash']
293   end
294
295   test "get full provenance for baz file" do
296     authorize_with :active
297     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
298     assert_response :success
299     resp = JSON.parse(@response.body)
300     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
301     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
302     assert_not_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
303     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq'] # bar->baz
304     assert_not_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon'] # foo->bar
305   end
306
307   test "get no provenance for foo file" do
308     # spectator user cannot even see baz collection
309     authorize_with :spectator
310     get :provenance, id: '1f4b0bc7583c2a7f9102c395f4ffc5e3+45'
311     assert_response 404
312   end
313
314   test "get partial provenance for baz file" do
315     # spectator user can see bar->baz job, but not foo->bar job
316     authorize_with :spectator
317     get :provenance, id: 'ea10d51bcf88862dbcc36eb292017dfd+45'
318     assert_response :success
319     resp = JSON.parse(@response.body)
320     assert_not_nil resp['ea10d51bcf88862dbcc36eb292017dfd+45'] # baz
321     assert_not_nil resp['fa7aeb5140e2848d39b416daeef4ffc5+45'] # bar
322     assert_not_nil resp['zzzzz-8i9sb-cjs4pklxxjykyuq']     # bar->baz
323     assert_nil resp['zzzzz-8i9sb-aceg2bnq7jt7kon']         # foo->bar
324     assert_nil resp['1f4b0bc7583c2a7f9102c395f4ffc5e3+45'] # foo
325   end
326
327   test "search collections with 'any' operator" do
328     authorize_with :active
329     get :index, {
330       where: { any: ['contains', '7f9102c395f4ffc5e3'] }
331     }
332     assert_response :success
333     found = assigns(:objects).collect(&:portable_data_hash)
334     assert_equal 2, found.count
335     assert_equal true, !!found.index('1f4b0bc7583c2a7f9102c395f4ffc5e3+45')
336   end
337
338   [false, true].each do |permit_unsigned|
339     test "create collection with signed manifest, permit_unsigned=#{permit_unsigned}" do
340       permit_unsigned_manifests permit_unsigned
341       authorize_with :active
342       locators = %w(
343       d41d8cd98f00b204e9800998ecf8427e+0
344       acbd18db4cc2f85cedef654fccc4a4d8+3
345       ea10d51bcf88862dbcc36eb292017dfd+45)
346
347       unsigned_manifest = locators.map { |loc|
348         ". " + loc + " 0:0:foo.txt\n"
349       }.join()
350       manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
351         '+' +
352         unsigned_manifest.length.to_s
353
354       # Build a manifest with both signed and unsigned locators.
355       signing_opts = {
356         key: Rails.configuration.blob_signing_key,
357         api_token: api_token(:active),
358       }
359       signed_locators = locators.collect do |x|
360         Blob.sign_locator x, signing_opts
361       end
362       if permit_unsigned
363         # Leave a non-empty blob unsigned.
364         signed_locators[1] = locators[1]
365       else
366         # Leave the empty blob unsigned. This should still be allowed.
367         signed_locators[0] = locators[0]
368       end
369       signed_manifest =
370         ". " + signed_locators[0] + " 0:0:foo.txt\n" +
371         ". " + signed_locators[1] + " 0:0:foo.txt\n" +
372         ". " + signed_locators[2] + " 0:0:foo.txt\n"
373
374       post :create, {
375         collection: {
376           manifest_text: signed_manifest,
377           portable_data_hash: manifest_uuid,
378         }
379       }
380       assert_response :success
381       assert_not_nil assigns(:object)
382       resp = JSON.parse(@response.body)
383       assert_equal manifest_uuid, resp['portable_data_hash']
384       # All of the locators in the output must be signed.
385       resp['manifest_text'].lines.each do |entry|
386         m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
387         if m
388           assert Blob.verify_signature m[0], signing_opts
389         end
390       end
391     end
392   end
393
394   test "create collection with signed manifest and explicit TTL" do
395     authorize_with :active
396     locators = %w(
397       d41d8cd98f00b204e9800998ecf8427e+0
398       acbd18db4cc2f85cedef654fccc4a4d8+3
399       ea10d51bcf88862dbcc36eb292017dfd+45)
400
401     unsigned_manifest = locators.map { |loc|
402       ". " + loc + " 0:0:foo.txt\n"
403     }.join()
404     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
405       '+' +
406       unsigned_manifest.length.to_s
407
408     # build a manifest with both signed and unsigned locators.
409     # TODO(twp): in phase 4, all locators will need to be signed, so
410     # this test should break and will need to be rewritten. Issue #2755.
411     signing_opts = {
412       key: Rails.configuration.blob_signing_key,
413       api_token: api_token(:active),
414       ttl: 3600   # 1 hour
415     }
416     signed_manifest =
417       ". " + locators[0] + " 0:0:foo.txt\n" +
418       ". " + Blob.sign_locator(locators[1], signing_opts) + " 0:0:foo.txt\n" +
419       ". " + Blob.sign_locator(locators[2], signing_opts) + " 0:0:foo.txt\n"
420
421     post :create, {
422       collection: {
423         manifest_text: signed_manifest,
424         portable_data_hash: manifest_uuid,
425       }
426     }
427     assert_response :success
428     assert_not_nil assigns(:object)
429     resp = JSON.parse(@response.body)
430     assert_equal manifest_uuid, resp['portable_data_hash']
431     # All of the locators in the output must be signed.
432     resp['manifest_text'].lines.each do |entry|
433       m = /([[:xdigit:]]{32}\+\S+)/.match(entry)
434       if m
435         assert Blob.verify_signature m[0], signing_opts
436       end
437     end
438   end
439
440   test "create fails with invalid signature" do
441     authorize_with :active
442     signing_opts = {
443       key: Rails.configuration.blob_signing_key,
444       api_token: api_token(:active),
445     }
446
447     # Generate a locator with a bad signature.
448     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
449     bad_locator = unsigned_locator + "+Affffffff@ffffffff"
450     assert !Blob.verify_signature(bad_locator, signing_opts)
451
452     # Creating a collection with this locator should
453     # produce 403 Permission denied.
454     unsigned_manifest = ". #{unsigned_locator} 0:0:foo.txt\n"
455     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
456       '+' +
457       unsigned_manifest.length.to_s
458
459     bad_manifest = ". #{bad_locator} 0:0:foo.txt\n"
460     post :create, {
461       collection: {
462         manifest_text: bad_manifest,
463         portable_data_hash: manifest_uuid
464       }
465     }
466
467     assert_response 403
468   end
469
470   test "create fails with uuid of signed manifest" do
471     authorize_with :active
472     signing_opts = {
473       key: Rails.configuration.blob_signing_key,
474       api_token: api_token(:active),
475     }
476
477     unsigned_locator = "d41d8cd98f00b204e9800998ecf8427e+0"
478     signed_locator = Blob.sign_locator(unsigned_locator, signing_opts)
479     signed_manifest = ". #{signed_locator} 0:0:foo.txt\n"
480     manifest_uuid = Digest::MD5.hexdigest(signed_manifest) +
481       '+' +
482       signed_manifest.length.to_s
483
484     post :create, {
485       collection: {
486         manifest_text: signed_manifest,
487         portable_data_hash: manifest_uuid
488       }
489     }
490
491     assert_response 422
492   end
493
494   test "multiple locators per line" do
495     permit_unsigned_manifests
496     authorize_with :active
497     locators = %w(
498       d41d8cd98f00b204e9800998ecf8427e+0
499       acbd18db4cc2f85cedef654fccc4a4d8+3
500       ea10d51bcf88862dbcc36eb292017dfd+45)
501
502     manifest_text = [".", *locators, "0:0:foo.txt\n"].join(" ")
503     manifest_uuid = Digest::MD5.hexdigest(manifest_text) +
504       '+' +
505       manifest_text.length.to_s
506
507     test_collection = {
508       manifest_text: manifest_text,
509       portable_data_hash: manifest_uuid,
510     }
511     post_collection = Marshal.load(Marshal.dump(test_collection))
512     post :create, {
513       collection: post_collection
514     }
515     assert_response :success
516     assert_not_nil assigns(:object)
517     resp = JSON.parse(@response.body)
518     assert_equal manifest_uuid, resp['portable_data_hash']
519
520     # The manifest in the response will have had permission hints added.
521     # Remove any permission hints in the response before comparing it to the source.
522     stripped_manifest = resp['manifest_text'].gsub(/\+A[A-Za-z0-9@_-]+/, '')
523     assert_equal manifest_text, stripped_manifest
524   end
525
526   test "multiple signed locators per line" do
527     permit_unsigned_manifests
528     authorize_with :active
529     locators = %w(
530       d41d8cd98f00b204e9800998ecf8427e+0
531       acbd18db4cc2f85cedef654fccc4a4d8+3
532       ea10d51bcf88862dbcc36eb292017dfd+45)
533
534     signing_opts = {
535       key: Rails.configuration.blob_signing_key,
536       api_token: api_token(:active),
537     }
538
539     unsigned_manifest = [".", *locators, "0:0:foo.txt\n"].join(" ")
540     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest) +
541       '+' +
542       unsigned_manifest.length.to_s
543
544     signed_locators = locators.map { |loc| Blob.sign_locator loc, signing_opts }
545     signed_manifest = [".", *signed_locators, "0:0:foo.txt\n"].join(" ")
546
547     post :create, {
548       collection: {
549         manifest_text: signed_manifest,
550         portable_data_hash: manifest_uuid,
551       }
552     }
553     assert_response :success
554     assert_not_nil assigns(:object)
555     resp = JSON.parse(@response.body)
556     assert_equal manifest_uuid, resp['portable_data_hash']
557     # All of the locators in the output must be signed.
558     # Each line is of the form "path locator locator ... 0:0:file.txt"
559     # entry.split[1..-2] will yield just the tokens in the middle of the line
560     returned_locator_count = 0
561     resp['manifest_text'].lines.each do |entry|
562       entry.split[1..-2].each do |tok|
563         returned_locator_count += 1
564         assert Blob.verify_signature tok, signing_opts
565       end
566     end
567     assert_equal locators.count, returned_locator_count
568   end
569
570   test 'Reject manifest with unsigned blob' do
571     authorize_with :active
572     unsigned_manifest = ". 0cc175b9c0f1b6a831c399e269772661+1 0:1:a.txt\n"
573     manifest_uuid = Digest::MD5.hexdigest(unsigned_manifest)
574     post :create, {
575       collection: {
576         manifest_text: unsigned_manifest,
577         portable_data_hash: manifest_uuid,
578       }
579     }
580     assert_response 403,
581     "Creating a collection with unsigned blobs should respond 403"
582     assert_empty Collection.where('uuid like ?', manifest_uuid+'%'),
583     "Collection should not exist in database after failed create"
584   end
585
586   test 'List expired collection returns empty list' do
587     authorize_with :active
588     get :index, {
589       where: {name: 'expired_collection'},
590     }
591     assert_response :success
592     found = assigns(:objects)
593     assert_equal 0, found.count
594   end
595
596   test 'Show expired collection returns 404' do
597     authorize_with :active
598     get :show, {
599       id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
600     }
601     assert_response 404
602   end
603
604   test 'Update expired collection returns 404' do
605     authorize_with :active
606     post :update, {
607       id: 'zzzzz-4zz18-mto52zx1s7sn3ih',
608       collection: {
609         name: "still expired"
610       }
611     }
612     assert_response 404
613   end
614
615   test 'List collection with future expiration time succeeds' do
616     authorize_with :active
617     get :index, {
618       where: {name: 'collection_expires_in_future'},
619     }
620     found = assigns(:objects)
621     assert_equal 1, found.count
622   end
623
624
625   test 'Show collection with future expiration time succeeds' do
626     authorize_with :active
627     get :show, {
628       id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
629     }
630     assert_response :success
631   end
632
633   test 'Update collection with future expiration time succeeds' do
634     authorize_with :active
635     post :update, {
636       id: 'zzzzz-4zz18-padkqo7yb8d9i3j',
637       collection: {
638         name: "still not expired"
639       }
640     }
641     assert_response :success
642   end
643 end