7709: Suppress Ruby warnings while loading gems.
[arvados.git] / services / api / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
3   begin
4     verbose_orig = $VERBOSE
5     begin
6       $VERBOSE = nil
7       require 'simplecov'
8       require 'simplecov-rcov'
9     ensure
10       $VERBOSE = verbose_orig
11     end
12     class SimpleCov::Formatter::MergedFormatter
13       def format(result)
14         SimpleCov::Formatter::HTMLFormatter.new.format(result)
15         SimpleCov::Formatter::RcovFormatter.new.format(result)
16       end
17     end
18     SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
19     SimpleCov.start do
20       add_filter '/test/'
21       add_filter 'initializers/secret_token'
22       add_filter 'initializers/omniauth'
23     end
24   rescue Exception => e
25     $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
26   end
27 end
28
29 require File.expand_path('../../config/environment', __FILE__)
30 require 'rails/test_help'
31 require 'mocha'
32 require 'mocha/mini_test'
33
34 module ArvadosTestSupport
35   def json_response
36     Oj.strict_load response.body
37   end
38
39   def api_token(api_client_auth_name)
40     api_client_authorizations(api_client_auth_name).api_token
41   end
42
43   def auth(api_client_auth_name)
44     {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
45   end
46
47   def show_errors model
48     return lambda { model.errors.full_messages.inspect }
49   end
50 end
51
52 class ActiveSupport::TestCase
53   include FactoryGirl::Syntax::Methods
54   fixtures :all
55
56   include ArvadosTestSupport
57   include CurrentApiClient
58
59   teardown do
60     Thread.current[:api_client_ip_address] = nil
61     Thread.current[:api_client_authorization] = nil
62     Thread.current[:api_client_uuid] = nil
63     Thread.current[:api_client] = nil
64     Thread.current[:user] = nil
65     restore_configuration
66     User.invalidate_permissions_cache
67   end
68
69   def assert_not_allowed
70     # Provide a block that calls a Rails boolean "true or false" success value,
71     # like model.save or model.destroy.  This method will test that it either
72     # returns false, or raises a Permission Denied exception.
73     begin
74       refute(yield)
75     rescue ArvadosModel::PermissionDeniedError
76     end
77   end
78
79   def add_permission_link from_who, to_what, perm_type
80     act_as_system_user do
81       Link.create!(tail_uuid: from_who.uuid,
82                    head_uuid: to_what.uuid,
83                    link_class: 'permission',
84                    name: perm_type)
85     end
86   end
87
88   def restore_configuration
89     # Restore configuration settings changed during tests
90     $application_config.each do |k,v|
91       if k.match(/^[^.]*$/)
92         Rails.configuration.send (k + '='), v
93       end
94     end
95   end
96
97   def set_user_from_auth(auth_name)
98     client_auth = api_client_authorizations(auth_name)
99     Thread.current[:api_client_authorization] = client_auth
100     Thread.current[:api_client] = client_auth.api_client
101     Thread.current[:user] = client_auth.user
102   end
103
104   def expect_json
105     self.request.headers["Accept"] = "text/json"
106   end
107
108   def authorize_with api_client_auth_name
109     authorize_with_token api_client_authorizations(api_client_auth_name).api_token
110   end
111
112   def authorize_with_token token
113     t = token
114     t = t.api_token if t.respond_to? :api_token
115     ArvadosApiToken.new.call("rack.input" => "",
116                              "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
117   end
118
119   def self.skip_slow_tests?
120     !(ENV['RAILS_TEST_SHORT'] || '').empty?
121   end
122
123   def self.skip(*args, &block)
124   end
125
126   def self.slow_test(name, &block)
127     define_method(name, block) unless skip_slow_tests?
128   end
129 end
130
131 class ActionController::TestCase
132   setup do
133     @test_counter = 0
134   end
135
136   def check_counter action
137     @test_counter += 1
138     if @test_counter == 2
139       assert_equal 1, 2, "Multiple actions in functional test"
140     end
141   end
142
143   [:get, :post, :put, :patch, :delete].each do |method|
144     define_method method do |action, *args|
145       check_counter action
146       super action, *args
147     end
148   end
149
150   def self.suite
151     s = super
152     def s.run(*args)
153       @test_case.startup()
154       begin
155         super
156       ensure
157         @test_case.shutdown()
158       end
159     end
160     s
161   end
162   def self.startup; end
163   def self.shutdown; end
164 end
165
166 class ActionDispatch::IntegrationTest
167   teardown do
168     Thread.current[:api_client_ip_address] = nil
169     Thread.current[:api_client_authorization] = nil
170     Thread.current[:api_client_uuid] = nil
171     Thread.current[:api_client] = nil
172     Thread.current[:user] = nil
173   end
174 end
175
176 # Ensure permissions are computed from the test fixtures.
177 User.invalidate_permissions_cache