4058: If a portable data hash correpsonds to a single collection, automatically
[arvados.git] / apps / workbench / test / test_helper.rb
index cb3be104ae3bcaa32b54c709c6f86c5f79865624..1a07e43be1fb51c5fe3c9a7796121ea684a9407c 100644 (file)
@@ -22,6 +22,7 @@ end
 
 require File.expand_path('../../config/environment', __FILE__)
 require 'rails/test_help'
+require 'mocha/mini_test'
 
 class ActiveSupport::TestCase
   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
@@ -35,10 +36,18 @@ class ActiveSupport::TestCase
     Thread.current[:arvados_api_token] = auth['api_token']
   end
 
-  def teardown
+  teardown do
     Thread.current[:arvados_api_token] = nil
+    Thread.current[:user] = nil
     Thread.current[:reader_tokens] = nil
-    super
+    # Diagnostics suite doesn't run a server, so there's no cache to clear.
+    Rails.cache.clear unless (Rails.env == "diagnostics")
+    # Restore configuration settings changed during tests
+    $application_config.each do |k,v|
+      if k.match /^[^.]*$/
+        Rails.configuration.send (k + '='), v
+      end
+    end
   end
 end
 
@@ -49,7 +58,7 @@ module ApiFixtureLoader
 
   module ClassMethods
     @@api_fixtures = {}
-    def api_fixture(name)
+    def api_fixture(name, *keys)
       # Returns the data structure from the named API server test fixture.
       @@api_fixtures[name] ||= \
       begin
@@ -57,10 +66,16 @@ module ApiFixtureLoader
                          'test', 'fixtures', "#{name}.yml")
         YAML.load(IO.read(path))
       end
+      keys.inject(@@api_fixtures[name]) { |hash, key| hash[key] }
     end
   end
-  def api_fixture name
-    self.class.api_fixture name
+  def api_fixture(name, *keys)
+    self.class.api_fixture(name, *keys)
+  end
+
+  def find_fixture(object_class, name)
+    object_class.find(api_fixture(object_class.to_s.pluralize.underscore,
+                                  name, "uuid"))
   end
 end
 
@@ -131,7 +146,7 @@ class ApiServerForTests
       make_ssl_cert
       _system('bundle', 'exec', 'rake', 'db:test:load')
       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
-      _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3001',
+      _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3000',
               '--pid-file', SERVER_PID_PATH,
               '--ssl',
               '--ssl-certificate', 'self-signed.pem',
@@ -153,45 +168,22 @@ class ApiServerForTests
 end
 
 class ActionController::TestCase
-  def reset_counter
-    $counter = 0
+  setup do
+    @counter = 0
   end
 
   def check_counter action
-    $counter += 1
-    if $counter > 1
-      raise "Too many actions on a single instance of ActionController::TestCase #{action}"
+    @counter += 1
+    if @counter == 2
+      assert_equal 1, 2, "Multiple actions in functional test"
     end
   end
 
-  alias original_run_callbacks run_callbacks
-  def run_callbacks(kind, &block)
-    reset_counter
-    original_run_callbacks(kind, &block)
-  end
-
-  alias original_get get
-  def get(action, *args)
-    check_counter action
-    original_get(action, *args)
-  end
-
-  alias original_post post
-  def post(action, *args)
-    check_counter action
-    original_post(action, *args)
-  end
-
-  alias original_put put
-  def put(action, *args)
-    check_counter action
-    original_put(action, *args)
-  end
-
-  alias original_delete delete
-  def delete(action, *args)
-    check_counter action
-    original_delete(action, *args)
+  [:get, :post, :put, :patch, :delete].each do |method|
+    define_method method do |action, *args|
+      check_counter action
+      super action, *args
+    end
   end
 end