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