Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / services / api / test / unit / log_test.rb
1 require 'test_helper'
2
3 class LogTest < ActiveSupport::TestCase
4   include CurrentApiClient
5
6   EVENT_TEST_METHODS = {
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],
10   }
11
12   def setup
13     @start_time = Time.now
14     @log_count = 1
15   end
16
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}")
23     end
24   end
25
26   def get_logs_about(thing)
27     Log.where(object_uuid: thing.uuid).order("created_at ASC").all
28   end
29
30   def assert_logged(thing, event_type)
31     logs = get_logs_about(thing)
32     assert_equal(@log_count, logs.size, "log count mismatch")
33     @log_count += 1
34     log = logs.last
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")
47     else
48       assert_in_delta(timestamp, log.event_at, 1, "log timestamp mismatch")
49     end
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?
55   end
56
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
60         .each do |attributes|
61         refute_includes(attributes, 'api_token',
62                         "auth log properties include sensitive API token")
63       end
64       yield props if block_given?
65     end
66   end
67
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
73   end
74
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")
78     u.save!
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")
85     end
86   end
87
88   test "updating a virtual machine makes a log" do
89     set_user_from_auth :admin_trustedclient
90     vm = virtual_machines(:testvm)
91     orig_etag = vm.etag
92     vm.hostname = 'testvm.testshell'
93     vm.save!
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")
101     end
102   end
103
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'
110     auth.destroy
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")
115     end
116   end
117
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
122     client.save!
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")
129       end
130     end
131   end
132
133   test "updating a group twice makes two logs" do
134     set_user_from_auth :admin_trustedclient
135     group = groups(:empty_lonely_group)
136     name1 = group.name
137     name2 = "#{name1} under test"
138     group.name = name2
139     group.save!
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")
145     end
146     group.name = name1
147     group.save!
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")
153     end
154   end
155
156   test "making a log doesn't get logged" do
157     set_user_from_auth :active_trustedclient
158     log = Log.new
159     log.save!
160     assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
161   end
162
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 }
170   end
171
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 }
179   end
180
181   test "failure saving log causes failure saving object" do
182     Log.class_eval do
183       alias_method :_orig_validations, :perform_validations
184       def perform_validations(options)
185         false
186       end
187     end
188     begin
189       set_user_from_auth :active_trustedclient
190       user = users(:active)
191       user.first_name = 'Test'
192       assert_raise(ActiveRecord::RecordInvalid) { user.save! }
193     ensure
194       Log.class_eval do
195         alias_method :perform_validations, :_orig_validations
196       end
197     end
198   end
199
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'
206     auth.save!
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'
210     auth.save!
211     assert_logged(auth, :update)
212   end
213
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)
219     auth.save!
220     assert_auth_logged_with_clean_properties(auth, :create)
221     auth.expires_at = Time.now
222     auth.save!
223     assert_auth_logged_with_clean_properties(auth, :update)
224     auth.destroy
225     assert_auth_logged_with_clean_properties(auth, :destroy)
226   end
227 end