1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require_relative '../lib/update_permissions'
7 ENV["RAILS_ENV"] = "test"
8 unless ENV["NO_COVERAGE_TEST"]
10 verbose_orig = $VERBOSE
14 require 'simplecov-rcov'
16 $VERBOSE = verbose_orig
18 class SimpleCov::Formatter::MergedFormatter
20 SimpleCov::Formatter::HTMLFormatter.new.format(result)
21 SimpleCov::Formatter::RcovFormatter.new.format(result)
24 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
27 add_filter 'initializers/secret_token'
30 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
34 require File.expand_path('../../config/environment', __FILE__)
35 require 'rails/test_help'
37 require 'mocha/minitest'
39 module ArvadosTestSupport
41 Oj.strict_load response.body
44 def api_token(api_client_auth_name)
45 api_client_authorizations(api_client_auth_name).token
48 def auth(api_client_auth_name)
49 {'HTTP_AUTHORIZATION' => "Bearer #{api_token(api_client_auth_name)}"}
52 def full_text_excluded_columns
54 # All the columns that contain a UUID or PDH as of June 2024/Arvados 3.0.
55 # It's okay if this list gets out-of-date, it just needs to be complete
56 # enough to test that full text indexes exclude the right columns.
57 "authorized_user_uuid",
59 "cancelled_by_client_uuid",
60 "cancelled_by_user_uuid",
63 "current_version_uuid",
71 "modified_by_client_uuid",
72 "modified_by_user_uuid",
81 "redirect_to_user_uuid",
82 "requesting_container_uuid",
92 return lambda { model.errors.full_messages.inspect }
96 class ActiveSupport::TestCase
97 include FactoryBot::Syntax::Methods
100 include ArvadosTestSupport
101 include CurrentApiClient
104 Thread.current[:api_client_ip_address] = nil
105 Thread.current[:api_client_authorization] = nil
106 Thread.current[:token] = nil
107 Thread.current[:user] = nil
108 restore_configuration
112 # Confirm that any changed configuration doesn't include non-symbol keys
113 $arvados_config.keys.each do |conf_name|
114 conf = Rails.configuration.send(conf_name)
115 confirm_keys_as_symbols(conf, conf_name) if conf.respond_to?('keys')
119 def assert_equal(expect, *args)
127 def assert_not_allowed
128 # Provide a block that calls a Rails boolean "true or false" success value,
129 # like model.save or model.destroy. This method will test that it either
130 # returns false, or raises a Permission Denied exception.
133 rescue ArvadosModel::PermissionDeniedError
137 def add_permission_link from_who, to_what, perm_type
138 act_as_system_user do
139 Link.create!(tail_uuid: from_who.uuid,
140 head_uuid: to_what.uuid,
141 link_class: 'permission',
146 def confirm_keys_as_symbols(conf, conf_name)
147 assert(conf.is_a?(ActiveSupport::OrderedOptions), "#{conf_name} should be an OrderedOptions object")
148 conf.keys.each do |k|
149 assert(k.is_a?(Symbol), "Key '#{k}' on section '#{conf_name}' should be a Symbol")
150 confirm_keys_as_symbols(conf[k], "#{conf_name}.#{k}") if conf[k].respond_to?('keys')
154 def restore_configuration
155 # Restore configuration settings changed during tests
156 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
157 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
160 def set_user_from_auth(auth_name)
161 client_auth = api_client_authorizations(auth_name)
162 client_auth.user.forget_cached_group_perms
163 Thread.current[:api_client_authorization] = client_auth
164 Thread.current[:user] = client_auth.user
165 Thread.current[:token] = client_auth.token
169 self.request.headers["Accept"] = "text/json"
172 def authorize_with api_client_auth_name
173 authorize_with_token api_client_authorizations(api_client_auth_name).token
176 def authorize_with_token token
178 t = t.token if t.respond_to? :token
179 ArvadosApiToken.new.call("rack.input" => "",
180 "HTTP_AUTHORIZATION" => "Bearer #{t}")
183 def salt_token(fixture:, remote:)
184 auth = api_client_authorizations(fixture)
186 token = auth.api_token
187 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
188 return "v2/#{uuid}/#{hmac}"
191 def self.skip_slow_tests?
192 !(ENV['RAILS_TEST_SHORT'] || '').empty?
195 def self.skip(*args, &block)
198 def self.slow_test(name, &block)
199 test(name, &block) unless skip_slow_tests?
203 class ActionController::TestCase
206 self.request.headers['Accept'] = 'application/json'
207 self.request.headers['Content-Type'] = 'application/json'
210 def check_counter action
212 if @test_counter == 2
213 assert_equal 1, 2, "Multiple actions in functional test"
217 [:get, :post, :put, :patch, :delete].each do |method|
218 define_method method do |action, **kwargs|
220 # After Rails 5.0 upgrade, some params don't get properly serialized.
221 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
222 # if not passed upstream as a JSON string.
223 if kwargs[:params].is_a?(Hash)
224 kwargs[:params].each do |key, _|
225 next if key == :exclude_script_versions # Job Reuse tests
226 # Keys could be: :filters, :where, etc
227 if [Array, Hash].include?(kwargs[:params][key].class)
228 kwargs[:params][key] = SafeJSON.dump(kwargs[:params][key])
232 super action, **kwargs
243 @test_case.shutdown()
248 def self.startup; end
249 def self.shutdown; end
252 class ActionDispatch::IntegrationTest
254 Thread.current[:api_client_ip_address] = nil
255 Thread.current[:api_client_authorization] = nil
256 Thread.current[:token] = nil
257 Thread.current[:user] = nil
261 # Ensure permissions are computed from the test fixtures.