Merge branch '2755-python-sdk-permissions'
[arvados.git] / services / api / test / unit / arvados_model_test.rb
1 require 'test_helper'
2
3 class ArvadosModelTest < ActiveSupport::TestCase
4   fixtures :all
5
6   def create_with_attrs attrs
7     a = Specimen.create({material: 'caloric'}.merge(attrs))
8     a if a.valid?
9   end
10
11   test 'non-admin cannot assign uuid' do
12     set_user_from_auth :active_trustedclient
13     want_uuid = Specimen.generate_uuid
14     a = create_with_attrs(uuid: want_uuid)
15     assert_not_equal want_uuid, a.uuid, "Non-admin should not assign uuid."
16     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
17   end
18
19   test 'admin can assign valid uuid' do
20     set_user_from_auth :admin_trustedclient
21     want_uuid = Specimen.generate_uuid
22     a = create_with_attrs(uuid: want_uuid)
23     assert_equal want_uuid, a.uuid, "Admin should assign valid uuid."
24     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
25   end
26
27   test 'admin cannot assign empty uuid' do
28     set_user_from_auth :admin_trustedclient
29     a = create_with_attrs(uuid: "")
30     assert_not_equal "", a.uuid, "Admin should not assign empty uuid."
31     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
32   end
33
34   [ {:a => 'foo'},
35     {'a' => :foo},
36     {:a => ['foo', 'bar']},
37     {'a' => [:foo, 'bar']},
38     {'a' => ['foo', :bar]},
39     {:a => [:foo, :bar]},
40     {:a => {'foo' => {'bar' => 'baz'}}},
41     {'a' => {:foo => {'bar' => 'baz'}}},
42     {'a' => {'foo' => {:bar => 'baz'}}},
43     {'a' => {'foo' => {'bar' => :baz}}},
44     {'a' => {'foo' => ['bar', :baz]}},
45     {'a' => {['foo', :foo] => ['bar', 'baz']}},
46   ].each do |x|
47     test "refuse symbol keys in serialized attribute: #{x.inspect}" do
48       set_user_from_auth :admin_trustedclient
49       assert_nothing_raised do
50         Link.create!(link_class: 'test',
51                      properties: {})
52       end
53       assert_raises ActiveRecord::RecordInvalid do
54         Link.create!(link_class: 'test',
55                      properties: x)
56       end
57     end
58   end
59
60   test "Stringify symbols coming from serialized attribute in database" do
61     set_user_from_auth :admin_trustedclient
62     fixed = Link.find_by_uuid(links(:has_symbol_keys_in_database_somehow).uuid)
63     assert_equal(["baz", "foo"], fixed.properties.keys.sort,
64                  "Hash symbol keys from DB did not get stringified.")
65     assert_equal(['waz', 'waz', 'waz', 1, nil, false, true],
66                  fixed.properties['baz'],
67                  "Array symbol values from DB did not get stringified.")
68     assert_equal true, fixed.save, "Failed to save fixed model back to db."
69   end
70
71   test "No HashWithIndifferentAccess in database" do
72     set_user_from_auth :admin_trustedclient
73     assert_raises ActiveRecord::RecordInvalid do
74       Link.create!(link_class: 'test',
75                    properties: {'foo' => 'bar'}.with_indifferent_access)
76     end
77   end
78 end