1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
5 require 'simplecov-rcov'
6 class SimpleCov::Formatter::MergedFormatter
8 SimpleCov::Formatter::HTMLFormatter.new.format(result)
9 SimpleCov::Formatter::RcovFormatter.new.format(result)
12 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
15 add_filter 'initializers/secret_token'
16 add_filter 'initializers/omniauth'
19 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
23 require File.expand_path('../../config/environment', __FILE__)
25 require 'rails/test_help'
26 require 'minitest-rails'
27 require 'minitest/autorun'
30 module ArvadosTestSupport
32 Oj.strict_load response.body
35 def api_token(api_client_auth_name)
36 api_client_authorizations(api_client_auth_name).api_token
39 def auth(api_client_auth_name)
40 {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
44 return lambda { model.errors.full_messages.inspect }
48 class ActiveSupport::TestCase
49 include FactoryGirl::Syntax::Methods
52 include ArvadosTestSupport
53 include CurrentApiClient
56 Rails.logger.warn "\n\n#{'=' * 70}\n#{self.class}\##{method_name}\n#{'-' * 70}\n\n"
60 Thread.current[:api_client_ip_address] = nil
61 Thread.current[:api_client_authorization] = nil
62 Thread.current[:api_client_uuid] = nil
63 Thread.current[:api_client] = nil
64 Thread.current[:user] = nil
68 def assert_not_allowed
69 # Provide a block that calls a Rails boolean "true or false" success value,
70 # like model.save or model.destroy. This method will test that it either
71 # returns false, or raises a Permission Denied exception.
74 rescue ArvadosModel::PermissionDeniedError
78 def add_permission_link from_who, to_what, perm_type
80 Link.create!(tail_uuid: from_who.uuid,
81 head_uuid: to_what.uuid,
82 link_class: 'permission',
87 def restore_configuration
88 # Restore configuration settings changed during tests
89 $application_config.each do |k,v|
91 Rails.configuration.send (k + '='), v
96 def set_user_from_auth(auth_name)
97 client_auth = api_client_authorizations(auth_name)
98 Thread.current[:api_client_authorization] = client_auth
99 Thread.current[:api_client] = client_auth.api_client
100 Thread.current[:user] = client_auth.user
104 self.request.headers["Accept"] = "text/json"
107 def authorize_with api_client_auth_name
108 authorize_with_token api_client_authorizations(api_client_auth_name).api_token
111 def authorize_with_token token
113 t = t.api_token if t.respond_to? :api_token
114 ArvadosApiToken.new.call("rack.input" => "",
115 "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
118 def self.skip_slow_tests?
119 !(ENV['RAILS_TEST_SHORT'] || '').empty?
122 def self.skip(*args, &block)
125 def self.slow_test(name, &block)
126 define_method(name, block) unless skip_slow_tests?
129 alias_method :skip, :omit
132 class ActionController::TestCase
137 def check_counter action
139 if @test_counter == 2
140 assert_equal 1, 2, "Multiple actions in functional test"
144 [:get, :post, :put, :patch, :delete].each do |method|
145 define_method method do |action, *args|
158 @test_case.shutdown()
163 def self.startup; end
164 def self.shutdown; end
167 class ActionDispatch::IntegrationTest
169 Thread.current[:api_client_ip_address] = nil
170 Thread.current[:api_client_authorization] = nil
171 Thread.current[:api_client_uuid] = nil
172 Thread.current[:api_client] = nil
173 Thread.current[:user] = nil
177 # Ensure permissions are computed from the test fixtures.
178 User.invalidate_permissions_cache