Merge branch 'master' into 6859-fix-invalid-manifests
[arvados.git] / services / api / lib / current_api_client.rb
index 404de4f4c3dd89e3178dd3e2a0a6f6cabd04d06a..2e78612fc2d8b0ea3883cbb964d73c3e443e9c21 100644 (file)
@@ -60,7 +60,7 @@ module CurrentApiClient
         Thread.current[:user] = User.new(is_admin: true,
                                          is_active: true,
                                          uuid: system_user_uuid)
-        User.where('uuid=?', system_user_uuid).
+        User.where(uuid: system_user_uuid).
           first_or_create!(is_active: true,
                            is_admin: true,
                            email: 'root',
@@ -79,6 +79,7 @@ module CurrentApiClient
           Group.where(uuid: system_group_uuid).
             first_or_create!(name: "System group",
                              description: "System group") do |g|
+            g.save!
             User.all.collect(&:uuid).each do |user_uuid|
               Link.create!(link_class: 'permission',
                            name: 'can_manage',
@@ -145,21 +146,34 @@ module CurrentApiClient
     end
   end
 
+  def anonymous_group_read_permission
+    $anonymous_group_read_permission =
+        check_cache $anonymous_group_read_permission do
+      act_as_system_user do
+        Link.where(tail_uuid: all_users_group.uuid,
+                   head_uuid: anonymous_group.uuid,
+                   link_class: "permission",
+                   name: "can_read").first_or_create!
+      end
+    end
+  end
+
   def anonymous_user
     $anonymous_user = check_cache $anonymous_user do
       act_as_system_user do
-        anon = User.where('uuid=?', anonymous_user_uuid).
+        User.where(uuid: anonymous_user_uuid).
           first_or_create!(is_active: false,
                            is_admin: false,
                            email: 'anonymous',
                            first_name: 'Anonymous',
-                           last_name: '')
-        Link.where(tail_uuid: anonymous_user_uuid,
-                   head_uuid: anonymous_group_uuid,
-                   link_class: 'permission',
-                   name: 'can_read').
-          first_or_create!
-        anon
+                           last_name: '') do |u|
+          u.save!
+          Link.where(tail_uuid: anonymous_user_uuid,
+                     head_uuid: anonymous_group.uuid,
+                     link_class: 'permission',
+                     name: 'can_read').
+            first_or_create!
+        end
       end
     end
   end
@@ -172,7 +186,7 @@ module CurrentApiClient
     $empty_collection = check_cache $empty_collection do
       act_as_system_user do
         ActiveRecord::Base.transaction do
-          $empty_collection = Collection.
+          Collection.
             where(portable_data_hash: empty_collection_uuid).
             first_or_create!(manifest_text: '', owner_uuid: anonymous_group.uuid)
         end
@@ -185,9 +199,24 @@ module CurrentApiClient
   # If the given value is nil, or the cache has been cleared since it
   # was set, yield. Otherwise, return the given value.
   def check_cache value
-    Rails.cache.fetch "CurrentApiClient.$globals" do
-      value = nil
-      true
+    if not Rails.env.test? and
+        ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
+        not File.owned? ActionController::Base.cache_store.cache_path
+      # If we don't own the cache dir, we're probably
+      # crunch-dispatch. Whoever we are, using this cache is likely to
+      # either fail or screw up the cache for someone else. So we'll
+      # just assume the $globals are OK to live forever.
+      #
+      # The reason for making the globals expire with the cache in the
+      # first place is to avoid leaking state between test cases: in
+      # production, we don't expect the database seeds to ever go away
+      # even when the cache is cleared, so there's no particular
+      # reason to expire our global variables.
+    else
+      Rails.cache.fetch "CurrentApiClient.$globals" do
+        value = nil
+        true
+      end
     end
     return value unless value.nil?
     yield