13996: Fixing test WIP
[arvados.git] / services / api / test / functional / arvados / v1 / collections_controller_test.rb
index 2b2a736901e15d9aa081147b4a902c36b1d24089..7d67668c627b505d0d856396d5a83eb1294bfd67 100644 (file)
@@ -11,7 +11,7 @@ class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
 
   def permit_unsigned_manifests isok=true
     # Set security model for the life of a test.
-    Rails.configuration.permit_create_collection_with_unsigned_manifest = isok
+    Rails.configuration.Collections["BlobSigning"] = !isok
   end
 
   def assert_signed_manifest manifest_text, label='', token: false
@@ -24,7 +24,7 @@ class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
         exp = tok[/\+A[[:xdigit:]]+@([[:xdigit:]]+)/, 1].to_i(16)
         sig = Blob.sign_locator(
           bare,
-          key: Rails.configuration.blob_signing_key,
+          key: Rails.configuration.Collections["BlobSigningKey"],
           expire: exp,
           api_token: token)[/\+A[^\+]*/, 0]
         assert_includes tok, sig
@@ -88,7 +88,7 @@ class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
       token = api_client_authorizations(:active).send(token_method)
       signed = Blob.sign_locator(
         'acbd18db4cc2f85cedef654fccc4a4d8+3',
-        key: Rails.configuration.blob_signing_key,
+        key: Rails.configuration.Collections["BlobSigningKey"],
         api_token: token)
       authorize_with_token token
       put :update, params: {
@@ -221,7 +221,7 @@ class Arvados::V1::CollectionsControllerTest < ActionController::TestCase
   def request_capped_index(params={})
     authorize_with :user1_with_load
     coll1 = collections(:collection_1_of_201)
-    Rails.configuration.max_index_database_read =
+    Rails.configuration.API["MaxIndexDatabaseRead"] =
       yield(coll1.manifest_text.size)
     get :index, params: {
       select: %w(uuid manifest_text),
@@ -566,7 +566,7 @@ EOS
 
       # Build a manifest with both signed and unsigned locators.
       signing_opts = {
-        key: Rails.configuration.blob_signing_key,
+        key: Rails.configuration.Collections["BlobSigningKey"],
         api_token: api_token(:active),
       }
       signed_locators = locators.collect do |x|
@@ -622,7 +622,7 @@ EOS
     # TODO(twp): in phase 4, all locators will need to be signed, so
     # this test should break and will need to be rewritten. Issue #2755.
     signing_opts = {
-      key: Rails.configuration.blob_signing_key,
+      key: Rails.configuration.Collections["BlobSigningKey"],
       api_token: api_token(:active),
       ttl: 3600   # 1 hour
     }
@@ -653,7 +653,7 @@ EOS
   test "create fails with invalid signature" do
     authorize_with :active
     signing_opts = {
-      key: Rails.configuration.blob_signing_key,
+      key: Rails.configuration.Collections["BlobSigningKey"],
       api_token: api_token(:active),
     }
 
@@ -683,7 +683,7 @@ EOS
   test "create fails with uuid of signed manifest" do
     authorize_with :active
     signing_opts = {
-      key: Rails.configuration.blob_signing_key,
+      key: Rails.configuration.Collections["BlobSigningKey"],
       api_token: api_token(:active),
     }
 
@@ -755,7 +755,7 @@ EOS
       ea10d51bcf88862dbcc36eb292017dfd+45)
 
     signing_opts = {
-      key: Rails.configuration.blob_signing_key,
+      key: Rails.configuration.Collections["BlobSigningKey"],
       api_token: api_token(:active),
     }
 
@@ -903,7 +903,7 @@ EOS
 
   [1, 5, nil].each do |ask|
     test "Set replication_desired=#{ask.inspect}" do
-      Rails.configuration.default_collection_replication = 2
+      Rails.configuration.Collections["DefaultReplication"] = 2
       authorize_with :active
       put :update, params: {
         id: collections(:replication_undesired_unconfirmed).uuid,
@@ -939,6 +939,89 @@ EOS
     assert_equal 'value_1', json_response['properties']['property_1']
   end
 
+  [
+    [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", 1, 34],
+    [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:30:foo.txt 0:30:foo1.txt 0:30:foo2.txt 0:30:foo3.txt 0:30:foo4.txt\n", 5, 184],
+    [". d41d8cd98f00b204e9800998ecf8427e 0:0:.\n", 0, 0]
+  ].each do |manifest, count, size|
+    test "create collection with valid manifest #{manifest} and expect file stats" do
+      authorize_with :active
+      post :create, params: {
+        collection: {
+          manifest_text: manifest
+        }
+      }
+      assert_response 200
+      assert_equal count, json_response['file_count']
+      assert_equal size, json_response['file_size_total']
+    end
+  end
+
+  test "update collection manifest and expect new file stats" do
+    authorize_with :active
+    post :update, params: {
+      id: collections(:collection_owned_by_active_with_file_stats).uuid,
+      collection: {
+        manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n"
+      }
+    }
+    assert_response 200
+    assert_equal 1, json_response['file_count']
+    assert_equal 34, json_response['file_size_total']
+  end
+
+  [
+    ['file_count', 1],
+    ['file_size_total', 34]
+  ].each do |attribute, val|
+    test "create collection with #{attribute} and expect overwrite" do
+      authorize_with :active
+      post :create, params: {
+        collection: {
+          manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n",
+          "#{attribute}": 10
+        }
+      }
+      assert_response 200
+      assert_equal val, json_response[attribute]
+    end
+  end
+
+  [
+    ['file_count', 1],
+    ['file_size_total', 3]
+  ].each do |attribute, val|
+    test "update collection with #{attribute} and expect ignore" do
+      authorize_with :active
+      post :update, params: {
+        id: collections(:collection_owned_by_active_with_file_stats).uuid,
+        collection: {
+          "#{attribute}": 10
+        }
+      }
+      assert_response 200
+      assert_equal val, json_response[attribute]
+    end
+  end
+
+  [
+    ['file_count', 1],
+    ['file_size_total', 34]
+  ].each do |attribute, val|
+    test "update collection with #{attribute} and manifest and expect manifest values" do
+      authorize_with :active
+      post :update, params: {
+        id: collections(:collection_owned_by_active_with_file_stats).uuid,
+        collection: {
+          manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n",
+          "#{attribute}": 10
+        }
+      }
+      assert_response 200
+      assert_equal val, json_response[attribute]
+    end
+  end
+
   [
     ". 0:0:foo.txt",
     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
@@ -1093,7 +1176,7 @@ EOS
     assert_response 200
     c = Collection.find_by_uuid(uuid)
     assert_operator c.trash_at, :<, db_current_time
-    assert_equal c.delete_at, c.trash_at + Rails.configuration.blob_signature_ttl
+    assert_equal c.delete_at, c.trash_at + Rails.configuration.Collections["BlobSigningTTL"]
   end
 
   test 'delete long-trashed collection immediately using http DELETE verb' do
@@ -1125,7 +1208,7 @@ EOS
       assert_response 200
       c = Collection.find_by_uuid(uuid)
       assert_operator c.trash_at, :<, db_current_time
-      assert_operator c.delete_at, :>=, time_before_trashing + Rails.configuration.default_trash_lifetime
+      assert_operator c.delete_at, :>=, time_before_trashing + Rails.configuration.Collections["DefaultTrashLifetime"]
     end
   end
 
@@ -1290,8 +1373,8 @@ EOS
   end
 
   test "update collection with versioning enabled" do
-    Rails.configuration.collection_versioning = true
-    Rails.configuration.preserve_version_if_idle = 1 # 1 second
+    Rails.configuration.Collections["CollectionVersioning"] = true
+    Rails.configuration.Collections["PreserveVersionIfIdle"] = 1 # 1 second
 
     col = collections(:collection_owned_by_active)
     assert_equal 2, col.version
@@ -1300,7 +1383,7 @@ EOS
     token = api_client_authorizations(:active).v2token
     signed = Blob.sign_locator(
       'acbd18db4cc2f85cedef654fccc4a4d8+3',
-      key: Rails.configuration.blob_signing_key,
+      key: Rails.configuration.Collections["BlobSigningKey"],
       api_token: token)
     authorize_with_token token
     put :update, params: {