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[:api_client_uuid] = nil
106 Thread.current[:api_client] = nil
107 Thread.current[:token] = nil
108 Thread.current[:user] = nil
109 restore_configuration
113 # Confirm that any changed configuration doesn't include non-symbol keys
114 $arvados_config.keys.each do |conf_name|
115 conf = Rails.configuration.send(conf_name)
116 confirm_keys_as_symbols(conf, conf_name) if conf.respond_to?('keys')
120 def assert_equal(expect, *args)
128 def assert_not_allowed
129 # Provide a block that calls a Rails boolean "true or false" success value,
130 # like model.save or model.destroy. This method will test that it either
131 # returns false, or raises a Permission Denied exception.
134 rescue ArvadosModel::PermissionDeniedError
138 def add_permission_link from_who, to_what, perm_type
139 act_as_system_user do
140 Link.create!(tail_uuid: from_who.uuid,
141 head_uuid: to_what.uuid,
142 link_class: 'permission',
147 def confirm_keys_as_symbols(conf, conf_name)
148 assert(conf.is_a?(ActiveSupport::OrderedOptions), "#{conf_name} should be an OrderedOptions object")
149 conf.keys.each do |k|
150 assert(k.is_a?(Symbol), "Key '#{k}' on section '#{conf_name}' should be a Symbol")
151 confirm_keys_as_symbols(conf[k], "#{conf_name}.#{k}") if conf[k].respond_to?('keys')
155 def restore_configuration
156 # Restore configuration settings changed during tests
157 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
158 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
161 def set_user_from_auth(auth_name)
162 client_auth = api_client_authorizations(auth_name)
163 client_auth.user.forget_cached_group_perms
164 Thread.current[:api_client_authorization] = client_auth
165 Thread.current[:api_client] = client_auth.api_client
166 Thread.current[:user] = client_auth.user
167 Thread.current[:token] = client_auth.token
171 self.request.headers["Accept"] = "text/json"
174 def authorize_with api_client_auth_name
175 authorize_with_token api_client_authorizations(api_client_auth_name).token
178 def authorize_with_token token
180 t = t.token if t.respond_to? :token
181 ArvadosApiToken.new.call("rack.input" => "",
182 "HTTP_AUTHORIZATION" => "Bearer #{t}")
185 def salt_token(fixture:, remote:)
186 auth = api_client_authorizations(fixture)
188 token = auth.api_token
189 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
190 return "v2/#{uuid}/#{hmac}"
193 def self.skip_slow_tests?
194 !(ENV['RAILS_TEST_SHORT'] || '').empty?
197 def self.skip(*args, &block)
200 def self.slow_test(name, &block)
201 test(name, &block) unless skip_slow_tests?
205 class ActionController::TestCase
208 self.request.headers['Accept'] = 'application/json'
209 self.request.headers['Content-Type'] = 'application/json'
212 def check_counter action
214 if @test_counter == 2
215 assert_equal 1, 2, "Multiple actions in functional test"
219 [:get, :post, :put, :patch, :delete].each do |method|
220 define_method method do |action, **kwargs|
222 # After Rails 5.0 upgrade, some params don't get properly serialized.
223 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
224 # if not passed upstream as a JSON string.
225 if kwargs[:params].is_a?(Hash)
226 kwargs[:params].each do |key, _|
227 next if key == :exclude_script_versions # Job Reuse tests
228 # Keys could be: :filters, :where, etc
229 if [Array, Hash].include?(kwargs[:params][key].class)
230 kwargs[:params][key] = SafeJSON.dump(kwargs[:params][key])
234 super action, **kwargs
245 @test_case.shutdown()
250 def self.startup; end
251 def self.shutdown; end
254 class ActionDispatch::IntegrationTest
256 Thread.current[:api_client_ip_address] = nil
257 Thread.current[:api_client_authorization] = nil
258 Thread.current[:api_client_uuid] = nil
259 Thread.current[:api_client] = nil
260 Thread.current[:token] = nil
261 Thread.current[:user] = nil
265 # Ensure permissions are computed from the test fixtures.