1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class ArvadosModelTest < ActiveSupport::TestCase
10 def create_with_attrs attrs
11 a = Collection.create({properties: {'foo' => 'bar'}}.merge(attrs))
15 test 'non-admin cannot assign uuid' do
16 set_user_from_auth :active_trustedclient
17 want_uuid = Collection.generate_uuid
18 a = create_with_attrs(uuid: want_uuid)
19 assert_nil a, "Non-admin should not assign uuid."
22 test 'admin can assign valid uuid' do
23 set_user_from_auth :admin_trustedclient
24 want_uuid = Collection.generate_uuid
25 a = create_with_attrs(uuid: want_uuid)
26 assert_equal want_uuid, a.uuid, "Admin should assign valid uuid."
27 assert a.uuid.length==27, "Auto assigned uuid length is wrong."
30 test 'admin cannot assign uuid with wrong object type' do
31 set_user_from_auth :admin_trustedclient
32 want_uuid = Group.generate_uuid
33 a = create_with_attrs(uuid: want_uuid)
34 assert_nil a, "Admin should not be able to assign invalid uuid."
37 test 'admin cannot assign badly formed uuid' do
38 set_user_from_auth :admin_trustedclient
39 a = create_with_attrs(uuid: "ntoheunthaoesunhasoeuhtnsaoeunhtsth")
40 assert_nil a, "Admin should not be able to assign invalid uuid."
43 test 'admin cannot assign empty uuid' do
44 set_user_from_auth :admin_trustedclient
45 a = create_with_attrs(uuid: "")
46 assert_nil a, "Admin cannot assign empty uuid."
51 {:a => ['foo', 'bar']},
52 {'a' => [:foo, 'bar']},
53 {'a' => ['foo', :bar]},
55 {:a => {'foo' => {'bar' => 'baz'}}},
56 {'a' => {:foo => {'bar' => 'baz'}}},
57 {'a' => {'foo' => {:bar => 'baz'}}},
58 {'a' => {'foo' => {'bar' => :baz}}},
59 {'a' => {'foo' => ['bar', :baz]}},
61 test "prevent symbol keys in serialized db columns: #{x.inspect}" do
62 set_user_from_auth :active
63 link = Link.create!(link_class: 'test',
65 raw = ActiveRecord::Base.connection.
66 select_value("select properties from links where uuid='#{link.uuid}'")
67 refute_match(/:[fb]/, raw)
72 {'a' => {['foo', :foo] => 'bar'}},
73 {'a' => {{'foo' => 'bar'} => 'bar'}},
74 {'a' => {['foo', :foo] => ['bar', 'baz']}},
76 test "refuse non-string keys in serialized db columns: #{x.inspect}" do
77 set_user_from_auth :active
78 assert_raises(ArgumentError) do
79 Link.create!(link_class: 'test',
85 test "No HashWithIndifferentAccess in database" do
86 set_user_from_auth :admin_trustedclient
87 link = Link.create!(link_class: 'test',
88 properties: {'foo' => 'bar'}.with_indifferent_access)
89 raw = ActiveRecord::Base.connection.
90 select_value("select properties from links where uuid='#{link.uuid}'")
91 assert_equal '{"foo": "bar"}', raw
94 test "store long string" do
95 set_user_from_auth :active
97 while longstring.length < 2**16
98 longstring = longstring + longstring
100 g = Group.create! name: 'Has a long description', description: longstring, group_class: "project"
101 g = Group.find_by_uuid g.uuid
102 assert_equal g.description, longstring
105 [['uuid', {unique: true}],
106 ['owner_uuid', {}]].each do |the_column, requires|
107 test "unique index on all models with #{the_column}" do
109 ActiveRecord::Base.connection.tables.each do |table|
110 columns = ActiveRecord::Base.connection.columns(table)
112 next unless columns.collect(&:name).include? the_column
114 indexes = ActiveRecord::Base.connection.indexes(table).reject do |index|
115 requires.map do |key, val|
116 index.send(key) == val
119 assert_includes indexes.collect(&:columns), [the_column], 'no index'
122 # Sanity check: make sure we didn't just systematically miss everything.
123 assert_operator(10, :<, checked,
124 "Only #{checked} tables have a #{the_column}?!")
128 test "search index exists on models that go into projects" do
129 ActiveRecord::Base.descendants.each do |model_class|
130 next if model_class.abstract_class?
131 next if !model_class.respond_to?('searchable_columns')
133 search_index_columns = model_class.searchable_columns('ilike')
134 # Disappointing, but text columns aren't indexed yet.
135 search_index_columns -= model_class.columns.select { |c|
136 c.type == :text or c.name == 'description' or c.name == 'file_names'
138 next if search_index_columns.empty?
140 indexes = ActiveRecord::Base.connection.indexes(model_class.table_name)
141 search_index_by_columns = indexes.select do |index|
142 # After rails 5.0 upgrade, AR::Base.connection.indexes() started to include
143 # GIN indexes, with its 'columns' attribute being a String like
145 index.columns.is_a?(Array) ? index.columns.sort == search_index_columns.sort : false
147 search_index_by_name = indexes.select do |index|
148 index.name == "#{model_class.table_name}_search_index"
150 assert !search_index_by_columns.empty?, "#{model_class.table_name} (#{model_class.to_s}) has no search index with columns #{search_index_columns}. Instead found search index with columns #{search_index_by_name.first.andand.columns}"
155 %w[collections collections_trgm_text_search_idx],
156 %w[container_requests container_requests_trgm_text_search_idx],
157 %w[groups groups_trgm_text_search_idx],
158 %w[workflows workflows_trgm_text_search_idx]
162 test "trigram index exists on #{table} model" do
163 table_class = table.classify.constantize
164 expect = table_class.full_text_searchable_columns
166 conn = ActiveRecord::Base.connection
167 conn.exec_query("SELECT indexdef FROM pg_indexes WHERE tablename = '#{table}' AND indexname = '#{indexname}'").each do |res|
168 searchable = res['indexdef'].scan(/COALESCE\(+([A-Za-z_]+)/).flatten
169 ok = (expect == searchable)
170 assert ok, "Invalid or no trigram index on #{table} named #{indexname}\nexpect: #{expect.inspect}\nfound: #{searchable}"
174 test "UUID and hash columns are excluded from #{table} full text index" do
175 class_name = model.first.classify
176 actual = class_name.constantize.full_text_searchable_columns
178 actual & full_text_excluded_columns, [],
179 "UUID/hash columns returned by #{class_name}.full_text_searchable_columns",
184 test "selectable_attributes includes database attributes" do
185 assert_includes(Collection.selectable_attributes, "name")
188 test "selectable_attributes includes non-database attributes" do
189 assert_includes(Collection.selectable_attributes, "unsigned_manifest_text")
192 test "selectable_attributes includes common attributes in extensions" do
193 assert_includes(Collection.selectable_attributes, "uuid")
196 test "selectable_attributes does not include unexposed attributes" do
197 refute_includes(Collection.selectable_attributes, "id")
200 test "selectable_attributes on a non-default template" do
201 attr_a = Collection.selectable_attributes(:common)
202 assert_includes(attr_a, "uuid")
203 refute_includes(attr_a, "name")
206 test 'create and retrieve using created_at time' do
207 set_user_from_auth :active
208 group = Group.create! name: 'test create and retrieve group', group_class: "project"
209 assert group.valid?, "group is not valid"
211 results = Group.where(created_at: group.created_at)
212 assert_includes results.map(&:uuid), group.uuid,
213 "Expected new group uuid in results when searched with its created_at timestamp"
216 test 'create and update twice and expect different update times' do
217 set_user_from_auth :active
218 group = Group.create! name: 'test create and retrieve group', group_class: "project"
219 assert group.valid?, "group is not valid"
222 group.update!(name: "test create and update name 1")
223 results = Group.where(uuid: group.uuid)
224 assert_equal "test create and update name 1", results.first.name, "Expected name to be updated to 1"
225 modified_at_1 = results.first.modified_at.to_f
228 group.update!(name: "test create and update name 2")
229 results = Group.where(uuid: group.uuid)
230 assert_equal "test create and update name 2", results.first.name, "Expected name to be updated to 2"
231 modified_at_2 = results.first.modified_at.to_f
233 assert_equal true, (modified_at_2 > modified_at_1), "Expected modified time 2 to be newer than 1"
236 test 'jsonb column' do
237 set_user_from_auth :active
239 c = Collection.create!(properties: {})
240 assert_equal({}, c.properties)
242 c.update(properties: {'foo' => 'foo'})
244 assert_equal({'foo' => 'foo'}, c.properties)
246 c.update(properties: nil)
248 assert_equal({}, c.properties)
250 c.update(properties: {foo: 'bar'})
251 assert_equal({'foo' => 'bar'}, c.properties)
253 assert_equal({'foo' => 'bar'}, c.properties)
256 test 'serialized attributes dirty tracking with audit log settings' do
257 Rails.configuration.AuditLogs.MaxDeleteBatch = 1000
258 set_user_from_auth :admin
259 [false, true].each do |auditlogs_enabled|
261 Rails.configuration.AuditLogs.MaxAge = 3600
263 Rails.configuration.AuditLogs.MaxAge = 0
265 tested_serialized = false
267 User.find_by_uuid(users(:active).uuid),
268 ContainerRequest.find_by_uuid(container_requests(:queued).uuid),
269 Container.find_by_uuid(containers(:queued).uuid),
270 Group.find_by_uuid(groups(:afiltergroup).uuid),
271 Collection.find_by_uuid(collections(:collection_with_one_property).uuid),
273 if !obj.class.serialized_attributes.empty?
274 tested_serialized = true
276 # obj shouldn't have changed since it's just retrieved from the database
277 assert_not(obj.changed?, "#{obj.class} model's attribute(s) appear as changed: '#{obj.changes.keys.join(',')}' with audit logs #{auditlogs_enabled ? '': 'not '}enabled.")
279 assert(tested_serialized, "did not test any models with serialized attributes")