CWL spec -> CWL standards
[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, "X-Request-Id value missing on error message")
29     end
30   end
31
32   def check_404(errmsg="Path not found")
33     assert_response 404
34     json_response['errors'].each do |err|
35       assert(err.include?(errmsg), "error message '#{err}' expected to include '#{errmsg}'")
36     end
37     check_error_token
38   end
39
40   test "requesting nonexistent object returns 404 error" do
41     authorize_with :admin
42     get(:show, params: {id: BAD_UUID})
43     check_404
44   end
45
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})
49     check_404
50   end
51
52   test "submitting bad object returns error" do
53     authorize_with :spectator
54     post(:create, params: {specimen: {badattr: "badvalue"}})
55     assert_response 422
56     check_error_token
57   end
58
59   test "X-Request-Id header" do
60     authorize_with :spectator
61     get(:index)
62     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
63   end
64
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'
70     get(:index)
71     assert_equal 'abcdefG', response.headers['X-Request-Id']
72   end
73
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
77     get(:index)
78     assert_match /^req-[0-9a-zA-Z]{20}$/, response.headers['X-Request-Id']
79   end
80
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: {
86         group: {},
87         ensure_unique_name: bogus
88       }
89       assert_response 422
90       assert_match(/parameter must be a boolean/, json_response['errors'].first,
91                    'Helpful error message not found')
92     end
93   end
94
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: {
103           group: {},
104           ensure_unique_name: boolparam
105         }
106         assert_response :success
107       end
108
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: {
114           group: {
115             name: groups(:aproject).name,
116             owner_uuid: groups(:aproject).owner_uuid
117           },
118           ensure_unique_name: boolparam
119         }
120         assert_response (bool ? :success : 422)
121       end
122     end
123   end
124
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":')
129     end
130     @controller = Arvados::V1::GroupsController.new
131     authorize_with :active
132     post :create, params: {
133       group: {},
134     }
135   end
136 end