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__)
24 require 'rails/test_help'
26 require 'mocha/mini_test'
28 module ArvadosTestSupport
30 Oj.strict_load response.body
33 def api_token(api_client_auth_name)
34 api_client_authorizations(api_client_auth_name).api_token
37 def auth(api_client_auth_name)
38 {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
42 return lambda { model.errors.full_messages.inspect }
46 class ActiveSupport::TestCase
47 include FactoryGirl::Syntax::Methods
50 include ArvadosTestSupport
51 include CurrentApiClient
54 Rails.logger.warn "\n\n#{'=' * 70}\n#{self.class}\##{method_name}\n#{'-' * 70}\n\n"
58 Thread.current[:api_client_ip_address] = nil
59 Thread.current[:api_client_authorization] = nil
60 Thread.current[:api_client_uuid] = nil
61 Thread.current[:api_client] = nil
62 Thread.current[:user] = nil
66 def assert_not_allowed
67 # Provide a block that calls a Rails boolean "true or false" success value,
68 # like model.save or model.destroy. This method will test that it either
69 # returns false, or raises a Permission Denied exception.
72 rescue ArvadosModel::PermissionDeniedError
76 def add_permission_link from_who, to_what, perm_type
78 Link.create!(tail_uuid: from_who.uuid,
79 head_uuid: to_what.uuid,
80 link_class: 'permission',
85 def restore_configuration
86 # Restore configuration settings changed during tests
87 $application_config.each do |k,v|
89 Rails.configuration.send (k + '='), v
94 def set_user_from_auth(auth_name)
95 client_auth = api_client_authorizations(auth_name)
96 Thread.current[:api_client_authorization] = client_auth
97 Thread.current[:api_client] = client_auth.api_client
98 Thread.current[:user] = client_auth.user
102 self.request.headers["Accept"] = "text/json"
105 def authorize_with api_client_auth_name
106 authorize_with_token api_client_authorizations(api_client_auth_name).api_token
109 def authorize_with_token token
111 t = t.api_token if t.respond_to? :api_token
112 ArvadosApiToken.new.call("rack.input" => "",
113 "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
116 def self.skip_slow_tests?
117 !(ENV['RAILS_TEST_SHORT'] || '').empty?
120 def self.skip(*args, &block)
123 def self.slow_test(name, &block)
124 define_method(name, block) unless skip_slow_tests?
128 class ActionController::TestCase
133 def check_counter action
135 if @test_counter == 2
136 assert_equal 1, 2, "Multiple actions in functional test"
140 [:get, :post, :put, :patch, :delete].each do |method|
141 define_method method do |action, *args|
154 @test_case.shutdown()
159 def self.startup; end
160 def self.shutdown; end
163 class ActionDispatch::IntegrationTest
165 Thread.current[:api_client_ip_address] = nil
166 Thread.current[:api_client_authorization] = nil
167 Thread.current[:api_client_uuid] = nil
168 Thread.current[:api_client] = nil
169 Thread.current[:user] = nil
173 # Ensure permissions are computed from the test fixtures.
174 User.invalidate_permissions_cache