1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class GroupTest < ActiveSupport::TestCase
9 test "cannot set owner_uuid to object with existing ownership cycle" do
10 set_user_from_auth :active_trustedclient
12 # First make sure we have lots of permission on the bad group by
13 # renaming it to "{current name} is mine all mine"
14 g = groups(:bad_group_has_ownership_cycle_b)
15 g.name += " is mine all mine"
16 assert g.save, "active user should be able to modify group #{g.uuid}"
18 # Use the group as the owner of a new object
20 create(owner_uuid: groups(:bad_group_has_ownership_cycle_b).uuid)
21 assert s.valid?, "ownership should pass validation #{s.errors.messages}"
22 assert_equal false, s.save, "should not save object with #{g.uuid} as owner"
24 # Use the group as the new owner of an existing object
25 s = specimens(:in_aproject)
26 s.owner_uuid = groups(:bad_group_has_ownership_cycle_b).uuid
27 assert s.valid?, "ownership should pass validation"
28 assert_equal false, s.save, "should not save object with #{g.uuid} as owner"
31 test "cannot create a new ownership cycle" do
32 set_user_from_auth :active_trustedclient
34 g_foo = Group.create!(name: "foo")
35 g_bar = Group.create!(name: "bar")
37 g_foo.owner_uuid = g_bar.uuid
38 assert g_foo.save, lambda { g_foo.errors.messages }
39 g_bar.owner_uuid = g_foo.uuid
40 assert g_bar.valid?, "ownership cycle should not prevent validation"
41 assert_equal false, g_bar.save, "should not create an ownership loop"
42 assert g_bar.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/)
45 test "cannot create a single-object ownership cycle" do
46 set_user_from_auth :active_trustedclient
48 g_foo = Group.create!(name: "foo")
51 # Ensure I have permission to manage this group even when its owner changes
52 perm_link = Link.create!(tail_uuid: users(:active).uuid,
53 head_uuid: g_foo.uuid,
54 link_class: 'permission',
58 g_foo.owner_uuid = g_foo.uuid
59 assert_equal false, g_foo.save, "should not create an ownership loop"
60 assert g_foo.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/)
63 test "trash group hides contents" do
64 set_user_from_auth :active_trustedclient
66 g_foo = Group.create!(name: "foo")
67 col = Collection.create!(owner_uuid: g_foo.uuid)
69 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).any?
70 g_foo.update! is_trashed: true
71 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).empty?
72 assert Collection.readable_by(users(:active), {:include_trash => true}).where(uuid: col.uuid).any?
73 g_foo.update! is_trashed: false
74 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).any?
78 set_user_from_auth :active_trustedclient
80 g_foo = Group.create!(name: "foo")
81 g_bar = Group.create!(name: "bar", owner_uuid: g_foo.uuid)
82 g_baz = Group.create!(name: "baz", owner_uuid: g_bar.uuid)
84 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
85 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
86 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).any?
87 g_foo.update! is_trashed: true
88 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).empty?
89 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).empty?
90 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
92 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_foo.uuid).any?
93 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_bar.uuid).any?
94 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_baz.uuid).any?
98 test "trash subgroup" do
99 set_user_from_auth :active_trustedclient
101 g_foo = Group.create!(name: "foo")
102 g_bar = Group.create!(name: "bar", owner_uuid: g_foo.uuid)
103 g_baz = Group.create!(name: "baz", owner_uuid: g_bar.uuid)
105 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
106 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
107 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).any?
108 g_bar.update! is_trashed: true
110 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
111 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).empty?
112 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
114 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_bar.uuid).any?
115 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_baz.uuid).any?
118 test "trash subsubgroup" do
119 set_user_from_auth :active_trustedclient
121 g_foo = Group.create!(name: "foo")
122 g_bar = Group.create!(name: "bar", owner_uuid: g_foo.uuid)
123 g_baz = Group.create!(name: "baz", owner_uuid: g_bar.uuid)
125 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
126 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
127 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).any?
128 g_baz.update! is_trashed: true
129 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
130 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
131 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
132 assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_baz.uuid).any?
136 test "trash group propagates to subgroups" do
137 set_user_from_auth :active_trustedclient
139 g_foo = groups(:trashed_project)
140 g_bar = groups(:trashed_subproject)
141 g_baz = groups(:trashed_subproject3)
142 col = collections(:collection_in_trashed_subproject)
144 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).empty?
145 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).empty?
146 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
147 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).empty?
149 set_user_from_auth :admin
150 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).empty?
151 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).empty?
152 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
153 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).empty?
155 set_user_from_auth :active_trustedclient
156 g_foo.update! is_trashed: false
157 assert Group.readable_by(users(:active)).where(uuid: g_foo.uuid).any?
158 assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
159 assert Collection.readable_by(users(:active)).where(uuid: col.uuid).any?
161 # this one should still be trashed.
162 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
164 g_baz.update! is_trashed: false
165 assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).any?
168 test "trashed does not propagate across permission links" do
169 set_user_from_auth :admin
171 g_foo = Group.create!(name: "foo")
172 u_bar = User.create!(first_name: "bar")
174 assert Group.readable_by(users(:admin)).where(uuid: g_foo.uuid).any?
175 assert User.readable_by(users(:admin)).where(uuid: u_bar.uuid).any?
176 g_foo.update! is_trashed: true
178 assert Group.readable_by(users(:admin)).where(uuid: g_foo.uuid).empty?
179 assert User.readable_by(users(:admin)).where(uuid: u_bar.uuid).any?
181 g_foo.update! is_trashed: false
182 ln = Link.create!(tail_uuid: g_foo.uuid,
183 head_uuid: u_bar.uuid,
184 link_class: "permission",
186 g_foo.update! is_trashed: true
188 assert Group.readable_by(users(:admin)).where(uuid: g_foo.uuid).empty?
189 assert User.readable_by(users(:admin)).where(uuid: u_bar.uuid).any?
192 test "move projects to trash in SweepTrashedObjects" do
193 p = groups(:trashed_on_next_sweep)
194 assert_empty Group.where('uuid=? and is_trashed=true', p.uuid)
195 SweepTrashedObjects.sweep_now
196 assert_not_empty Group.where('uuid=? and is_trashed=true', p.uuid)
199 test "delete projects and their contents in SweepTrashedObjects" do
200 g_foo = groups(:trashed_project)
201 g_bar = groups(:trashed_subproject)
202 g_baz = groups(:trashed_subproject3)
203 col = collections(:collection_in_trashed_subproject)
204 job = jobs(:job_in_trashed_project)
205 cr = container_requests(:cr_in_trashed_project)
206 # Save how many objects were before the sweep
207 user_nr_was = User.all.length
208 coll_nr_was = Collection.all.length
209 group_nr_was = Group.where('group_class<>?', 'project').length
210 project_nr_was = Group.where(group_class: 'project').length
211 cr_nr_was = ContainerRequest.all.length
212 job_nr_was = Job.all.length
213 assert_not_empty Group.where(uuid: g_foo.uuid)
214 assert_not_empty Group.where(uuid: g_bar.uuid)
215 assert_not_empty Group.where(uuid: g_baz.uuid)
216 assert_not_empty Collection.where(uuid: col.uuid)
217 assert_not_empty Job.where(uuid: job.uuid)
218 assert_not_empty ContainerRequest.where(uuid: cr.uuid)
219 SweepTrashedObjects.sweep_now
220 assert_empty Group.where(uuid: g_foo.uuid)
221 assert_empty Group.where(uuid: g_bar.uuid)
222 assert_empty Group.where(uuid: g_baz.uuid)
223 assert_empty Collection.where(uuid: col.uuid)
224 assert_empty Job.where(uuid: job.uuid)
225 assert_empty ContainerRequest.where(uuid: cr.uuid)
226 # No unwanted deletions should have happened
227 assert_equal user_nr_was, User.all.length
228 assert_equal coll_nr_was-2, # collection_in_trashed_subproject
229 Collection.all.length # & deleted_on_next_sweep collections
230 assert_equal group_nr_was, Group.where('group_class<>?', 'project').length
231 assert_equal project_nr_was-3, Group.where(group_class: 'project').length
232 assert_equal cr_nr_was-1, ContainerRequest.all.length
233 assert_equal job_nr_was-1, Job.all.length