audited 5.4.3 → 5.5.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +2 -1
- data/audited.gemspec +38 -0
- data/lib/audited/auditor.rb +2 -1
- data/lib/audited/version.rb +1 -1
- data/lib/audited.rb +6 -2
- metadata +18 -53
- data/.github/workflows/buildlight.yml +0 -15
- data/.github/workflows/ci.yml +0 -145
- data/.github/workflows/publish_gem.yml +0 -28
- data/.gitignore +0 -17
- data/.standard.yml +0 -5
- data/.yardopts +0 -3
- data/gemfiles/rails50.gemfile +0 -12
- data/gemfiles/rails51.gemfile +0 -12
- data/gemfiles/rails52.gemfile +0 -12
- data/gemfiles/rails60.gemfile +0 -10
- data/gemfiles/rails61.gemfile +0 -10
- data/gemfiles/rails70.gemfile +0 -10
- data/gemfiles/rails71.gemfile +0 -10
- data/spec/audited/audit_spec.rb +0 -357
- data/spec/audited/auditor_spec.rb +0 -1264
- data/spec/audited/rspec_matchers_spec.rb +0 -69
- data/spec/audited/sweeper_spec.rb +0 -133
- data/spec/audited_spec.rb +0 -14
- data/spec/audited_spec_helpers.rb +0 -36
- data/spec/rails_app/app/assets/config/manifest.js +0 -2
- data/spec/rails_app/config/application.rb +0 -42
- data/spec/rails_app/config/database.yml +0 -26
- data/spec/rails_app/config/environment.rb +0 -5
- data/spec/rails_app/config/environments/test.rb +0 -52
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails_app/config/initializers/inflections.rb +0 -2
- data/spec/rails_app/config/initializers/secret_token.rb +0 -3
- data/spec/rails_app/config/routes.rb +0 -3
- data/spec/spec_helper.rb +0 -24
- data/spec/support/active_record/models.rb +0 -181
- data/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb +0 -11
- data/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb +0 -11
- data/spec/support/active_record/schema.rb +0 -90
- data/test/db/version_1.rb +0 -17
- data/test/db/version_2.rb +0 -18
- data/test/db/version_3.rb +0 -18
- data/test/db/version_4.rb +0 -19
- data/test/db/version_5.rb +0 -17
- data/test/db/version_6.rb +0 -19
- data/test/install_generator_test.rb +0 -62
- data/test/test_helper.rb +0 -18
- data/test/upgrade_generator_test.rb +0 -97
@@ -1,1264 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
# not testing proxy_respond_to? hack / 2 methods / deprecation of `version`
|
4
|
-
# also, an additional 6 around `after_touch` for Versions before 6.
|
5
|
-
uncovered = (ActiveRecord::VERSION::MAJOR < 6) ? 15 : 9
|
6
|
-
SingleCov.covered! uncovered: uncovered
|
7
|
-
|
8
|
-
class ConditionalPrivateCompany < ::ActiveRecord::Base
|
9
|
-
self.table_name = "companies"
|
10
|
-
|
11
|
-
audited if: :foo?
|
12
|
-
|
13
|
-
private def foo?
|
14
|
-
true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class ConditionalCompany < ::ActiveRecord::Base
|
19
|
-
self.table_name = "companies"
|
20
|
-
|
21
|
-
audited if: :public?
|
22
|
-
|
23
|
-
def public?
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class ExclusiveCompany < ::ActiveRecord::Base
|
28
|
-
self.table_name = "companies"
|
29
|
-
audited if: proc { false }
|
30
|
-
end
|
31
|
-
|
32
|
-
class ExclusionaryCompany < ::ActiveRecord::Base
|
33
|
-
self.table_name = "companies"
|
34
|
-
|
35
|
-
audited unless: :non_profit?
|
36
|
-
|
37
|
-
def non_profit?
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class ExclusionaryCompany2 < ::ActiveRecord::Base
|
42
|
-
self.table_name = "companies"
|
43
|
-
audited unless: proc { |c| c.exclusive? }
|
44
|
-
|
45
|
-
def exclusive?
|
46
|
-
true
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
class InclusiveCompany < ::ActiveRecord::Base
|
51
|
-
self.table_name = "companies"
|
52
|
-
audited if: proc { true }
|
53
|
-
end
|
54
|
-
|
55
|
-
class InclusiveCompany2 < ::ActiveRecord::Base
|
56
|
-
self.table_name = "companies"
|
57
|
-
audited unless: proc { false }
|
58
|
-
end
|
59
|
-
|
60
|
-
class Secret < ::ActiveRecord::Base
|
61
|
-
audited
|
62
|
-
end
|
63
|
-
|
64
|
-
class Secret2 < ::ActiveRecord::Base
|
65
|
-
audited
|
66
|
-
self.non_audited_columns = ["delta", "top_secret", "created_at"]
|
67
|
-
end
|
68
|
-
|
69
|
-
describe Audited::Auditor do
|
70
|
-
describe "configuration" do
|
71
|
-
it "should include instance methods" do
|
72
|
-
expect(Models::ActiveRecord::User.new).to be_a_kind_of(Audited::Auditor::AuditedInstanceMethods)
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should include class methods" do
|
76
|
-
expect(Models::ActiveRecord::User).to be_a_kind_of(Audited::Auditor::AuditedClassMethods)
|
77
|
-
end
|
78
|
-
|
79
|
-
["created_at", "updated_at", "created_on", "updated_on", "lock_version", "id", "password"].each do |column|
|
80
|
-
it "should not audit #{column}" do
|
81
|
-
expect(Models::ActiveRecord::User.non_audited_columns).to include(column)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context "should be configurable which conditions are audited" do
|
86
|
-
subject { ConditionalCompany.new.send(:auditing_enabled) }
|
87
|
-
|
88
|
-
context "when condition method is private" do
|
89
|
-
subject { ConditionalPrivateCompany.new.send(:auditing_enabled) }
|
90
|
-
|
91
|
-
it { is_expected.to be_truthy }
|
92
|
-
end
|
93
|
-
|
94
|
-
context "when passing a method name" do
|
95
|
-
context "when conditions are true" do
|
96
|
-
before { allow_any_instance_of(ConditionalCompany).to receive(:public?).and_return(true) }
|
97
|
-
it { is_expected.to be_truthy }
|
98
|
-
end
|
99
|
-
|
100
|
-
context "when conditions are false" do
|
101
|
-
before { allow_any_instance_of(ConditionalCompany).to receive(:public?).and_return(false) }
|
102
|
-
it { is_expected.to be_falsey }
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
context "when passing a Proc" do
|
107
|
-
context "when conditions are true" do
|
108
|
-
subject { InclusiveCompany.new.send(:auditing_enabled) }
|
109
|
-
|
110
|
-
it { is_expected.to be_truthy }
|
111
|
-
end
|
112
|
-
|
113
|
-
context "when conditions are false" do
|
114
|
-
subject { ExclusiveCompany.new.send(:auditing_enabled) }
|
115
|
-
it { is_expected.to be_falsey }
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "should be configurable which conditions aren't audited" do
|
121
|
-
context "when using a method name" do
|
122
|
-
subject { ExclusionaryCompany.new.send(:auditing_enabled) }
|
123
|
-
|
124
|
-
context "when conditions are true" do
|
125
|
-
before { allow_any_instance_of(ExclusionaryCompany).to receive(:non_profit?).and_return(true) }
|
126
|
-
it { is_expected.to be_falsey }
|
127
|
-
end
|
128
|
-
|
129
|
-
context "when conditions are false" do
|
130
|
-
before { allow_any_instance_of(ExclusionaryCompany).to receive(:non_profit?).and_return(false) }
|
131
|
-
it { is_expected.to be_truthy }
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
context "when using a proc" do
|
136
|
-
context "when conditions are true" do
|
137
|
-
subject { ExclusionaryCompany2.new.send(:auditing_enabled) }
|
138
|
-
it { is_expected.to be_falsey }
|
139
|
-
end
|
140
|
-
|
141
|
-
context "when conditions are false" do
|
142
|
-
subject { InclusiveCompany2.new.send(:auditing_enabled) }
|
143
|
-
it { is_expected.to be_truthy }
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should be configurable which attributes are not audited via ignored_attributes" do
|
149
|
-
Audited.ignored_attributes = ["delta", "top_secret", "created_at", "updated_at"]
|
150
|
-
|
151
|
-
expect(Secret.non_audited_columns).to include("delta", "top_secret", "created_at")
|
152
|
-
end
|
153
|
-
|
154
|
-
it "should be configurable which attributes are not audited via non_audited_columns=" do
|
155
|
-
expect(Secret2.non_audited_columns).to include("delta", "top_secret", "created_at")
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should not save non-audited columns" do
|
159
|
-
previous = Models::ActiveRecord::User.non_audited_columns
|
160
|
-
begin
|
161
|
-
Models::ActiveRecord::User.non_audited_columns += [:favourite_device]
|
162
|
-
|
163
|
-
expect(create_user.audits.first.audited_changes.keys.any? { |col| ["favourite_device", "created_at", "updated_at", "password"].include?(col) }).to eq(false)
|
164
|
-
ensure
|
165
|
-
Models::ActiveRecord::User.non_audited_columns = previous
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
it "should not save other columns than specified in 'only' option" do
|
170
|
-
user = Models::ActiveRecord::UserOnlyPassword.create
|
171
|
-
user.instance_eval do
|
172
|
-
def non_column_attr
|
173
|
-
@non_column_attr
|
174
|
-
end
|
175
|
-
|
176
|
-
def non_column_attr=(val)
|
177
|
-
attribute_will_change!("non_column_attr")
|
178
|
-
@non_column_attr = val
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
user.password = "password"
|
183
|
-
user.non_column_attr = "some value"
|
184
|
-
user.save!
|
185
|
-
expect(user.audits.last.audited_changes.keys).to eq(%w[password])
|
186
|
-
end
|
187
|
-
|
188
|
-
it "should save attributes not specified in 'except' option" do
|
189
|
-
user = Models::ActiveRecord::User.create
|
190
|
-
user.instance_eval do
|
191
|
-
def non_column_attr
|
192
|
-
@non_column_attr
|
193
|
-
end
|
194
|
-
|
195
|
-
def non_column_attr=(val)
|
196
|
-
attribute_will_change!("non_column_attr")
|
197
|
-
@non_column_attr = val
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
user.password = "password"
|
202
|
-
user.non_column_attr = "some value"
|
203
|
-
user.save!
|
204
|
-
expect(user.audits.last.audited_changes.keys).to eq(%w[non_column_attr])
|
205
|
-
end
|
206
|
-
|
207
|
-
it "should redact columns specified in 'redacted' option" do
|
208
|
-
redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
|
209
|
-
user = Models::ActiveRecord::UserRedactedPassword.create(password: "password")
|
210
|
-
user.save!
|
211
|
-
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
212
|
-
user.password = "new_password"
|
213
|
-
user.save!
|
214
|
-
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
215
|
-
end
|
216
|
-
|
217
|
-
it "should redact columns specified in 'redacted' option when there are multiple specified" do
|
218
|
-
redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
|
219
|
-
user =
|
220
|
-
Models::ActiveRecord::UserMultipleRedactedAttributes.create(
|
221
|
-
password: "password"
|
222
|
-
)
|
223
|
-
user.save!
|
224
|
-
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
225
|
-
# Saving '[REDACTED]' value for 'ssn' even if value wasn't set explicitly when record was created
|
226
|
-
expect(user.audits.last.audited_changes["ssn"]).to eq(redacted)
|
227
|
-
|
228
|
-
user.password = "new_password"
|
229
|
-
user.ssn = 987654321
|
230
|
-
user.save!
|
231
|
-
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
232
|
-
expect(user.audits.last.audited_changes["ssn"]).to eq([redacted, redacted])
|
233
|
-
|
234
|
-
# If we haven't changed any attrs from 'redacted' list, audit should not contain these keys
|
235
|
-
user.name = "new name"
|
236
|
-
user.save!
|
237
|
-
expect(user.audits.last.audited_changes).to have_key("name")
|
238
|
-
expect(user.audits.last.audited_changes).not_to have_key("password")
|
239
|
-
expect(user.audits.last.audited_changes).not_to have_key("ssn")
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should redact columns in 'redacted' column with custom option" do
|
243
|
-
user = Models::ActiveRecord::UserRedactedPasswordCustomRedaction.create(password: "password")
|
244
|
-
user.save!
|
245
|
-
expect(user.audits.last.audited_changes["password"]).to eq(["My", "Custom", "Value", 7])
|
246
|
-
end
|
247
|
-
|
248
|
-
context "when ignored_default_callbacks is set" do
|
249
|
-
before { Audited.ignored_default_callbacks = [:create] }
|
250
|
-
after { Audited.ignored_default_callbacks = [] }
|
251
|
-
|
252
|
-
it "should remove create callback" do
|
253
|
-
class DefaultCallback < ::ActiveRecord::Base
|
254
|
-
audited
|
255
|
-
end
|
256
|
-
|
257
|
-
expect(DefaultCallback.audited_options[:on]).to eq([:update, :touch, :destroy])
|
258
|
-
end
|
259
|
-
|
260
|
-
it "should keep create callback if specified" do
|
261
|
-
class CallbacksSpecified < ::ActiveRecord::Base
|
262
|
-
audited on: [:create, :update, :destroy]
|
263
|
-
end
|
264
|
-
|
265
|
-
expect(CallbacksSpecified.audited_options[:on]).to eq([:create, :update, :destroy])
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
if ::ActiveRecord::VERSION::MAJOR >= 7
|
270
|
-
it "should filter encrypted attributes" do
|
271
|
-
user = Models::ActiveRecord::UserWithEncryptedPassword.create(password: "password")
|
272
|
-
user.save
|
273
|
-
expect(user.audits.last.audited_changes["password"]).to eq("[FILTERED]")
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
278
|
-
describe "'json' and 'jsonb' audited_changes column type" do
|
279
|
-
let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
|
280
|
-
|
281
|
-
after do
|
282
|
-
run_migrations(:down, migrations_path)
|
283
|
-
end
|
284
|
-
|
285
|
-
it "should work if column type is 'json'" do
|
286
|
-
run_migrations(:up, migrations_path, 1)
|
287
|
-
Audited::Audit.reset_column_information
|
288
|
-
expect(Audited::Audit.columns_hash["audited_changes"].sql_type).to eq("json")
|
289
|
-
|
290
|
-
user = Models::ActiveRecord::User.create
|
291
|
-
user.name = "new name"
|
292
|
-
user.save!
|
293
|
-
expect(user.audits.last.audited_changes).to eq({"name" => [nil, "new name"]})
|
294
|
-
end
|
295
|
-
|
296
|
-
it "should work if column type is 'jsonb'" do
|
297
|
-
run_migrations(:up, migrations_path, 2)
|
298
|
-
Audited::Audit.reset_column_information
|
299
|
-
expect(Audited::Audit.columns_hash["audited_changes"].sql_type).to eq("jsonb")
|
300
|
-
|
301
|
-
user = Models::ActiveRecord::User.create
|
302
|
-
user.name = "new name"
|
303
|
-
user.save!
|
304
|
-
expect(user.audits.last.audited_changes).to eq({"name" => [nil, "new name"]})
|
305
|
-
end
|
306
|
-
end
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
describe :new do
|
311
|
-
it "should allow mass assignment of all unprotected attributes" do
|
312
|
-
yesterday = 1.day.ago
|
313
|
-
|
314
|
-
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name: "name",
|
315
|
-
username: "username",
|
316
|
-
password: "password",
|
317
|
-
activated: true,
|
318
|
-
suspended_at: yesterday,
|
319
|
-
logins: 2)
|
320
|
-
|
321
|
-
expect(u.name).to eq("name")
|
322
|
-
expect(u.username).to eq("username")
|
323
|
-
expect(u.password).to eq("password")
|
324
|
-
expect(u.activated).to eq(true)
|
325
|
-
expect(u.suspended_at.to_i).to eq(yesterday.to_i)
|
326
|
-
expect(u.logins).to eq(2)
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
describe "on create" do
|
331
|
-
let(:user) { create_user status: :reliable, audit_comment: "Create" }
|
332
|
-
|
333
|
-
it "should change the audit count" do
|
334
|
-
expect {
|
335
|
-
user
|
336
|
-
}.to change(Audited::Audit, :count).by(1)
|
337
|
-
end
|
338
|
-
|
339
|
-
it "should create associated audit" do
|
340
|
-
expect(user.audits.count).to eq(1)
|
341
|
-
end
|
342
|
-
|
343
|
-
it "should set the action to create" do
|
344
|
-
expect(user.audits.first.action).to eq("create")
|
345
|
-
expect(Audited::Audit.creates.order(:id).last).to eq(user.audits.first)
|
346
|
-
expect(user.audits.creates.count).to eq(1)
|
347
|
-
expect(user.audits.updates.count).to eq(0)
|
348
|
-
expect(user.audits.destroys.count).to eq(0)
|
349
|
-
end
|
350
|
-
|
351
|
-
it "should store all the audited attributes" do
|
352
|
-
expect(user.audits.first.audited_changes).to eq(user.audited_attributes)
|
353
|
-
end
|
354
|
-
|
355
|
-
it "should store enum value" do
|
356
|
-
expect(user.audits.first.audited_changes["status"]).to eq(1)
|
357
|
-
end
|
358
|
-
|
359
|
-
context "when store_synthesized_enums is set to true" do
|
360
|
-
before { Audited.store_synthesized_enums = true }
|
361
|
-
after { Audited.store_synthesized_enums = false }
|
362
|
-
|
363
|
-
it "should store enum value as Rails synthesized value" do
|
364
|
-
expect(user.audits.first.audited_changes["status"]).to eq("reliable")
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
it "should store comment" do
|
369
|
-
expect(user.audits.first.comment).to eq("Create")
|
370
|
-
end
|
371
|
-
|
372
|
-
it "should not audit an attribute which is excepted if specified on create or destroy" do
|
373
|
-
on_create_destroy_except_name = Models::ActiveRecord::OnCreateDestroyExceptName.create(name: "Bart")
|
374
|
-
expect(on_create_destroy_except_name.audits.first.audited_changes.keys.any? { |col| ["name"].include? col }).to eq(false)
|
375
|
-
end
|
376
|
-
|
377
|
-
it "should not save an audit if only specified on update/destroy" do
|
378
|
-
expect {
|
379
|
-
Models::ActiveRecord::OnUpdateDestroy.create!(name: "Bart")
|
380
|
-
}.to_not change(Audited::Audit, :count)
|
381
|
-
end
|
382
|
-
|
383
|
-
it "should save readonly columns" do
|
384
|
-
expect {
|
385
|
-
Models::ActiveRecord::UserWithReadOnlyAttrs.create!(name: "Bart")
|
386
|
-
}.to change(Audited::Audit, :count)
|
387
|
-
end
|
388
|
-
end
|
389
|
-
|
390
|
-
describe "on update" do
|
391
|
-
before do
|
392
|
-
@user = create_user(name: "Brandon", status: :active, audit_comment: "Update")
|
393
|
-
end
|
394
|
-
|
395
|
-
it "should save an audit" do
|
396
|
-
expect {
|
397
|
-
@user.update_attribute(:name, "Someone")
|
398
|
-
}.to change(Audited::Audit, :count).by(1)
|
399
|
-
expect {
|
400
|
-
@user.update_attribute(:name, "Someone else")
|
401
|
-
}.to change(Audited::Audit, :count).by(1)
|
402
|
-
end
|
403
|
-
|
404
|
-
it "should set the action to 'update'" do
|
405
|
-
@user.update! name: "Changed"
|
406
|
-
expect(@user.audits.last.action).to eq("update")
|
407
|
-
expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
|
408
|
-
expect(@user.audits.updates.last).to eq(@user.audits.last)
|
409
|
-
end
|
410
|
-
|
411
|
-
it "should store the changed attributes" do
|
412
|
-
@user.update! name: "Changed"
|
413
|
-
expect(@user.audits.last.audited_changes).to eq({"name" => ["Brandon", "Changed"]})
|
414
|
-
end
|
415
|
-
|
416
|
-
it "should store changed enum values" do
|
417
|
-
@user.update! status: 1
|
418
|
-
expect(@user.audits.last.audited_changes["status"]).to eq([0, 1])
|
419
|
-
end
|
420
|
-
|
421
|
-
it "should store audit comment" do
|
422
|
-
expect(@user.audits.last.comment).to eq("Update")
|
423
|
-
end
|
424
|
-
|
425
|
-
it "should not save an audit if only specified on create/destroy" do
|
426
|
-
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(name: "Bart")
|
427
|
-
expect {
|
428
|
-
on_create_destroy.update! name: "Changed"
|
429
|
-
}.to_not change(Audited::Audit, :count)
|
430
|
-
end
|
431
|
-
|
432
|
-
it "should not save an audit if the value doesn't change after type casting" do
|
433
|
-
@user.update! logins: 0, activated: true
|
434
|
-
expect { @user.update_attribute :logins, "0" }.to_not change(Audited::Audit, :count)
|
435
|
-
expect { @user.update_attribute :activated, 1 }.to_not change(Audited::Audit, :count)
|
436
|
-
expect { @user.update_attribute :activated, "1" }.to_not change(Audited::Audit, :count)
|
437
|
-
end
|
438
|
-
|
439
|
-
context "with readonly attributes" do
|
440
|
-
before do
|
441
|
-
@user = create_user_with_readonly_attrs(status: "active")
|
442
|
-
end
|
443
|
-
|
444
|
-
it "should not save readonly columns" do
|
445
|
-
expect { @user.update! status: "banned" }.to_not change(Audited::Audit, :count)
|
446
|
-
end
|
447
|
-
end
|
448
|
-
|
449
|
-
describe "with no dirty changes" do
|
450
|
-
it "does not create an audit if the record is not changed" do
|
451
|
-
expect {
|
452
|
-
@user.save!
|
453
|
-
}.to_not change(Audited::Audit, :count)
|
454
|
-
end
|
455
|
-
|
456
|
-
it "creates an audit when an audit comment is present" do
|
457
|
-
expect {
|
458
|
-
@user.audit_comment = "Comment"
|
459
|
-
@user.save!
|
460
|
-
}.to change(Audited::Audit, :count)
|
461
|
-
end
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
if ::ActiveRecord::VERSION::MAJOR >= 6
|
466
|
-
describe "on touch" do
|
467
|
-
before do
|
468
|
-
@user = create_user(name: "Brandon", status: :active)
|
469
|
-
end
|
470
|
-
|
471
|
-
it "should save an audit" do
|
472
|
-
expect { @user.touch(:suspended_at) }.to change(Audited::Audit, :count).by(1)
|
473
|
-
end
|
474
|
-
|
475
|
-
it "should set the action to 'update'" do
|
476
|
-
@user.touch(:suspended_at)
|
477
|
-
expect(@user.audits.last.action).to eq("update")
|
478
|
-
expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
|
479
|
-
expect(@user.audits.updates.last).to eq(@user.audits.last)
|
480
|
-
end
|
481
|
-
|
482
|
-
it "should store the changed attributes" do
|
483
|
-
@user.touch(:suspended_at)
|
484
|
-
expect(@user.audits.last.audited_changes["suspended_at"][0]).to be_nil
|
485
|
-
expect(Time.parse(@user.audits.last.audited_changes["suspended_at"][1].to_s)).to be_within(2.seconds).of(Time.current)
|
486
|
-
end
|
487
|
-
|
488
|
-
it "should store audit comment" do
|
489
|
-
@user.audit_comment = "Here exists a touch comment"
|
490
|
-
@user.touch(:suspended_at)
|
491
|
-
expect(@user.audits.last.action).to eq("update")
|
492
|
-
expect(@user.audits.last.comment).to eq("Here exists a touch comment")
|
493
|
-
end
|
494
|
-
|
495
|
-
it "should not save an audit if only specified on create/destroy" do
|
496
|
-
on_create_destroy = Models::ActiveRecord::OnCreateDestroyUser.create(name: "Bart")
|
497
|
-
expect {
|
498
|
-
on_create_destroy.touch(:suspended_at)
|
499
|
-
}.to_not change(Audited::Audit, :count)
|
500
|
-
end
|
501
|
-
|
502
|
-
it "should store an audit if touch is the only audit" do
|
503
|
-
on_touch = Models::ActiveRecord::OnTouchOnly.create(name: "Bart")
|
504
|
-
expect {
|
505
|
-
on_touch.update(name: "NotBart")
|
506
|
-
}.to_not change(Audited::Audit, :count)
|
507
|
-
expect {
|
508
|
-
on_touch.touch(:suspended_at)
|
509
|
-
}.to change(on_touch.audits, :count).from(0).to(1)
|
510
|
-
|
511
|
-
@user.audits.destroy_all
|
512
|
-
expect(@user.audits).to be_empty
|
513
|
-
expect {
|
514
|
-
@user.touch(:suspended_at)
|
515
|
-
}.to change(@user.audits, :count).from(0).to(1)
|
516
|
-
end
|
517
|
-
|
518
|
-
context "don't double audit" do
|
519
|
-
let(:user) { Models::ActiveRecord::Owner.create(name: "OwnerUser", suspended_at: 1.month.ago, companies_attributes: [{name: "OwnedCompany"}]) }
|
520
|
-
let(:company) { user.companies.first }
|
521
|
-
|
522
|
-
it "should only create 1 (create) audit for object" do
|
523
|
-
expect(user.audits.count).to eq(1)
|
524
|
-
expect(user.audits.first.action).to eq("create")
|
525
|
-
end
|
526
|
-
|
527
|
-
it "should only create 1 (create) audit for nested resource" do
|
528
|
-
expect(company.audits.count).to eq(1)
|
529
|
-
expect(company.audits.first.action).to eq("create")
|
530
|
-
end
|
531
|
-
|
532
|
-
context "after creating" do
|
533
|
-
it "updating / touching nested resource shouldn't save touch audit on parent object" do
|
534
|
-
expect { company.touch(:type) }.not_to change(user.audits, :count)
|
535
|
-
expect { company.update(type: "test") }.not_to change(user.audits, :count)
|
536
|
-
end
|
537
|
-
|
538
|
-
it "updating / touching parent object shouldn't save previous data" do
|
539
|
-
expect { user.touch(:suspended_at) }.to change(user.audits, :count).from(1).to(2)
|
540
|
-
expect(user.audits.last.action).to eq("update")
|
541
|
-
expect(user.audits.last.audited_changes.keys).to eq(%w[suspended_at])
|
542
|
-
end
|
543
|
-
end
|
544
|
-
|
545
|
-
context "after updating" do
|
546
|
-
it "changing nested resource shouldn't audit owner" do
|
547
|
-
expect { user.update(username: "test") }.to change(user.audits, :count).from(1).to(2)
|
548
|
-
expect { company.update(type: "test") }.not_to change(user.audits, :count)
|
549
|
-
|
550
|
-
expect { user.touch(:suspended_at) }.to change(user.audits, :count).from(2).to(3)
|
551
|
-
expect { company.update(type: "another_test") }.not_to change(user.audits, :count)
|
552
|
-
end
|
553
|
-
end
|
554
|
-
end
|
555
|
-
end
|
556
|
-
end
|
557
|
-
|
558
|
-
describe "on destroy" do
|
559
|
-
before do
|
560
|
-
@user = create_user(status: :active)
|
561
|
-
end
|
562
|
-
|
563
|
-
it "should save an audit" do
|
564
|
-
expect {
|
565
|
-
@user.destroy
|
566
|
-
}.to change(Audited::Audit, :count)
|
567
|
-
|
568
|
-
expect(@user.audits.size).to eq(2)
|
569
|
-
end
|
570
|
-
|
571
|
-
it "should set the action to 'destroy'" do
|
572
|
-
@user.destroy
|
573
|
-
|
574
|
-
expect(@user.audits.last.action).to eq("destroy")
|
575
|
-
expect(Audited::Audit.destroys.order(:id).last).to eq(@user.audits.last)
|
576
|
-
expect(@user.audits.destroys.last).to eq(@user.audits.last)
|
577
|
-
end
|
578
|
-
|
579
|
-
it "should store all of the audited attributes" do
|
580
|
-
@user.destroy
|
581
|
-
|
582
|
-
expect(@user.audits.last.audited_changes).to eq(@user.audited_attributes)
|
583
|
-
end
|
584
|
-
|
585
|
-
it "should store enum value" do
|
586
|
-
@user.destroy
|
587
|
-
expect(@user.audits.last.audited_changes["status"]).to eq(0)
|
588
|
-
end
|
589
|
-
|
590
|
-
it "should be able to reconstruct a destroyed record without history" do
|
591
|
-
@user.audits.delete_all
|
592
|
-
@user.destroy
|
593
|
-
|
594
|
-
revision = @user.audits.first.revision
|
595
|
-
expect(revision.name).to eq(@user.name)
|
596
|
-
end
|
597
|
-
|
598
|
-
it "should not save an audit if only specified on create/update" do
|
599
|
-
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(name: "Bart")
|
600
|
-
|
601
|
-
expect {
|
602
|
-
on_create_update.destroy
|
603
|
-
}.to_not change(Audited::Audit, :count)
|
604
|
-
end
|
605
|
-
|
606
|
-
it "should audit dependent destructions" do
|
607
|
-
owner = Models::ActiveRecord::Owner.create!
|
608
|
-
company = owner.companies.create!
|
609
|
-
|
610
|
-
expect {
|
611
|
-
owner.destroy
|
612
|
-
}.to change(Audited::Audit, :count)
|
613
|
-
|
614
|
-
expect(company.audits.map { |a| a.action }).to eq(["create", "destroy"])
|
615
|
-
end
|
616
|
-
end
|
617
|
-
|
618
|
-
describe "on destroy with unsaved object" do
|
619
|
-
let(:user) { Models::ActiveRecord::User.new }
|
620
|
-
|
621
|
-
it "should not audit on 'destroy'" do
|
622
|
-
expect {
|
623
|
-
user.destroy
|
624
|
-
}.to_not raise_error
|
625
|
-
|
626
|
-
expect(user.audits).to be_empty
|
627
|
-
end
|
628
|
-
end
|
629
|
-
|
630
|
-
describe "associated with" do
|
631
|
-
let(:owner) { Models::ActiveRecord::Owner.create(name: "Models::ActiveRecord::Owner") }
|
632
|
-
let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
633
|
-
|
634
|
-
it "should record the associated object on create" do
|
635
|
-
expect(owned_company.audits.first.associated).to eq(owner)
|
636
|
-
end
|
637
|
-
|
638
|
-
it "should store the associated object on update" do
|
639
|
-
owned_company.update_attribute(:name, "The Auditors")
|
640
|
-
expect(owned_company.audits.last.associated).to eq(owner)
|
641
|
-
end
|
642
|
-
|
643
|
-
it "should store the associated object on destroy" do
|
644
|
-
owned_company.destroy
|
645
|
-
expect(owned_company.audits.last.associated).to eq(owner)
|
646
|
-
end
|
647
|
-
end
|
648
|
-
|
649
|
-
describe "has associated audits" do
|
650
|
-
let!(:owner) { Models::ActiveRecord::Owner.create!(name: "Models::ActiveRecord::Owner") }
|
651
|
-
let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
652
|
-
|
653
|
-
it "should list the associated audits" do
|
654
|
-
expect(owner.associated_audits.length).to eq(1)
|
655
|
-
expect(owner.associated_audits.first.auditable).to eq(owned_company)
|
656
|
-
end
|
657
|
-
end
|
658
|
-
|
659
|
-
describe "max_audits" do
|
660
|
-
it "should respect global setting" do
|
661
|
-
stub_global_max_audits(10) do
|
662
|
-
expect(Models::ActiveRecord::User.audited_options[:max_audits]).to eq(10)
|
663
|
-
end
|
664
|
-
end
|
665
|
-
|
666
|
-
it "should respect per model setting" do
|
667
|
-
stub_global_max_audits(10) do
|
668
|
-
expect(Models::ActiveRecord::MaxAuditsUser.audited_options[:max_audits]).to eq(5)
|
669
|
-
end
|
670
|
-
end
|
671
|
-
|
672
|
-
it "should delete old audits when keeped amount exceeded" do
|
673
|
-
stub_global_max_audits(2) do
|
674
|
-
user = create_versions(2)
|
675
|
-
user.update(name: "John")
|
676
|
-
expect(user.audits.pluck(:version)).to eq([2, 3])
|
677
|
-
end
|
678
|
-
end
|
679
|
-
|
680
|
-
it "should not delete old audits when keeped amount not exceeded" do
|
681
|
-
stub_global_max_audits(3) do
|
682
|
-
user = create_versions(2)
|
683
|
-
user.update(name: "John")
|
684
|
-
expect(user.audits.pluck(:version)).to eq([1, 2, 3])
|
685
|
-
end
|
686
|
-
end
|
687
|
-
|
688
|
-
it "should delete old extra audits after introducing limit" do
|
689
|
-
stub_global_max_audits(nil) do
|
690
|
-
user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
|
691
|
-
user.update!(name: "Foobar")
|
692
|
-
user.update!(name: "Awesome", username: "keepers")
|
693
|
-
user.update!(activated: true)
|
694
|
-
|
695
|
-
Audited.max_audits = 3
|
696
|
-
Models::ActiveRecord::User.send(:normalize_audited_options)
|
697
|
-
user.update!(favourite_device: "Android Phone")
|
698
|
-
audits = user.audits
|
699
|
-
|
700
|
-
expect(audits.count).to eq(3)
|
701
|
-
expect(audits[0].audited_changes).to include({"name" => ["Foobar", "Awesome"], "username" => ["brandon", "keepers"]})
|
702
|
-
expect(audits[1].audited_changes).to eq({"activated" => [nil, true]})
|
703
|
-
expect(audits[2].audited_changes).to eq({"favourite_device" => [nil, "Android Phone"]})
|
704
|
-
end
|
705
|
-
end
|
706
|
-
|
707
|
-
it "should add comment line for combined audit" do
|
708
|
-
stub_global_max_audits(2) do
|
709
|
-
user = Models::ActiveRecord::User.create!(name: "Foobar 1")
|
710
|
-
user.update(name: "Foobar 2", audit_comment: "First audit comment")
|
711
|
-
user.update(name: "Foobar 3", audit_comment: "Second audit comment")
|
712
|
-
expect(user.audits.first.comment).to match(/First audit comment.+is the result of multiple/m)
|
713
|
-
end
|
714
|
-
end
|
715
|
-
|
716
|
-
def stub_global_max_audits(max_audits)
|
717
|
-
previous_max_audits = Audited.max_audits
|
718
|
-
previous_user_audited_options = Models::ActiveRecord::User.audited_options.dup
|
719
|
-
begin
|
720
|
-
Audited.max_audits = max_audits
|
721
|
-
Models::ActiveRecord::User.send(:normalize_audited_options) # reloads audited_options
|
722
|
-
yield
|
723
|
-
ensure
|
724
|
-
Audited.max_audits = previous_max_audits
|
725
|
-
Models::ActiveRecord::User.audited_options = previous_user_audited_options
|
726
|
-
end
|
727
|
-
end
|
728
|
-
end
|
729
|
-
|
730
|
-
describe "revisions" do
|
731
|
-
let(:user) { create_versions }
|
732
|
-
|
733
|
-
it "should return an Array of Users" do
|
734
|
-
expect(user.revisions).to be_a_kind_of(Array)
|
735
|
-
user.revisions.each { |version| expect(version).to be_a_kind_of Models::ActiveRecord::User }
|
736
|
-
end
|
737
|
-
|
738
|
-
it "should have one revision for a new record" do
|
739
|
-
expect(create_user.revisions.size).to eq(1)
|
740
|
-
end
|
741
|
-
|
742
|
-
it "should have one revision for each audit" do
|
743
|
-
expect(user.audits.size).to eql(user.revisions.size)
|
744
|
-
end
|
745
|
-
|
746
|
-
it "should set the attributes for each revision" do
|
747
|
-
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
748
|
-
u.update! name: "Foobar"
|
749
|
-
u.update! name: "Awesome", username: "keepers"
|
750
|
-
|
751
|
-
expect(u.revisions.size).to eql(3)
|
752
|
-
|
753
|
-
expect(u.revisions[0].name).to eql("Brandon")
|
754
|
-
expect(u.revisions[0].username).to eql("brandon")
|
755
|
-
|
756
|
-
expect(u.revisions[1].name).to eql("Foobar")
|
757
|
-
expect(u.revisions[1].username).to eql("brandon")
|
758
|
-
|
759
|
-
expect(u.revisions[2].name).to eql("Awesome")
|
760
|
-
expect(u.revisions[2].username).to eql("keepers")
|
761
|
-
end
|
762
|
-
|
763
|
-
it "access to only recent revisions" do
|
764
|
-
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
765
|
-
u.update! name: "Foobar"
|
766
|
-
u.update! name: "Awesome", username: "keepers"
|
767
|
-
|
768
|
-
expect(u.revisions(2).size).to eq(2)
|
769
|
-
|
770
|
-
expect(u.revisions(2)[0].name).to eq("Foobar")
|
771
|
-
expect(u.revisions(2)[0].username).to eq("brandon")
|
772
|
-
|
773
|
-
expect(u.revisions(2)[1].name).to eq("Awesome")
|
774
|
-
expect(u.revisions(2)[1].username).to eq("keepers")
|
775
|
-
end
|
776
|
-
|
777
|
-
it "should be empty if no audits exist" do
|
778
|
-
user.audits.delete_all
|
779
|
-
expect(user.revisions).to be_empty
|
780
|
-
end
|
781
|
-
|
782
|
-
it "should ignore attributes that have been deleted" do
|
783
|
-
user.audits.last.update! audited_changes: {old_attribute: "old value"}
|
784
|
-
expect { user.revisions }.to_not raise_error
|
785
|
-
end
|
786
|
-
end
|
787
|
-
|
788
|
-
describe "revisions" do
|
789
|
-
let(:user) { create_versions(5) }
|
790
|
-
|
791
|
-
it "should maintain identity" do
|
792
|
-
expect(user.revision(1)).to eq(user)
|
793
|
-
end
|
794
|
-
|
795
|
-
it "should find the given revision" do
|
796
|
-
revision = user.revision(3)
|
797
|
-
expect(revision).to be_a_kind_of(Models::ActiveRecord::User)
|
798
|
-
expect(revision.audit_version).to eq(3)
|
799
|
-
expect(revision.name).to eq("Foobar 3")
|
800
|
-
end
|
801
|
-
|
802
|
-
it "should find the previous revision with :previous" do
|
803
|
-
revision = user.revision(:previous)
|
804
|
-
expect(revision.audit_version).to eq(4)
|
805
|
-
# expect(revision).to eq(user.revision(4))
|
806
|
-
expect(revision.attributes).to eq(user.revision(4).attributes)
|
807
|
-
end
|
808
|
-
|
809
|
-
it "should be able to get the previous revision repeatedly" do
|
810
|
-
previous = user.revision(:previous)
|
811
|
-
expect(previous.audit_version).to eq(4)
|
812
|
-
expect(previous.revision(:previous).audit_version).to eq(3)
|
813
|
-
end
|
814
|
-
|
815
|
-
it "should be able to set protected attributes" do
|
816
|
-
u = Models::ActiveRecord::User.create(name: "Brandon")
|
817
|
-
u.update_attribute :logins, 1
|
818
|
-
u.update_attribute :logins, 2
|
819
|
-
|
820
|
-
expect(u.revision(3).logins).to eq(2)
|
821
|
-
expect(u.revision(2).logins).to eq(1)
|
822
|
-
expect(u.revision(1).logins).to eq(0)
|
823
|
-
end
|
824
|
-
|
825
|
-
it "should set attributes directly" do
|
826
|
-
u = Models::ActiveRecord::User.create(name: "<Joe>")
|
827
|
-
expect(u.revision(1).name).to eq("<Joe>")
|
828
|
-
end
|
829
|
-
|
830
|
-
it "should set the attributes for each revision" do
|
831
|
-
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
832
|
-
u.update! name: "Foobar"
|
833
|
-
u.update! name: "Awesome", username: "keepers"
|
834
|
-
|
835
|
-
expect(u.revision(3).name).to eq("Awesome")
|
836
|
-
expect(u.revision(3).username).to eq("keepers")
|
837
|
-
|
838
|
-
expect(u.revision(2).name).to eq("Foobar")
|
839
|
-
expect(u.revision(2).username).to eq("brandon")
|
840
|
-
|
841
|
-
expect(u.revision(1).name).to eq("Brandon")
|
842
|
-
expect(u.revision(1).username).to eq("brandon")
|
843
|
-
end
|
844
|
-
|
845
|
-
it "should correctly restore revision with enum" do
|
846
|
-
u = Models::ActiveRecord::User.create(status: :active)
|
847
|
-
u.update_attribute(:status, :reliable)
|
848
|
-
u.update_attribute(:status, :banned)
|
849
|
-
|
850
|
-
expect(u.revision(3)).to be_banned
|
851
|
-
expect(u.revision(2)).to be_reliable
|
852
|
-
expect(u.revision(1)).to be_active
|
853
|
-
end
|
854
|
-
|
855
|
-
it "should be able to get time for first revision" do
|
856
|
-
suspended_at = Time.zone.now
|
857
|
-
u = Models::ActiveRecord::User.create(suspended_at: suspended_at)
|
858
|
-
expect(u.revision(1).suspended_at.to_s).to eq(suspended_at.to_s)
|
859
|
-
end
|
860
|
-
|
861
|
-
it "should not raise an error when no previous audits exist" do
|
862
|
-
user.audits.destroy_all
|
863
|
-
expect { user.revision(:previous) }.to_not raise_error
|
864
|
-
end
|
865
|
-
|
866
|
-
it "should mark revision's attributes as changed" do
|
867
|
-
expect(user.revision(1).name_changed?).to eq(true)
|
868
|
-
end
|
869
|
-
|
870
|
-
it "should record new audit when saving revision" do
|
871
|
-
expect {
|
872
|
-
user.revision(1).save!
|
873
|
-
}.to change(user.audits, :count).by(1)
|
874
|
-
end
|
875
|
-
|
876
|
-
it "should re-insert destroyed records" do
|
877
|
-
user.destroy
|
878
|
-
expect {
|
879
|
-
user.revision(1).save!
|
880
|
-
}.to change(Models::ActiveRecord::User, :count).by(1)
|
881
|
-
end
|
882
|
-
|
883
|
-
it "should return nil for values greater than the number of revisions" do
|
884
|
-
expect(user.revision(user.revisions.count + 1)).to be_nil
|
885
|
-
end
|
886
|
-
|
887
|
-
it "should work with array attributes" do
|
888
|
-
u = Models::ActiveRecord::User.create!(phone_numbers: ["+1 800-444-4444"])
|
889
|
-
u.update!(phone_numbers: ["+1 804-222-1111", "+1 317 222-2222"])
|
890
|
-
|
891
|
-
expect(u.revision(0).phone_numbers).to eq(["+1 804-222-1111", "+1 317 222-2222"])
|
892
|
-
expect(u.revision(1).phone_numbers).to eq(["+1 800-444-4444"])
|
893
|
-
end
|
894
|
-
end
|
895
|
-
|
896
|
-
describe "revision_at" do
|
897
|
-
let(:user) { create_user }
|
898
|
-
|
899
|
-
it "should find the latest revision before the given time" do
|
900
|
-
audit = user.audits.first
|
901
|
-
audit.created_at = 1.hour.ago
|
902
|
-
audit.save!
|
903
|
-
user.update! name: "updated"
|
904
|
-
expect(user.revision_at(2.minutes.ago).audit_version).to eq(1)
|
905
|
-
end
|
906
|
-
|
907
|
-
it "should be nil if given a time before audits" do
|
908
|
-
expect(user.revision_at(1.week.ago)).to be_nil
|
909
|
-
end
|
910
|
-
end
|
911
|
-
|
912
|
-
describe "own_and_associated_audits" do
|
913
|
-
it "should return audits for self and associated audits" do
|
914
|
-
owner = Models::ActiveRecord::Owner.create!
|
915
|
-
company = owner.companies.create!
|
916
|
-
company.update!(name: "Collective Idea")
|
917
|
-
|
918
|
-
other_owner = Models::ActiveRecord::Owner.create!
|
919
|
-
other_owner.companies.create!
|
920
|
-
|
921
|
-
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
922
|
-
end
|
923
|
-
|
924
|
-
it "should return audits for STI classes" do
|
925
|
-
# Where parent is STI
|
926
|
-
sti_company = Models::ActiveRecord::Company::STICompany.create!
|
927
|
-
sti_company.update!(name: "Collective Idea")
|
928
|
-
expect(sti_company.own_and_associated_audits).to match_array(sti_company.audits)
|
929
|
-
|
930
|
-
# Where associated is STI
|
931
|
-
owner = Models::ActiveRecord::Owner.create!
|
932
|
-
company = owner.companies.create! type: "Models::ActiveRecord::OwnedCompany::STICompany"
|
933
|
-
company.update!(name: "Collective Idea")
|
934
|
-
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
935
|
-
end
|
936
|
-
|
937
|
-
it "should order audits by creation time" do
|
938
|
-
owner = Models::ActiveRecord::Owner.create!
|
939
|
-
first_audit = owner.audits.first
|
940
|
-
first_audit.update_column(:created_at, 1.year.ago)
|
941
|
-
|
942
|
-
company = owner.companies.create!
|
943
|
-
second_audit = company.audits.first
|
944
|
-
second_audit.update_column(:created_at, 1.month.ago)
|
945
|
-
|
946
|
-
company.update!(name: "Collective Idea")
|
947
|
-
third_audit = company.audits.last
|
948
|
-
expect(owner.own_and_associated_audits.to_a).to eq([third_audit, second_audit, first_audit])
|
949
|
-
end
|
950
|
-
end
|
951
|
-
|
952
|
-
describe "without auditing" do
|
953
|
-
it "should not save an audit when calling #save_without_auditing" do
|
954
|
-
expect {
|
955
|
-
u = Models::ActiveRecord::User.new(name: "Brandon")
|
956
|
-
expect(u.save_without_auditing).to eq(true)
|
957
|
-
}.to_not change(Audited::Audit, :count)
|
958
|
-
end
|
959
|
-
|
960
|
-
it "should not save an audit inside of the #without_auditing block" do
|
961
|
-
expect {
|
962
|
-
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
963
|
-
}.to_not change(Audited::Audit, :count)
|
964
|
-
end
|
965
|
-
|
966
|
-
context "when global audits are disabled" do
|
967
|
-
it "should re-enable class audits after #without_auditing block" do
|
968
|
-
Audited.auditing_enabled = false
|
969
|
-
Models::ActiveRecord::User.without_auditing {}
|
970
|
-
Audited.auditing_enabled = true
|
971
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
|
972
|
-
end
|
973
|
-
end
|
974
|
-
|
975
|
-
it "should reset auditing status even it raises an exception" do
|
976
|
-
begin
|
977
|
-
Models::ActiveRecord::User.without_auditing { raise }
|
978
|
-
rescue
|
979
|
-
nil
|
980
|
-
end
|
981
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
982
|
-
end
|
983
|
-
|
984
|
-
it "should be thread safe using a #without_auditing block" do
|
985
|
-
skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
|
986
|
-
|
987
|
-
t1 = Thread.new do
|
988
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
989
|
-
Models::ActiveRecord::User.without_auditing do
|
990
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
991
|
-
Models::ActiveRecord::User.create!(name: "Bart")
|
992
|
-
sleep 1
|
993
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
994
|
-
end
|
995
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
996
|
-
end
|
997
|
-
|
998
|
-
t2 = Thread.new do
|
999
|
-
sleep 0.5
|
1000
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
1001
|
-
Models::ActiveRecord::User.create!(name: "Lisa")
|
1002
|
-
end
|
1003
|
-
t1.join
|
1004
|
-
t2.join
|
1005
|
-
|
1006
|
-
expect(Models::ActiveRecord::User.find_by_name("Bart").audits.count).to eq(0)
|
1007
|
-
expect(Models::ActiveRecord::User.find_by_name("Lisa").audits.count).to eq(1)
|
1008
|
-
end
|
1009
|
-
|
1010
|
-
it "should not save an audit when auditing is globally disabled" do
|
1011
|
-
expect(Audited.auditing_enabled).to eq(true)
|
1012
|
-
Audited.auditing_enabled = false
|
1013
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
1014
|
-
|
1015
|
-
user = create_user
|
1016
|
-
expect(user.audits.count).to eq(0)
|
1017
|
-
|
1018
|
-
Audited.auditing_enabled = true
|
1019
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
1020
|
-
|
1021
|
-
user.update!(name: "Test")
|
1022
|
-
expect(user.audits.count).to eq(1)
|
1023
|
-
Models::ActiveRecord::User.enable_auditing
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
describe "with auditing" do
|
1028
|
-
it "should save an audit when calling #save_with_auditing" do
|
1029
|
-
expect {
|
1030
|
-
u = Models::ActiveRecord::User.new(name: "Brandon")
|
1031
|
-
Models::ActiveRecord::User.auditing_enabled = false
|
1032
|
-
expect(u.save_with_auditing).to eq(true)
|
1033
|
-
Models::ActiveRecord::User.auditing_enabled = true
|
1034
|
-
}.to change(Audited::Audit, :count).by(1)
|
1035
|
-
end
|
1036
|
-
|
1037
|
-
it "should save an audit inside of the #with_auditing block" do
|
1038
|
-
expect {
|
1039
|
-
Models::ActiveRecord::User.auditing_enabled = false
|
1040
|
-
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
1041
|
-
Models::ActiveRecord::User.auditing_enabled = true
|
1042
|
-
}.to change(Audited::Audit, :count).by(1)
|
1043
|
-
end
|
1044
|
-
|
1045
|
-
context "when global audits are disabled" do
|
1046
|
-
it "should re-enable class audits after #with_auditing block" do
|
1047
|
-
Audited.auditing_enabled = false
|
1048
|
-
Models::ActiveRecord::User.with_auditing {}
|
1049
|
-
Audited.auditing_enabled = true
|
1050
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
|
1051
|
-
end
|
1052
|
-
end
|
1053
|
-
|
1054
|
-
it "should reset auditing status even it raises an exception" do
|
1055
|
-
Models::ActiveRecord::User.disable_auditing
|
1056
|
-
begin
|
1057
|
-
Models::ActiveRecord::User.with_auditing { raise }
|
1058
|
-
rescue
|
1059
|
-
nil
|
1060
|
-
end
|
1061
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
1062
|
-
Models::ActiveRecord::User.enable_auditing
|
1063
|
-
end
|
1064
|
-
|
1065
|
-
it "should be thread safe using a #with_auditing block" do
|
1066
|
-
skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
|
1067
|
-
|
1068
|
-
t1 = Thread.new do
|
1069
|
-
Models::ActiveRecord::User.disable_auditing
|
1070
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
1071
|
-
Models::ActiveRecord::User.with_auditing do
|
1072
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
1073
|
-
|
1074
|
-
Models::ActiveRecord::User.create!(name: "Shaggy")
|
1075
|
-
sleep 1
|
1076
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
1077
|
-
end
|
1078
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
1079
|
-
Models::ActiveRecord::User.enable_auditing
|
1080
|
-
end
|
1081
|
-
|
1082
|
-
t2 = Thread.new do
|
1083
|
-
sleep 0.5
|
1084
|
-
Models::ActiveRecord::User.disable_auditing
|
1085
|
-
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
1086
|
-
Models::ActiveRecord::User.create!(name: "Scooby")
|
1087
|
-
Models::ActiveRecord::User.enable_auditing
|
1088
|
-
end
|
1089
|
-
t1.join
|
1090
|
-
t2.join
|
1091
|
-
|
1092
|
-
Models::ActiveRecord::User.enable_auditing
|
1093
|
-
expect(Models::ActiveRecord::User.find_by_name("Shaggy").audits.count).to eq(1)
|
1094
|
-
expect(Models::ActiveRecord::User.find_by_name("Scooby").audits.count).to eq(0)
|
1095
|
-
end
|
1096
|
-
end
|
1097
|
-
|
1098
|
-
describe "comment required" do
|
1099
|
-
describe "on create" do
|
1100
|
-
it "should not validate when audit_comment is not supplied when initialized" do
|
1101
|
-
expect(Models::ActiveRecord::CommentRequiredUser.new(name: "Foo")).not_to be_valid
|
1102
|
-
end
|
1103
|
-
|
1104
|
-
it "should not validate when audit_comment is not supplied trying to create" do
|
1105
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).not_to be_valid
|
1106
|
-
end
|
1107
|
-
|
1108
|
-
it "should validate when audit_comment is supplied" do
|
1109
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo", audit_comment: "Create")).to be_valid
|
1110
|
-
end
|
1111
|
-
|
1112
|
-
it "should validate when audit_comment is not supplied, and creating is not being audited" do
|
1113
|
-
expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name: "Foo")).to be_valid
|
1114
|
-
expect(Models::ActiveRecord::OnDestroyCommentRequiredUser.create(name: "Foo")).to be_valid
|
1115
|
-
end
|
1116
|
-
|
1117
|
-
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
1118
|
-
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
1119
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
|
1120
|
-
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
1121
|
-
end
|
1122
|
-
|
1123
|
-
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
1124
|
-
expect(Models::ActiveRecord::CommentRequiredUser.new(password: "Foo")).to be_valid
|
1125
|
-
end
|
1126
|
-
end
|
1127
|
-
|
1128
|
-
describe "on update" do
|
1129
|
-
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
1130
|
-
let(:on_create_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
1131
|
-
let(:on_destroy_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
1132
|
-
|
1133
|
-
it "should not validate when audit_comment is not supplied" do
|
1134
|
-
expect(user.update(name: "Test")).to eq(false)
|
1135
|
-
end
|
1136
|
-
|
1137
|
-
it "should validate when audit_comment is not supplied, and updating is not being audited" do
|
1138
|
-
expect(on_create_user.update(name: "Test")).to eq(true)
|
1139
|
-
expect(on_destroy_user.update(name: "Test")).to eq(true)
|
1140
|
-
end
|
1141
|
-
|
1142
|
-
it "should validate when audit_comment is supplied" do
|
1143
|
-
expect(user.update(name: "Test", audit_comment: "Update")).to eq(true)
|
1144
|
-
end
|
1145
|
-
|
1146
|
-
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
1147
|
-
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
1148
|
-
expect(user.update(name: "Test")).to eq(true)
|
1149
|
-
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
1150
|
-
end
|
1151
|
-
|
1152
|
-
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
1153
|
-
expect(user.update(password: "Test")).to eq(true)
|
1154
|
-
end
|
1155
|
-
end
|
1156
|
-
|
1157
|
-
describe "on destroy" do
|
1158
|
-
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
1159
|
-
let(:on_create_user) { Models::ActiveRecord::OnCreateCommentRequiredUser.create!(audit_comment: "Create") }
|
1160
|
-
let(:on_update_user) { Models::ActiveRecord::OnUpdateCommentRequiredUser.create }
|
1161
|
-
|
1162
|
-
it "should not validate when audit_comment is not supplied" do
|
1163
|
-
expect(user.destroy).to eq(false)
|
1164
|
-
end
|
1165
|
-
|
1166
|
-
it "should validate when audit_comment is supplied" do
|
1167
|
-
user.audit_comment = "Destroy"
|
1168
|
-
expect(user.destroy).to eq(user)
|
1169
|
-
end
|
1170
|
-
|
1171
|
-
it "should validate when audit_comment is not supplied, and destroying is not being audited" do
|
1172
|
-
expect(on_create_user.destroy).to eq(on_create_user)
|
1173
|
-
expect(on_update_user.destroy).to eq(on_update_user)
|
1174
|
-
end
|
1175
|
-
|
1176
|
-
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
1177
|
-
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
1178
|
-
expect(user.destroy).to eq(user)
|
1179
|
-
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
1180
|
-
end
|
1181
|
-
end
|
1182
|
-
end
|
1183
|
-
|
1184
|
-
describe "no update with comment only" do
|
1185
|
-
let(:user) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
|
1186
|
-
|
1187
|
-
it "does not create an audit when only an audit_comment is present" do
|
1188
|
-
user.audit_comment = "Comment"
|
1189
|
-
expect { user.save! }.to_not change(Audited::Audit, :count)
|
1190
|
-
end
|
1191
|
-
end
|
1192
|
-
|
1193
|
-
describe "attr_protected and attr_accessible" do
|
1194
|
-
it "should not raise error when attr_accessible is set and protected is false" do
|
1195
|
-
expect {
|
1196
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
1197
|
-
}.to_not raise_error
|
1198
|
-
end
|
1199
|
-
|
1200
|
-
it "should not rause an error when attr_accessible is declared before audited" do
|
1201
|
-
expect {
|
1202
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
1203
|
-
}.to_not raise_error
|
1204
|
-
end
|
1205
|
-
end
|
1206
|
-
|
1207
|
-
describe "audit_as" do
|
1208
|
-
let(:user) { Models::ActiveRecord::User.create name: "Testing" }
|
1209
|
-
|
1210
|
-
it "should record user objects" do
|
1211
|
-
Models::ActiveRecord::Company.audit_as(user) do
|
1212
|
-
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1213
|
-
company.update! name: "The Auditors"
|
1214
|
-
|
1215
|
-
company.audits.each do |audit|
|
1216
|
-
expect(audit.user).to eq(user)
|
1217
|
-
end
|
1218
|
-
end
|
1219
|
-
end
|
1220
|
-
|
1221
|
-
it "should record usernames" do
|
1222
|
-
Models::ActiveRecord::Company.audit_as(user.name) do
|
1223
|
-
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1224
|
-
company.update! name: "The Auditors"
|
1225
|
-
|
1226
|
-
company.audits.each do |audit|
|
1227
|
-
expect(audit.user).to eq(user.name)
|
1228
|
-
end
|
1229
|
-
end
|
1230
|
-
end
|
1231
|
-
end
|
1232
|
-
|
1233
|
-
describe "after_audit" do
|
1234
|
-
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1235
|
-
|
1236
|
-
it "should invoke after_audit callback on create" do
|
1237
|
-
expect(user.bogus_attr).to be_nil
|
1238
|
-
expect(user.save).to eq(true)
|
1239
|
-
expect(user.bogus_attr).to eq("do something")
|
1240
|
-
end
|
1241
|
-
end
|
1242
|
-
|
1243
|
-
describe "around_audit" do
|
1244
|
-
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1245
|
-
|
1246
|
-
it "should invoke around_audit callback on create" do
|
1247
|
-
expect(user.around_attr).to be_nil
|
1248
|
-
expect(user.save).to eq(true)
|
1249
|
-
expect(user.around_attr).to eq(user.audits.last)
|
1250
|
-
end
|
1251
|
-
end
|
1252
|
-
|
1253
|
-
describe "STI auditing" do
|
1254
|
-
it "should correctly disable auditing when using STI" do
|
1255
|
-
company = Models::ActiveRecord::Company::STICompany.create name: "The auditors"
|
1256
|
-
expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
|
1257
|
-
expect {
|
1258
|
-
Models::ActiveRecord::Company.auditing_enabled = false
|
1259
|
-
company.update! name: "STI auditors"
|
1260
|
-
Models::ActiveRecord::Company.auditing_enabled = true
|
1261
|
-
}.to_not change(Audited::Audit, :count)
|
1262
|
-
end
|
1263
|
-
end
|
1264
|
-
end
|