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)
32 def check_404(errmsg="Path not found")
34 assert_equal([errmsg], json_response['errors'])
38 test "requesting nonexistent object returns 404 error" do
40 get(:show, params: {id: BAD_UUID})
44 test "requesting object without read permission returns 404 error" do
45 authorize_with :spectator
46 get(:show, params: {id: specimens(:owned_by_active_user).uuid})
50 test "submitting bad object returns error" do
51 authorize_with :spectator
52 post(:create, params: {specimen: {badattr: "badvalue"}})
57 test "X-Request-Id header" do
58 authorize_with :spectator
60 assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
63 # The response header is the one that gets logged, so this test also
64 # ensures we log the ID supplied in the request, if any.
65 test "X-Request-Id given by client" do
66 authorize_with :spectator
67 @request.headers['X-Request-Id'] = 'abcdefG'
69 assert_equal 'abcdefG', response.headers['X-Request-Id']
72 test "X-Request-Id given by client is ignored if too long" do
73 authorize_with :spectator
74 @request.headers['X-Request-Id'] = 'abcdefG' * 1000
76 assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
79 ['foo', '', 'FALSE', 'TRUE', nil, [true], {a:true}, '"true"'].each do |bogus|
80 test "bogus boolean parameter #{bogus.inspect} returns error" do
81 @controller = Arvados::V1::GroupsController.new
82 authorize_with :active
83 post :create, params: {
85 ensure_unique_name: bogus
88 assert_match(/parameter must be a boolean/, json_response['errors'].first,
89 'Helpful error message not found')
93 [[true, [true, 'true', 1, '1']],
94 [false, [false, 'false', 0, '0']]].each do |bool, boolparams|
95 boolparams.each do |boolparam|
96 # Ensure boolparam is acceptable as a boolean
97 test "boolean parameter #{boolparam.inspect} acceptable" do
98 @controller = Arvados::V1::GroupsController.new
99 authorize_with :active
100 post :create, params: {
102 ensure_unique_name: boolparam
104 assert_response :success
107 # Ensure boolparam is acceptable as the _intended_ boolean
108 test "boolean parameter #{boolparam.inspect} accepted as #{bool.inspect}" do
109 @controller = Arvados::V1::GroupsController.new
110 authorize_with :active
111 post :create, params: {
113 name: groups(:aproject).name,
114 owner_uuid: groups(:aproject).owner_uuid
116 ensure_unique_name: boolparam
118 assert_response (bool ? :success : 422)