8784: Fix test for latest firefox.
[arvados.git] / services / api / test / functional / application_controller_test.rb
1 require 'test_helper'
2
3 class ApplicationControllerTest < ActionController::TestCase
4   BAD_UUID = "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
5
6   def now_timestamp
7     Time.now.utc.to_i
8   end
9
10   setup do
11     # These tests are meant to check behavior in ApplicationController.
12     # We instantiate a small concrete controller for convenience.
13     @controller = Arvados::V1::SpecimensController.new
14     @start_stamp = now_timestamp
15   end
16
17   def check_error_token
18     token = json_response['error_token']
19     assert_not_nil token
20     token_time = token.split('+', 2).first.to_i
21     assert_operator(token_time, :>=, @start_stamp, "error token too old")
22     assert_operator(token_time, :<=, now_timestamp, "error token too new")
23   end
24
25   def check_404(errmsg="Path not found")
26     assert_response 404
27     assert_equal([errmsg], json_response['errors'])
28     check_error_token
29   end
30
31   test "requesting nonexistent object returns 404 error" do
32     authorize_with :admin
33     get(:show, id: BAD_UUID)
34     check_404
35   end
36
37   test "requesting object without read permission returns 404 error" do
38     authorize_with :spectator
39     get(:show, id: specimens(:owned_by_active_user).uuid)
40     check_404
41   end
42
43   test "submitting bad object returns error" do
44     authorize_with :spectator
45     post(:create, specimen: {badattr: "badvalue"})
46     assert_response 422
47     check_error_token
48   end
49
50   ['foo', '', 'FALSE', 'TRUE', nil, [true], {a:true}, '"true"'].each do |bogus|
51     test "bogus boolean parameter #{bogus.inspect} returns error" do
52       @controller = Arvados::V1::GroupsController.new
53       authorize_with :active
54       post :create, {
55         group: {},
56         ensure_unique_name: bogus
57       }
58       assert_response 422
59       assert_match(/parameter must be a boolean/, json_response['errors'].first,
60                    'Helpful error message not found')
61     end
62   end
63
64   [[true, [true, 'true', 1, '1']],
65    [false, [false, 'false', 0, '0']]].each do |bool, boolparams|
66     boolparams.each do |boolparam|
67       # Ensure boolparam is acceptable as a boolean
68       test "boolean parameter #{boolparam.inspect} acceptable" do
69         @controller = Arvados::V1::GroupsController.new
70         authorize_with :active
71         post :create, {
72           group: {},
73           ensure_unique_name: boolparam
74         }
75         assert_response :success
76       end
77
78       # Ensure boolparam is acceptable as the _intended_ boolean
79       test "boolean parameter #{boolparam.inspect} accepted as #{bool.inspect}" do
80         @controller = Arvados::V1::GroupsController.new
81         authorize_with :active
82         post :create, {
83           group: {
84             name: groups(:aproject).name,
85             owner_uuid: groups(:aproject).owner_uuid
86           },
87           ensure_unique_name: boolparam
88         }
89         assert_response (bool ? :success : 422)
90       end
91     end
92   end
93 end