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 end with "hash" or "uuid" as of June 2024/Arvados 3.0.
55 # It's okay if this list isn't complete, 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",
62 "current_version_uuid",
70 "modified_by_client_uuid",
71 "modified_by_user_uuid",
80 "redirect_to_user_uuid",
81 "requesting_container_uuid",
91 return lambda { model.errors.full_messages.inspect }
95 class ActiveSupport::TestCase
96 include FactoryBot::Syntax::Methods
99 include ArvadosTestSupport
100 include CurrentApiClient
103 Thread.current[:api_client_ip_address] = nil
104 Thread.current[:api_client_authorization] = nil
105 Thread.current[:token] = nil
106 Thread.current[:user] = nil
107 restore_configuration
111 # Confirm that any changed configuration doesn't include non-symbol keys
112 $arvados_config.keys.each do |conf_name|
113 conf = Rails.configuration.send(conf_name)
114 confirm_keys_as_symbols(conf, conf_name) if conf.respond_to?('keys')
118 def assert_equal(expect, *args)
126 def assert_not_allowed
127 # Provide a block that calls a Rails boolean "true or false" success value,
128 # like model.save or model.destroy. This method will test that it either
129 # returns false, or raises a Permission Denied exception.
132 rescue ArvadosModel::PermissionDeniedError
136 def add_permission_link from_who, to_what, perm_type
137 act_as_system_user do
138 Link.create!(tail_uuid: from_who.uuid,
139 head_uuid: to_what.uuid,
140 link_class: 'permission',
145 def confirm_keys_as_symbols(conf, conf_name)
146 assert(conf.is_a?(ActiveSupport::OrderedOptions), "#{conf_name} should be an OrderedOptions object")
147 conf.keys.each do |k|
148 assert(k.is_a?(Symbol), "Key '#{k}' on section '#{conf_name}' should be a Symbol")
149 confirm_keys_as_symbols(conf[k], "#{conf_name}.#{k}") if conf[k].respond_to?('keys')
153 def restore_configuration
154 # Restore configuration settings changed during tests
155 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
156 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
159 def set_user_from_auth(auth_name)
160 client_auth = api_client_authorizations(auth_name)
161 client_auth.user.forget_cached_group_perms
162 Thread.current[:api_client_authorization] = client_auth
163 Thread.current[:user] = client_auth.user
164 Thread.current[:token] = client_auth.token
168 self.request.headers["Accept"] = "text/json"
171 def authorize_with api_client_auth_name
172 authorize_with_token api_client_authorizations(api_client_auth_name).token
175 def authorize_with_token token
177 t = t.token if t.respond_to? :token
178 ArvadosApiToken.new.call("rack.input" => "",
179 "HTTP_AUTHORIZATION" => "Bearer #{t}")
182 def salt_token(fixture:, remote:)
183 auth = api_client_authorizations(fixture)
185 token = auth.api_token
186 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
187 return "v2/#{uuid}/#{hmac}"
190 def self.skip_slow_tests?
191 !(ENV['RAILS_TEST_SHORT'] || '').empty?
194 def self.skip(*args, &block)
197 def self.slow_test(name, &block)
198 test(name, &block) unless skip_slow_tests?
202 class ActionController::TestCase
205 self.request.headers['Accept'] = 'application/json'
206 self.request.headers['Content-Type'] = 'application/json'
209 def check_counter action
211 if @test_counter == 2
212 assert_equal 1, 2, "Multiple actions in functional test"
216 [:get, :post, :put, :patch, :delete].each do |method|
217 define_method method do |action, **kwargs|
219 # After Rails 5.0 upgrade, some params don't get properly serialized.
220 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
221 # if not passed upstream as a JSON string.
222 if kwargs[:params].is_a?(Hash)
223 kwargs[:params].each do |key, _|
224 next if key == :exclude_script_versions # Job Reuse tests
225 # Keys could be: :filters, :where, etc
226 if [Array, Hash].include?(kwargs[:params][key].class)
227 kwargs[:params][key] = SafeJSON.dump(kwargs[:params][key])
231 super action, **kwargs
242 @test_case.shutdown()
247 def self.startup; end
248 def self.shutdown; end
251 class ActionDispatch::IntegrationTest
253 Thread.current[:api_client_ip_address] = nil
254 Thread.current[:api_client_authorization] = nil
255 Thread.current[:token] = nil
256 Thread.current[:user] = nil
260 # Ensure permissions are computed from the test fixtures.