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