Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / functional / arvados / v1 / keep_disks_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 Arvados::V1::KeepDisksControllerTest < ActionController::TestCase
8
9   def default_ping_opts
10     {ping_secret: '', service_ssl_flag: false, service_port: 1234}
11   end
12
13   test "add keep disk with admin token" do
14     authorize_with :admin
15     post :ping, default_ping_opts.
16       merge(filesystem_uuid: 'eb1e77a1-db84-4193-b6e6-ca2894f67d5f')
17     assert_response :success
18     assert_not_nil assigns(:object)
19     new_keep_disk = JSON.parse(@response.body)
20     assert_not_nil new_keep_disk['uuid']
21     assert_not_nil new_keep_disk['ping_secret']
22     assert_not_equal '', new_keep_disk['ping_secret']
23   end
24
25   [
26     {},
27     {filesystem_uuid: ''},
28   ].each do |opts|
29     test "add keep disk with[out] filesystem_uuid #{opts}" do
30       authorize_with :admin
31       post :ping, default_ping_opts.merge(opts)
32       assert_response :success
33       assert_not_nil JSON.parse(@response.body)['uuid']
34     end
35   end
36
37   test "refuse to add keep disk without admin token" do
38     post :ping, default_ping_opts
39     assert_response 404
40   end
41
42   test "ping keep disk" do
43     post :ping, default_ping_opts.
44       merge(id: keep_disks(:nonfull).uuid,
45             ping_secret: keep_disks(:nonfull).ping_secret,
46             filesystem_uuid: keep_disks(:nonfull).filesystem_uuid)
47     assert_response :success
48     assert_not_nil assigns(:object)
49     keep_disk = JSON.parse(@response.body)
50     assert_not_nil keep_disk['uuid']
51     assert_not_nil keep_disk['ping_secret']
52   end
53
54   test "admin should get index with ping_secret" do
55     authorize_with :admin
56     get :index
57     assert_response :success
58     assert_not_nil assigns(:objects)
59     items = JSON.parse(@response.body)['items']
60     assert_not_equal 0, items.size
61     assert_not_nil items[0]['ping_secret']
62   end
63
64   # inactive user sees keep disks
65   test "inactive user should get index" do
66     authorize_with :inactive
67     get :index
68     assert_response :success
69     items = JSON.parse(@response.body)['items']
70     assert_not_equal 0, items.size
71
72     # Check these are still included
73     assert items[0]['service_host']
74     assert items[0]['service_port']
75   end
76
77   # active user sees non-secret attributes of keep disks
78   test "active user should get non-empty index with no ping_secret" do
79     authorize_with :active
80     get :index
81     assert_response :success
82     items = JSON.parse(@response.body)['items']
83     assert_not_equal 0, items.size
84     items.each do |item|
85       assert_nil item['ping_secret']
86       assert_not_nil item['is_readable']
87       assert_not_nil item['is_writable']
88       assert_not_nil item['service_host']
89       assert_not_nil item['service_port']
90     end
91   end
92
93   test "search keep_services with 'any' operator" do
94     authorize_with :active
95     get :index, {
96       where: { any: ['contains', 'o2t1q5w'] }
97     }
98     assert_response :success
99     found = assigns(:objects).collect(&:uuid)
100     assert_equal true, !!found.index('zzzzz-penuu-5w2o2t1q5wy7fhn')
101   end
102
103
104 end