3 class LogTest < ActiveSupport::TestCase
4 include CurrentApiClient
7 :create => [:created_at, :assert_nil, :assert_not_nil],
8 :update => [:modified_at, :assert_not_nil, :assert_not_nil],
9 :delete => [nil, :assert_not_nil, :assert_nil],
13 @start_time = Time.now
17 def assert_properties(test_method, event, props, *keys)
18 verb = (test_method == :assert_nil) ? 'have nil' : 'define'
19 keys.each do |prop_name|
20 assert_includes(props, prop_name, "log properties missing #{prop_name}")
21 self.send(test_method, props[prop_name],
22 "#{event.to_s} log should #{verb} #{prop_name}")
26 def get_logs_about(thing)
27 Log.where(object_uuid: thing.uuid).order("created_at ASC").all
30 def assert_logged(thing, event_type)
31 logs = get_logs_about(thing)
32 assert_equal(@log_count, logs.size, "log count mismatch")
35 props = log.properties
36 assert_equal(current_user.andand.uuid, log.owner_uuid,
37 "log is not owned by current user")
38 assert_equal(current_user.andand.uuid, log.modified_by_user_uuid,
39 "log is not 'modified by' current user")
40 assert_equal(current_api_client.andand.uuid, log.modified_by_client_uuid,
41 "log is not 'modified by' current client")
42 assert_equal(thing.uuid, log.object_uuid, "log UUID mismatch")
43 assert_equal(event_type.to_s, log.event_type, "log event type mismatch")
44 time_method, old_props_test, new_props_test = EVENT_TEST_METHODS[event_type]
45 if time_method.nil? or (timestamp = thing.send(time_method)).nil?
46 assert(log.event_at >= @start_time, "log timestamp too old")
48 assert_in_delta(timestamp, log.event_at, 1, "log timestamp mismatch")
50 assert_properties(old_props_test, event_type, props,
51 'old_etag', 'old_attributes')
52 assert_properties(new_props_test, event_type, props,
53 'new_etag', 'new_attributes')
54 yield props if block_given?
57 def assert_logged_with_clean_properties(obj, event_type, excluded_attr)
58 assert_logged(obj, event_type) do |props|
59 ['old_attributes', 'new_attributes'].map do |logattr|
60 attributes = props[logattr]
61 next if attributes.nil?
62 refute_includes(attributes, excluded_attr,
63 "log #{logattr} includes #{excluded_attr}")
65 yield props if block_given?
69 test "creating a user makes a log" do
70 set_user_from_auth :admin_trustedclient
71 u = User.new(first_name: "Log", last_name: "Test")
73 assert_logged(u, :create) do |props|
74 assert_equal(u.etag, props['new_etag'], "new user etag mismatch")
75 assert_equal(u.first_name, props['new_attributes']['first_name'],
76 "new user first name mismatch")
77 assert_equal(u.last_name, props['new_attributes']['last_name'],
78 "new user first name mismatch")
82 test "updating a virtual machine makes a log" do
83 set_user_from_auth :admin_trustedclient
84 vm = virtual_machines(:testvm)
86 vm.hostname = 'testvm.testshell'
88 assert_logged(vm, :update) do |props|
89 assert_equal(orig_etag, props['old_etag'], "updated VM old etag mismatch")
90 assert_equal(vm.etag, props['new_etag'], "updated VM new etag mismatch")
91 assert_equal('testvm.shell', props['old_attributes']['hostname'],
92 "updated VM old name mismatch")
93 assert_equal('testvm.testshell', props['new_attributes']['hostname'],
94 "updated VM new name mismatch")
98 test "old_attributes preserves values deep inside a hash" do
99 set_user_from_auth :active
100 it = specimens(:owned_by_active_user)
101 it.properties = {'foo' => {'bar' => ['baz', 'qux', {'quux' => 'bleat'}]}}
104 it.properties['foo']['bar'][2]['quux'] = 'blert'
106 assert_logged it, :update do |props|
107 assert_equal 'bleat', props['old_attributes']['properties']['foo']['bar'][2]['quux']
108 assert_equal 'blert', props['new_attributes']['properties']['foo']['bar'][2]['quux']
112 test "destroying an authorization makes a log" do
113 set_user_from_auth :admin_trustedclient
114 auth = api_client_authorizations(:spectator)
115 orig_etag = auth.etag
116 orig_attrs = auth.attributes
117 orig_attrs.delete 'api_token'
119 assert_logged(auth, :delete) do |props|
120 assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
121 assert_equal(orig_attrs, props['old_attributes'],
122 "destroyed auth attributes mismatch")
126 test "saving an unchanged client still makes a log" do
127 set_user_from_auth :admin_trustedclient
128 client = api_clients(:untrusted)
129 client.is_trusted = client.is_trusted
131 assert_logged(client, :update) do |props|
132 ['old', 'new'].each do |age|
133 assert_equal(client.etag, props["#{age}_etag"],
134 "unchanged client #{age} etag mismatch")
135 assert_equal(client.attributes, props["#{age}_attributes"],
136 "unchanged client #{age} attributes mismatch")
141 test "updating a group twice makes two logs" do
142 set_user_from_auth :admin_trustedclient
143 group = groups(:empty_lonely_group)
145 name2 = "#{name1} under test"
148 assert_logged(group, :update) do |props|
149 assert_equal(name1, props['old_attributes']['name'],
150 "group start name mismatch")
151 assert_equal(name2, props['new_attributes']['name'],
152 "group updated name mismatch")
156 assert_logged(group, :update) do |props|
157 assert_equal(name2, props['old_attributes']['name'],
158 "group pre-revert name mismatch")
159 assert_equal(name1, props['new_attributes']['name'],
160 "group final name mismatch")
164 test "making a log doesn't get logged" do
165 set_user_from_auth :active_trustedclient
168 assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
171 test "non-admins can't modify or delete logs" do
172 set_user_from_auth :active_trustedclient
173 log = Log.new(summary: "immutable log test")
174 assert_nothing_raised { log.save! }
175 log.summary = "log mutation test should fail"
176 assert_raise(ArvadosModel::PermissionDeniedError) { log.save! }
177 assert_raise(ArvadosModel::PermissionDeniedError) { log.destroy }
180 test "admins can modify and delete logs" do
181 set_user_from_auth :admin_trustedclient
182 log = Log.new(summary: "admin log mutation test")
183 assert_nothing_raised { log.save! }
184 log.summary = "admin mutated log test"
185 assert_nothing_raised { log.save! }
186 assert_nothing_raised { log.destroy }
189 test "failure saving log causes failure saving object" do
191 alias_method :_orig_validations, :perform_validations
192 def perform_validations(options)
197 set_user_from_auth :active_trustedclient
198 user = users(:active)
199 user.first_name = 'Test'
200 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
203 alias_method :perform_validations, :_orig_validations
208 test "don't log changes only to ApiClientAuthorization.last_used_*" do
209 set_user_from_auth :admin_trustedclient
210 auth = api_client_authorizations(:spectator)
211 start_log_count = get_logs_about(auth).size
212 auth.last_used_at = Time.now
213 auth.last_used_by_ip_address = '::1'
215 assert_equal(start_log_count, get_logs_about(auth).size,
216 "log count changed after 'using' ApiClientAuthorization")
217 auth.created_by_ip_address = '::1'
219 assert_logged(auth, :update)
222 test "token isn't included in ApiClientAuthorization logs" do
223 set_user_from_auth :admin_trustedclient
224 auth = ApiClientAuthorization.new
225 auth.user = users(:spectator)
226 auth.api_client = api_clients(:untrusted)
228 assert_logged_with_clean_properties(auth, :create, 'api_token')
229 auth.expires_at = Time.now
231 assert_logged_with_clean_properties(auth, :update, 'api_token')
233 assert_logged_with_clean_properties(auth, :delete, 'api_token')
236 test "use ownership and permission links to determine which logs a user can see" do
238 :admin_changes_repository2,
239 :admin_changes_specimen,
240 :system_adds_foo_file,
242 :log_owned_by_active,
243 :crunchstat_for_running_job]
245 c = Log.readable_by(users(:admin)).order("id asc").each.to_a
246 assert_log_result c, known_logs, known_logs
248 c = Log.readable_by(users(:active)).order("id asc").each.to_a
249 assert_log_result c, known_logs, [:admin_changes_repository2, # owned by active
250 :system_adds_foo_file, # readable via link
251 :system_adds_baz, # readable via 'all users' group
252 :log_owned_by_active, # log owned by active
253 :crunchstat_for_running_job] # log & job owned by active
255 c = Log.readable_by(users(:spectator)).order("id asc").each.to_a
256 assert_log_result c, known_logs, [:noop, # object_uuid is spectator
257 :admin_changes_specimen, # object_uuid is a specimen owned by spectator
258 :system_adds_baz] # readable via 'all users' group
261 def assert_log_result result, known_logs, expected_logs
262 # All of expected_logs must appear in result. Additional logs can
263 # appear too, but only if they are _not_ listed in known_logs
264 # (i.e., we do not make any assertions about logs not mentioned in
265 # either "known" or "expected".)
266 result_ids = result.collect(&:id)
267 expected_logs.each do |want|
268 assert_includes result_ids, logs(want).id
270 (known_logs - expected_logs).each do |notwant|
271 refute_includes result_ids, logs(notwant).id
275 test "non-empty configuration.unlogged_attributes" do
276 Rails.configuration.unlogged_attributes = ["manifest_text"]
277 txt = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n"
279 act_as_system_user do
280 coll = Collection.create(manifest_text: txt)
281 assert_logged_with_clean_properties(coll, :create, 'manifest_text')
282 coll.name = "testing"
284 assert_logged_with_clean_properties(coll, :update, 'manifest_text')
286 assert_logged_with_clean_properties(coll, :delete, 'manifest_text')
290 test "empty configuration.unlogged_attributes" do
291 Rails.configuration.unlogged_attributes = []
292 txt = ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n"
294 act_as_system_user do
295 coll = Collection.create(manifest_text: txt)
296 assert_logged(coll, :create) do |props|
297 assert_equal(txt, props['new_attributes']['manifest_text'])
299 coll.update_attributes!(name: "testing")
300 assert_logged(coll, :update) do |props|
301 assert_equal(txt, props['old_attributes']['manifest_text'])
302 assert_equal(txt, props['new_attributes']['manifest_text'])
305 assert_logged(coll, :delete) do |props|
306 assert_equal(txt, props['old_attributes']['manifest_text'])