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_not_allowed
70 # Provide a block that calls a Rails boolean "true or false" success value,
71 # like model.save or model.destroy. This method will test that it either
72 # returns false, or raises a Permission Denied exception.
75 rescue ArvadosModel::PermissionDeniedError
79 def add_permission_link from_who, to_what, perm_type
81 Link.create!(tail_uuid: from_who.uuid,
82 head_uuid: to_what.uuid,
83 link_class: 'permission',
88 def restore_configuration
89 # Restore configuration settings changed during tests
90 $application_config.each do |k,v|
92 Rails.configuration.send (k + '='), v
97 def set_user_from_auth(auth_name)
98 client_auth = api_client_authorizations(auth_name)
99 Thread.current[:api_client_authorization] = client_auth
100 Thread.current[:api_client] = client_auth.api_client
101 Thread.current[:user] = client_auth.user
105 self.request.headers["Accept"] = "text/json"
108 def authorize_with api_client_auth_name
109 authorize_with_token api_client_authorizations(api_client_auth_name).api_token
112 def authorize_with_token token
114 t = t.api_token if t.respond_to? :api_token
115 ArvadosApiToken.new.call("rack.input" => "",
116 "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
119 def self.skip_slow_tests?
120 !(ENV['RAILS_TEST_SHORT'] || '').empty?
123 def self.skip(*args, &block)
126 def self.slow_test(name, &block)
127 define_method(name, block) unless skip_slow_tests?
131 class ActionController::TestCase
136 def check_counter action
138 if @test_counter == 2
139 assert_equal 1, 2, "Multiple actions in functional test"
143 [:get, :post, :put, :patch, :delete].each do |method|
144 define_method method do |action, *args|
157 @test_case.shutdown()
162 def self.startup; end
163 def self.shutdown; end
166 class ActionDispatch::IntegrationTest
168 Thread.current[:api_client_ip_address] = nil
169 Thread.current[:api_client_authorization] = nil
170 Thread.current[:api_client_uuid] = nil
171 Thread.current[:api_client] = nil
172 Thread.current[:user] = nil
176 # Ensure permissions are computed from the test fixtures.
177 User.invalidate_permissions_cache