Merge branch '10858-build-packages-improvements'
[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 require 'mocha'
26
27 module ArvadosTestSupport
28   def json_response
29     Oj.strict_load response.body
30   end
31
32   def api_token(api_client_auth_name)
33     api_client_authorizations(api_client_auth_name).api_token
34   end
35
36   def auth(api_client_auth_name)
37     {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
38   end
39
40   def show_errors model
41     return lambda { model.errors.full_messages.inspect }
42   end
43 end
44
45 class ActiveSupport::TestCase
46   include FactoryGirl::Syntax::Methods
47   fixtures :all
48
49   include ArvadosTestSupport
50   include CurrentApiClient
51
52   setup do
53     Rails.logger.warn "\n\n#{'=' * 70}\n#{self.class}\##{method_name}\n#{'-' * 70}\n\n"
54   end
55
56   teardown do
57     Thread.current[:api_client_ip_address] = nil
58     Thread.current[:api_client_authorization] = nil
59     Thread.current[:api_client_uuid] = nil
60     Thread.current[:api_client] = nil
61     Thread.current[:user] = nil
62     restore_configuration
63   end
64
65   def assert_not_allowed
66     # Provide a block that calls a Rails boolean "true or false" success value,
67     # like model.save or model.destroy.  This method will test that it either
68     # returns false, or raises a Permission Denied exception.
69     begin
70       refute(yield)
71     rescue ArvadosModel::PermissionDeniedError
72     end
73   end
74
75   def add_permission_link from_who, to_what, perm_type
76     act_as_system_user do
77       Link.create!(tail_uuid: from_who.uuid,
78                    head_uuid: to_what.uuid,
79                    link_class: 'permission',
80                    name: perm_type)
81     end
82   end
83
84   def restore_configuration
85     # Restore configuration settings changed during tests
86     $application_config.each do |k,v|
87       if k.match(/^[^.]*$/)
88         Rails.configuration.send (k + '='), v
89       end
90     end
91   end
92
93   def set_user_from_auth(auth_name)
94     client_auth = api_client_authorizations(auth_name)
95     Thread.current[:api_client_authorization] = client_auth
96     Thread.current[:api_client] = client_auth.api_client
97     Thread.current[:user] = client_auth.user
98   end
99
100   def expect_json
101     self.request.headers["Accept"] = "text/json"
102   end
103
104   def authorize_with api_client_auth_name
105     authorize_with_token api_client_authorizations(api_client_auth_name).api_token
106   end
107
108   def authorize_with_token token
109     t = token
110     t = t.api_token if t.respond_to? :api_token
111     ArvadosApiToken.new.call("rack.input" => "",
112                              "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
113   end
114
115   def self.skip_slow_tests?
116     !(ENV['RAILS_TEST_SHORT'] || '').empty?
117   end
118
119   def self.skip(*args, &block)
120   end
121
122   def self.slow_test(name, &block)
123     define_method(name, block) unless skip_slow_tests?
124   end
125
126   alias_method :skip, :omit
127 end
128
129 class ActionController::TestCase
130   setup do
131     @test_counter = 0
132   end
133
134   def check_counter action
135     @test_counter += 1
136     if @test_counter == 2
137       assert_equal 1, 2, "Multiple actions in functional test"
138     end
139   end
140
141   [:get, :post, :put, :patch, :delete].each do |method|
142     define_method method do |action, *args|
143       check_counter action
144       super action, *args
145     end
146   end
147
148   def self.suite
149     s = super
150     def s.run(*args)
151       @test_case.startup()
152       begin
153         super
154       ensure
155         @test_case.shutdown()
156       end
157     end
158     s
159   end
160   def self.startup; end
161   def self.shutdown; end
162 end
163
164 class ActionDispatch::IntegrationTest
165   teardown do
166     Thread.current[:api_client_ip_address] = nil
167     Thread.current[:api_client_authorization] = nil
168     Thread.current[:api_client_uuid] = nil
169     Thread.current[:api_client] = nil
170     Thread.current[:user] = nil
171   end
172 end
173
174 # Ensure permissions are computed from the test fixtures.
175 User.invalidate_permissions_cache