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