Merge branch '3448-keep-put-timestamps'
[arvados.git] / services / api / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
3   begin
4     require 'simplecov'
5     require 'simplecov-rcov'
6     class SimpleCov::Formatter::MergedFormatter
7       def format(result)
8         SimpleCov::Formatter::HTMLFormatter.new.format(result)
9         SimpleCov::Formatter::RcovFormatter.new.format(result)
10       end
11     end
12     SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
13     SimpleCov.start do
14       add_filter '/test/'
15       add_filter 'initializers/secret_token'
16       add_filter 'initializers/omniauth'
17     end
18   rescue Exception => e
19     $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
20   end
21 end
22
23 require File.expand_path('../../config/environment', __FILE__)
24 require 'rails/test_help'
25
26 module ArvadosTestSupport
27   def json_response
28     ActiveSupport::JSON.decode @response.body
29   end
30
31   def api_token(api_client_auth_name)
32     api_client_authorizations(api_client_auth_name).api_token
33   end
34
35   def auth(api_client_auth_name)
36     {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
37   end
38 end
39
40 class ActiveSupport::TestCase
41   fixtures :all
42
43   include ArvadosTestSupport
44
45   teardown do
46     Thread.current[:api_client_ip_address] = nil
47     Thread.current[:api_client_authorization] = nil
48     Thread.current[:api_client_uuid] = nil
49     Thread.current[:api_client] = nil
50     Thread.current[:user] = nil
51     # Restore configuration settings changed during tests
52     $application_config.each do |k,v|
53       if k.match /^[^.]*$/
54         Rails.configuration.send (k + '='), v
55       end
56     end
57   end
58
59   def set_user_from_auth(auth_name)
60     client_auth = api_client_authorizations(auth_name)
61     Thread.current[:api_client_authorization] = client_auth
62     Thread.current[:api_client] = client_auth.api_client
63     Thread.current[:user] = client_auth.user
64   end
65
66   def expect_json
67     self.request.headers["Accept"] = "text/json"
68   end
69
70   def authorize_with(api_client_auth_name)
71     ArvadosApiToken.new.call ({"rack.input" => "", "HTTP_AUTHORIZATION" => "OAuth2 #{api_client_authorizations(api_client_auth_name).api_token}"})
72   end
73 end
74
75 class ActionDispatch::IntegrationTest
76   teardown do
77     Thread.current[:api_client_ip_address] = nil
78     Thread.current[:api_client_authorization] = nil
79     Thread.current[:api_client_uuid] = nil
80     Thread.current[:api_client] = nil
81     Thread.current[:user] = nil
82   end
83 end
84
85 # Ensure permissions are computed from the test fixtures.
86 User.invalidate_permissions_cache