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)}"}
53 return lambda { model.errors.full_messages.inspect }
57 class ActiveSupport::TestCase
58 include FactoryBot::Syntax::Methods
61 include ArvadosTestSupport
62 include CurrentApiClient
65 Thread.current[:api_client_ip_address] = nil
66 Thread.current[:api_client_authorization] = nil
67 Thread.current[:api_client_uuid] = nil
68 Thread.current[:api_client] = nil
69 Thread.current[:token] = nil
70 Thread.current[:user] = nil
75 # Confirm that any changed configuration doesn't include non-symbol keys
76 $arvados_config.keys.each do |conf_name|
77 conf = Rails.configuration.send(conf_name)
78 confirm_keys_as_symbols(conf, conf_name) if conf.respond_to?('keys')
82 def assert_equal(expect, *args)
90 def assert_not_allowed
91 # Provide a block that calls a Rails boolean "true or false" success value,
92 # like model.save or model.destroy. This method will test that it either
93 # returns false, or raises a Permission Denied exception.
96 rescue ArvadosModel::PermissionDeniedError
100 def add_permission_link from_who, to_what, perm_type
101 act_as_system_user do
102 Link.create!(tail_uuid: from_who.uuid,
103 head_uuid: to_what.uuid,
104 link_class: 'permission',
109 def confirm_keys_as_symbols(conf, conf_name)
110 assert(conf.is_a?(ActiveSupport::OrderedOptions), "#{conf_name} should be an OrderedOptions object")
111 conf.keys.each do |k|
112 assert(k.is_a?(Symbol), "Key '#{k}' on section '#{conf_name}' should be a Symbol")
113 confirm_keys_as_symbols(conf[k], "#{conf_name}.#{k}") if conf[k].respond_to?('keys')
117 def restore_configuration
118 # Restore configuration settings changed during tests
119 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
120 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
123 def set_user_from_auth(auth_name)
124 client_auth = api_client_authorizations(auth_name)
125 client_auth.user.forget_cached_group_perms
126 Thread.current[:api_client_authorization] = client_auth
127 Thread.current[:api_client] = client_auth.api_client
128 Thread.current[:user] = client_auth.user
129 Thread.current[:token] = client_auth.token
133 self.request.headers["Accept"] = "text/json"
136 def authorize_with api_client_auth_name
137 authorize_with_token api_client_authorizations(api_client_auth_name).token
140 def authorize_with_token token
142 t = t.token if t.respond_to? :token
143 ArvadosApiToken.new.call("rack.input" => "",
144 "HTTP_AUTHORIZATION" => "Bearer #{t}")
147 def salt_token(fixture:, remote:)
148 auth = api_client_authorizations(fixture)
150 token = auth.api_token
151 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
152 return "v2/#{uuid}/#{hmac}"
155 def self.skip_slow_tests?
156 !(ENV['RAILS_TEST_SHORT'] || '').empty?
159 def self.skip(*args, &block)
162 def self.slow_test(name, &block)
163 test(name, &block) unless skip_slow_tests?
167 class ActionController::TestCase
170 self.request.headers['Accept'] = 'application/json'
171 self.request.headers['Content-Type'] = 'application/json'
174 def check_counter action
176 if @test_counter == 2
177 assert_equal 1, 2, "Multiple actions in functional test"
181 [:get, :post, :put, :patch, :delete].each do |method|
182 define_method method do |action, **kwargs|
184 # After Rails 5.0 upgrade, some params don't get properly serialized.
185 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
186 # if not passed upstream as a JSON string.
187 if kwargs[:params].is_a?(Hash)
188 kwargs[:params].each do |key, _|
189 next if key == :exclude_script_versions # Job Reuse tests
190 # Keys could be: :filters, :where, etc
191 if [Array, Hash].include?(kwargs[:params][key].class)
192 kwargs[:params][key] = SafeJSON.dump(kwargs[:params][key])
196 super action, **kwargs
207 @test_case.shutdown()
212 def self.startup; end
213 def self.shutdown; end
216 class ActionDispatch::IntegrationTest
218 Thread.current[:api_client_ip_address] = nil
219 Thread.current[:api_client_authorization] = nil
220 Thread.current[:api_client_uuid] = nil
221 Thread.current[:api_client] = nil
222 Thread.current[:token] = nil
223 Thread.current[:user] = nil
227 # Ensure permissions are computed from the test fixtures.