Merge branch '7167-keep-rsync' of git.curoverse.com:arvados into 7167-keep-rsync
[arvados.git] / services / api / test / unit / owner_test.rb
1 require 'test_helper'
2
3 # Test referential integrity: ensure we cannot leave any object
4 # without owners by deleting a user or group.
5 #
6 # "o" is an owner.
7 # "i" is an item.
8
9 class OwnerTest < ActiveSupport::TestCase
10   fixtures :users, :groups, :specimens
11
12   setup do
13     set_user_from_auth :admin_trustedclient
14   end
15
16   User.all
17   Group.all
18   [User, Group].each do |o_class|
19     test "create object with legit #{o_class} owner" do
20       o = o_class.create!
21       i = Specimen.create(owner_uuid: o.uuid)
22       assert i.valid?, "new item should pass validation"
23       assert i.uuid, "new item should have an ID"
24       assert Specimen.where(uuid: i.uuid).any?, "new item should really be in DB"
25     end
26
27     test "create object with non-existent #{o_class} owner" do
28       assert_raises(ActiveRecord::RecordInvalid,
29                     "create should fail with random owner_uuid") do
30         i = Specimen.create!(owner_uuid: o_class.generate_uuid)
31       end
32
33       i = Specimen.create(owner_uuid: o_class.generate_uuid)
34       assert !i.valid?, "object with random owner_uuid should not be valid?"
35
36       i = Specimen.new(owner_uuid: o_class.generate_uuid)
37       assert !i.valid?, "new item should not pass validation"
38       assert !i.uuid, "new item should not have an ID"
39     end
40
41     [User, Group].each do |new_o_class|
42       test "change owner from legit #{o_class} to legit #{new_o_class} owner" do
43         o = o_class.create!
44         i = Specimen.create!(owner_uuid: o.uuid)
45         new_o = new_o_class.create!
46         assert(Specimen.where(uuid: i.uuid).any?,
47                "new item should really be in DB")
48         assert(i.update_attributes(owner_uuid: new_o.uuid),
49                "should change owner_uuid from #{o.uuid} to #{new_o.uuid}")
50       end
51     end
52
53     test "delete #{o_class} that owns nothing" do
54       o = o_class.create!
55       assert(o_class.where(uuid: o.uuid).any?,
56              "new #{o_class} should really be in DB")
57       assert(o.destroy, "should delete #{o_class} that owns nothing")
58       assert_equal(false, o_class.where(uuid: o.uuid).any?,
59                    "#{o.uuid} should not be in DB after deleting")
60     end
61
62     test "change uuid of #{o_class} that owns nothing" do
63       # (we're relying on our admin credentials here)
64       o = o_class.create!
65       assert(o_class.where(uuid: o.uuid).any?,
66              "new #{o_class} should really be in DB")
67       old_uuid = o.uuid
68       new_uuid = o.uuid.sub(/..........$/, rand(2**256).to_s(36)[0..9])
69       assert(o.update_attributes(uuid: new_uuid),
70              "should change #{o_class} uuid from #{old_uuid} to #{new_uuid}")
71       assert_equal(false, o_class.where(uuid: old_uuid).any?,
72                    "#{old_uuid} should disappear when renamed to #{new_uuid}")
73     end
74   end
75
76   ['users(:active)', 'groups(:aproject)'].each do |ofixt|
77     test "delete #{ofixt} that owns other objects" do
78       o = eval ofixt
79       assert_equal(true, Specimen.where(owner_uuid: o.uuid).any?,
80                    "need something to be owned by #{o.uuid} for this test")
81
82       assert_raises(ActiveRecord::DeleteRestrictionError,
83                     "should not delete #{ofixt} that owns objects") do
84         o.destroy
85       end
86     end
87
88     test "change uuid of #{ofixt} that owns other objects" do
89       o = eval ofixt
90       assert_equal(true, Specimen.where(owner_uuid: o.uuid).any?,
91                    "need something to be owned by #{o.uuid} for this test")
92       old_uuid = o.uuid
93       new_uuid = o.uuid.sub(/..........$/, rand(2**256).to_s(36)[0..9])
94       assert(!o.update_attributes(uuid: new_uuid),
95              "should not change uuid of #{ofixt} that owns objects")
96     end
97   end
98
99   test "delete User that owns self" do
100     o = User.create!
101     assert User.where(uuid: o.uuid).any?, "new User should really be in DB"
102     assert_equal(true, o.update_attributes(owner_uuid: o.uuid),
103                  "setting owner to self should work")
104     assert(o.destroy, "should delete User that owns self")
105     assert_equal(false, User.where(uuid: o.uuid).any?,
106                  "#{o.uuid} should not be in DB after deleting")
107   end
108
109   test "change uuid of User that owns self" do
110     o = User.create!
111     assert User.where(uuid: o.uuid).any?, "new User should really be in DB"
112     assert_equal(true, o.update_attributes(owner_uuid: o.uuid),
113                  "setting owner to self should work")
114     old_uuid = o.uuid
115     new_uuid = o.uuid.sub(/..........$/, rand(2**256).to_s(36)[0..9])
116     assert(o.update_attributes(uuid: new_uuid),
117            "should change uuid of User that owns self")
118     assert_equal(false, User.where(uuid: old_uuid).any?,
119                  "#{old_uuid} should not be in DB after deleting")
120     assert_equal(true, User.where(uuid: new_uuid).any?,
121                  "#{new_uuid} should be in DB after renaming")
122     assert_equal(new_uuid, User.where(uuid: new_uuid).first.owner_uuid,
123                  "#{new_uuid} should be its own owner in DB after renaming")
124   end
125
126 end