2 require 'capybara/rails'
3 require 'capybara/poltergeist'
7 $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
9 class ActionDispatch::IntegrationTest
10 # Make the Capybara DSL available in all integration tests
13 def self.api_fixture(name)
14 # Returns the data structure from the named API server test fixture.
15 path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml")
16 YAML.load(IO.read(path))
19 @@API_AUTHS = api_fixture('api_client_authorizations')
21 def page_with_token(token, path='/')
22 # Generate a page path with an embedded API token.
23 # Typical usage: visit page_with_token('token_name', page)
24 # The token can be specified by the name of an api_client_authorizations
25 # fixture, or passed as a raw string.
26 api_token = ((@@API_AUTHS.include? token) ?
27 @@API_AUTHS[token]['api_token'] : token)
28 sep = (path.include? '?') ? '&' : '?'
29 q_string = URI.encode_www_form('api_token' => api_token)
30 "#{path}#{sep}#{q_string}"
34 class IntegrationTestRunner < MiniTest::Unit
35 # Launch the API server in test mode, with appropriate environment.
36 @@APIENV = {'RAILS_ENV' => 'test'}
37 ['GEM_HOME', 'GEM_PATH', 'PATH'].each { |key| @@APIENV[key] = ENV[key] }
40 if not system(@@APIENV, *cmd, {unsetenv_others: true})
41 raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
46 Capybara.javascript_driver = :poltergeist
47 server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir|
48 _system('bundle', 'exec', 'rake', 'db:test:load')
49 _system('bundle', 'exec', 'rake', 'db:fixtures:load')
50 _system('bundle', 'exec', 'rails', 'server', '-d')
51 `cat tmp/pids/server.pid`.to_i
56 Process.kill('TERM', server_pid)
61 MiniTest::Unit.runner = IntegrationTestRunner.new