1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require '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'
28 add_filter 'initializers/omniauth'
31 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
35 require File.expand_path('../../config/environment', __FILE__)
36 require 'rails/test_help'
38 require 'mocha/minitest'
40 module ArvadosTestSupport
42 Oj.strict_load response.body
45 def api_token(api_client_auth_name)
46 api_client_authorizations(api_client_auth_name).token
49 def auth(api_client_auth_name)
50 {'HTTP_AUTHORIZATION' => "Bearer #{api_token(api_client_auth_name)}"}
54 return lambda { model.errors.full_messages.inspect }
58 class ActiveSupport::TestCase
59 include FactoryBot::Syntax::Methods
62 include ArvadosTestSupport
63 include CurrentApiClient
66 Thread.current[:api_client_ip_address] = nil
67 Thread.current[:api_client_authorization] = nil
68 Thread.current[:api_client_uuid] = nil
69 Thread.current[:api_client] = nil
70 Thread.current[:token] = nil
71 Thread.current[:user] = nil
75 def assert_equal(expect, *args)
83 def assert_not_allowed
84 # Provide a block that calls a Rails boolean "true or false" success value,
85 # like model.save or model.destroy. This method will test that it either
86 # returns false, or raises a Permission Denied exception.
89 rescue ArvadosModel::PermissionDeniedError
93 def add_permission_link from_who, to_what, perm_type
95 Link.create!(tail_uuid: from_who.uuid,
96 head_uuid: to_what.uuid,
97 link_class: 'permission',
102 def restore_configuration
103 # Restore configuration settings changed during tests
104 ConfigLoader.copy_into_config $arvados_config, Rails.configuration
105 ConfigLoader.copy_into_config $remaining_config, Rails.configuration
108 def set_user_from_auth(auth_name)
109 client_auth = api_client_authorizations(auth_name)
110 Thread.current[:api_client_authorization] = client_auth
111 Thread.current[:api_client] = client_auth.api_client
112 Thread.current[:user] = client_auth.user
113 Thread.current[:token] = client_auth.token
117 self.request.headers["Accept"] = "text/json"
120 def authorize_with api_client_auth_name
121 authorize_with_token api_client_authorizations(api_client_auth_name).token
124 def authorize_with_token token
126 t = t.token if t.respond_to? :token
127 ArvadosApiToken.new.call("rack.input" => "",
128 "HTTP_AUTHORIZATION" => "Bearer #{t}")
131 def salt_token(fixture:, remote:)
132 auth = api_client_authorizations(fixture)
134 token = auth.api_token
135 hmac = OpenSSL::HMAC.hexdigest('sha1', token, remote)
136 return "v2/#{uuid}/#{hmac}"
139 def self.skip_slow_tests?
140 !(ENV['RAILS_TEST_SHORT'] || '').empty?
143 def self.skip(*args, &block)
146 def self.slow_test(name, &block)
147 test(name, &block) unless skip_slow_tests?
151 class ActionController::TestCase
154 self.request.headers['Accept'] = 'application/json'
155 self.request.headers['Content-Type'] = 'application/json'
158 def check_counter action
160 if @test_counter == 2
161 assert_equal 1, 2, "Multiple actions in functional test"
165 [:get, :post, :put, :patch, :delete].each do |method|
166 define_method method do |action, *args|
168 # After Rails 5.0 upgrade, some params don't get properly serialized.
169 # One case are filters: [['attr', 'op', 'val']] become [['attr'], ['op'], ['val']]
170 # if not passed upstream as a JSON string.
171 if args[0].is_a?(Hash) && args[0][:params].is_a?(Hash)
172 args[0][:params].each do |key, _|
173 next if key == :exclude_script_versions # Job Reuse tests
174 # Keys could be: :filters, :where, etc
175 if [Array, Hash].include?(args[0][:params][key].class)
176 args[0][:params][key] = SafeJSON.dump(args[0][:params][key])
191 @test_case.shutdown()
196 def self.startup; end
197 def self.shutdown; end
200 class ActionDispatch::IntegrationTest
202 Thread.current[:api_client_ip_address] = nil
203 Thread.current[:api_client_authorization] = nil
204 Thread.current[:api_client_uuid] = nil
205 Thread.current[:api_client] = nil
206 Thread.current[:token] = nil
207 Thread.current[:user] = nil
211 # Ensure permissions are computed from the test fixtures.