4058: If a portable data hash correpsonds to a single collection, automatically
[arvados.git] / apps / workbench / test / test_helper.rb
index e833f970c02d211905cd504a4d7a25a13a25a016..1a07e43be1fb51c5fe3c9a7796121ea684a9407c 100644 (file)
@@ -1,4 +1,5 @@
-ENV["RAILS_ENV"] = "test"
+ENV["RAILS_ENV"] = "test" if (ENV["RAILS_ENV"] != "diagnostics")
+
 unless ENV["NO_COVERAGE_TEST"]
   begin
     require 'simplecov'
@@ -21,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
@@ -34,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
 
@@ -48,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
@@ -56,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
 
@@ -70,6 +86,9 @@ class ActiveSupport::TestCase
       arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
     }
   end
+  def json_response
+    Oj.load(@response.body)
+  end
 end
 
 class ApiServerForTests
@@ -127,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',
@@ -148,4 +167,26 @@ class ApiServerForTests
   end
 end
 
-ApiServerForTests.run
+class ActionController::TestCase
+  setup do
+    @counter = 0
+  end
+
+  def check_counter action
+    @counter += 1
+    if @counter == 2
+      assert_equal 1, 2, "Multiple actions in functional test"
+    end
+  end
+
+  [:get, :post, :put, :patch, :delete].each do |method|
+    define_method method do |action, *args|
+      check_counter action
+      super action, *args
+    end
+  end
+end
+
+if ENV["RAILS_ENV"].eql? 'test'
+  ApiServerForTests.run
+end