1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
4 verbose_orig = $VERBOSE
8 require 'simplecov-rcov'
10 $VERBOSE = verbose_orig
12 class SimpleCov::Formatter::MergedFormatter
14 SimpleCov::Formatter::HTMLFormatter.new.format(result)
15 SimpleCov::Formatter::RcovFormatter.new.format(result)
18 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
21 add_filter 'initializers/secret_token'
22 add_filter 'initializers/omniauth'
25 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
29 require File.expand_path('../../config/environment', __FILE__)
30 require 'rails/test_help'
32 require 'mocha/mini_test'
34 module ArvadosTestSupport
36 Oj.strict_load response.body
39 def api_token(api_client_auth_name)
40 api_client_authorizations(api_client_auth_name).api_token
43 def auth(api_client_auth_name)
44 {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
48 return lambda { model.errors.full_messages.inspect }
52 class ActiveSupport::TestCase
53 include FactoryGirl::Syntax::Methods
56 include ArvadosTestSupport
57 include CurrentApiClient
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
66 User.invalidate_permissions_cache
69 def assert_equal(expect, *args)
77 def assert_not_allowed
78 # Provide a block that calls a Rails boolean "true or false" success value,
79 # like model.save or model.destroy. This method will test that it either
80 # returns false, or raises a Permission Denied exception.
83 rescue ArvadosModel::PermissionDeniedError
87 def add_permission_link from_who, to_what, perm_type
89 Link.create!(tail_uuid: from_who.uuid,
90 head_uuid: to_what.uuid,
91 link_class: 'permission',
96 def restore_configuration
97 # Restore configuration settings changed during tests
98 $application_config.each do |k,v|
100 Rails.configuration.send (k + '='), v
105 def set_user_from_auth(auth_name)
106 client_auth = api_client_authorizations(auth_name)
107 Thread.current[:api_client_authorization] = client_auth
108 Thread.current[:api_client] = client_auth.api_client
109 Thread.current[:user] = client_auth.user
113 self.request.headers["Accept"] = "text/json"
116 def authorize_with api_client_auth_name
117 authorize_with_token api_client_authorizations(api_client_auth_name).api_token
120 def authorize_with_token token
122 t = t.api_token if t.respond_to? :api_token
123 ArvadosApiToken.new.call("rack.input" => "",
124 "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
127 def self.skip_slow_tests?
128 !(ENV['RAILS_TEST_SHORT'] || '').empty?
131 def self.skip(*args, &block)
134 def self.slow_test(name, &block)
135 define_method(name, block) unless skip_slow_tests?
139 class ActionController::TestCase
144 def check_counter action
146 if @test_counter == 2
147 assert_equal 1, 2, "Multiple actions in functional test"
151 [:get, :post, :put, :patch, :delete].each do |method|
152 define_method method do |action, *args|
165 @test_case.shutdown()
170 def self.startup; end
171 def self.shutdown; end
174 class ActionDispatch::IntegrationTest
176 Thread.current[:api_client_ip_address] = nil
177 Thread.current[:api_client_authorization] = nil
178 Thread.current[:api_client_uuid] = nil
179 Thread.current[:api_client] = nil
180 Thread.current[:user] = nil
184 # Ensure permissions are computed from the test fixtures.
185 User.invalidate_permissions_cache