1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 ENV["RAILS_ENV"] = "test"
6 unless ENV["NO_COVERAGE_TEST"]
8 verbose_orig = $VERBOSE
12 require 'simplecov-rcov'
14 $VERBOSE = verbose_orig
16 class SimpleCov::Formatter::MergedFormatter
18 SimpleCov::Formatter::HTMLFormatter.new.format(result)
19 SimpleCov::Formatter::RcovFormatter.new.format(result)
22 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
25 add_filter 'initializers/secret_token'
26 add_filter 'initializers/omniauth'
29 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
33 require File.expand_path('../../config/environment', __FILE__)
34 require 'rails/test_help'
36 require 'mocha/minitest'
38 module ArvadosTestSupport
40 Oj.strict_load response.body
43 def api_token(api_client_auth_name)
44 api_client_authorizations(api_client_auth_name).token
47 def auth(api_client_auth_name)
48 {'HTTP_AUTHORIZATION' => "Bearer #{api_token(api_client_auth_name)}"}
52 return lambda { model.errors.full_messages.inspect }
56 class ActiveSupport::TestCase
57 include FactoryBot::Syntax::Methods
60 include ArvadosTestSupport
61 include CurrentApiClient
64 Thread.current[:api_client_ip_address] = nil
65 Thread.current[:api_client_authorization] = nil
66 Thread.current[:api_client_uuid] = nil
67 Thread.current[:api_client] = nil
68 Thread.current[:token] = nil
69 Thread.current[:user] = nil
73 def assert_equal(expect, *args)
81 def assert_not_allowed
82 # Provide a block that calls a Rails boolean "true or false" success value,
83 # like model.save or model.destroy. This method will test that it either
84 # returns false, or raises a Permission Denied exception.
87 rescue ArvadosModel::PermissionDeniedError
91 def add_permission_link from_who, to_what, perm_type
93 Link.create!(tail_uuid: from_who.uuid,
94 head_uuid: to_what.uuid,
95 link_class: 'permission',
100 def restore_configuration
101 # Restore configuration settings changed during tests
102 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
103 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
106 def set_user_from_auth(auth_name)
107 client_auth = api_client_authorizations(auth_name)
108 Thread.current[:api_client_authorization] = client_auth
109 Thread.current[:api_client] = client_auth.api_client
110 Thread.current[:user] = client_auth.user
111 Thread.current[:token] = client_auth.token
115 self.request.headers["Accept"] = "text/json"
118 def authorize_with api_client_auth_name
119 authorize_with_token api_client_authorizations(api_client_auth_name).token
122 def authorize_with_token token
124 t = t.token if t.respond_to? :token
125 ArvadosApiToken.new.call("rack.input" => "",
126 "HTTP_AUTHORIZATION" => "Bearer #{t}")
129 def salt_token(fixture:, remote:)
130 auth = api_client_authorizations(fixture)
132 token = auth.api_token
133 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
134 return "v2/#{uuid}/#{hmac}"
137 def self.skip_slow_tests?
138 !(ENV['RAILS_TEST_SHORT'] || '').empty?
141 def self.skip(*args, &block)
144 def self.slow_test(name, &block)
145 test(name, &block) unless skip_slow_tests?
149 class ActionController::TestCase
152 self.request.headers['Accept'] = 'application/json'
153 self.request.headers['Content-Type'] = 'application/json'
156 def check_counter action
158 if @test_counter == 2
159 assert_equal 1, 2, "Multiple actions in functional test"
163 [:get, :post, :put, :patch, :delete].each do |method|
164 define_method method do |action, *args|
166 # After Rails 5.0 upgrade, some params don't get properly serialized.
167 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
168 # if not passed upstream as a JSON string.
169 if args[0].is_a?(Hash) && args[0][:params].is_a?(Hash)
170 args[0][:params].each do |key, _|
171 next if key == :exclude_script_versions # Job Reuse tests
172 # Keys could be: :filters, :where, etc
173 if [Array, Hash].include?(args[0][:params][key].class)
174 args[0][:params][key] = SafeJSON.dump(args[0][:params][key])
189 @test_case.shutdown()
194 def self.startup; end
195 def self.shutdown; end
198 class ActionDispatch::IntegrationTest
200 Thread.current[:api_client_ip_address] = nil
201 Thread.current[:api_client_authorization] = nil
202 Thread.current[:api_client_uuid] = nil
203 Thread.current[:api_client] = nil
204 Thread.current[:token] = nil
205 Thread.current[:user] = nil
209 # Ensure permissions are computed from the test fixtures.
210 User.invalidate_permissions_cache