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