Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / helpers / users_test_helper.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 module UsersTestHelper
6   def verify_link(response_items, link_object_name, expect_link, link_class,
7         link_name, head_uuid, tail_uuid, head_kind, fetch_object, class_name)
8     link = find_obj_in_resp response_items, 'arvados#link', link_object_name
9
10     if !expect_link
11       assert_nil link, "Expected no link for #{link_object_name}"
12       return
13     end
14
15     assert_not_nil link, "Expected link for #{link_object_name}"
16
17     if fetch_object
18       object = Object.const_get(class_name).where(name: head_uuid)
19       assert [] != object, "expected #{class_name} with name #{head_uuid}"
20       head_uuid = object.first[:uuid]
21     end
22     assert_equal link_class, link['link_class'],
23         "did not find expected link_class for #{link_object_name}"
24
25     assert_equal link_name, link['name'],
26         "did not find expected link_name for #{link_object_name}"
27
28     assert_equal tail_uuid, link['tail_uuid'],
29         "did not find expected tail_uuid for #{link_object_name}"
30
31     assert_equal head_kind, link['head_kind'],
32         "did not find expected head_kind for #{link_object_name}"
33
34     assert_equal head_uuid, link['head_uuid'],
35         "did not find expected head_uuid for #{link_object_name}"
36   end
37
38   def verify_system_group_permission_link_for user_uuid
39     assert_equal 1, Link.where(link_class: 'permission',
40                                name: 'can_manage',
41                                tail_uuid: system_group_uuid,
42                                head_uuid: user_uuid).count
43   end
44
45   def verify_link_existence uuid, email, expect_oid_login_perms,
46       expect_repo_perms, expect_vm_perms, expect_group_perms, expect_signatures
47     # verify that all links are deleted for the user
48     oid_login_perms = Link.where(tail_uuid: email,
49                                  link_class: 'permission',
50                                  name: 'can_login').where("head_uuid like ?", User.uuid_like_pattern)
51     if expect_oid_login_perms
52       assert oid_login_perms.any?, "expected oid_login_perms"
53     else
54       assert !oid_login_perms.any?, "expected all oid_login_perms deleted"
55     end
56
57     repo_perms = Link.where(tail_uuid: uuid,
58                             link_class: 'permission',
59                             name: 'can_manage').where("head_uuid like ?", Repository.uuid_like_pattern)
60     if expect_repo_perms
61       assert repo_perms.any?, "expected repo_perms"
62     else
63       assert !repo_perms.any?, "expected all repo_perms deleted"
64     end
65
66     vm_login_perms = Link.
67       where(tail_uuid: uuid,
68             link_class: 'permission',
69             name: 'can_login').
70       where("head_uuid like ?",
71             VirtualMachine.uuid_like_pattern).
72       where('uuid <> ?',
73             links(:auto_setup_vm_login_username_can_login_to_test_vm).uuid)
74     if expect_vm_perms
75       assert vm_login_perms.any?, "expected vm_login_perms"
76     else
77       assert !vm_login_perms.any?, "expected all vm_login_perms deleted"
78     end
79
80     group = Group.where(name: 'All users').select do |g|
81       g[:uuid].match(/-f+$/)
82     end.first
83     group_read_perms = Link.where(tail_uuid: uuid,
84                                   head_uuid: group[:uuid],
85                                   link_class: 'permission',
86                                   name: 'can_read')
87     if expect_group_perms
88       assert group_read_perms.any?, "expected all users group read perms"
89     else
90       assert !group_read_perms.any?, "expected all users group perm deleted"
91     end
92
93     signed_uuids = Link.where(link_class: 'signature',
94                               tail_uuid: uuid)
95
96     if expect_signatures
97       assert signed_uuids.any?, "expected signatures"
98     else
99       assert !signed_uuids.any?, "expected all signatures deleted"
100     end
101
102   end
103
104 end