Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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   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")
71     u.save!
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")
78     end
79   end
80
81   test "updating a virtual machine makes a log" do
82     set_user_from_auth :admin_trustedclient
83     vm = virtual_machines(:testvm)
84     orig_etag = vm.etag
85     vm.hostname = 'testvm.testshell'
86     vm.save!
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")
94     end
95   end
96
97   test "destroying an authorization makes a log" do
98     set_user_from_auth :admin_trustedclient
99     auth = api_client_authorizations(:spectator)
100     orig_etag = auth.etag
101     orig_attrs = auth.attributes
102     orig_attrs.delete 'api_token'
103     auth.destroy
104     assert_logged(auth, :destroy) do |props|
105       assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
106       assert_equal(orig_attrs, props['old_attributes'],
107                    "destroyed auth attributes mismatch")
108     end
109   end
110
111   test "saving an unchanged client still makes a log" do
112     set_user_from_auth :admin_trustedclient
113     client = api_clients(:untrusted)
114     client.is_trusted = client.is_trusted
115     client.save!
116     assert_logged(client, :update) do |props|
117       ['old', 'new'].each do |age|
118         assert_equal(client.etag, props["#{age}_etag"],
119                      "unchanged client #{age} etag mismatch")
120         assert_equal(client.attributes, props["#{age}_attributes"],
121                      "unchanged client #{age} attributes mismatch")
122       end
123     end
124   end
125
126   test "updating a group twice makes two logs" do
127     set_user_from_auth :admin_trustedclient
128     group = groups(:empty_lonely_group)
129     name1 = group.name
130     name2 = "#{name1} under test"
131     group.name = name2
132     group.save!
133     assert_logged(group, :update) do |props|
134       assert_equal(name1, props['old_attributes']['name'],
135                    "group start name mismatch")
136       assert_equal(name2, props['new_attributes']['name'],
137                    "group updated name mismatch")
138     end
139     group.name = name1
140     group.save!
141     assert_logged(group, :update) do |props|
142       assert_equal(name2, props['old_attributes']['name'],
143                    "group pre-revert name mismatch")
144       assert_equal(name1, props['new_attributes']['name'],
145                    "group final name mismatch")
146     end
147   end
148
149   test "making a log doesn't get logged" do
150     set_user_from_auth :active_trustedclient
151     log = Log.new
152     log.save!
153     assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
154   end
155
156   test "non-admins can't modify or delete logs" do
157     set_user_from_auth :active_trustedclient
158     log = Log.new(summary: "immutable log test")
159     assert_nothing_raised { log.save! }
160     log.summary = "log mutation test should fail"
161     assert_raise(ArvadosModel::PermissionDeniedError) { log.save! }
162     assert_raise(ArvadosModel::PermissionDeniedError) { log.destroy }
163   end
164
165   test "admins can modify and delete logs" do
166     set_user_from_auth :admin_trustedclient
167     log = Log.new(summary: "admin log mutation test")
168     assert_nothing_raised { log.save! }
169     log.summary = "admin mutated log test"
170     assert_nothing_raised { log.save! }
171     assert_nothing_raised { log.destroy }
172   end
173
174   test "failure saving log causes failure saving object" do
175     Log.class_eval do
176       alias_method :_orig_validations, :perform_validations
177       def perform_validations(options)
178         false
179       end
180     end
181     begin
182       set_user_from_auth :active_trustedclient
183       user = users(:active)
184       user.first_name = 'Test'
185       assert_raise(ActiveRecord::RecordInvalid) { user.save! }
186     ensure
187       Log.class_eval do
188         alias_method :perform_validations, :_orig_validations
189       end
190     end
191   end
192
193   test "don't log changes only to ApiClientAuthorization.last_used_*" do
194     set_user_from_auth :admin_trustedclient
195     auth = api_client_authorizations(:spectator)
196     start_log_count = get_logs_about(auth).size
197     auth.last_used_at = Time.now
198     auth.last_used_by_ip_address = '::1'
199     auth.save!
200     assert_equal(start_log_count, get_logs_about(auth).size,
201                  "log count changed after 'using' ApiClientAuthorization")
202     auth.created_by_ip_address = '::1'
203     auth.save!
204     assert_logged(auth, :update)
205   end
206
207   test "token isn't included in ApiClientAuthorization logs" do
208     set_user_from_auth :admin_trustedclient
209     auth = ApiClientAuthorization.new
210     auth.user = users(:spectator)
211     auth.api_client = api_clients(:untrusted)
212     auth.save!
213     assert_auth_logged_with_clean_properties(auth, :create)
214     auth.expires_at = Time.now
215     auth.save!
216     assert_auth_logged_with_clean_properties(auth, :update)
217     auth.destroy
218     assert_auth_logged_with_clean_properties(auth, :destroy)
219   end
220
221   test "use ownership and permission links to determine which logs a user can see" do
222     c = Log.readable_by(users(:admin)).order("id asc").each.to_a
223     assert_equal 6, c.size
224     assert_equal 1, c[0].id # no-op
225     assert_equal 2, c[1].id # admin changes repository foo, which is owned by active user
226     assert_equal 3, c[2].id # admin changes specimen owned_by_spectator
227     assert_equal 4, c[3].id # foo collection added, readable by active through link
228     assert_equal 5, c[4].id # baz collection added, readable by active and spectator through group 'all users' group membership
229     assert_equal 6, c[5].id # log_owned_by_active
230
231     c = Log.readable_by(users(:active)).order("id asc").each.to_a
232     assert_equal 4, c.size
233     assert_equal 2, c[0].id # admin changes repository foo, which is owned by active user
234     assert_equal 4, c[1].id # foo collection added, readable by active through link
235     assert_equal 5, c[2].id # baz collection added, readable by active and spectator through group 'all users' group membership
236     assert_equal 6, c[3].id # log_owned_by_active
237
238     c = Log.readable_by(users(:spectator)).order("id asc").each.to_a
239     assert_equal 2, c.size
240     assert_equal 3, c[0].id # admin changes specimen owned_by_spectator
241     assert_equal 5, c[1].id # baz collection added, readable by active and spectator through group 'all users' group membership
242   end
243 end