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 :destroy => [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 { |k| props[k] }.compact
61 refute_includes(attributes, excluded_attr,
62 "log properties includes #{excluded_attr}")
64 yield props if block_given?
68 test "creating a user makes a log" do
69 set_user_from_auth :admin_trustedclient
70 u = User.new(first_name: "Log", last_name: "Test")
72 assert_logged(u, :create) do |props|
73 assert_equal(u.etag, props['new_etag'], "new user etag mismatch")
74 assert_equal(u.first_name, props['new_attributes']['first_name'],
75 "new user first name mismatch")
76 assert_equal(u.last_name, props['new_attributes']['last_name'],
77 "new user first name mismatch")
81 test "updating a virtual machine makes a log" do
82 set_user_from_auth :admin_trustedclient
83 vm = virtual_machines(:testvm)
85 vm.hostname = 'testvm.testshell'
87 assert_logged(vm, :update) do |props|
88 assert_equal(orig_etag, props['old_etag'], "updated VM old etag mismatch")
89 assert_equal(vm.etag, props['new_etag'], "updated VM new etag mismatch")
90 assert_equal('testvm.shell', props['old_attributes']['hostname'],
91 "updated VM old name mismatch")
92 assert_equal('testvm.testshell', props['new_attributes']['hostname'],
93 "updated VM new name mismatch")
97 test "old_attributes preserves values deep inside a hash" do
98 set_user_from_auth :active
99 it = specimens(:owned_by_active_user)
100 it.properties = {'foo' => {'bar' => ['baz', 'qux', {'quux' => 'bleat'}]}}
103 it.properties['foo']['bar'][2]['quux'] = 'blert'
105 assert_logged it, :update do |props|
106 assert_equal 'bleat', props['old_attributes']['properties']['foo']['bar'][2]['quux']
107 assert_equal 'blert', props['new_attributes']['properties']['foo']['bar'][2]['quux']
111 test "destroying an authorization makes a log" do
112 set_user_from_auth :admin_trustedclient
113 auth = api_client_authorizations(:spectator)
114 orig_etag = auth.etag
115 orig_attrs = auth.attributes
116 orig_attrs.delete 'api_token'
118 assert_logged(auth, :destroy) do |props|
119 assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
120 assert_equal(orig_attrs, props['old_attributes'],
121 "destroyed auth attributes mismatch")
125 test "saving an unchanged client still makes a log" do
126 set_user_from_auth :admin_trustedclient
127 client = api_clients(:untrusted)
128 client.is_trusted = client.is_trusted
130 assert_logged(client, :update) do |props|
131 ['old', 'new'].each do |age|
132 assert_equal(client.etag, props["#{age}_etag"],
133 "unchanged client #{age} etag mismatch")
134 assert_equal(client.attributes, props["#{age}_attributes"],
135 "unchanged client #{age} attributes mismatch")
140 test "updating a group twice makes two logs" do
141 set_user_from_auth :admin_trustedclient
142 group = groups(:empty_lonely_group)
144 name2 = "#{name1} under test"
147 assert_logged(group, :update) do |props|
148 assert_equal(name1, props['old_attributes']['name'],
149 "group start name mismatch")
150 assert_equal(name2, props['new_attributes']['name'],
151 "group updated name mismatch")
155 assert_logged(group, :update) do |props|
156 assert_equal(name2, props['old_attributes']['name'],
157 "group pre-revert name mismatch")
158 assert_equal(name1, props['new_attributes']['name'],
159 "group final name mismatch")
163 test "making a log doesn't get logged" do
164 set_user_from_auth :active_trustedclient
167 assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
170 test "non-admins can't modify or delete logs" do
171 set_user_from_auth :active_trustedclient
172 log = Log.new(summary: "immutable log test")
173 assert_nothing_raised { log.save! }
174 log.summary = "log mutation test should fail"
175 assert_raise(ArvadosModel::PermissionDeniedError) { log.save! }
176 assert_raise(ArvadosModel::PermissionDeniedError) { log.destroy }
179 test "admins can modify and delete logs" do
180 set_user_from_auth :admin_trustedclient
181 log = Log.new(summary: "admin log mutation test")
182 assert_nothing_raised { log.save! }
183 log.summary = "admin mutated log test"
184 assert_nothing_raised { log.save! }
185 assert_nothing_raised { log.destroy }
188 test "failure saving log causes failure saving object" do
190 alias_method :_orig_validations, :perform_validations
191 def perform_validations(options)
196 set_user_from_auth :active_trustedclient
197 user = users(:active)
198 user.first_name = 'Test'
199 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
202 alias_method :perform_validations, :_orig_validations
207 test "don't log changes only to ApiClientAuthorization.last_used_*" do
208 set_user_from_auth :admin_trustedclient
209 auth = api_client_authorizations(:spectator)
210 start_log_count = get_logs_about(auth).size
211 auth.last_used_at = Time.now
212 auth.last_used_by_ip_address = '::1'
214 assert_equal(start_log_count, get_logs_about(auth).size,
215 "log count changed after 'using' ApiClientAuthorization")
216 auth.created_by_ip_address = '::1'
218 assert_logged(auth, :update)
221 test "token isn't included in ApiClientAuthorization logs" do
222 set_user_from_auth :admin_trustedclient
223 auth = ApiClientAuthorization.new
224 auth.user = users(:spectator)
225 auth.api_client = api_clients(:untrusted)
227 assert_logged_with_clean_properties(auth, :create, 'api_token')
228 auth.expires_at = Time.now
230 assert_logged_with_clean_properties(auth, :update, 'api_token')
232 assert_logged_with_clean_properties(auth, :destroy, 'api_token')
235 test "use ownership and permission links to determine which logs a user can see" do
237 :admin_changes_repository2,
238 :admin_changes_specimen,
239 :system_adds_foo_file,
241 :log_owned_by_active,
242 :crunchstat_for_running_job]
244 c = Log.readable_by(users(:admin)).order("id asc").each.to_a
245 assert_log_result c, known_logs, known_logs
247 c = Log.readable_by(users(:active)).order("id asc").each.to_a
248 assert_log_result c, known_logs, [:admin_changes_repository2, # owned by active
249 :system_adds_foo_file, # readable via link
250 :system_adds_baz, # readable via 'all users' group
251 :log_owned_by_active, # log owned by active
252 :crunchstat_for_running_job] # log & job owned by active
254 c = Log.readable_by(users(:spectator)).order("id asc").each.to_a
255 assert_log_result c, known_logs, [:admin_changes_specimen, # owned by spectator
256 :system_adds_baz] # readable via 'all users' group
259 def assert_log_result result, known_logs, expected_logs
260 # All of expected_logs must appear in result. Additional logs can
261 # appear too, but only if they are _not_ listed in known_logs
262 # (i.e., we do not make any assertions about logs not mentioned in
263 # either "known" or "expected".)
264 result_ids = result.collect &:id
265 expected_logs.each do |want|
266 assert_includes result_ids, logs(want).id
268 (known_logs - expected_logs).each do |notwant|
269 refute_includes result_ids, logs(notwant).id
273 test "manifest_text not included in collection logs" do
274 act_as_system_user do
275 coll = Collection.create(manifest_text: ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo\n")
276 assert_logged_with_clean_properties(coll, :create, 'manifest_text')
277 coll.name = "testing"
279 assert_logged_with_clean_properties(coll, :update, 'manifest_text')
281 assert_logged_with_clean_properties(coll, :destroy, 'manifest_text')