Merge branch 'master' into 9998-unsigned_manifest
[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 uuid with wrong object type' do
27     set_user_from_auth :admin_trustedclient
28     want_uuid = Human.generate_uuid
29     a = create_with_attrs(uuid: want_uuid)
30     assert_nil a, "Admin should not be able to assign invalid uuid."
31   end
32
33   test 'admin cannot assign badly formed uuid' do
34     set_user_from_auth :admin_trustedclient
35     a = create_with_attrs(uuid: "ntoheunthaoesunhasoeuhtnsaoeunhtsth")
36     assert_nil a, "Admin should not be able to assign invalid uuid."
37   end
38
39   test 'admin cannot assign empty uuid' do
40     set_user_from_auth :admin_trustedclient
41     a = create_with_attrs(uuid: "")
42     assert_nil a, "Admin cannot assign empty uuid."
43   end
44
45   [ {:a => 'foo'},
46     {'a' => :foo},
47     {:a => ['foo', 'bar']},
48     {'a' => [:foo, 'bar']},
49     {'a' => ['foo', :bar]},
50     {:a => [:foo, :bar]},
51     {:a => {'foo' => {'bar' => 'baz'}}},
52     {'a' => {:foo => {'bar' => 'baz'}}},
53     {'a' => {'foo' => {:bar => 'baz'}}},
54     {'a' => {'foo' => {'bar' => :baz}}},
55     {'a' => {'foo' => ['bar', :baz]}},
56     {'a' => {['foo', :foo] => ['bar', 'baz']}},
57   ].each do |x|
58     test "refuse symbol keys in serialized attribute: #{x.inspect}" do
59       set_user_from_auth :admin_trustedclient
60       assert_nothing_raised do
61         Link.create!(link_class: 'test',
62                      properties: {})
63       end
64       assert_raises ActiveRecord::RecordInvalid do
65         Link.create!(link_class: 'test',
66                      properties: x)
67       end
68     end
69   end
70
71   test "Stringify symbols coming from serialized attribute in database" do
72     set_user_from_auth :admin_trustedclient
73     fixed = Link.find_by_uuid(links(:has_symbol_keys_in_database_somehow).uuid)
74     assert_equal(["baz", "foo"], fixed.properties.keys.sort,
75                  "Hash symbol keys from DB did not get stringified.")
76     assert_equal(['waz', 'waz', 'waz', 1, nil, false, true],
77                  fixed.properties['baz'],
78                  "Array symbol values from DB did not get stringified.")
79     assert_equal true, fixed.save, "Failed to save fixed model back to db."
80   end
81
82   test "No HashWithIndifferentAccess in database" do
83     set_user_from_auth :admin_trustedclient
84     assert_raises ActiveRecord::RecordInvalid do
85       Link.create!(link_class: 'test',
86                    properties: {'foo' => 'bar'}.with_indifferent_access)
87     end
88   end
89
90   test "store long string" do
91     set_user_from_auth :active
92     longstring = "a"
93     while longstring.length < 2**16
94       longstring = longstring + longstring
95     end
96     g = Group.create! name: 'Has a long description', description: longstring
97     g = Group.find_by_uuid g.uuid
98     assert_equal g.description, longstring
99   end
100
101   [['uuid', {unique: true}],
102    ['owner_uuid', {}]].each do |the_column, requires|
103     test "unique index on all models with #{the_column}" do
104       checked = 0
105       ActiveRecord::Base.connection.tables.each do |table|
106         columns = ActiveRecord::Base.connection.columns(table)
107
108         next unless columns.collect(&:name).include? the_column
109
110         indexes = ActiveRecord::Base.connection.indexes(table).reject do |index|
111           requires.map do |key, val|
112             index.send(key) == val
113           end.include? false
114         end
115         assert_includes indexes.collect(&:columns), [the_column], 'no index'
116         checked += 1
117       end
118       # Sanity check: make sure we didn't just systematically miss everything.
119       assert_operator(10, :<, checked,
120                       "Only #{checked} tables have a #{the_column}?!")
121     end
122   end
123
124   test "search index exists on models that go into projects" do
125     all_tables =  ActiveRecord::Base.connection.tables
126     all_tables.delete 'schema_migrations'
127
128     all_tables.each do |table|
129       table_class = table.classify.constantize
130       if table_class.respond_to?('searchable_columns')
131         search_index_columns = table_class.searchable_columns('ilike')
132         # Disappointing, but text columns aren't indexed yet.
133         search_index_columns -= table_class.columns.select { |c|
134           c.type == :text or c.name == 'description' or c.name == 'file_names'
135         }.collect(&:name)
136
137         indexes = ActiveRecord::Base.connection.indexes(table)
138         search_index_by_columns = indexes.select do |index|
139           index.columns.sort == search_index_columns.sort
140         end
141         search_index_by_name = indexes.select do |index|
142           index.name == "#{table}_search_index"
143         end
144         assert !search_index_by_columns.empty?, "#{table} has no search index with columns #{search_index_columns}. Instead found search index with columns #{search_index_by_name.first.andand.columns}"
145       end
146     end
147   end
148
149   test "selectable_attributes includes database attributes" do
150     assert_includes(Job.selectable_attributes, "success")
151   end
152
153   test "selectable_attributes includes non-database attributes" do
154     assert_includes(Job.selectable_attributes, "node_uuids")
155   end
156
157   test "selectable_attributes includes common attributes in extensions" do
158     assert_includes(Job.selectable_attributes, "uuid")
159   end
160
161   test "selectable_attributes does not include unexposed attributes" do
162     refute_includes(Job.selectable_attributes, "nodes")
163   end
164
165   test "selectable_attributes on a non-default template" do
166     attr_a = Job.selectable_attributes(:common)
167     assert_includes(attr_a, "uuid")
168     refute_includes(attr_a, "success")
169   end
170
171   test 'create and retrieve using created_at time' do
172     set_user_from_auth :active
173     group = Group.create! name: 'test create and retrieve group'
174     assert group.valid?, "group is not valid"
175
176     results = Group.where(created_at: group.created_at)
177     assert_includes results.map(&:uuid), group.uuid,
178       "Expected new group uuid in results when searched with its created_at timestamp"
179   end
180
181   test 'create and update twice and expect different update times' do
182     set_user_from_auth :active
183     group = Group.create! name: 'test create and retrieve group'
184     assert group.valid?, "group is not valid"
185
186     # update 1
187     group.update_attributes!(name: "test create and update name 1")
188     results = Group.where(uuid: group.uuid)
189     assert_equal "test create and update name 1", results.first.name, "Expected name to be updated to 1"
190     updated_at_1 = results.first.updated_at.to_f
191
192     # update 2
193     group.update_attributes!(name: "test create and update name 2")
194     results = Group.where(uuid: group.uuid)
195     assert_equal "test create and update name 2", results.first.name, "Expected name to be updated to 2"
196     updated_at_2 = results.first.updated_at.to_f
197
198     assert_equal true, (updated_at_2 > updated_at_1), "Expected updated time 2 to be newer than 1"
199   end
200 end