Merge branch '8784-dir-listings'
[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     assert_equal([errmsg], json_response['errors'])
32     check_error_token
33   end
34
35   test "requesting nonexistent object returns 404 error" do
36     authorize_with :admin
37     get(:show, id: BAD_UUID)
38     check_404
39   end
40
41   test "requesting object without read permission returns 404 error" do
42     authorize_with :spectator
43     get(:show, id: specimens(:owned_by_active_user).uuid)
44     check_404
45   end
46
47   test "submitting bad object returns error" do
48     authorize_with :spectator
49     post(:create, specimen: {badattr: "badvalue"})
50     assert_response 422
51     check_error_token
52   end
53
54   ['foo', '', 'FALSE', 'TRUE', nil, [true], {a:true}, '"true"'].each do |bogus|
55     test "bogus boolean parameter #{bogus.inspect} returns error" do
56       @controller = Arvados::V1::GroupsController.new
57       authorize_with :active
58       post :create, {
59         group: {},
60         ensure_unique_name: bogus
61       }
62       assert_response 422
63       assert_match(/parameter must be a boolean/, json_response['errors'].first,
64                    'Helpful error message not found')
65     end
66   end
67
68   [[true, [true, 'true', 1, '1']],
69    [false, [false, 'false', 0, '0']]].each do |bool, boolparams|
70     boolparams.each do |boolparam|
71       # Ensure boolparam is acceptable as a boolean
72       test "boolean parameter #{boolparam.inspect} acceptable" do
73         @controller = Arvados::V1::GroupsController.new
74         authorize_with :active
75         post :create, {
76           group: {},
77           ensure_unique_name: boolparam
78         }
79         assert_response :success
80       end
81
82       # Ensure boolparam is acceptable as the _intended_ boolean
83       test "boolean parameter #{boolparam.inspect} accepted as #{bool.inspect}" do
84         @controller = Arvados::V1::GroupsController.new
85         authorize_with :active
86         post :create, {
87           group: {
88             name: groups(:aproject).name,
89             owner_uuid: groups(:aproject).owner_uuid
90           },
91           ensure_unique_name: boolparam
92         }
93         assert_response (bool ? :success : 422)
94       end
95     end
96   end
97 end