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 assert_logged(thing, event_type)
26 logs = Log.where(object_uuid: thing.uuid).order("created_at ASC").all
27 assert_equal(@log_count, logs.size, "log count mismatch")
30 props = log.properties
31 assert_equal(system_user_uuid, log.owner_uuid,
32 "log is not owned by system user")
33 assert_equal(current_user.andand.uuid, log.modified_by_user_uuid,
34 "log is not 'modified by' current user")
35 assert_equal(current_api_client.andand.uuid, log.modified_by_client_uuid,
36 "log is not 'modified by' current client")
37 assert_equal(thing.kind, log.object_kind, "log kind mismatch")
38 assert_equal(thing.uuid, log.object_uuid, "log UUID mismatch")
39 assert_equal(event_type.to_s, log.event_type, "log event type mismatch")
40 time_method, old_props_test, new_props_test = EVENT_TEST_METHODS[event_type]
41 if time_method.nil? or (timestamp = thing.send(time_method)).nil?
42 assert(log.event_at >= @start_time, "log timestamp too old")
44 assert_in_delta(timestamp, log.event_at, 1, "log timestamp mismatch")
46 assert_properties(old_props_test, event_type, props,
47 'old_etag', 'old_attributes')
48 assert_properties(new_props_test, event_type, props,
49 'new_etag', 'new_attributes')
50 yield props if block_given?
53 def set_user_from_auth(auth_name)
54 client_auth = api_client_authorizations(auth_name)
55 Thread.current[:api_client_authorization] = client_auth
56 Thread.current[:api_client] = client_auth.api_client
57 Thread.current[:user] = client_auth.user
60 test "creating a user makes a log" do
61 set_user_from_auth :admin_trustedclient
62 u = User.new(first_name: "Log", last_name: "Test")
64 assert_logged(u, :create) do |props|
65 assert_equal(u.etag, props['new_etag'], "new user etag mismatch")
66 assert_equal(u.first_name, props['new_attributes']['first_name'],
67 "new user first name mismatch")
68 assert_equal(u.last_name, props['new_attributes']['last_name'],
69 "new user first name mismatch")
73 test "updating a virtual machine makes a log" do
74 set_user_from_auth :admin_trustedclient
75 vm = virtual_machines(:testvm)
77 vm.hostname = 'testvm.testshell'
79 assert_logged(vm, :update) do |props|
80 assert_equal(orig_etag, props['old_etag'], "updated VM old etag mismatch")
81 assert_equal(vm.etag, props['new_etag'], "updated VM new etag mismatch")
82 assert_equal('testvm.shell', props['old_attributes']['hostname'],
83 "updated VM old name mismatch")
84 assert_equal('testvm.testshell', props['new_attributes']['hostname'],
85 "updated VM new name mismatch")
89 test "destroying an authorization makes a log" do
90 set_user_from_auth :admin_trustedclient
91 auth = api_client_authorizations(:spectator)
93 orig_attrs = auth.attributes
95 assert_logged(auth, :destroy) do |props|
96 assert_equal(orig_etag, props['old_etag'], "destroyed auth etag mismatch")
97 assert_equal(orig_attrs, props['old_attributes'],
98 "destroyed auth attributes mismatch")
102 test "saving an unchanged client still makes a log" do
103 set_user_from_auth :admin_trustedclient
104 client = api_clients(:untrusted)
105 client.is_trusted = client.is_trusted
107 assert_logged(client, :update) do |props|
108 ['old', 'new'].each do |age|
109 assert_equal(client.etag, props["#{age}_etag"],
110 "unchanged client #{age} etag mismatch")
111 assert_equal(client.attributes, props["#{age}_attributes"],
112 "unchanged client #{age} attributes mismatch")
117 test "updating a group twice makes two logs" do
118 set_user_from_auth :admin_trustedclient
119 group = groups(:empty_lonely_group)
121 name2 = "#{name1} under test"
124 assert_logged(group, :update) do |props|
125 assert_equal(name1, props['old_attributes']['name'],
126 "group start name mismatch")
127 assert_equal(name2, props['new_attributes']['name'],
128 "group updated name mismatch")
132 assert_logged(group, :update) do |props|
133 assert_equal(name2, props['old_attributes']['name'],
134 "group pre-revert name mismatch")
135 assert_equal(name1, props['new_attributes']['name'],
136 "group final name mismatch")