8784: Fix test for latest firefox.
[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 "cannot delete an object referenced by unwritable links" do
11     ob = act_as_user users(:active) do
12       Specimen.create
13     end
14     link = act_as_user users(:admin) do
15       Link.create(tail_uuid: users(:active).uuid,
16                   head_uuid: ob.uuid,
17                   link_class: 'test',
18                   name: 'test')
19     end
20     assert_equal users(:admin).uuid, link.owner_uuid
21     assert_raises(ArvadosModel::PermissionDeniedError,
22                   "should not delete #{ob.uuid} with link #{link.uuid}") do
23       act_as_user users(:active) do
24         ob.destroy
25       end
26     end
27     act_as_user users(:admin) do
28       ob.destroy
29     end
30     assert_empty Link.where(uuid: link.uuid)
31   end
32
33   def new_active_link_valid?(link_attrs)
34     set_user_from_auth :active
35     begin
36       Link.
37         create({link_class: "permission",
38                  name: "can_read",
39                  head_uuid: groups(:aproject).uuid,
40                }.merge(link_attrs)).
41         valid?
42     rescue ArvadosModel::PermissionDeniedError
43       false
44     end
45   end
46
47   test "non-admin project owner can make it public" do
48     assert(new_active_link_valid?(tail_uuid: groups(:anonymous_group).uuid),
49            "non-admin project owner can't make their project public")
50   end
51
52   test "link granting permission to nonexistent user is invalid" do
53     refute new_active_link_valid?(tail_uuid:
54                                   users(:active).uuid.sub(/-\w+$/, "-#{'z' * 15}"))
55   end
56
57   test "link granting non-project permission to unreadable user is invalid" do
58     refute new_active_link_valid?(tail_uuid: users(:admin).uuid,
59                                   head_uuid: collections(:bar_file).uuid)
60   end
61
62   test "user can't add a Collection to a Project without permission" do
63     refute new_active_link_valid?(link_class: "name",
64                                   name: "Permission denied test name",
65                                   tail_uuid: collections(:bar_file).uuid)
66   end
67
68   test "user can't add a User to a Project" do
69     # Users *can* give other users permissions to projects.
70     # This test helps ensure that that exception is specific to permissions.
71     refute new_active_link_valid?(link_class: "name",
72                                   name: "Permission denied test name",
73                                   tail_uuid: users(:admin).uuid)
74   end
75
76   test "link granting project permissions to unreadable user is invalid" do
77     refute new_active_link_valid?(tail_uuid: users(:admin).uuid)
78   end
79 end