Generate test coverage reports with SimpleCov.
[arvados.git] / services / api / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
3   require 'simplecov'
4   SimpleCov.start
5 end
6
7 require File.expand_path('../../config/environment', __FILE__)
8 require 'rails/test_help'
9
10 module ArvadosTestSupport
11   def json_response
12     ActiveSupport::JSON.decode @response.body
13   end
14
15   def api_token(api_client_auth_name)
16     api_client_authorizations(api_client_auth_name).api_token
17   end
18
19   def auth(api_client_auth_name)
20     {'HTTP_AUTHORIZATION' => "OAuth2 #{api_token(api_client_auth_name)}"}
21   end
22 end
23
24 class ActiveSupport::TestCase
25   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
26   #
27   # Note: You'll currently still have to declare fixtures explicitly in integration tests
28   # -- they do not yet inherit this setting
29   fixtures :all
30
31   include ArvadosTestSupport
32
33   teardown do
34     Thread.current[:api_client_ip_address] = nil
35     Thread.current[:api_client_authorization] = nil
36     Thread.current[:api_client_uuid] = nil
37     Thread.current[:api_client] = nil
38     Thread.current[:user] = nil
39   end
40
41   def expect_json
42     self.request.headers["Accept"] = "text/json"
43   end
44
45   def authorize_with(api_client_auth_name)
46     ArvadosApiToken.new.call ({"rack.input" => "", "HTTP_AUTHORIZATION" => "OAuth2 #{api_client_authorizations(api_client_auth_name).api_token}"})
47   end
48 end
49
50 class ActionDispatch::IntegrationTest
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   end
58 end
59
60 # Ensure permissions are computed from the test fixtures.
61 User.invalidate_permissions_cache