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