3036: Split out logic to determine if a user is actually allowed to set the
[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_nil a, "Non-admin should not assign uuid."
16   end
17
18   test 'admin can assign valid uuid' do
19     set_user_from_auth :admin_trustedclient
20     want_uuid = Specimen.generate_uuid
21     a = create_with_attrs(uuid: want_uuid)
22     assert_equal want_uuid, a.uuid, "Admin should assign valid uuid."
23     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
24   end
25
26   test 'admin cannot assign empty uuid' do
27     set_user_from_auth :admin_trustedclient
28     a = create_with_attrs(uuid: "")
29     assert_not_equal "", a.uuid, "Admin should not assign empty uuid."
30     assert a.uuid.length==27, "Auto assigned uuid length is wrong."
31   end
32
33   [ {:a => 'foo'},
34     {'a' => :foo},
35     {:a => ['foo', 'bar']},
36     {'a' => [:foo, 'bar']},
37     {'a' => ['foo', :bar]},
38     {:a => [:foo, :bar]},
39     {:a => {'foo' => {'bar' => 'baz'}}},
40     {'a' => {:foo => {'bar' => 'baz'}}},
41     {'a' => {'foo' => {:bar => 'baz'}}},
42     {'a' => {'foo' => {'bar' => :baz}}},
43     {'a' => {'foo' => ['bar', :baz]}},
44     {'a' => {['foo', :foo] => ['bar', 'baz']}},
45   ].each do |x|
46     test "refuse symbol keys in serialized attribute: #{x.inspect}" do
47       set_user_from_auth :admin_trustedclient
48       assert_nothing_raised do
49         Link.create!(link_class: 'test',
50                      properties: {})
51       end
52       assert_raises ActiveRecord::RecordInvalid do
53         Link.create!(link_class: 'test',
54                      properties: x)
55       end
56     end
57   end
58
59   test "Stringify symbols coming from serialized attribute in database" do
60     set_user_from_auth :admin_trustedclient
61     fixed = Link.find_by_uuid(links(:has_symbol_keys_in_database_somehow).uuid)
62     assert_equal(["baz", "foo"], fixed.properties.keys.sort,
63                  "Hash symbol keys from DB did not get stringified.")
64     assert_equal(['waz', 'waz', 'waz', 1, nil, false, true],
65                  fixed.properties['baz'],
66                  "Array symbol values from DB did not get stringified.")
67     assert_equal true, fixed.save, "Failed to save fixed model back to db."
68   end
69
70   test "No HashWithIndifferentAccess in database" do
71     set_user_from_auth :admin_trustedclient
72     assert_raises ActiveRecord::RecordInvalid do
73       Link.create!(link_class: 'test',
74                    properties: {'foo' => 'bar'}.with_indifferent_access)
75     end
76   end
77 end