4f112dbae5c9458d81ff18f46a86e214053cc15a
[arvados.git] / services / api / test / functional / application_controller_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 ApplicationControllerTest < ActionController::TestCase
8   BAD_UUID = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
9
10   def now_timestamp
11     Time.now.utc.to_i
12   end
13
14   setup do
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
19   end
20
21   def check_error_token
22     token = json_response['error_token']
23     assert_not_nil 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)
29     end
30   end
31
32   def check_404(errmsg="Path not found")
33     assert_response 404
34     assert_equal([errmsg], json_response['errors'])
35     check_error_token
36   end
37
38   test "requesting nonexistent object returns 404 error" do
39     authorize_with :admin
40     get(:show, params: {id: BAD_UUID})
41     check_404
42   end
43
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})
47     check_404
48   end
49
50   test "submitting bad object returns error" do
51     authorize_with :spectator
52     post(:create, params: {specimen: {badattr: "badvalue"}})
53     assert_response 422
54     check_error_token
55   end
56
57   test "X-Request-Id header" do
58     authorize_with :spectator
59     get(:index)
60     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
61   end
62
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'
68     get(:index)
69     assert_equal 'abcdefG', response.headers['X-Request-Id']
70   end
71
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
75     get(:index)
76     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
77   end
78
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: {
84         group: {},
85         ensure_unique_name: bogus
86       }
87       assert_response 422
88       assert_match(/parameter must be a boolean/, json_response['errors'].first,
89                    'Helpful error message not found')
90     end
91   end
92
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: {
101           group: {},
102           ensure_unique_name: boolparam
103         }
104         assert_response :success
105       end
106
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: {
112           group: {
113             name: groups(:aproject).name,
114             owner_uuid: groups(:aproject).owner_uuid
115           },
116           ensure_unique_name: boolparam
117         }
118         assert_response (bool ? :success : 422)
119       end
120     end
121   end
122 end