11809: Prune collection cache asynchronously.
[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_equal(expect, *args)
70     if expect.nil?
71       assert_nil(*args)
72     else
73       super
74     end
75   end
76
77   def assert_not_allowed
78     # Provide a block that calls a Rails boolean "true or false" success value,
79     # like model.save or model.destroy.  This method will test that it either
80     # returns false, or raises a Permission Denied exception.
81     begin
82       refute(yield)
83     rescue ArvadosModel::PermissionDeniedError
84     end
85   end
86
87   def add_permission_link from_who, to_what, perm_type
88     act_as_system_user do
89       Link.create!(tail_uuid: from_who.uuid,
90                    head_uuid: to_what.uuid,
91                    link_class: 'permission',
92                    name: perm_type)
93     end
94   end
95
96   def restore_configuration
97     # Restore configuration settings changed during tests
98     $application_config.each do |k,v|
99       if k.match(/^[^.]*$/)
100         Rails.configuration.send (k + '='), v
101       end
102     end
103   end
104
105   def set_user_from_auth(auth_name)
106     client_auth = api_client_authorizations(auth_name)
107     Thread.current[:api_client_authorization] = client_auth
108     Thread.current[:api_client] = client_auth.api_client
109     Thread.current[:user] = client_auth.user
110   end
111
112   def expect_json
113     self.request.headers["Accept"] = "text/json"
114   end
115
116   def authorize_with api_client_auth_name
117     authorize_with_token api_client_authorizations(api_client_auth_name).api_token
118   end
119
120   def authorize_with_token token
121     t = token
122     t = t.api_token if t.respond_to? :api_token
123     ArvadosApiToken.new.call("rack.input" => "",
124                              "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
125   end
126
127   def self.skip_slow_tests?
128     !(ENV['RAILS_TEST_SHORT'] || '').empty?
129   end
130
131   def self.skip(*args, &block)
132   end
133
134   def self.slow_test(name, &block)
135     define_method(name, block) unless skip_slow_tests?
136   end
137 end
138
139 class ActionController::TestCase
140   setup do
141     @test_counter = 0
142   end
143
144   def check_counter action
145     @test_counter += 1
146     if @test_counter == 2
147       assert_equal 1, 2, "Multiple actions in functional test"
148     end
149   end
150
151   [:get, :post, :put, :patch, :delete].each do |method|
152     define_method method do |action, *args|
153       check_counter action
154       super action, *args
155     end
156   end
157
158   def self.suite
159     s = super
160     def s.run(*args)
161       @test_case.startup()
162       begin
163         super
164       ensure
165         @test_case.shutdown()
166       end
167     end
168     s
169   end
170   def self.startup; end
171   def self.shutdown; end
172 end
173
174 class ActionDispatch::IntegrationTest
175   teardown do
176     Thread.current[:api_client_ip_address] = nil
177     Thread.current[:api_client_authorization] = nil
178     Thread.current[:api_client_uuid] = nil
179     Thread.current[:api_client] = nil
180     Thread.current[:user] = nil
181   end
182 end
183
184 # Ensure permissions are computed from the test fixtures.
185 User.invalidate_permissions_cache