17417: Merge branch 'main' into 17417-add-arm64
[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   end
28
29   def check_404(errmsg="Path not found")
30     assert_response 404
31     json_response['errors'].each do |err|
32       assert(err.include?(errmsg), "error message '#{err}' expected to include '#{errmsg}'")
33     end
34     check_error_token
35   end
36
37   test "requesting nonexistent object returns 404 error" do
38     authorize_with :admin
39     get(:show, params: {id: BAD_UUID})
40     check_404
41   end
42
43   test "requesting object without read permission returns 404 error" do
44     authorize_with :spectator
45     get(:show, params: {id: specimens(:owned_by_active_user).uuid})
46     check_404
47   end
48
49   test "submitting bad object returns error" do
50     authorize_with :spectator
51     post(:create, params: {specimen: {badattr: "badvalue"}})
52     assert_response 422
53     check_error_token
54   end
55
56   ['foo', '', 'FALSE', 'TRUE', nil, [true], {a:true}, '"true"'].each do |bogus|
57     test "bogus boolean parameter #{bogus.inspect} returns error" do
58       @controller = Arvados::V1::GroupsController.new
59       authorize_with :active
60       post :create, params: {
61         group: {},
62         ensure_unique_name: bogus
63       }
64       assert_response 422
65       assert_match(/parameter must be a boolean/, json_response['errors'].first,
66                    'Helpful error message not found')
67     end
68   end
69
70   [[true, [true, 'true', 1, '1']],
71    [false, [false, 'false', 0, '0']]].each do |bool, boolparams|
72     boolparams.each do |boolparam|
73       # Ensure boolparam is acceptable as a boolean
74       test "boolean parameter #{boolparam.inspect} acceptable" do
75         @controller = Arvados::V1::GroupsController.new
76         authorize_with :active
77         post :create, params: {
78           group: {group_class: "project"},
79           ensure_unique_name: boolparam
80         }
81         assert_response :success
82       end
83
84       # Ensure boolparam is acceptable as the _intended_ boolean
85       test "boolean parameter #{boolparam.inspect} accepted as #{bool.inspect}" do
86         @controller = Arvados::V1::GroupsController.new
87         authorize_with :active
88         post :create, params: {
89           group: {
90             name: groups(:aproject).name,
91             owner_uuid: groups(:aproject).owner_uuid,
92             group_class: "project"
93           },
94           ensure_unique_name: boolparam
95         }
96         assert_response (bool ? :success : 422)
97       end
98     end
99   end
100
101   test "exceptions with backtraces get logged at exception_backtrace key" do
102     Group.stubs(:new).raises(Exception, 'Whoops')
103     Rails.logger.expects(:info).with(any_parameters) do |param|
104       param.include?('Whoops') and param.include?('"exception_backtrace":')
105     end
106     @controller = Arvados::V1::GroupsController.new
107     authorize_with :active
108     post :create, params: {
109       group: {},
110     }
111   end
112 end