1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class SysControllerTest < ActionController::TestCase
8 include CurrentApiClient
11 test "trash_sweep - delete expired tokens" do
12 assert_not_empty ApiClientAuthorization.where(uuid: api_client_authorizations(:expired).uuid)
15 assert_response :success
16 assert_empty ApiClientAuthorization.where(uuid: api_client_authorizations(:expired).uuid)
19 test "trash_sweep - fail with non-admin token" do
20 authorize_with :active
25 test "trash_sweep - move collections to trash" do
26 c = collections(:trashed_on_next_sweep)
27 refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
28 assert_raises(ActiveRecord::RecordNotUnique) do
29 act_as_user users(:active) do
30 Collection.create!(owner_uuid: c.owner_uuid,
36 assert_response :success
37 c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
39 act_as_user users(:active) do
40 assert Collection.create!(owner_uuid: c.owner_uuid,
45 test "trash_sweep - delete collections" do
46 uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
47 assert_not_empty Collection.where(uuid: uuid)
50 assert_response :success
51 assert_empty Collection.where(uuid: uuid)
54 test "trash_sweep - delete referring links" do
55 uuid = collections(:trashed_on_next_sweep).uuid
57 assert_raises ActiveRecord::RecordInvalid do
58 # Cannot create because :trashed_on_next_sweep is already trashed
59 Link.create!(head_uuid: uuid,
60 tail_uuid: system_user_uuid,
61 link_class: 'whatever',
65 # Bump trash_at to now + 1 minute
66 Collection.where(uuid: uuid).
67 update(trash_at: db_current_time + (1).minute)
69 # Not considered trashed now
70 Link.create!(head_uuid: uuid,
71 tail_uuid: system_user_uuid,
72 link_class: 'whatever',
75 past = db_current_time
76 Collection.where(uuid: uuid).
77 update_all(is_trashed: true, trash_at: past, delete_at: past)
78 assert_not_empty Collection.where(uuid: uuid)
81 assert_response :success
82 assert_empty Collection.where(uuid: uuid)
85 test "trash_sweep - move projects to trash" do
86 p = groups(:trashed_on_next_sweep)
87 assert_empty Group.where('uuid=? and is_trashed=true', p.uuid)
90 assert_response :success
91 assert_not_empty Group.where('uuid=? and is_trashed=true', p.uuid)
94 test "trash_sweep - delete projects and their contents" do
95 g_foo = groups(:trashed_project)
96 g_bar = groups(:trashed_subproject)
97 g_baz = groups(:trashed_subproject3)
98 col = collections(:collection_in_trashed_subproject)
99 job = jobs(:job_in_trashed_project)
100 cr = container_requests(:cr_in_trashed_project)
101 # Save how many objects were before the sweep
102 user_nr_was = User.all.length
103 coll_nr_was = Collection.all.length
104 group_nr_was = Group.where('group_class<>?', 'project').length
105 project_nr_was = Group.where(group_class: 'project').length
106 cr_nr_was = ContainerRequest.all.length
107 job_nr_was = Job.all.length
108 assert_not_empty Group.where(uuid: g_foo.uuid)
109 assert_not_empty Group.where(uuid: g_bar.uuid)
110 assert_not_empty Group.where(uuid: g_baz.uuid)
111 assert_not_empty Collection.where(uuid: col.uuid)
112 assert_not_empty Job.where(uuid: job.uuid)
113 assert_not_empty ContainerRequest.where(uuid: cr.uuid)
115 authorize_with :admin
117 assert_response :success
119 assert_empty Group.where(uuid: g_foo.uuid)
120 assert_empty Group.where(uuid: g_bar.uuid)
121 assert_empty Group.where(uuid: g_baz.uuid)
122 assert_empty Collection.where(uuid: col.uuid)
123 assert_empty Job.where(uuid: job.uuid)
124 assert_empty ContainerRequest.where(uuid: cr.uuid)
125 # No unwanted deletions should have happened
126 assert_equal user_nr_was, User.all.length
127 assert_equal coll_nr_was-2, # collection_in_trashed_subproject
128 Collection.all.length # & deleted_on_next_sweep collections
129 assert_equal group_nr_was, Group.where('group_class<>?', 'project').length
130 assert_equal project_nr_was-3, Group.where(group_class: 'project').length
131 assert_equal cr_nr_was-1, ContainerRequest.all.length
132 assert_equal job_nr_was-1, Job.all.length