audited 4.7.0 → 4.10.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.

Potentially problematic release.


This version of audited might be problematic. Click here for more details.

@@ -1,5 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
+ SingleCov.covered! uncovered: 13 # not testing proxy_respond_to? hack / 2 methods / deprecation of `version`
4
+
3
5
  describe Audited::Auditor do
4
6
 
5
7
  describe "configuration" do
@@ -20,6 +22,24 @@ describe Audited::Auditor do
20
22
  context "should be configurable which conditions are audited" do
21
23
  subject { ConditionalCompany.new.send(:auditing_enabled) }
22
24
 
25
+ context "when condition method is private" do
26
+ subject { ConditionalPrivateCompany.new.send(:auditing_enabled) }
27
+
28
+ before do
29
+ class ConditionalPrivateCompany < ::ActiveRecord::Base
30
+ self.table_name = 'companies'
31
+
32
+ audited if: :foo?
33
+
34
+ private def foo?
35
+ true
36
+ end
37
+ end
38
+ end
39
+
40
+ it { is_expected.to be_truthy }
41
+ end
42
+
23
43
  context "when passing a method name" do
24
44
  before do
25
45
  class ConditionalCompany < ::ActiveRecord::Base
@@ -192,7 +212,40 @@ describe Audited::Auditor do
192
212
  expect(user.audits.last.audited_changes.keys).to eq(%w{non_column_attr})
193
213
  end
194
214
 
195
- if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' && Rails.version >= "4.2.0.0" # Postgres json and jsonb support was added in Rails 4.2
215
+ it "should redact columns specified in 'redacted' option" do
216
+ redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
217
+ user = Models::ActiveRecord::UserRedactedPassword.create(password: "password")
218
+ user.save!
219
+ expect(user.audits.last.audited_changes['password']).to eq(redacted)
220
+ user.password = "new_password"
221
+ user.save!
222
+ expect(user.audits.last.audited_changes['password']).to eq([redacted, redacted])
223
+ end
224
+
225
+ it "should redact columns specified in 'redacted' option when there are multiple specified" do
226
+ redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
227
+ user =
228
+ Models::ActiveRecord::UserMultipleRedactedAttributes.create(
229
+ password: "password",
230
+ ssn: 123456789
231
+ )
232
+ user.save!
233
+ expect(user.audits.last.audited_changes['password']).to eq(redacted)
234
+ expect(user.audits.last.audited_changes['ssn']).to eq(redacted)
235
+ user.password = "new_password"
236
+ user.ssn = 987654321
237
+ user.save!
238
+ expect(user.audits.last.audited_changes['password']).to eq([redacted, redacted])
239
+ expect(user.audits.last.audited_changes['ssn']).to eq([redacted, redacted])
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::Base.connection.adapter_name == 'PostgreSQL'
196
249
  describe "'json' and 'jsonb' audited_changes column type" do
197
250
  let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
198
251
 
@@ -246,7 +299,7 @@ describe Audited::Auditor do
246
299
  end
247
300
 
248
301
  describe "on create" do
249
- let( :user ) { create_user audit_comment: "Create" }
302
+ let( :user ) { create_user status: :reliable, audit_comment: "Create" }
250
303
 
251
304
  it "should change the audit count" do
252
305
  expect {
@@ -270,6 +323,10 @@ describe Audited::Auditor do
270
323
  expect(user.audits.first.audited_changes).to eq(user.audited_attributes)
271
324
  end
272
325
 
326
+ it "should store enum value" do
327
+ expect(user.audits.first.audited_changes["status"]).to eq(1)
328
+ end
329
+
273
330
  it "should store comment" do
274
331
  expect(user.audits.first.comment).to eq('Create')
275
332
  end
@@ -288,7 +345,7 @@ describe Audited::Auditor do
288
345
 
289
346
  describe "on update" do
290
347
  before do
291
- @user = create_user( name: 'Brandon', audit_comment: 'Update' )
348
+ @user = create_user( name: 'Brandon', status: :active, audit_comment: 'Update' )
292
349
  end
293
350
 
294
351
  it "should save an audit" do
@@ -301,17 +358,22 @@ describe Audited::Auditor do
301
358
  end
302
359
 
303
360
  it "should set the action to 'update'" do
304
- @user.update_attributes name: 'Changed'
361
+ @user.update! name: 'Changed'
305
362
  expect(@user.audits.last.action).to eq('update')
306
363
  expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
307
364
  expect(@user.audits.updates.last).to eq(@user.audits.last)
308
365
  end
309
366
 
310
367
  it "should store the changed attributes" do
311
- @user.update_attributes name: 'Changed'
368
+ @user.update! name: 'Changed'
312
369
  expect(@user.audits.last.audited_changes).to eq({ 'name' => ['Brandon', 'Changed'] })
313
370
  end
314
371
 
372
+ it "should store changed enum values" do
373
+ @user.update! status: 1
374
+ expect(@user.audits.last.audited_changes["status"]).to eq([0, 1])
375
+ end
376
+
315
377
  it "should store audit comment" do
316
378
  expect(@user.audits.last.comment).to eq('Update')
317
379
  end
@@ -319,12 +381,12 @@ describe Audited::Auditor do
319
381
  it "should not save an audit if only specified on create/destroy" do
320
382
  on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create( name: 'Bart' )
321
383
  expect {
322
- on_create_destroy.update_attributes name: 'Changed'
384
+ on_create_destroy.update! name: 'Changed'
323
385
  }.to_not change( Audited::Audit, :count )
324
386
  end
325
387
 
326
388
  it "should not save an audit if the value doesn't change after type casting" do
327
- @user.update_attributes! logins: 0, activated: true
389
+ @user.update! logins: 0, activated: true
328
390
  expect { @user.update_attribute :logins, '0' }.to_not change( Audited::Audit, :count )
329
391
  expect { @user.update_attribute :activated, 1 }.to_not change( Audited::Audit, :count )
330
392
  expect { @user.update_attribute :activated, '1' }.to_not change( Audited::Audit, :count )
@@ -348,7 +410,7 @@ describe Audited::Auditor do
348
410
 
349
411
  describe "on destroy" do
350
412
  before do
351
- @user = create_user
413
+ @user = create_user(status: :active)
352
414
  end
353
415
 
354
416
  it "should save an audit" do
@@ -373,6 +435,11 @@ describe Audited::Auditor do
373
435
  expect(@user.audits.last.audited_changes).to eq(@user.audited_attributes)
374
436
  end
375
437
 
438
+ it "should store enum value" do
439
+ @user.destroy
440
+ expect(@user.audits.last.audited_changes["status"]).to eq(0)
441
+ end
442
+
376
443
  it "should be able to reconstruct a destroyed record without history" do
377
444
  @user.audits.delete_all
378
445
  @user.destroy
@@ -474,13 +541,13 @@ describe Audited::Auditor do
474
541
  it "should delete old extra audits after introducing limit" do
475
542
  stub_global_max_audits(nil) do
476
543
  user = Models::ActiveRecord::User.create!(name: 'Brandon', username: 'brandon')
477
- user.update_attributes(name: 'Foobar')
478
- user.update_attributes(name: 'Awesome', username: 'keepers')
479
- user.update_attributes(activated: true)
544
+ user.update!(name: 'Foobar')
545
+ user.update!(name: 'Awesome', username: 'keepers')
546
+ user.update!(activated: true)
480
547
 
481
548
  Audited.max_audits = 3
482
549
  Models::ActiveRecord::User.send(:normalize_audited_options)
483
- user.update_attributes(favourite_device: 'Android Phone')
550
+ user.update!(favourite_device: 'Android Phone')
484
551
  audits = user.audits
485
552
 
486
553
  expect(audits.count).to eq(3)
@@ -531,8 +598,8 @@ describe Audited::Auditor do
531
598
 
532
599
  it "should set the attributes for each revision" do
533
600
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
534
- u.update_attributes name: 'Foobar'
535
- u.update_attributes name: 'Awesome', username: 'keepers'
601
+ u.update! name: 'Foobar'
602
+ u.update! name: 'Awesome', username: 'keepers'
536
603
 
537
604
  expect(u.revisions.size).to eql(3)
538
605
 
@@ -548,8 +615,8 @@ describe Audited::Auditor do
548
615
 
549
616
  it "access to only recent revisions" do
550
617
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
551
- u.update_attributes name: 'Foobar'
552
- u.update_attributes name: 'Awesome', username: 'keepers'
618
+ u.update! name: 'Foobar'
619
+ u.update! name: 'Awesome', username: 'keepers'
553
620
 
554
621
  expect(u.revisions(2).size).to eq(2)
555
622
 
@@ -566,7 +633,7 @@ describe Audited::Auditor do
566
633
  end
567
634
 
568
635
  it "should ignore attributes that have been deleted" do
569
- user.audits.last.update_attributes audited_changes: {old_attribute: 'old value'}
636
+ user.audits.last.update! audited_changes: {old_attribute: 'old value'}
570
637
  expect { user.revisions }.to_not raise_error
571
638
  end
572
639
  end
@@ -581,21 +648,21 @@ describe Audited::Auditor do
581
648
  it "should find the given revision" do
582
649
  revision = user.revision(3)
583
650
  expect(revision).to be_a_kind_of( Models::ActiveRecord::User )
584
- expect(revision.version).to eq(3)
651
+ expect(revision.audit_version).to eq(3)
585
652
  expect(revision.name).to eq('Foobar 3')
586
653
  end
587
654
 
588
655
  it "should find the previous revision with :previous" do
589
656
  revision = user.revision(:previous)
590
- expect(revision.version).to eq(4)
657
+ expect(revision.audit_version).to eq(4)
591
658
  #expect(revision).to eq(user.revision(4))
592
659
  expect(revision.attributes).to eq(user.revision(4).attributes)
593
660
  end
594
661
 
595
662
  it "should be able to get the previous revision repeatedly" do
596
663
  previous = user.revision(:previous)
597
- expect(previous.version).to eq(4)
598
- expect(previous.revision(:previous).version).to eq(3)
664
+ expect(previous.audit_version).to eq(4)
665
+ expect(previous.revision(:previous).audit_version).to eq(3)
599
666
  end
600
667
 
601
668
  it "should be able to set protected attributes" do
@@ -615,8 +682,8 @@ describe Audited::Auditor do
615
682
 
616
683
  it "should set the attributes for each revision" do
617
684
  u = Models::ActiveRecord::User.create(name: 'Brandon', username: 'brandon')
618
- u.update_attributes name: 'Foobar'
619
- u.update_attributes name: 'Awesome', username: 'keepers'
685
+ u.update! name: 'Foobar'
686
+ u.update! name: 'Awesome', username: 'keepers'
620
687
 
621
688
  expect(u.revision(3).name).to eq('Awesome')
622
689
  expect(u.revision(3).username).to eq('keepers')
@@ -628,6 +695,16 @@ describe Audited::Auditor do
628
695
  expect(u.revision(1).username).to eq('brandon')
629
696
  end
630
697
 
698
+ it "should correctly restore revision with enum" do
699
+ u = Models::ActiveRecord::User.create(status: :active)
700
+ u.update_attribute(:status, :reliable)
701
+ u.update_attribute(:status, :banned)
702
+
703
+ expect(u.revision(3)).to be_banned
704
+ expect(u.revision(2)).to be_reliable
705
+ expect(u.revision(1)).to be_active
706
+ end
707
+
631
708
  it "should be able to get time for first revision" do
632
709
  suspended_at = Time.zone.now
633
710
  u = Models::ActiveRecord::User.create(suspended_at: suspended_at)
@@ -668,8 +745,8 @@ describe Audited::Auditor do
668
745
  audit = user.audits.first
669
746
  audit.created_at = 1.hour.ago
670
747
  audit.save!
671
- user.update_attributes name: 'updated'
672
- expect(user.revision_at( 2.minutes.ago ).version).to eq(1)
748
+ user.update! name: 'updated'
749
+ expect(user.revision_at( 2.minutes.ago ).audit_version).to eq(1)
673
750
  end
674
751
 
675
752
  it "should be nil if given a time before audits" do
@@ -677,6 +754,33 @@ describe Audited::Auditor do
677
754
  end
678
755
  end
679
756
 
757
+ describe "own_and_associated_audits" do
758
+ it "should return audits for self and associated audits" do
759
+ owner = Models::ActiveRecord::Owner.create!
760
+ company = owner.companies.create!
761
+ company.update!(name: "Collective Idea")
762
+
763
+ other_owner = Models::ActiveRecord::Owner.create!
764
+ other_owner.companies.create!
765
+
766
+ expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
767
+ end
768
+
769
+ it "should order audits by creation time" do
770
+ owner = Models::ActiveRecord::Owner.create!
771
+ first_audit = owner.audits.first
772
+ first_audit.update_column(:created_at, 1.year.ago)
773
+
774
+ company = owner.companies.create!
775
+ second_audit = company.audits.first
776
+ second_audit.update_column(:created_at, 1.month.ago)
777
+
778
+ company.update!(name: "Collective Idea")
779
+ third_audit = company.audits.last
780
+ expect(owner.own_and_associated_audits.to_a).to eq([third_audit, second_audit, first_audit])
781
+ end
782
+ end
783
+
680
784
  describe "without auditing" do
681
785
  it "should not save an audit when calling #save_without_auditing" do
682
786
  expect {
@@ -697,31 +801,103 @@ describe Audited::Auditor do
697
801
  end
698
802
 
699
803
  it "should be thread safe using a #without_auditing block" do
700
- begin
701
- t1 = Thread.new do
702
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
703
- Models::ActiveRecord::User.without_auditing do
704
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
705
- Models::ActiveRecord::User.create!( name: 'Bart' )
706
- sleep 1
707
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
708
- end
709
- expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
804
+ skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
805
+
806
+ t1 = Thread.new do
807
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
808
+ Models::ActiveRecord::User.without_auditing do
809
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
810
+ Models::ActiveRecord::User.create!( name: 'Bart' )
811
+ sleep 1
812
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
710
813
  end
814
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
815
+ end
816
+
817
+ t2 = Thread.new do
818
+ sleep 0.5
819
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
820
+ Models::ActiveRecord::User.create!( name: 'Lisa' )
821
+ end
822
+ t1.join
823
+ t2.join
824
+
825
+ expect(Models::ActiveRecord::User.find_by_name('Bart').audits.count).to eq(0)
826
+ expect(Models::ActiveRecord::User.find_by_name('Lisa').audits.count).to eq(1)
827
+ end
828
+
829
+ it "should not save an audit when auditing is globally disabled" do
830
+ expect(Audited.auditing_enabled).to eq(true)
831
+ Audited.auditing_enabled = false
832
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
833
+
834
+ user = create_user
835
+ expect(user.audits.count).to eq(0)
836
+
837
+ Audited.auditing_enabled = true
838
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
839
+
840
+ user.update!(name: 'Test')
841
+ expect(user.audits.count).to eq(1)
842
+ Models::ActiveRecord::User.enable_auditing
843
+ end
844
+ end
845
+
846
+ describe "with auditing" do
847
+ it "should save an audit when calling #save_with_auditing" do
848
+ expect {
849
+ u = Models::ActiveRecord::User.new(name: 'Brandon')
850
+ Models::ActiveRecord::User.auditing_enabled = false
851
+ expect(u.save_with_auditing).to eq(true)
852
+ Models::ActiveRecord::User.auditing_enabled = true
853
+ }.to change( Audited::Audit, :count ).by(1)
854
+ end
855
+
856
+ it "should save an audit inside of the #with_auditing block" do
857
+ expect {
858
+ Models::ActiveRecord::User.auditing_enabled = false
859
+ Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!( name: 'Brandon' ) }
860
+ Models::ActiveRecord::User.auditing_enabled = true
861
+ }.to change( Audited::Audit, :count ).by(1)
862
+ end
863
+
864
+ it "should reset auditing status even it raises an exception" do
865
+ Models::ActiveRecord::User.disable_auditing
866
+ Models::ActiveRecord::User.with_auditing { raise } rescue nil
867
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
868
+ Models::ActiveRecord::User.enable_auditing
869
+ end
711
870
 
712
- t2 = Thread.new do
713
- sleep 0.5
871
+ it "should be thread safe using a #with_auditing block" do
872
+ skip if Models::ActiveRecord::User.connection.class.name.include?("SQLite")
873
+
874
+ t1 = Thread.new do
875
+ Models::ActiveRecord::User.disable_auditing
876
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
877
+ Models::ActiveRecord::User.with_auditing do
878
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
879
+
880
+ Models::ActiveRecord::User.create!( name: 'Shaggy' )
881
+ sleep 1
714
882
  expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
715
- Models::ActiveRecord::User.create!( name: 'Lisa' )
716
883
  end
717
- t1.join
718
- t2.join
884
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
885
+ Models::ActiveRecord::User.enable_auditing
886
+ end
719
887
 
720
- expect(Models::ActiveRecord::User.find_by_name('Bart').audits.count).to eq(0)
721
- expect(Models::ActiveRecord::User.find_by_name('Lisa').audits.count).to eq(1)
722
- rescue ActiveRecord::StatementInvalid
723
- STDERR.puts "Thread safety tests cannot be run with SQLite"
888
+ t2 = Thread.new do
889
+ sleep 0.5
890
+ Models::ActiveRecord::User.disable_auditing
891
+ expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
892
+ Models::ActiveRecord::User.create!( name: 'Scooby' )
893
+ Models::ActiveRecord::User.enable_auditing
724
894
  end
895
+ t1.join
896
+ t2.join
897
+
898
+ Models::ActiveRecord::User.enable_auditing
899
+ expect(Models::ActiveRecord::User.find_by_name('Shaggy').audits.count).to eq(1)
900
+ expect(Models::ActiveRecord::User.find_by_name('Scooby').audits.count).to eq(0)
725
901
  end
726
902
  end
727
903
 
@@ -758,21 +934,21 @@ describe Audited::Auditor do
758
934
  let( :on_destroy_user ) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
759
935
 
760
936
  it "should not validate when audit_comment is not supplied" do
761
- expect(user.update_attributes(name: 'Test')).to eq(false)
937
+ expect(user.update(name: 'Test')).to eq(false)
762
938
  end
763
939
 
764
940
  it "should validate when audit_comment is not supplied, and updating is not being audited" do
765
- expect(on_create_user.update_attributes(name: 'Test')).to eq(true)
766
- expect(on_destroy_user.update_attributes(name: 'Test')).to eq(true)
941
+ expect(on_create_user.update(name: 'Test')).to eq(true)
942
+ expect(on_destroy_user.update(name: 'Test')).to eq(true)
767
943
  end
768
944
 
769
945
  it "should validate when audit_comment is supplied" do
770
- expect(user.update_attributes(name: 'Test', audit_comment: 'Update')).to eq(true)
946
+ expect(user.update(name: 'Test', audit_comment: 'Update')).to eq(true)
771
947
  end
772
948
 
773
949
  it "should validate when audit_comment is not supplied, and auditing is disabled" do
774
950
  Models::ActiveRecord::CommentRequiredUser.disable_auditing
775
- expect(user.update_attributes(name: 'Test')).to eq(true)
951
+ expect(user.update(name: 'Test')).to eq(true)
776
952
  Models::ActiveRecord::CommentRequiredUser.enable_auditing
777
953
  end
778
954
  end
@@ -805,6 +981,16 @@ describe Audited::Auditor do
805
981
 
806
982
  end
807
983
 
984
+ describe "no update with comment only" do
985
+ let( :user ) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
986
+
987
+ it "does not create an audit when only an audit_comment is present" do
988
+ user.audit_comment = "Comment"
989
+ expect { user.save! }.to_not change( Audited::Audit, :count )
990
+ end
991
+
992
+ end
993
+
808
994
  describe "attr_protected and attr_accessible" do
809
995
 
810
996
  it "should not raise error when attr_accessible is set and protected is false" do
@@ -826,7 +1012,7 @@ describe Audited::Auditor do
826
1012
  it "should record user objects" do
827
1013
  Models::ActiveRecord::Company.audit_as( user ) do
828
1014
  company = Models::ActiveRecord::Company.create name: 'The auditors'
829
- company.update_attributes name: 'The Auditors'
1015
+ company.update! name: 'The Auditors'
830
1016
 
831
1017
  company.audits.each do |audit|
832
1018
  expect(audit.user).to eq(user)
@@ -837,7 +1023,7 @@ describe Audited::Auditor do
837
1023
  it "should record usernames" do
838
1024
  Models::ActiveRecord::Company.audit_as( user.name ) do
839
1025
  company = Models::ActiveRecord::Company.create name: 'The auditors'
840
- company.update_attributes name: 'The Auditors'
1026
+ company.update! name: 'The Auditors'
841
1027
 
842
1028
  company.audits.each do |audit|
843
1029
  expect(audit.user).to eq(user.name)
@@ -847,7 +1033,7 @@ describe Audited::Auditor do
847
1033
  end
848
1034
 
849
1035
  describe "after_audit" do
850
- let( :user ) { user = Models::ActiveRecord::UserWithAfterAudit.new }
1036
+ let( :user ) { Models::ActiveRecord::UserWithAfterAudit.new }
851
1037
 
852
1038
  it "should invoke after_audit callback on create" do
853
1039
  expect(user.bogus_attr).to be_nil
@@ -857,7 +1043,7 @@ describe Audited::Auditor do
857
1043
  end
858
1044
 
859
1045
  describe "around_audit" do
860
- let( :user ) { user = Models::ActiveRecord::UserWithAfterAudit.new }
1046
+ let( :user ) { Models::ActiveRecord::UserWithAfterAudit.new }
861
1047
 
862
1048
  it "should invoke around_audit callback on create" do
863
1049
  expect(user.around_attr).to be_nil
@@ -872,7 +1058,7 @@ describe Audited::Auditor do
872
1058
  expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
873
1059
  expect {
874
1060
  Models::ActiveRecord::Company.auditing_enabled = false
875
- company.update_attributes name: 'STI auditors'
1061
+ company.update! name: 'STI auditors'
876
1062
  Models::ActiveRecord::Company.auditing_enabled = true
877
1063
  }.to_not change( Audited::Audit, :count )
878
1064
  end
@@ -1,5 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
+ SingleCov.covered! uncovered: 2 # 2 conditional on_load conditions
4
+
3
5
  class AuditsController < ActionController::Base
4
6
  before_action :populate_user
5
7
 
@@ -11,7 +13,7 @@ class AuditsController < ActionController::Base
11
13
  end
12
14
 
13
15
  def update
14
- current_user.update_attributes(password: 'foo')
16
+ current_user.update!(password: 'foo')
15
17
  head :ok
16
18
  end
17
19
 
@@ -27,14 +29,13 @@ describe AuditsController do
27
29
  include RSpec::Rails::ControllerExampleGroup
28
30
  render_views
29
31
 
30
- before(:each) do
32
+ before do
31
33
  Audited.current_user_method = :current_user
32
34
  end
33
35
 
34
36
  let(:user) { create_user }
35
37
 
36
38
  describe "POST audit" do
37
-
38
39
  it "should audit user" do
39
40
  controller.send(:current_user=, user)
40
41
  expect {
@@ -44,6 +45,15 @@ describe AuditsController do
44
45
  expect(controller.company.audits.last.user).to eq(user)
45
46
  end
46
47
 
48
+ it "does not audit when method is not found" do
49
+ controller.send(:current_user=, user)
50
+ Audited.current_user_method = :nope
51
+ expect {
52
+ post :create
53
+ }.to change( Audited::Audit, :count )
54
+ expect(controller.company.audits.last.user).to eq(nil)
55
+ end
56
+
47
57
  it "should support custom users for sweepers" do
48
58
  controller.send(:custom_user=, user)
49
59
  Audited.current_user_method = :custom_user
@@ -84,7 +94,6 @@ describe AuditsController do
84
94
 
85
95
  expect(controller.company.audits.last.user).to eq(user)
86
96
  end
87
-
88
97
  end
89
98
 
90
99
  describe "PUT update" do
@@ -92,13 +101,13 @@ describe AuditsController do
92
101
  controller.send(:current_user=, user)
93
102
 
94
103
  expect {
95
- put :update, Rails::VERSION::MAJOR == 4 ? {id: 123} : {params: {id: 123}}
104
+ params = Rails::VERSION::MAJOR == 4 ? {id: 123} : {params: {id: 123}}
105
+ put :update, **params
96
106
  }.to_not change( Audited::Audit, :count )
97
107
  end
98
108
  end
99
109
  end
100
110
 
101
-
102
111
  describe Audited::Sweeper do
103
112
 
104
113
  it "should be thread-safe" do
@@ -20,8 +20,10 @@ module AuditedSpecHelpers
20
20
  def run_migrations(direction, migrations_paths, target_version = nil)
21
21
  if rails_below?('5.2.0.rc1')
22
22
  ActiveRecord::Migrator.send(direction, migrations_paths, target_version)
23
- else
23
+ elsif rails_below?('6.0.0.rc1')
24
24
  ActiveRecord::MigrationContext.new(migrations_paths).send(direction, target_version)
25
+ else
26
+ ActiveRecord::MigrationContext.new(migrations_paths, ActiveRecord::SchemaMigration).send(direction, target_version)
25
27
  end
26
28
  end
27
29
 
@@ -0,0 +1,2 @@
1
+ class ApplicationController < ActionController::Base
2
+ end
@@ -6,3 +6,8 @@ module RailsApp
6
6
  config.i18n.enforce_available_locales = true
7
7
  end
8
8
  end
9
+
10
+ require 'active_record/connection_adapters/sqlite3_adapter'
11
+ if ActiveRecord::ConnectionAdapters::SQLite3Adapter.respond_to?(:represent_boolean_as_integer)
12
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
13
+ end
@@ -19,6 +19,7 @@ mysql: &MYSQL
19
19
  username: root
20
20
  password:
21
21
  database: audited_test
22
+ charset: utf8
22
23
 
23
24
  test:
24
25
  <<: *<%= ENV['DB'] || 'SQLITE3MEM' %>
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
+ require 'bundler/setup'
3
+ require 'single_cov'
4
+ SingleCov.setup :rspec
2
5
 
3
- require 'bundler'
4
6
  if Bundler.definition.dependencies.map(&:name).include?('protected_attributes')
5
7
  require 'protected_attributes'
6
8
  end