audited 5.3.3 → 5.8.0

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