1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class ApplicationControllerTest < ActionController::TestCase
8 BAD_UUID = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
15 # These tests are meant to check behavior in ApplicationController.
16 # We instantiate a small concrete controller for convenience.
17 @controller = Arvados::V1::SpecimensController.new
18 @start_stamp = now_timestamp
22 token = json_response['error_token']
24 token_time = token.split('+', 2).first.to_i
25 assert_operator(token_time, :>=, @start_stamp, "error token too old")
26 assert_operator(token_time, :<=, now_timestamp, "error token too new")
27 json_response['errors'].each do |err|
28 assert_match(/req-[a-z0-9]{20}/, err, "X-Request-Id value missing on error message")
32 def check_404(errmsg="Path not found")
34 json_response['errors'].each do |err|
35 assert(err.include?(errmsg), "error message '#{err}' expected to include '#{errmsg}'")
40 test "requesting nonexistent object returns 404 error" do
42 get(:show, params: {id: BAD_UUID})
46 test "requesting object without read permission returns 404 error" do
47 authorize_with :spectator
48 get(:show, params: {id: specimens(:owned_by_active_user).uuid})
52 test "submitting bad object returns error" do
53 authorize_with :spectator
54 post(:create, params: {specimen: {badattr: "badvalue"}})
59 test "X-Request-Id header" do
60 authorize_with :spectator
62 assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
65 # The response header is the one that gets logged, so this test also
66 # ensures we log the ID supplied in the request, if any.
67 test "X-Request-Id given by client" do
68 authorize_with :spectator
69 @request.headers['X-Request-Id'] = 'abcdefG'
71 assert_equal 'abcdefG', response.headers['X-Request-Id']
74 test "X-Request-Id given by client is ignored if too long" do
75 authorize_with :spectator
76 @request.headers['X-Request-Id'] = 'abcdefG' * 1000
78 assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
81 ['foo', '', 'FALSE', 'TRUE', nil, [true], {a:true}, '"true"'].each do |bogus|
82 test "bogus boolean parameter #{bogus.inspect} returns error" do
83 @controller = Arvados::V1::GroupsController.new
84 authorize_with :active
85 post :create, params: {
87 ensure_unique_name: bogus
90 assert_match(/parameter must be a boolean/, json_response['errors'].first,
91 'Helpful error message not found')
95 [[true, [true, 'true', 1, '1']],
96 [false, [false, 'false', 0, '0']]].each do |bool, boolparams|
97 boolparams.each do |boolparam|
98 # Ensure boolparam is acceptable as a boolean
99 test "boolean parameter #{boolparam.inspect} acceptable" do
100 @controller = Arvados::V1::GroupsController.new
101 authorize_with :active
102 post :create, params: {
104 ensure_unique_name: boolparam
106 assert_response :success
109 # Ensure boolparam is acceptable as the _intended_ boolean
110 test "boolean parameter #{boolparam.inspect} accepted as #{bool.inspect}" do
111 @controller = Arvados::V1::GroupsController.new
112 authorize_with :active
113 post :create, params: {
115 name: groups(:aproject).name,
116 owner_uuid: groups(:aproject).owner_uuid
118 ensure_unique_name: boolparam
120 assert_response (bool ? :success : 422)
125 test "exceptions with backtraces get logged at exception_backtrace key" do
126 Group.stubs(:new).raises(Exception, 'Whoops')
127 Rails.logger.expects(:info).with(any_parameters) do |param|
128 param.include?('Whoops') and param.include?('"exception_backtrace":')
130 @controller = Arvados::V1::GroupsController.new
131 authorize_with :active
132 post :create, params: {