Merge branch '8886-async-permission-update' refs #8886
[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/mini_test'
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 end
40
41 class ActiveSupport::TestCase
42   include FactoryGirl::Syntax::Methods
43   fixtures :all
44
45   include ArvadosTestSupport
46
47   setup do
48     Rails.logger.warn "\n\n#{'=' * 70}\n#{self.class}\##{method_name}\n#{'-' * 70}\n\n"
49   end
50
51   teardown do
52     Thread.current[:api_client_ip_address] = nil
53     Thread.current[:api_client_authorization] = nil
54     Thread.current[:api_client_uuid] = nil
55     Thread.current[:api_client] = nil
56     Thread.current[:user] = nil
57     restore_configuration
58   end
59
60   def assert_not_allowed
61     # Provide a block that calls a Rails boolean "true or false" success value,
62     # like model.save or model.destroy.  This method will test that it either
63     # returns false, or raises a Permission Denied exception.
64     begin
65       refute(yield)
66     rescue ArvadosModel::PermissionDeniedError
67     end
68   end
69
70   def add_permission_link from_who, to_what, perm_type
71     act_as_system_user do
72       Link.create!(tail_uuid: from_who.uuid,
73                    head_uuid: to_what.uuid,
74                    link_class: 'permission',
75                    name: perm_type)
76     end
77   end
78
79   def restore_configuration
80     # Restore configuration settings changed during tests
81     $application_config.each do |k,v|
82       if k.match /^[^.]*$/
83         Rails.configuration.send (k + '='), v
84       end
85     end
86   end
87
88   def set_user_from_auth(auth_name)
89     client_auth = api_client_authorizations(auth_name)
90     Thread.current[:api_client_authorization] = client_auth
91     Thread.current[:api_client] = client_auth.api_client
92     Thread.current[:user] = client_auth.user
93   end
94
95   def expect_json
96     self.request.headers["Accept"] = "text/json"
97   end
98
99   def authorize_with api_client_auth_name
100     authorize_with_token api_client_authorizations(api_client_auth_name).api_token
101   end
102
103   def authorize_with_token token
104     t = token
105     t = t.api_token if t.respond_to? :api_token
106     ArvadosApiToken.new.call("rack.input" => "",
107                              "HTTP_AUTHORIZATION" => "OAuth2 #{t}")
108   end
109
110   def slow_test
111     skip "RAILS_TEST_SHORT is set" unless (ENV['RAILS_TEST_SHORT'] || '').empty?
112   end
113 end
114
115 class ActionController::TestCase
116   setup do
117     @test_counter = 0
118   end
119
120   def check_counter action
121     @test_counter += 1
122     if @test_counter == 2
123       assert_equal 1, 2, "Multiple actions in functional test"
124     end
125   end
126
127   [:get, :post, :put, :patch, :delete].each do |method|
128     define_method method do |action, *args|
129       check_counter action
130       super action, *args
131     end
132   end
133 end
134
135 class ActionDispatch::IntegrationTest
136   teardown do
137     Thread.current[:api_client_ip_address] = nil
138     Thread.current[:api_client_authorization] = nil
139     Thread.current[:api_client_uuid] = nil
140     Thread.current[:api_client] = nil
141     Thread.current[:user] = nil
142   end
143 end
144
145 # Ensure permissions are computed from the test fixtures.
146 User.invalidate_permissions_cache