audited 5.5.0 → 5.5.1.pre

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/audited.gemspec +38 -0
  3. data/lib/audited/version.rb +1 -1
  4. data/lib/audited.rb +6 -2
  5. metadata +18 -53
  6. data/.github/workflows/buildlight.yml +0 -15
  7. data/.github/workflows/ci.yml +0 -145
  8. data/.github/workflows/publish_gem.yml +0 -28
  9. data/.gitignore +0 -17
  10. data/.standard.yml +0 -5
  11. data/.yardopts +0 -3
  12. data/gemfiles/rails50.gemfile +0 -12
  13. data/gemfiles/rails51.gemfile +0 -12
  14. data/gemfiles/rails52.gemfile +0 -12
  15. data/gemfiles/rails60.gemfile +0 -10
  16. data/gemfiles/rails61.gemfile +0 -10
  17. data/gemfiles/rails70.gemfile +0 -10
  18. data/gemfiles/rails71.gemfile +0 -10
  19. data/spec/audited/audit_spec.rb +0 -357
  20. data/spec/audited/auditor_spec.rb +0 -1272
  21. data/spec/audited/rspec_matchers_spec.rb +0 -69
  22. data/spec/audited/sweeper_spec.rb +0 -133
  23. data/spec/audited_spec.rb +0 -14
  24. data/spec/audited_spec_helpers.rb +0 -36
  25. data/spec/rails_app/app/assets/config/manifest.js +0 -2
  26. data/spec/rails_app/config/application.rb +0 -42
  27. data/spec/rails_app/config/database.yml +0 -26
  28. data/spec/rails_app/config/environment.rb +0 -5
  29. data/spec/rails_app/config/environments/test.rb +0 -52
  30. data/spec/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  31. data/spec/rails_app/config/initializers/inflections.rb +0 -2
  32. data/spec/rails_app/config/initializers/secret_token.rb +0 -3
  33. data/spec/rails_app/config/routes.rb +0 -3
  34. data/spec/spec_helper.rb +0 -24
  35. data/spec/support/active_record/models.rb +0 -182
  36. data/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb +0 -11
  37. data/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb +0 -11
  38. data/spec/support/active_record/schema.rb +0 -90
  39. data/test/db/version_1.rb +0 -17
  40. data/test/db/version_2.rb +0 -18
  41. data/test/db/version_3.rb +0 -18
  42. data/test/db/version_4.rb +0 -19
  43. data/test/db/version_5.rb +0 -17
  44. data/test/db/version_6.rb +0 -19
  45. data/test/install_generator_test.rb +0 -62
  46. data/test/test_helper.rb +0 -18
  47. data/test/upgrade_generator_test.rb +0 -97
@@ -1,1272 +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
-
544
- it "updating nested resource through parent while changing an enum on parent shouldn't double audit" do
545
- user.status = :reliable
546
- user.companies_attributes = [{name: "test"}]
547
- expect { user.save }.to change(user.audits, :count).from(1).to(2)
548
- expect(user.audits.last.action).to eq("update")
549
- expect(user.audits.last.audited_changes.keys).to eq(%w[status])
550
- end
551
- end
552
-
553
- context "after updating" do
554
- it "changing nested resource shouldn't audit owner" do
555
- expect { user.update(username: "test") }.to change(user.audits, :count).from(1).to(2)
556
- expect { company.update(type: "test") }.not_to change(user.audits, :count)
557
-
558
- expect { user.touch(:suspended_at) }.to change(user.audits, :count).from(2).to(3)
559
- expect { company.update(type: "another_test") }.not_to change(user.audits, :count)
560
- end
561
- end
562
- end
563
- end
564
- end
565
-
566
- describe "on destroy" do
567
- before do
568
- @user = create_user(status: :active)
569
- end
570
-
571
- it "should save an audit" do
572
- expect {
573
- @user.destroy
574
- }.to change(Audited::Audit, :count)
575
-
576
- expect(@user.audits.size).to eq(2)
577
- end
578
-
579
- it "should set the action to 'destroy'" do
580
- @user.destroy
581
-
582
- expect(@user.audits.last.action).to eq("destroy")
583
- expect(Audited::Audit.destroys.order(:id).last).to eq(@user.audits.last)
584
- expect(@user.audits.destroys.last).to eq(@user.audits.last)
585
- end
586
-
587
- it "should store all of the audited attributes" do
588
- @user.destroy
589
-
590
- expect(@user.audits.last.audited_changes).to eq(@user.audited_attributes)
591
- end
592
-
593
- it "should store enum value" do
594
- @user.destroy
595
- expect(@user.audits.last.audited_changes["status"]).to eq(0)
596
- end
597
-
598
- it "should be able to reconstruct a destroyed record without history" do
599
- @user.audits.delete_all
600
- @user.destroy
601
-
602
- revision = @user.audits.first.revision
603
- expect(revision.name).to eq(@user.name)
604
- end
605
-
606
- it "should not save an audit if only specified on create/update" do
607
- on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(name: "Bart")
608
-
609
- expect {
610
- on_create_update.destroy
611
- }.to_not change(Audited::Audit, :count)
612
- end
613
-
614
- it "should audit dependent destructions" do
615
- owner = Models::ActiveRecord::Owner.create!
616
- company = owner.companies.create!
617
-
618
- expect {
619
- owner.destroy
620
- }.to change(Audited::Audit, :count)
621
-
622
- expect(company.audits.map { |a| a.action }).to eq(["create", "destroy"])
623
- end
624
- end
625
-
626
- describe "on destroy with unsaved object" do
627
- let(:user) { Models::ActiveRecord::User.new }
628
-
629
- it "should not audit on 'destroy'" do
630
- expect {
631
- user.destroy
632
- }.to_not raise_error
633
-
634
- expect(user.audits).to be_empty
635
- end
636
- end
637
-
638
- describe "associated with" do
639
- let(:owner) { Models::ActiveRecord::Owner.create(name: "Models::ActiveRecord::Owner") }
640
- let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
641
-
642
- it "should record the associated object on create" do
643
- expect(owned_company.audits.first.associated).to eq(owner)
644
- end
645
-
646
- it "should store the associated object on update" do
647
- owned_company.update_attribute(:name, "The Auditors")
648
- expect(owned_company.audits.last.associated).to eq(owner)
649
- end
650
-
651
- it "should store the associated object on destroy" do
652
- owned_company.destroy
653
- expect(owned_company.audits.last.associated).to eq(owner)
654
- end
655
- end
656
-
657
- describe "has associated audits" do
658
- let!(:owner) { Models::ActiveRecord::Owner.create!(name: "Models::ActiveRecord::Owner") }
659
- let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
660
-
661
- it "should list the associated audits" do
662
- expect(owner.associated_audits.length).to eq(1)
663
- expect(owner.associated_audits.first.auditable).to eq(owned_company)
664
- end
665
- end
666
-
667
- describe "max_audits" do
668
- it "should respect global setting" do
669
- stub_global_max_audits(10) do
670
- expect(Models::ActiveRecord::User.audited_options[:max_audits]).to eq(10)
671
- end
672
- end
673
-
674
- it "should respect per model setting" do
675
- stub_global_max_audits(10) do
676
- expect(Models::ActiveRecord::MaxAuditsUser.audited_options[:max_audits]).to eq(5)
677
- end
678
- end
679
-
680
- it "should delete old audits when keeped amount exceeded" do
681
- stub_global_max_audits(2) do
682
- user = create_versions(2)
683
- user.update(name: "John")
684
- expect(user.audits.pluck(:version)).to eq([2, 3])
685
- end
686
- end
687
-
688
- it "should not delete old audits when keeped amount not exceeded" do
689
- stub_global_max_audits(3) do
690
- user = create_versions(2)
691
- user.update(name: "John")
692
- expect(user.audits.pluck(:version)).to eq([1, 2, 3])
693
- end
694
- end
695
-
696
- it "should delete old extra audits after introducing limit" do
697
- stub_global_max_audits(nil) do
698
- user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
699
- user.update!(name: "Foobar")
700
- user.update!(name: "Awesome", username: "keepers")
701
- user.update!(activated: true)
702
-
703
- Audited.max_audits = 3
704
- Models::ActiveRecord::User.send(:normalize_audited_options)
705
- user.update!(favourite_device: "Android Phone")
706
- audits = user.audits
707
-
708
- expect(audits.count).to eq(3)
709
- expect(audits[0].audited_changes).to include({"name" => ["Foobar", "Awesome"], "username" => ["brandon", "keepers"]})
710
- expect(audits[1].audited_changes).to eq({"activated" => [nil, true]})
711
- expect(audits[2].audited_changes).to eq({"favourite_device" => [nil, "Android Phone"]})
712
- end
713
- end
714
-
715
- it "should add comment line for combined audit" do
716
- stub_global_max_audits(2) do
717
- user = Models::ActiveRecord::User.create!(name: "Foobar 1")
718
- user.update(name: "Foobar 2", audit_comment: "First audit comment")
719
- user.update(name: "Foobar 3", audit_comment: "Second audit comment")
720
- expect(user.audits.first.comment).to match(/First audit comment.+is the result of multiple/m)
721
- end
722
- end
723
-
724
- def stub_global_max_audits(max_audits)
725
- previous_max_audits = Audited.max_audits
726
- previous_user_audited_options = Models::ActiveRecord::User.audited_options.dup
727
- begin
728
- Audited.max_audits = max_audits
729
- Models::ActiveRecord::User.send(:normalize_audited_options) # reloads audited_options
730
- yield
731
- ensure
732
- Audited.max_audits = previous_max_audits
733
- Models::ActiveRecord::User.audited_options = previous_user_audited_options
734
- end
735
- end
736
- end
737
-
738
- describe "revisions" do
739
- let(:user) { create_versions }
740
-
741
- it "should return an Array of Users" do
742
- expect(user.revisions).to be_a_kind_of(Array)
743
- user.revisions.each { |version| expect(version).to be_a_kind_of Models::ActiveRecord::User }
744
- end
745
-
746
- it "should have one revision for a new record" do
747
- expect(create_user.revisions.size).to eq(1)
748
- end
749
-
750
- it "should have one revision for each audit" do
751
- expect(user.audits.size).to eql(user.revisions.size)
752
- end
753
-
754
- it "should set the attributes for each revision" do
755
- u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
756
- u.update! name: "Foobar"
757
- u.update! name: "Awesome", username: "keepers"
758
-
759
- expect(u.revisions.size).to eql(3)
760
-
761
- expect(u.revisions[0].name).to eql("Brandon")
762
- expect(u.revisions[0].username).to eql("brandon")
763
-
764
- expect(u.revisions[1].name).to eql("Foobar")
765
- expect(u.revisions[1].username).to eql("brandon")
766
-
767
- expect(u.revisions[2].name).to eql("Awesome")
768
- expect(u.revisions[2].username).to eql("keepers")
769
- end
770
-
771
- it "access to only recent revisions" do
772
- u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
773
- u.update! name: "Foobar"
774
- u.update! name: "Awesome", username: "keepers"
775
-
776
- expect(u.revisions(2).size).to eq(2)
777
-
778
- expect(u.revisions(2)[0].name).to eq("Foobar")
779
- expect(u.revisions(2)[0].username).to eq("brandon")
780
-
781
- expect(u.revisions(2)[1].name).to eq("Awesome")
782
- expect(u.revisions(2)[1].username).to eq("keepers")
783
- end
784
-
785
- it "should be empty if no audits exist" do
786
- user.audits.delete_all
787
- expect(user.revisions).to be_empty
788
- end
789
-
790
- it "should ignore attributes that have been deleted" do
791
- user.audits.last.update! audited_changes: {old_attribute: "old value"}
792
- expect { user.revisions }.to_not raise_error
793
- end
794
- end
795
-
796
- describe "revisions" do
797
- let(:user) { create_versions(5) }
798
-
799
- it "should maintain identity" do
800
- expect(user.revision(1)).to eq(user)
801
- end
802
-
803
- it "should find the given revision" do
804
- revision = user.revision(3)
805
- expect(revision).to be_a_kind_of(Models::ActiveRecord::User)
806
- expect(revision.audit_version).to eq(3)
807
- expect(revision.name).to eq("Foobar 3")
808
- end
809
-
810
- it "should find the previous revision with :previous" do
811
- revision = user.revision(:previous)
812
- expect(revision.audit_version).to eq(4)
813
- # expect(revision).to eq(user.revision(4))
814
- expect(revision.attributes).to eq(user.revision(4).attributes)
815
- end
816
-
817
- it "should be able to get the previous revision repeatedly" do
818
- previous = user.revision(:previous)
819
- expect(previous.audit_version).to eq(4)
820
- expect(previous.revision(:previous).audit_version).to eq(3)
821
- end
822
-
823
- it "should be able to set protected attributes" do
824
- u = Models::ActiveRecord::User.create(name: "Brandon")
825
- u.update_attribute :logins, 1
826
- u.update_attribute :logins, 2
827
-
828
- expect(u.revision(3).logins).to eq(2)
829
- expect(u.revision(2).logins).to eq(1)
830
- expect(u.revision(1).logins).to eq(0)
831
- end
832
-
833
- it "should set attributes directly" do
834
- u = Models::ActiveRecord::User.create(name: "<Joe>")
835
- expect(u.revision(1).name).to eq("&lt;Joe&gt;")
836
- end
837
-
838
- it "should set the attributes for each revision" do
839
- u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
840
- u.update! name: "Foobar"
841
- u.update! name: "Awesome", username: "keepers"
842
-
843
- expect(u.revision(3).name).to eq("Awesome")
844
- expect(u.revision(3).username).to eq("keepers")
845
-
846
- expect(u.revision(2).name).to eq("Foobar")
847
- expect(u.revision(2).username).to eq("brandon")
848
-
849
- expect(u.revision(1).name).to eq("Brandon")
850
- expect(u.revision(1).username).to eq("brandon")
851
- end
852
-
853
- it "should correctly restore revision with enum" do
854
- u = Models::ActiveRecord::User.create(status: :active)
855
- u.update_attribute(:status, :reliable)
856
- u.update_attribute(:status, :banned)
857
-
858
- expect(u.revision(3)).to be_banned
859
- expect(u.revision(2)).to be_reliable
860
- expect(u.revision(1)).to be_active
861
- end
862
-
863
- it "should be able to get time for first revision" do
864
- suspended_at = Time.zone.now
865
- u = Models::ActiveRecord::User.create(suspended_at: suspended_at)
866
- expect(u.revision(1).suspended_at.to_s).to eq(suspended_at.to_s)
867
- end
868
-
869
- it "should not raise an error when no previous audits exist" do
870
- user.audits.destroy_all
871
- expect { user.revision(:previous) }.to_not raise_error
872
- end
873
-
874
- it "should mark revision's attributes as changed" do
875
- expect(user.revision(1).name_changed?).to eq(true)
876
- end
877
-
878
- it "should record new audit when saving revision" do
879
- expect {
880
- user.revision(1).save!
881
- }.to change(user.audits, :count).by(1)
882
- end
883
-
884
- it "should re-insert destroyed records" do
885
- user.destroy
886
- expect {
887
- user.revision(1).save!
888
- }.to change(Models::ActiveRecord::User, :count).by(1)
889
- end
890
-
891
- it "should return nil for values greater than the number of revisions" do
892
- expect(user.revision(user.revisions.count + 1)).to be_nil
893
- end
894
-
895
- it "should work with array attributes" do
896
- u = Models::ActiveRecord::User.create!(phone_numbers: ["+1 800-444-4444"])
897
- u.update!(phone_numbers: ["+1 804-222-1111", "+1 317 222-2222"])
898
-
899
- expect(u.revision(0).phone_numbers).to eq(["+1 804-222-1111", "+1 317 222-2222"])
900
- expect(u.revision(1).phone_numbers).to eq(["+1 800-444-4444"])
901
- end
902
- end
903
-
904
- describe "revision_at" do
905
- let(:user) { create_user }
906
-
907
- it "should find the latest revision before the given time" do
908
- audit = user.audits.first
909
- audit.created_at = 1.hour.ago
910
- audit.save!
911
- user.update! name: "updated"
912
- expect(user.revision_at(2.minutes.ago).audit_version).to eq(1)
913
- end
914
-
915
- it "should be nil if given a time before audits" do
916
- expect(user.revision_at(1.week.ago)).to be_nil
917
- end
918
- end
919
-
920
- describe "own_and_associated_audits" do
921
- it "should return audits for self and associated audits" do
922
- owner = Models::ActiveRecord::Owner.create!
923
- company = owner.companies.create!
924
- company.update!(name: "Collective Idea")
925
-
926
- other_owner = Models::ActiveRecord::Owner.create!
927
- other_owner.companies.create!
928
-
929
- expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
930
- end
931
-
932
- it "should return audits for STI classes" do
933
- # Where parent is STI
934
- sti_company = Models::ActiveRecord::Company::STICompany.create!
935
- sti_company.update!(name: "Collective Idea")
936
- expect(sti_company.own_and_associated_audits).to match_array(sti_company.audits)
937
-
938
- # Where associated is STI
939
- owner = Models::ActiveRecord::Owner.create!
940
- company = owner.companies.create! type: "Models::ActiveRecord::OwnedCompany::STICompany"
941
- company.update!(name: "Collective Idea")
942
- expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
943
- end
944
-
945
- it "should order audits by creation time" do
946
- owner = Models::ActiveRecord::Owner.create!
947
- first_audit = owner.audits.first
948
- first_audit.update_column(:created_at, 1.year.ago)
949
-
950
- company = owner.companies.create!
951
- second_audit = company.audits.first
952
- second_audit.update_column(:created_at, 1.month.ago)
953
-
954
- company.update!(name: "Collective Idea")
955
- third_audit = company.audits.last
956
- expect(owner.own_and_associated_audits.to_a).to eq([third_audit, second_audit, first_audit])
957
- end
958
- end
959
-
960
- describe "without auditing" do
961
- it "should not save an audit when calling #save_without_auditing" do
962
- expect {
963
- u = Models::ActiveRecord::User.new(name: "Brandon")
964
- expect(u.save_without_auditing).to eq(true)
965
- }.to_not change(Audited::Audit, :count)
966
- end
967
-
968
- it "should not save an audit inside of the #without_auditing block" do
969
- expect {
970
- Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
971
- }.to_not change(Audited::Audit, :count)
972
- end
973
-
974
- context "when global audits are disabled" do
975
- it "should re-enable class audits after #without_auditing block" do
976
- Audited.auditing_enabled = false
977
- Models::ActiveRecord::User.without_auditing {}
978
- Audited.auditing_enabled = true
979
- expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
980
- end
981
- end
982
-
983
- it "should reset auditing status even it raises an exception" do
984
- begin
985
- Models::ActiveRecord::User.without_auditing { raise }
986
- rescue
987
- nil
988
- end
989
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
990
- end
991
-
992
- it "should be thread safe using a #without_auditing block" do
993
- skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
994
-
995
- t1 = Thread.new do
996
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
997
- Models::ActiveRecord::User.without_auditing do
998
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
999
- Models::ActiveRecord::User.create!(name: "Bart")
1000
- sleep 1
1001
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1002
- end
1003
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
1004
- end
1005
-
1006
- t2 = Thread.new do
1007
- sleep 0.5
1008
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
1009
- Models::ActiveRecord::User.create!(name: "Lisa")
1010
- end
1011
- t1.join
1012
- t2.join
1013
-
1014
- expect(Models::ActiveRecord::User.find_by_name("Bart").audits.count).to eq(0)
1015
- expect(Models::ActiveRecord::User.find_by_name("Lisa").audits.count).to eq(1)
1016
- end
1017
-
1018
- it "should not save an audit when auditing is globally disabled" do
1019
- expect(Audited.auditing_enabled).to eq(true)
1020
- Audited.auditing_enabled = false
1021
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1022
-
1023
- user = create_user
1024
- expect(user.audits.count).to eq(0)
1025
-
1026
- Audited.auditing_enabled = true
1027
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
1028
-
1029
- user.update!(name: "Test")
1030
- expect(user.audits.count).to eq(1)
1031
- Models::ActiveRecord::User.enable_auditing
1032
- end
1033
- end
1034
-
1035
- describe "with auditing" do
1036
- it "should save an audit when calling #save_with_auditing" do
1037
- expect {
1038
- u = Models::ActiveRecord::User.new(name: "Brandon")
1039
- Models::ActiveRecord::User.auditing_enabled = false
1040
- expect(u.save_with_auditing).to eq(true)
1041
- Models::ActiveRecord::User.auditing_enabled = true
1042
- }.to change(Audited::Audit, :count).by(1)
1043
- end
1044
-
1045
- it "should save an audit inside of the #with_auditing block" do
1046
- expect {
1047
- Models::ActiveRecord::User.auditing_enabled = false
1048
- Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
1049
- Models::ActiveRecord::User.auditing_enabled = true
1050
- }.to change(Audited::Audit, :count).by(1)
1051
- end
1052
-
1053
- context "when global audits are disabled" do
1054
- it "should re-enable class audits after #with_auditing block" do
1055
- Audited.auditing_enabled = false
1056
- Models::ActiveRecord::User.with_auditing {}
1057
- Audited.auditing_enabled = true
1058
- expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
1059
- end
1060
- end
1061
-
1062
- it "should reset auditing status even it raises an exception" do
1063
- Models::ActiveRecord::User.disable_auditing
1064
- begin
1065
- Models::ActiveRecord::User.with_auditing { raise }
1066
- rescue
1067
- nil
1068
- end
1069
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1070
- Models::ActiveRecord::User.enable_auditing
1071
- end
1072
-
1073
- it "should be thread safe using a #with_auditing block" do
1074
- skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
1075
-
1076
- t1 = Thread.new do
1077
- Models::ActiveRecord::User.disable_auditing
1078
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1079
- Models::ActiveRecord::User.with_auditing do
1080
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
1081
-
1082
- Models::ActiveRecord::User.create!(name: "Shaggy")
1083
- sleep 1
1084
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
1085
- end
1086
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1087
- Models::ActiveRecord::User.enable_auditing
1088
- end
1089
-
1090
- t2 = Thread.new do
1091
- sleep 0.5
1092
- Models::ActiveRecord::User.disable_auditing
1093
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
1094
- Models::ActiveRecord::User.create!(name: "Scooby")
1095
- Models::ActiveRecord::User.enable_auditing
1096
- end
1097
- t1.join
1098
- t2.join
1099
-
1100
- Models::ActiveRecord::User.enable_auditing
1101
- expect(Models::ActiveRecord::User.find_by_name("Shaggy").audits.count).to eq(1)
1102
- expect(Models::ActiveRecord::User.find_by_name("Scooby").audits.count).to eq(0)
1103
- end
1104
- end
1105
-
1106
- describe "comment required" do
1107
- describe "on create" do
1108
- it "should not validate when audit_comment is not supplied when initialized" do
1109
- expect(Models::ActiveRecord::CommentRequiredUser.new(name: "Foo")).not_to be_valid
1110
- end
1111
-
1112
- it "should not validate when audit_comment is not supplied trying to create" do
1113
- expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).not_to be_valid
1114
- end
1115
-
1116
- it "should validate when audit_comment is supplied" do
1117
- expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo", audit_comment: "Create")).to be_valid
1118
- end
1119
-
1120
- it "should validate when audit_comment is not supplied, and creating is not being audited" do
1121
- expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name: "Foo")).to be_valid
1122
- expect(Models::ActiveRecord::OnDestroyCommentRequiredUser.create(name: "Foo")).to be_valid
1123
- end
1124
-
1125
- it "should validate when audit_comment is not supplied, and auditing is disabled" do
1126
- Models::ActiveRecord::CommentRequiredUser.disable_auditing
1127
- expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
1128
- Models::ActiveRecord::CommentRequiredUser.enable_auditing
1129
- end
1130
-
1131
- it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
1132
- expect(Models::ActiveRecord::CommentRequiredUser.new(password: "Foo")).to be_valid
1133
- end
1134
- end
1135
-
1136
- describe "on update" do
1137
- let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
1138
- let(:on_create_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
1139
- let(:on_destroy_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
1140
-
1141
- it "should not validate when audit_comment is not supplied" do
1142
- expect(user.update(name: "Test")).to eq(false)
1143
- end
1144
-
1145
- it "should validate when audit_comment is not supplied, and updating is not being audited" do
1146
- expect(on_create_user.update(name: "Test")).to eq(true)
1147
- expect(on_destroy_user.update(name: "Test")).to eq(true)
1148
- end
1149
-
1150
- it "should validate when audit_comment is supplied" do
1151
- expect(user.update(name: "Test", audit_comment: "Update")).to eq(true)
1152
- end
1153
-
1154
- it "should validate when audit_comment is not supplied, and auditing is disabled" do
1155
- Models::ActiveRecord::CommentRequiredUser.disable_auditing
1156
- expect(user.update(name: "Test")).to eq(true)
1157
- Models::ActiveRecord::CommentRequiredUser.enable_auditing
1158
- end
1159
-
1160
- it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
1161
- expect(user.update(password: "Test")).to eq(true)
1162
- end
1163
- end
1164
-
1165
- describe "on destroy" do
1166
- let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
1167
- let(:on_create_user) { Models::ActiveRecord::OnCreateCommentRequiredUser.create!(audit_comment: "Create") }
1168
- let(:on_update_user) { Models::ActiveRecord::OnUpdateCommentRequiredUser.create }
1169
-
1170
- it "should not validate when audit_comment is not supplied" do
1171
- expect(user.destroy).to eq(false)
1172
- end
1173
-
1174
- it "should validate when audit_comment is supplied" do
1175
- user.audit_comment = "Destroy"
1176
- expect(user.destroy).to eq(user)
1177
- end
1178
-
1179
- it "should validate when audit_comment is not supplied, and destroying is not being audited" do
1180
- expect(on_create_user.destroy).to eq(on_create_user)
1181
- expect(on_update_user.destroy).to eq(on_update_user)
1182
- end
1183
-
1184
- it "should validate when audit_comment is not supplied, and auditing is disabled" do
1185
- Models::ActiveRecord::CommentRequiredUser.disable_auditing
1186
- expect(user.destroy).to eq(user)
1187
- Models::ActiveRecord::CommentRequiredUser.enable_auditing
1188
- end
1189
- end
1190
- end
1191
-
1192
- describe "no update with comment only" do
1193
- let(:user) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
1194
-
1195
- it "does not create an audit when only an audit_comment is present" do
1196
- user.audit_comment = "Comment"
1197
- expect { user.save! }.to_not change(Audited::Audit, :count)
1198
- end
1199
- end
1200
-
1201
- describe "attr_protected and attr_accessible" do
1202
- it "should not raise error when attr_accessible is set and protected is false" do
1203
- expect {
1204
- Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
1205
- }.to_not raise_error
1206
- end
1207
-
1208
- it "should not rause an error when attr_accessible is declared before audited" do
1209
- expect {
1210
- Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
1211
- }.to_not raise_error
1212
- end
1213
- end
1214
-
1215
- describe "audit_as" do
1216
- let(:user) { Models::ActiveRecord::User.create name: "Testing" }
1217
-
1218
- it "should record user objects" do
1219
- Models::ActiveRecord::Company.audit_as(user) do
1220
- company = Models::ActiveRecord::Company.create name: "The auditors"
1221
- company.update! name: "The Auditors"
1222
-
1223
- company.audits.each do |audit|
1224
- expect(audit.user).to eq(user)
1225
- end
1226
- end
1227
- end
1228
-
1229
- it "should record usernames" do
1230
- Models::ActiveRecord::Company.audit_as(user.name) do
1231
- company = Models::ActiveRecord::Company.create name: "The auditors"
1232
- company.update! name: "The Auditors"
1233
-
1234
- company.audits.each do |audit|
1235
- expect(audit.user).to eq(user.name)
1236
- end
1237
- end
1238
- end
1239
- end
1240
-
1241
- describe "after_audit" do
1242
- let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
1243
-
1244
- it "should invoke after_audit callback on create" do
1245
- expect(user.bogus_attr).to be_nil
1246
- expect(user.save).to eq(true)
1247
- expect(user.bogus_attr).to eq("do something")
1248
- end
1249
- end
1250
-
1251
- describe "around_audit" do
1252
- let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
1253
-
1254
- it "should invoke around_audit callback on create" do
1255
- expect(user.around_attr).to be_nil
1256
- expect(user.save).to eq(true)
1257
- expect(user.around_attr).to eq(user.audits.last)
1258
- end
1259
- end
1260
-
1261
- describe "STI auditing" do
1262
- it "should correctly disable auditing when using STI" do
1263
- company = Models::ActiveRecord::Company::STICompany.create name: "The auditors"
1264
- expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
1265
- expect {
1266
- Models::ActiveRecord::Company.auditing_enabled = false
1267
- company.update! name: "STI auditors"
1268
- Models::ActiveRecord::Company.auditing_enabled = true
1269
- }.to_not change(Audited::Audit, :count)
1270
- end
1271
- end
1272
- end