a2a1545cee93d7ffcdd5a63073881abc960caa90
[arvados.git] / services / api / test / integration / errors_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class ErrorsTest < ActionDispatch::IntegrationTest
8   fixtures :api_client_authorizations
9
10   %w(/arvados/v1/shoes /arvados/shoes /shoes /nodes /users).each do |path|
11     test "non-existent route #{path}" do
12       get path, params: {:format => :json}, headers: auth(:active)
13       assert_nil assigns(:objects)
14       assert_nil assigns(:object)
15       assert_not_nil json_response['errors']
16       assert_response 404
17       assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
18     end
19   end
20
21   n=0
22   Rails.application.routes.routes.each do |route|
23     test "route #{n += 1} '#{route.path.spec.to_s}' is not an accident" do
24       # Generally, new routes should appear under /arvados/v1/. If
25       # they appear elsewhere, that might have been caused by default
26       # rails generator behavior that we don't want.
27       assert_match(/^\/(|\*a|arvados\/v1\/.*|auth\/.*|login|logout|database\/reset|discovery\/.*|static\/.*|sys\/trash_sweep|themes\/.*|assets|_health\/.*)(\(\.:format\))?$/,
28                    route.path.spec.to_s,
29                    "Unexpected new route: #{route.path.spec}")
30     end
31   end
32
33   test "X-Request-Id header" do
34     get "/", headers: auth(:spectator)
35     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
36   end
37
38   test "X-Request-Id header on non-existant object URL" do
39     get "/arvados/v1/container_requests/invalid",
40       params: {:format => :json}, headers: auth(:active)
41     assert_response 404
42     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
43   end
44
45   # The response header is the one that gets logged, so this test also
46   # ensures we log the ID supplied in the request, if any.
47   test "X-Request-Id given by client" do
48     get "/", headers: auth(:spectator).merge({'X-Request-Id': 'abcdefG'})
49     assert_equal 'abcdefG', response.headers['X-Request-Id']
50   end
51
52   test "X-Request-Id given by client is ignored if too long" do
53     authorize_with :spectator
54     long_reqId = 'abcdefG' * 1000
55     get "/", headers: auth(:spectator).merge({'X-Request-Id': long_reqId})
56     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
57   end
58 end