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