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