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_auth_logged_with_clean_properties(auth, event_type)
58 assert_logged(auth, event_type) do |props|
59 ['old_attributes', 'new_attributes'].map { |k| props[k] }.compact
61 refute_includes(attributes, 'api_token',
62 "auth log properties include sensitive API token")
64 yield props if block_given?
68 def set_user_from_auth(auth_name)
69 client_auth = api_client_authorizations(auth_name)
70 Thread.current[:api_client_authorization] = client_auth
71 Thread.current[:api_client] = client_auth.api_client
72 Thread.current[:user] = client_auth.user
75 test "creating a user makes a log" do
76 set_user_from_auth :admin_trustedclient
77 u = User.new(first_name: "Log", last_name: "Test")
79 assert_logged(u, :create) do |props|
80 assert_equal(u.etag, props['new_etag'], "new user etag mismatch")
81 assert_equal(u.first_name, props['new_attributes']['first_name'],
82 "new user first name mismatch")
83 assert_equal(u.last_name, props['new_attributes']['last_name'],
84 "new user first name mismatch")
88 test "updating a virtual machine makes a log" do
89 set_user_from_auth :admin_trustedclient
90 vm = virtual_machines(:testvm)
92 vm.hostname = 'testvm.testshell'
94 assert_logged(vm, :update) do |props|
95 assert_equal(orig_etag, props['old_etag'], "updated VM old etag mismatch")
96 assert_equal(vm.etag, props['new_etag'], "updated VM new etag mismatch")
97 assert_equal('testvm.shell', props['old_attributes']['hostname'],
98 "updated VM old name mismatch")
99 assert_equal('testvm.testshell', props['new_attributes']['hostname'],
100 "updated VM new name mismatch")
104 test "destroying an authorization makes a log" do
105 set_user_from_auth :admin_trustedclient
106 auth = api_client_authorizations(:spectator)
107 orig_etag = auth.etag
108 orig_attrs = auth.attributes
109 orig_attrs.delete 'api_token'
111 assert_logged(auth, :destroy) do |props|
112 assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
113 assert_equal(orig_attrs, props['old_attributes'],
114 "destroyed auth attributes mismatch")
118 test "saving an unchanged client still makes a log" do
119 set_user_from_auth :admin_trustedclient
120 client = api_clients(:untrusted)
121 client.is_trusted = client.is_trusted
123 assert_logged(client, :update) do |props|
124 ['old', 'new'].each do |age|
125 assert_equal(client.etag, props["#{age}_etag"],
126 "unchanged client #{age} etag mismatch")
127 assert_equal(client.attributes, props["#{age}_attributes"],
128 "unchanged client #{age} attributes mismatch")
133 test "updating a group twice makes two logs" do
134 set_user_from_auth :admin_trustedclient
135 group = groups(:empty_lonely_group)
137 name2 = "#{name1} under test"
140 assert_logged(group, :update) do |props|
141 assert_equal(name1, props['old_attributes']['name'],
142 "group start name mismatch")
143 assert_equal(name2, props['new_attributes']['name'],
144 "group updated name mismatch")
148 assert_logged(group, :update) do |props|
149 assert_equal(name2, props['old_attributes']['name'],
150 "group pre-revert name mismatch")
151 assert_equal(name1, props['new_attributes']['name'],
152 "group final name mismatch")
156 test "making a log doesn't get logged" do
157 set_user_from_auth :active_trustedclient
160 assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
163 test "non-admins can't modify or delete logs" do
164 set_user_from_auth :active_trustedclient
165 log = Log.new(summary: "immutable log test")
166 assert_nothing_raised { log.save! }
167 log.summary = "log mutation test should fail"
168 assert_raise(ArvadosModel::PermissionDeniedError) { log.save! }
169 assert_raise(ArvadosModel::PermissionDeniedError) { log.destroy }
172 test "admins can modify and delete logs" do
173 set_user_from_auth :admin_trustedclient
174 log = Log.new(summary: "admin log mutation test")
175 assert_nothing_raised { log.save! }
176 log.summary = "admin mutated log test"
177 assert_nothing_raised { log.save! }
178 assert_nothing_raised { log.destroy }
181 test "failure saving log causes failure saving object" do
183 alias_method :_orig_validations, :perform_validations
184 def perform_validations(options)
189 set_user_from_auth :active_trustedclient
190 user = users(:active)
191 user.first_name = 'Test'
192 assert_raise(ActiveRecord::RecordInvalid) { user.save! }
195 alias_method :perform_validations, :_orig_validations
200 test "don't log changes only to ApiClientAuthorization.last_used_*" do
201 set_user_from_auth :admin_trustedclient
202 auth = api_client_authorizations(:spectator)
203 start_log_count = get_logs_about(auth).size
204 auth.last_used_at = Time.now
205 auth.last_used_by_ip_address = '::1'
207 assert_equal(start_log_count, get_logs_about(auth).size,
208 "log count changed after 'using' ApiClientAuthorization")
209 auth.created_by_ip_address = '::1'
211 assert_logged(auth, :update)
214 test "token isn't included in ApiClientAuthorization logs" do
215 set_user_from_auth :admin_trustedclient
216 auth = ApiClientAuthorization.new
217 auth.user = users(:spectator)
218 auth.api_client = api_clients(:untrusted)
220 assert_auth_logged_with_clean_properties(auth, :create)
221 auth.expires_at = Time.now
223 assert_auth_logged_with_clean_properties(auth, :update)
225 assert_auth_logged_with_clean_properties(auth, :destroy)