2760: Assign a generic name if link_class=name and no name is given.
[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(:afolder).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_raises ActiveRecord::RecordNotUnique do
17       b = Link.create!(tail_uuid: groups(:afolder).uuid,
18                        head_uuid: specimens(:owned_by_active_user).uuid,
19                        link_class: 'name',
20                        name: 'foo')
21     end
22   end
23
24   test 'name links with different tail_uuid need not be unique' do
25     a = Link.create!(tail_uuid: groups(:afolder).uuid,
26                      head_uuid: specimens(:owned_by_active_user).uuid,
27                      link_class: 'name',
28                      name: 'foo')
29     assert a.valid?, a.errors.to_s
30     b = Link.create!(tail_uuid: groups(:asubfolder).uuid,
31                      head_uuid: specimens(:owned_by_active_user).uuid,
32                      link_class: 'name',
33                      name: 'foo')
34     assert b.valid?, b.errors.to_s
35     assert_not_equal(a.uuid, b.uuid,
36                      "created two links and got the same uuid back.")
37   end
38
39   [nil, '', false].each do |name|
40     test "name links cannot be renamed to name=#{name.inspect}" do
41       a = Link.create!(tail_uuid: groups(:afolder).uuid,
42                        head_uuid: specimens(:owned_by_active_user).uuid,
43                        link_class: 'name',
44                        name: 'temp')
45       a.name = name
46       assert a.invalid?, "invalid name was accepted as valid?"
47     end
48
49     test "name links cannot be created with name=#{name.inspect}" do
50       a = Link.create(tail_uuid: groups(:afolder).uuid,
51                       head_uuid: specimens(:owned_by_active_user).uuid,
52                       link_class: 'name',
53                       name: name)
54       if a.name and !a.name.empty?
55         assert a.valid?, "name automatically assigned, but record not valid?"
56       else
57         assert a.invalid?, "invalid name was accepted as valid?"
58       end
59     end
60   end
61
62   test "cannot delete an object referenced by links" do
63     ob = Specimen.create
64     link = Link.create(tail_uuid: users(:active).uuid,
65                        head_uuid: ob.uuid,
66                        link_class: 'test',
67                        name: 'test')
68     assert_raises(ActiveRecord::DeleteRestrictionError,
69                   "should not delete #{ob.uuid} with link #{link.uuid}") do
70       ob.destroy
71     end
72   end
73
74   test "assign sequential generic name links" do
75     group = Group.create!(group_class: 'folder')
76     ob = Specimen.create!
77     25.times do |n|
78       link = Link.create!(link_class: 'name',
79                           tail_uuid: group.uuid, head_uuid: ob.uuid)
80       expect_name = 'New specimen' + (n==0 ? "" : " (#{n})")
81       assert_equal expect_name, link.name, "Expected sequential generic names"
82     end
83   end
84
85   test "assign sequential generic name links for a two-word model" do
86     group = Group.create!(group_class: 'folder')
87     ob = VirtualMachine.create!
88     5.times do |n|
89       link = Link.create!(link_class: 'name',
90                           tail_uuid: group.uuid, head_uuid: ob.uuid)
91       expect_name = 'New virtual machine' + (n==0 ? "" : " (#{n})")
92       assert_equal expect_name, link.name, "Expected sequential generic names"
93     end
94   end
95
96   test "cannot assign sequential generic name links for a bogus uuid type" do
97     group = Group.create!(group_class: 'folder')
98     link = Link.create(link_class: 'name',
99                        tail_uuid: group.uuid,
100                        head_uuid: 'zzzzz-abcde-123451234512345')
101     assert link.invalid?, "gave a bogus uuid, got automatic name #{link.name}"
102   end
103 end