Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / functional / arvados / v1 / virtual_machines_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::VirtualMachinesControllerTest < ActionController::TestCase
8   def get_logins_for(vm_sym)
9     authorize_with :admin
10     get(:logins, id: virtual_machines(vm_sym).uuid)
11   end
12
13   def find_login(sshkey_sym)
14     assert_response :success
15     want_key = authorized_keys(sshkey_sym).public_key
16     logins = json_response["items"].select do |login|
17       login["public_key"] == want_key
18     end
19     assert_equal(1, logins.size, "failed to find #{sshkey_sym} login")
20     logins.first
21   end
22
23   test "username propagated from permission" do
24     get_logins_for(:testvm2)
25     admin_login = find_login(:admin)
26     perm = links(:admin_can_login_to_testvm2)
27     assert_equal(perm.properties["username"], admin_login["username"])
28   end
29
30   test "groups propagated from permission" do
31     get_logins_for(:testvm2)
32     admin_login = find_login(:admin)
33     perm = links(:admin_can_login_to_testvm2)
34     assert_equal(perm.properties["groups"], admin_login["groups"])
35   end
36
37   test "groups is an empty list by default" do
38     get_logins_for(:testvm2)
39     active_login = find_login(:active)
40     assert_equal([], active_login["groups"])
41   end
42
43   test "logins without usernames not listed" do
44     get_logins_for(:testvm2)
45     assert_response :success
46     spectator_uuid = users(:spectator).uuid
47     assert_empty(json_response.
48                  select { |login| login["user_uuid"] == spectator_uuid })
49   end
50
51   test "logins without ssh keys are listed" do
52     u, vm = nil
53     act_as_system_user do
54       u = create :active_user, first_name: 'Bob', last_name: 'Blogin'
55       vm = VirtualMachine.create! hostname: 'foo.shell'
56       Link.create!(tail_uuid: u.uuid,
57                    head_uuid: vm.uuid,
58                    link_class: 'permission',
59                    name: 'can_login',
60                    properties: {'username' => 'bobblogin'})
61     end
62     authorize_with :admin
63     get :logins, id: vm.uuid
64     assert_response :success
65     assert_equal 1, json_response['items'].length
66     assert_nil json_response['items'][0]['public_key']
67     assert_nil json_response['items'][0]['authorized_key_uuid']
68     assert_equal u.uuid, json_response['items'][0]['user_uuid']
69     assert_equal 'bobblogin', json_response['items'][0]['username']
70   end
71
72   test 'get all logins' do
73     authorize_with :admin
74     get :get_all_logins
75     find_login :admin
76     find_login :active
77   end
78 end