3 # Test referential integrity: ensure we cannot leave any object
4 # without owners by deleting a user or group.
9 class OwnerTest < ActiveSupport::TestCase
10 fixtures :users, :groups, :specimens
13 set_user_from_auth :admin_trustedclient
18 [User, Group].each do |o_class|
19 test "create object with legit #{o_class} owner" do
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"
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)
33 i = Specimen.create(owner_uuid: o_class.generate_uuid)
34 assert !i.valid?, "object with random owner_uuid should not be valid?"
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"
41 [User, Group].each do |new_o_class|
42 test "change owner from legit #{o_class} to legit #{new_o_class} owner" do
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}")
53 test "delete #{o_class} that owns nothing" do
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")
62 test "change uuid of #{o_class} that owns nothing" do
63 # (we're relying on our admin credentials here)
65 assert(o_class.where(uuid: o.uuid).any?,
66 "new #{o_class} should really be in DB")
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}")
76 ['users(:active)', 'groups(:aproject)'].each do |ofixt|
77 test "delete #{ofixt} that owns other objects" do
79 assert_equal(true, Specimen.where(owner_uuid: o.uuid).any?,
80 "need something to be owned by #{o.uuid} for this test")
82 assert_raises(ActiveRecord::DeleteRestrictionError,
83 "should not delete #{ofixt} that owns objects") do
88 test "change uuid of #{ofixt} that owns other objects" do
90 assert_equal(true, Specimen.where(owner_uuid: o.uuid).any?,
91 "need something to be owned by #{o.uuid} for this test")
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")
99 test "delete User that owns self" do
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")
109 test "change uuid of User that owns self" do
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")
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")