Merge branch 'master' into 3889-functional-testing
[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   include FactoryGirl::Syntax::Methods
42   fixtures :all
43
44   include ArvadosTestSupport
45
46   teardown do
47     Thread.current[:api_client_ip_address] = nil
48     Thread.current[:api_client_authorization] = nil
49     Thread.current[:api_client_uuid] = nil
50     Thread.current[:api_client] = nil
51     Thread.current[:user] = nil
52     # Restore configuration settings changed during tests
53     $application_config.each do |k,v|
54       if k.match /^[^.]*$/
55         Rails.configuration.send (k + '='), v
56       end
57     end
58   end
59
60   def set_user_from_auth(auth_name)
61     client_auth = api_client_authorizations(auth_name)
62     Thread.current[:api_client_authorization] = client_auth
63     Thread.current[:api_client] = client_auth.api_client
64     Thread.current[:user] = client_auth.user
65   end
66
67   def expect_json
68     self.request.headers["Accept"] = "text/json"
69   end
70
71   def authorize_with api_client_auth_name
72     authorize_with_token api_client_authorizations(api_client_auth_name).api_token
73   end
74
75   def authorize_with_token token
76     t = token
77     t = t.api_token if t.respond_to? :api_token
78     ArvadosApiToken.new.call("rack.input" => "",
79                              "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
80   end
81 end
82
83 class ActionController::TestCase
84   def reset_counter
85     $counter = 0
86   end
87
88   def check_counter action
89     $counter += 1
90     if $counter > 1
91       raise "Too many actions on a single instance of ActionController::TestCase #{action}"
92     end
93   end
94
95   alias original_run_callbacks run_callbacks
96   def run_callbacks(kind, &block)
97     reset_counter
98     original_run_callbacks(kind, &block)
99   end
100
101   alias original_get get
102   def get(action, *args)
103     check_counter action
104     original_get(action, *args)
105   end
106
107   alias original_post post
108   def post(action, *args)
109     check_counter action
110     original_post(action, *args)
111   end
112
113   alias original_put put
114   def put(action, *args)
115     check_counter action
116     original_put(action, *args)
117   end
118
119   alias original_delete delete
120   def delete(action, *args)
121     check_counter action
122     original_delete(action, *args)
123   end
124 end
125
126 class ActionDispatch::IntegrationTest
127   teardown do
128     Thread.current[:api_client_ip_address] = nil
129     Thread.current[:api_client_authorization] = nil
130     Thread.current[:api_client_uuid] = nil
131     Thread.current[:api_client] = nil
132     Thread.current[:user] = nil
133   end
134 end
135
136 # Ensure permissions are computed from the test fixtures.
137 User.invalidate_permissions_cache