Merge branch 'master' into 2380-ssh-doc
[arvados.git] / services / api / test / unit / link_test.rb
1 require 'test_helper'
2
3 class LinkTest < ActiveSupport::TestCase
4   fixtures :all
5
6   setup do
7     set_user_from_auth :admin_trustedclient
8   end
9
10   test 'name links with the same tail_uuid must be unique' do
11     a = Link.create!(tail_uuid: groups(:aproject).uuid,
12                      head_uuid: specimens(:owned_by_active_user).uuid,
13                      link_class: 'name',
14                      name: 'foo')
15     assert a.valid?, a.errors.to_s
16     assert_equal groups(:aproject).uuid, a.owner_uuid
17     assert_raises ActiveRecord::RecordNotUnique do
18       b = Link.create!(tail_uuid: groups(:aproject).uuid,
19                        head_uuid: specimens(:owned_by_active_user).uuid,
20                        link_class: 'name',
21                        name: 'foo')
22     end
23   end
24
25   test 'name links with different tail_uuid need not be unique' do
26     a = Link.create!(tail_uuid: groups(:aproject).uuid,
27                      head_uuid: specimens(:owned_by_active_user).uuid,
28                      link_class: 'name',
29                      name: 'foo')
30     assert a.valid?, a.errors.to_s
31     assert_equal groups(:aproject).uuid, a.owner_uuid
32     b = Link.create!(tail_uuid: groups(:asubproject).uuid,
33                      head_uuid: specimens(:owned_by_active_user).uuid,
34                      link_class: 'name',
35                      name: 'foo')
36     assert b.valid?, b.errors.to_s
37     assert_equal groups(:asubproject).uuid, b.owner_uuid
38     assert_not_equal(a.uuid, b.uuid,
39                      "created two links and got the same uuid back.")
40   end
41
42   [nil, '', false].each do |name|
43     test "name links cannot have name=#{name.inspect}" do
44       a = Link.create(tail_uuid: groups(:aproject).uuid,
45                       head_uuid: specimens(:owned_by_active_user).uuid,
46                       link_class: 'name',
47                       name: name)
48       assert a.invalid?, "invalid name was accepted as valid?"
49     end
50   end
51
52   test "cannot delete an object referenced by links" do
53     ob = Specimen.create
54     link = Link.create(tail_uuid: users(:active).uuid,
55                        head_uuid: ob.uuid,
56                        link_class: 'test',
57                        name: 'test')
58     assert_equal users(:admin).uuid, link.owner_uuid
59     assert_raises(ActiveRecord::DeleteRestrictionError,
60                   "should not delete #{ob.uuid} with link #{link.uuid}") do
61       ob.destroy
62     end
63   end
64 end