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) ? 'not include' : 'include'
19 keys.each do |prop_name|
20 self.send(test_method, props[prop_name],
21 "#{event.to_s} log should #{verb} #{prop_name}")
25 def get_logs_about(thing)
26 Log.where(object_uuid: thing.uuid).order("created_at ASC").all
29 def assert_logged(thing, event_type)
30 logs = get_logs_about(thing)
31 assert_equal(@log_count, logs.size, "log count mismatch")
34 props = log.properties
35 assert_equal(current_user.andand.uuid, log.owner_uuid,
36 "log is not owned by current user")
37 assert_equal(current_user.andand.uuid, log.modified_by_user_uuid,
38 "log is not 'modified by' current user")
39 assert_equal(current_api_client.andand.uuid, log.modified_by_client_uuid,
40 "log is not 'modified by' current client")
41 assert_equal(thing.kind, log.object_kind, "log kind mismatch")
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 set_user_from_auth(auth_name)
58 client_auth = api_client_authorizations(auth_name)
59 Thread.current[:api_client_authorization] = client_auth
60 Thread.current[:api_client] = client_auth.api_client
61 Thread.current[:user] = client_auth.user
64 test "creating a user makes a log" do
65 set_user_from_auth :admin_trustedclient
66 u = User.new(first_name: "Log", last_name: "Test")
68 assert_logged(u, :create) do |props|
69 assert_equal(u.etag, props['new_etag'], "new user etag mismatch")
70 assert_equal(u.first_name, props['new_attributes']['first_name'],
71 "new user first name mismatch")
72 assert_equal(u.last_name, props['new_attributes']['last_name'],
73 "new user first name mismatch")
77 test "updating a virtual machine makes a log" do
78 set_user_from_auth :admin_trustedclient
79 vm = virtual_machines(:testvm)
81 vm.hostname = 'testvm.testshell'
83 assert_logged(vm, :update) do |props|
84 assert_equal(orig_etag, props['old_etag'], "updated VM old etag mismatch")
85 assert_equal(vm.etag, props['new_etag'], "updated VM new etag mismatch")
86 assert_equal('testvm.shell', props['old_attributes']['hostname'],
87 "updated VM old name mismatch")
88 assert_equal('testvm.testshell', props['new_attributes']['hostname'],
89 "updated VM new name mismatch")
93 test "destroying an authorization makes a log" do
94 set_user_from_auth :admin_trustedclient
95 auth = api_client_authorizations(:spectator)
97 orig_attrs = auth.attributes
99 assert_logged(auth, :destroy) do |props|
100 assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
101 assert_equal(orig_attrs, props['old_attributes'],
102 "destroyed auth attributes mismatch")
106 test "saving an unchanged client still makes a log" do
107 set_user_from_auth :admin_trustedclient
108 client = api_clients(:untrusted)
109 client.is_trusted = client.is_trusted
111 assert_logged(client, :update) do |props|
112 ['old', 'new'].each do |age|
113 assert_equal(client.etag, props["#{age}_etag"],
114 "unchanged client #{age} etag mismatch")
115 assert_equal(client.attributes, props["#{age}_attributes"],
116 "unchanged client #{age} attributes mismatch")
121 test "updating a group twice makes two logs" do
122 set_user_from_auth :admin_trustedclient
123 group = groups(:empty_lonely_group)
125 name2 = "#{name1} under test"
128 assert_logged(group, :update) do |props|
129 assert_equal(name1, props['old_attributes']['name'],
130 "group start name mismatch")
131 assert_equal(name2, props['new_attributes']['name'],
132 "group updated name mismatch")
136 assert_logged(group, :update) do |props|
137 assert_equal(name2, props['old_attributes']['name'],
138 "group pre-revert name mismatch")
139 assert_equal(name1, props['new_attributes']['name'],
140 "group final name mismatch")
144 test "making a log doesn't get logged" do
145 set_user_from_auth :active_trustedclient
148 assert_equal(0, get_logs_about(log).size, "made a Log about a Log")
151 test "non-admins can't modify or delete logs" do
152 set_user_from_auth :active_trustedclient
153 log = Log.new(summary: "immutable log test")
154 assert_nothing_raised { log.save! }
155 log.summary = "log mutation test should fail"
156 assert_raise(ArvadosModel::PermissionDeniedError) { log.save! }
157 assert_raise(ArvadosModel::PermissionDeniedError) { log.destroy }
160 test "admins can modify and delete logs" do
161 set_user_from_auth :admin_trustedclient
162 log = Log.new(summary: "admin log mutation test")
163 assert_nothing_raised { log.save! }
164 log.summary = "admin mutated log test"
165 assert_nothing_raised { log.save! }
166 assert_nothing_raised { log.destroy }