audited 4.10.0 → 5.2.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.
- checksums.yaml +4 -4
- data/.github/workflows/buildlight.yml +15 -0
- data/.github/workflows/ci.yml +128 -0
- data/.standard.yml +5 -0
- data/Appraisals +20 -18
- data/CHANGELOG.md +90 -1
- data/Gemfile +1 -1
- data/README.md +52 -14
- data/Rakefile +6 -6
- data/gemfiles/rails50.gemfile +1 -0
- data/gemfiles/rails51.gemfile +1 -0
- data/gemfiles/rails52.gemfile +2 -1
- data/gemfiles/rails70.gemfile +10 -0
- data/lib/audited/audit.rb +24 -25
- data/lib/audited/auditor.rb +91 -56
- data/lib/audited/railtie.rb +16 -0
- data/lib/audited/rspec_matchers.rb +5 -3
- data/lib/audited/sweeper.rb +3 -10
- data/lib/audited/version.rb +3 -1
- data/lib/audited-rspec.rb +3 -1
- data/lib/audited.rb +33 -9
- data/lib/generators/audited/install_generator.rb +9 -7
- data/lib/generators/audited/migration.rb +12 -2
- data/lib/generators/audited/migration_helper.rb +3 -1
- data/lib/generators/audited/templates/add_association_to_audits.rb +2 -0
- data/lib/generators/audited/templates/add_comment_to_audits.rb +2 -0
- data/lib/generators/audited/templates/add_remote_address_to_audits.rb +2 -0
- data/lib/generators/audited/templates/add_request_uuid_to_audits.rb +2 -0
- data/lib/generators/audited/templates/add_version_to_auditable_index.rb +2 -0
- data/lib/generators/audited/templates/install.rb +2 -0
- data/lib/generators/audited/templates/rename_association_to_associated.rb +2 -0
- data/lib/generators/audited/templates/rename_changes_to_audited_changes.rb +2 -0
- data/lib/generators/audited/templates/rename_parent_to_association.rb +2 -0
- data/lib/generators/audited/templates/revert_polymorphic_indexes_order.rb +2 -0
- data/lib/generators/audited/upgrade_generator.rb +16 -14
- data/spec/audited/audit_spec.rb +68 -46
- data/spec/audited/auditor_spec.rb +310 -253
- data/spec/audited/sweeper_spec.rb +19 -19
- data/spec/audited_spec.rb +18 -0
- data/spec/audited_spec_helpers.rb +5 -7
- data/spec/rails_app/app/assets/config/manifest.js +2 -1
- data/spec/rails_app/config/application.rb +9 -3
- data/spec/rails_app/config/database.yml +3 -2
- data/spec/rails_app/config/environment.rb +1 -1
- data/spec/rails_app/config/environments/test.rb +10 -5
- data/spec/rails_app/config/initializers/secret_token.rb +2 -2
- data/spec/spec_helper.rb +14 -14
- data/spec/support/active_record/models.rb +24 -12
- data/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb +1 -2
- data/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb +1 -2
- data/spec/support/active_record/schema.rb +25 -19
- data/test/db/version_1.rb +2 -2
- data/test/db/version_2.rb +2 -2
- data/test/db/version_3.rb +2 -3
- data/test/db/version_4.rb +2 -3
- data/test/db/version_5.rb +0 -1
- data/test/db/version_6.rb +1 -1
- data/test/install_generator_test.rb +18 -19
- data/test/test_helper.rb +5 -5
- data/test/upgrade_generator_test.rb +13 -18
- metadata +31 -31
- data/.rubocop.yml +0 -25
- data/.travis.yml +0 -63
- data/gemfiles/rails42.gemfile +0 -11
- data/spec/rails_app/app/controllers/application_controller.rb +0 -2
- data/spec/rails_app/config/environments/development.rb +0 -21
- data/spec/rails_app/config/environments/production.rb +0 -35
@@ -1,19 +1,79 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
SingleCov.covered! uncovered:
|
3
|
+
SingleCov.covered! uncovered: 9 # not testing proxy_respond_to? hack / 2 methods / deprecation of `version`
|
4
4
|
|
5
|
-
|
5
|
+
class ConditionalPrivateCompany < ::ActiveRecord::Base
|
6
|
+
self.table_name = "companies"
|
7
|
+
|
8
|
+
audited if: :foo?
|
9
|
+
|
10
|
+
private def foo?
|
11
|
+
true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ConditionalCompany < ::ActiveRecord::Base
|
16
|
+
self.table_name = "companies"
|
17
|
+
|
18
|
+
audited if: :public?
|
19
|
+
|
20
|
+
def public?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ExclusiveCompany < ::ActiveRecord::Base
|
25
|
+
self.table_name = "companies"
|
26
|
+
audited if: proc { false }
|
27
|
+
end
|
28
|
+
|
29
|
+
class ExclusionaryCompany < ::ActiveRecord::Base
|
30
|
+
self.table_name = "companies"
|
31
|
+
|
32
|
+
audited unless: :non_profit?
|
33
|
+
|
34
|
+
def non_profit?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ExclusionaryCompany2 < ::ActiveRecord::Base
|
39
|
+
self.table_name = "companies"
|
40
|
+
audited unless: proc { |c| c.exclusive? }
|
41
|
+
|
42
|
+
def exclusive?
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class InclusiveCompany < ::ActiveRecord::Base
|
48
|
+
self.table_name = "companies"
|
49
|
+
audited if: proc { true }
|
50
|
+
end
|
51
|
+
|
52
|
+
class InclusiveCompany2 < ::ActiveRecord::Base
|
53
|
+
self.table_name = "companies"
|
54
|
+
audited unless: proc { false }
|
55
|
+
end
|
6
56
|
|
57
|
+
class Secret < ::ActiveRecord::Base
|
58
|
+
audited
|
59
|
+
end
|
60
|
+
|
61
|
+
class Secret2 < ::ActiveRecord::Base
|
62
|
+
audited
|
63
|
+
self.non_audited_columns = ["delta", "top_secret", "created_at"]
|
64
|
+
end
|
65
|
+
|
66
|
+
describe Audited::Auditor do
|
7
67
|
describe "configuration" do
|
8
68
|
it "should include instance methods" do
|
9
|
-
expect(Models::ActiveRecord::User.new).to be_a_kind_of(
|
69
|
+
expect(Models::ActiveRecord::User.new).to be_a_kind_of(Audited::Auditor::AuditedInstanceMethods)
|
10
70
|
end
|
11
71
|
|
12
72
|
it "should include class methods" do
|
13
|
-
expect(Models::ActiveRecord::User).to be_a_kind_of(
|
73
|
+
expect(Models::ActiveRecord::User).to be_a_kind_of(Audited::Auditor::AuditedClassMethods)
|
14
74
|
end
|
15
75
|
|
16
|
-
[
|
76
|
+
["created_at", "updated_at", "created_on", "updated_on", "lock_version", "id", "password"].each do |column|
|
17
77
|
it "should not audit #{column}" do
|
18
78
|
expect(Models::ActiveRecord::User.non_audited_columns).to include(column)
|
19
79
|
end
|
@@ -25,64 +85,29 @@ describe Audited::Auditor do
|
|
25
85
|
context "when condition method is private" do
|
26
86
|
subject { ConditionalPrivateCompany.new.send(:auditing_enabled) }
|
27
87
|
|
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
88
|
it { is_expected.to be_truthy }
|
41
89
|
end
|
42
90
|
|
43
91
|
context "when passing a method name" do
|
44
|
-
before do
|
45
|
-
class ConditionalCompany < ::ActiveRecord::Base
|
46
|
-
self.table_name = 'companies'
|
47
|
-
|
48
|
-
audited if: :public?
|
49
|
-
|
50
|
-
def public?; end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
92
|
context "when conditions are true" do
|
55
93
|
before { allow_any_instance_of(ConditionalCompany).to receive(:public?).and_return(true) }
|
56
|
-
it
|
94
|
+
it { is_expected.to be_truthy }
|
57
95
|
end
|
58
96
|
|
59
97
|
context "when conditions are false" do
|
60
98
|
before { allow_any_instance_of(ConditionalCompany).to receive(:public?).and_return(false) }
|
61
|
-
it
|
99
|
+
it { is_expected.to be_falsey }
|
62
100
|
end
|
63
101
|
end
|
64
102
|
|
65
103
|
context "when passing a Proc" do
|
66
104
|
context "when conditions are true" do
|
67
|
-
before do
|
68
|
-
class InclusiveCompany < ::ActiveRecord::Base
|
69
|
-
self.table_name = 'companies'
|
70
|
-
audited if: Proc.new { true }
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
105
|
subject { InclusiveCompany.new.send(:auditing_enabled) }
|
75
106
|
|
76
107
|
it { is_expected.to be_truthy }
|
77
108
|
end
|
78
109
|
|
79
110
|
context "when conditions are false" do
|
80
|
-
before do
|
81
|
-
class ExclusiveCompany < ::ActiveRecord::Base
|
82
|
-
self.table_name = 'companies'
|
83
|
-
audited if: Proc.new { false }
|
84
|
-
end
|
85
|
-
end
|
86
111
|
subject { ExclusiveCompany.new.send(:auditing_enabled) }
|
87
112
|
it { is_expected.to be_falsey }
|
88
113
|
end
|
@@ -91,76 +116,40 @@ describe Audited::Auditor do
|
|
91
116
|
|
92
117
|
context "should be configurable which conditions aren't audited" do
|
93
118
|
context "when using a method name" do
|
94
|
-
before do
|
95
|
-
class ExclusionaryCompany < ::ActiveRecord::Base
|
96
|
-
self.table_name = 'companies'
|
97
|
-
|
98
|
-
audited unless: :non_profit?
|
99
|
-
|
100
|
-
def non_profit?; end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
119
|
subject { ExclusionaryCompany.new.send(:auditing_enabled) }
|
105
120
|
|
106
121
|
context "when conditions are true" do
|
107
122
|
before { allow_any_instance_of(ExclusionaryCompany).to receive(:non_profit?).and_return(true) }
|
108
|
-
it
|
123
|
+
it { is_expected.to be_falsey }
|
109
124
|
end
|
110
125
|
|
111
126
|
context "when conditions are false" do
|
112
127
|
before { allow_any_instance_of(ExclusionaryCompany).to receive(:non_profit?).and_return(false) }
|
113
|
-
it
|
128
|
+
it { is_expected.to be_truthy }
|
114
129
|
end
|
115
130
|
end
|
116
131
|
|
117
132
|
context "when using a proc" do
|
118
133
|
context "when conditions are true" do
|
119
|
-
|
120
|
-
|
121
|
-
self.table_name = 'companies'
|
122
|
-
audited unless: Proc.new { |c| c.exclusive? }
|
123
|
-
|
124
|
-
def exclusive?
|
125
|
-
true
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
subject { ExclusionaryCompany.new.send(:auditing_enabled) }
|
131
|
-
it { is_expected.to be_falsey }
|
134
|
+
subject { ExclusionaryCompany2.new.send(:auditing_enabled) }
|
135
|
+
it { is_expected.to be_falsey }
|
132
136
|
end
|
133
137
|
|
134
138
|
context "when conditions are false" do
|
135
|
-
|
136
|
-
|
137
|
-
self.table_name = 'companies'
|
138
|
-
audited unless: Proc.new { false }
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
subject { InclusiveCompany.new.send(:auditing_enabled) }
|
143
|
-
it { is_expected.to be_truthy }
|
139
|
+
subject { InclusiveCompany2.new.send(:auditing_enabled) }
|
140
|
+
it { is_expected.to be_truthy }
|
144
141
|
end
|
145
142
|
end
|
146
143
|
end
|
147
144
|
|
148
145
|
it "should be configurable which attributes are not audited via ignored_attributes" do
|
149
|
-
Audited.ignored_attributes = [
|
150
|
-
class Secret < ::ActiveRecord::Base
|
151
|
-
audited
|
152
|
-
end
|
146
|
+
Audited.ignored_attributes = ["delta", "top_secret", "created_at"]
|
153
147
|
|
154
|
-
expect(Secret.non_audited_columns).to include(
|
148
|
+
expect(Secret.non_audited_columns).to include("delta", "top_secret", "created_at")
|
155
149
|
end
|
156
150
|
|
157
151
|
it "should be configurable which attributes are not audited via non_audited_columns=" do
|
158
|
-
|
159
|
-
audited
|
160
|
-
self.non_audited_columns = ['delta', 'top_secret', 'created_at']
|
161
|
-
end
|
162
|
-
|
163
|
-
expect(Secret2.non_audited_columns).to include('delta', 'top_secret', 'created_at')
|
152
|
+
expect(Secret2.non_audited_columns).to include("delta", "top_secret", "created_at")
|
164
153
|
end
|
165
154
|
|
166
155
|
it "should not save non-audited columns" do
|
@@ -168,7 +157,7 @@ describe Audited::Auditor do
|
|
168
157
|
begin
|
169
158
|
Models::ActiveRecord::User.non_audited_columns += [:favourite_device]
|
170
159
|
|
171
|
-
expect(create_user.audits.first.audited_changes.keys.any? { |col| [
|
160
|
+
expect(create_user.audits.first.audited_changes.keys.any? { |col| ["favourite_device", "created_at", "updated_at", "password"].include?(col) }).to eq(false)
|
172
161
|
ensure
|
173
162
|
Models::ActiveRecord::User.non_audited_columns = previous
|
174
163
|
end
|
@@ -190,7 +179,7 @@ describe Audited::Auditor do
|
|
190
179
|
user.password = "password"
|
191
180
|
user.non_column_attr = "some value"
|
192
181
|
user.save!
|
193
|
-
expect(user.audits.last.audited_changes.keys).to eq(%w
|
182
|
+
expect(user.audits.last.audited_changes.keys).to eq(%w[password])
|
194
183
|
end
|
195
184
|
|
196
185
|
it "should save attributes not specified in 'except' option" do
|
@@ -209,17 +198,17 @@ describe Audited::Auditor do
|
|
209
198
|
user.password = "password"
|
210
199
|
user.non_column_attr = "some value"
|
211
200
|
user.save!
|
212
|
-
expect(user.audits.last.audited_changes.keys).to eq(%w
|
201
|
+
expect(user.audits.last.audited_changes.keys).to eq(%w[non_column_attr])
|
213
202
|
end
|
214
203
|
|
215
204
|
it "should redact columns specified in 'redacted' option" do
|
216
205
|
redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
|
217
206
|
user = Models::ActiveRecord::UserRedactedPassword.create(password: "password")
|
218
207
|
user.save!
|
219
|
-
expect(user.audits.last.audited_changes[
|
208
|
+
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
220
209
|
user.password = "new_password"
|
221
210
|
user.save!
|
222
|
-
expect(user.audits.last.audited_changes[
|
211
|
+
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
223
212
|
end
|
224
213
|
|
225
214
|
it "should redact columns specified in 'redacted' option when there are multiple specified" do
|
@@ -230,22 +219,30 @@ describe Audited::Auditor do
|
|
230
219
|
ssn: 123456789
|
231
220
|
)
|
232
221
|
user.save!
|
233
|
-
expect(user.audits.last.audited_changes[
|
234
|
-
expect(user.audits.last.audited_changes[
|
222
|
+
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
223
|
+
expect(user.audits.last.audited_changes["ssn"]).to eq(redacted)
|
235
224
|
user.password = "new_password"
|
236
225
|
user.ssn = 987654321
|
237
226
|
user.save!
|
238
|
-
expect(user.audits.last.audited_changes[
|
239
|
-
expect(user.audits.last.audited_changes[
|
227
|
+
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
228
|
+
expect(user.audits.last.audited_changes["ssn"]).to eq([redacted, redacted])
|
240
229
|
end
|
241
230
|
|
242
231
|
it "should redact columns in 'redacted' column with custom option" do
|
243
232
|
user = Models::ActiveRecord::UserRedactedPasswordCustomRedaction.create(password: "password")
|
244
233
|
user.save!
|
245
|
-
expect(user.audits.last.audited_changes[
|
234
|
+
expect(user.audits.last.audited_changes["password"]).to eq(["My", "Custom", "Value", 7])
|
235
|
+
end
|
236
|
+
|
237
|
+
if ::ActiveRecord::VERSION::MAJOR >= 7
|
238
|
+
it "should filter encrypted attributes" do
|
239
|
+
user = Models::ActiveRecord::UserWithEncryptedPassword.create(password: "password")
|
240
|
+
user.save
|
241
|
+
expect(user.audits.last.audited_changes["password"]).to eq("[FILTERED]")
|
242
|
+
end
|
246
243
|
end
|
247
244
|
|
248
|
-
if ActiveRecord::Base.connection.adapter_name ==
|
245
|
+
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
249
246
|
describe "'json' and 'jsonb' audited_changes column type" do
|
250
247
|
let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
|
251
248
|
|
@@ -282,16 +279,16 @@ describe Audited::Auditor do
|
|
282
279
|
it "should allow mass assignment of all unprotected attributes" do
|
283
280
|
yesterday = 1.day.ago
|
284
281
|
|
285
|
-
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name:
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
282
|
+
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name: "name",
|
283
|
+
username: "username",
|
284
|
+
password: "password",
|
285
|
+
activated: true,
|
286
|
+
suspended_at: yesterday,
|
287
|
+
logins: 2)
|
291
288
|
|
292
|
-
expect(u.name).to eq(
|
293
|
-
expect(u.username).to eq(
|
294
|
-
expect(u.password).to eq(
|
289
|
+
expect(u.name).to eq("name")
|
290
|
+
expect(u.username).to eq("username")
|
291
|
+
expect(u.password).to eq("password")
|
295
292
|
expect(u.activated).to eq(true)
|
296
293
|
expect(u.suspended_at.to_i).to eq(yesterday.to_i)
|
297
294
|
expect(u.logins).to eq(2)
|
@@ -299,12 +296,12 @@ describe Audited::Auditor do
|
|
299
296
|
end
|
300
297
|
|
301
298
|
describe "on create" do
|
302
|
-
let(
|
299
|
+
let(:user) { create_user status: :reliable, audit_comment: "Create" }
|
303
300
|
|
304
301
|
it "should change the audit count" do
|
305
302
|
expect {
|
306
303
|
user
|
307
|
-
}.to change(
|
304
|
+
}.to change(Audited::Audit, :count).by(1)
|
308
305
|
end
|
309
306
|
|
310
307
|
it "should create associated audit" do
|
@@ -312,7 +309,7 @@ describe Audited::Auditor do
|
|
312
309
|
end
|
313
310
|
|
314
311
|
it "should set the action to create" do
|
315
|
-
expect(user.audits.first.action).to eq(
|
312
|
+
expect(user.audits.first.action).to eq("create")
|
316
313
|
expect(Audited::Audit.creates.order(:id).last).to eq(user.audits.first)
|
317
314
|
expect(user.audits.creates.count).to eq(1)
|
318
315
|
expect(user.audits.updates.count).to eq(0)
|
@@ -327,46 +324,55 @@ describe Audited::Auditor do
|
|
327
324
|
expect(user.audits.first.audited_changes["status"]).to eq(1)
|
328
325
|
end
|
329
326
|
|
327
|
+
context "when store_synthesized_enums is set to true" do
|
328
|
+
before { Audited.store_synthesized_enums = true }
|
329
|
+
after { Audited.store_synthesized_enums = false }
|
330
|
+
|
331
|
+
it "should store enum value as Rails synthesized value" do
|
332
|
+
expect(user.audits.first.audited_changes["status"]).to eq("reliable")
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
330
336
|
it "should store comment" do
|
331
|
-
expect(user.audits.first.comment).to eq(
|
337
|
+
expect(user.audits.first.comment).to eq("Create")
|
332
338
|
end
|
333
339
|
|
334
340
|
it "should not audit an attribute which is excepted if specified on create or destroy" do
|
335
|
-
on_create_destroy_except_name = Models::ActiveRecord::OnCreateDestroyExceptName.create(name:
|
336
|
-
expect(on_create_destroy_except_name.audits.first.audited_changes.keys.any?{|col| [
|
341
|
+
on_create_destroy_except_name = Models::ActiveRecord::OnCreateDestroyExceptName.create(name: "Bart")
|
342
|
+
expect(on_create_destroy_except_name.audits.first.audited_changes.keys.any? { |col| ["name"].include? col }).to eq(false)
|
337
343
|
end
|
338
344
|
|
339
345
|
it "should not save an audit if only specified on update/destroy" do
|
340
346
|
expect {
|
341
|
-
Models::ActiveRecord::OnUpdateDestroy.create!(
|
342
|
-
}.to_not change(
|
347
|
+
Models::ActiveRecord::OnUpdateDestroy.create!(name: "Bart")
|
348
|
+
}.to_not change(Audited::Audit, :count)
|
343
349
|
end
|
344
350
|
end
|
345
351
|
|
346
352
|
describe "on update" do
|
347
353
|
before do
|
348
|
-
@user = create_user(
|
354
|
+
@user = create_user(name: "Brandon", status: :active, audit_comment: "Update")
|
349
355
|
end
|
350
356
|
|
351
357
|
it "should save an audit" do
|
352
358
|
expect {
|
353
359
|
@user.update_attribute(:name, "Someone")
|
354
|
-
}.to change(
|
360
|
+
}.to change(Audited::Audit, :count).by(1)
|
355
361
|
expect {
|
356
362
|
@user.update_attribute(:name, "Someone else")
|
357
|
-
}.to change(
|
363
|
+
}.to change(Audited::Audit, :count).by(1)
|
358
364
|
end
|
359
365
|
|
360
366
|
it "should set the action to 'update'" do
|
361
|
-
@user.update! name:
|
362
|
-
expect(@user.audits.last.action).to eq(
|
367
|
+
@user.update! name: "Changed"
|
368
|
+
expect(@user.audits.last.action).to eq("update")
|
363
369
|
expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
|
364
370
|
expect(@user.audits.updates.last).to eq(@user.audits.last)
|
365
371
|
end
|
366
372
|
|
367
373
|
it "should store the changed attributes" do
|
368
|
-
@user.update! name:
|
369
|
-
expect(@user.audits.last.audited_changes).to eq({
|
374
|
+
@user.update! name: "Changed"
|
375
|
+
expect(@user.audits.last.audited_changes).to eq({"name" => ["Brandon", "Changed"]})
|
370
376
|
end
|
371
377
|
|
372
378
|
it "should store changed enum values" do
|
@@ -375,35 +381,35 @@ describe Audited::Auditor do
|
|
375
381
|
end
|
376
382
|
|
377
383
|
it "should store audit comment" do
|
378
|
-
expect(@user.audits.last.comment).to eq(
|
384
|
+
expect(@user.audits.last.comment).to eq("Update")
|
379
385
|
end
|
380
386
|
|
381
387
|
it "should not save an audit if only specified on create/destroy" do
|
382
|
-
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(
|
388
|
+
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(name: "Bart")
|
383
389
|
expect {
|
384
|
-
on_create_destroy.update! name:
|
385
|
-
}.to_not change(
|
390
|
+
on_create_destroy.update! name: "Changed"
|
391
|
+
}.to_not change(Audited::Audit, :count)
|
386
392
|
end
|
387
393
|
|
388
394
|
it "should not save an audit if the value doesn't change after type casting" do
|
389
395
|
@user.update! logins: 0, activated: true
|
390
|
-
expect { @user.update_attribute :logins,
|
391
|
-
expect { @user.update_attribute :activated, 1 }.to_not change(
|
392
|
-
expect { @user.update_attribute :activated,
|
396
|
+
expect { @user.update_attribute :logins, "0" }.to_not change(Audited::Audit, :count)
|
397
|
+
expect { @user.update_attribute :activated, 1 }.to_not change(Audited::Audit, :count)
|
398
|
+
expect { @user.update_attribute :activated, "1" }.to_not change(Audited::Audit, :count)
|
393
399
|
end
|
394
400
|
|
395
401
|
describe "with no dirty changes" do
|
396
402
|
it "does not create an audit if the record is not changed" do
|
397
403
|
expect {
|
398
404
|
@user.save!
|
399
|
-
}.to_not change(
|
405
|
+
}.to_not change(Audited::Audit, :count)
|
400
406
|
end
|
401
407
|
|
402
408
|
it "creates an audit when an audit comment is present" do
|
403
409
|
expect {
|
404
410
|
@user.audit_comment = "Comment"
|
405
411
|
@user.save!
|
406
|
-
}.to change(
|
412
|
+
}.to change(Audited::Audit, :count)
|
407
413
|
end
|
408
414
|
end
|
409
415
|
end
|
@@ -416,7 +422,7 @@ describe Audited::Auditor do
|
|
416
422
|
it "should save an audit" do
|
417
423
|
expect {
|
418
424
|
@user.destroy
|
419
|
-
}.to change(
|
425
|
+
}.to change(Audited::Audit, :count)
|
420
426
|
|
421
427
|
expect(@user.audits.size).to eq(2)
|
422
428
|
end
|
@@ -424,7 +430,7 @@ describe Audited::Auditor do
|
|
424
430
|
it "should set the action to 'destroy'" do
|
425
431
|
@user.destroy
|
426
432
|
|
427
|
-
expect(@user.audits.last.action).to eq(
|
433
|
+
expect(@user.audits.last.action).to eq("destroy")
|
428
434
|
expect(Audited::Audit.destroys.order(:id).last).to eq(@user.audits.last)
|
429
435
|
expect(@user.audits.destroys.last).to eq(@user.audits.last)
|
430
436
|
end
|
@@ -449,11 +455,11 @@ describe Audited::Auditor do
|
|
449
455
|
end
|
450
456
|
|
451
457
|
it "should not save an audit if only specified on create/update" do
|
452
|
-
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(
|
458
|
+
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(name: "Bart")
|
453
459
|
|
454
460
|
expect {
|
455
461
|
on_create_update.destroy
|
456
|
-
}.to_not change(
|
462
|
+
}.to_not change(Audited::Audit, :count)
|
457
463
|
end
|
458
464
|
|
459
465
|
it "should audit dependent destructions" do
|
@@ -462,9 +468,9 @@ describe Audited::Auditor do
|
|
462
468
|
|
463
469
|
expect {
|
464
470
|
owner.destroy
|
465
|
-
}.to change(
|
471
|
+
}.to change(Audited::Audit, :count)
|
466
472
|
|
467
|
-
expect(company.audits.map { |a| a.action }).to eq([
|
473
|
+
expect(company.audits.map { |a| a.action }).to eq(["create", "destroy"])
|
468
474
|
end
|
469
475
|
end
|
470
476
|
|
@@ -476,20 +482,20 @@ describe Audited::Auditor do
|
|
476
482
|
user.destroy
|
477
483
|
}.to_not raise_error
|
478
484
|
|
479
|
-
expect(
|
485
|
+
expect(user.audits).to be_empty
|
480
486
|
end
|
481
487
|
end
|
482
488
|
|
483
489
|
describe "associated with" do
|
484
|
-
let(:owner) { Models::ActiveRecord::Owner.create(name:
|
485
|
-
let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name:
|
490
|
+
let(:owner) { Models::ActiveRecord::Owner.create(name: "Models::ActiveRecord::Owner") }
|
491
|
+
let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
486
492
|
|
487
493
|
it "should record the associated object on create" do
|
488
494
|
expect(owned_company.audits.first.associated).to eq(owner)
|
489
495
|
end
|
490
496
|
|
491
497
|
it "should store the associated object on update" do
|
492
|
-
owned_company.update_attribute(:name,
|
498
|
+
owned_company.update_attribute(:name, "The Auditors")
|
493
499
|
expect(owned_company.audits.last.associated).to eq(owner)
|
494
500
|
end
|
495
501
|
|
@@ -500,8 +506,8 @@ describe Audited::Auditor do
|
|
500
506
|
end
|
501
507
|
|
502
508
|
describe "has associated audits" do
|
503
|
-
let!(:owner) { Models::ActiveRecord::Owner.create!(name:
|
504
|
-
let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name:
|
509
|
+
let!(:owner) { Models::ActiveRecord::Owner.create!(name: "Models::ActiveRecord::Owner") }
|
510
|
+
let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
505
511
|
|
506
512
|
it "should list the associated audits" do
|
507
513
|
expect(owner.associated_audits.length).to eq(1)
|
@@ -525,7 +531,7 @@ describe Audited::Auditor do
|
|
525
531
|
it "should delete old audits when keeped amount exceeded" do
|
526
532
|
stub_global_max_audits(2) do
|
527
533
|
user = create_versions(2)
|
528
|
-
user.update(name:
|
534
|
+
user.update(name: "John")
|
529
535
|
expect(user.audits.pluck(:version)).to eq([2, 3])
|
530
536
|
end
|
531
537
|
end
|
@@ -533,35 +539,35 @@ describe Audited::Auditor do
|
|
533
539
|
it "should not delete old audits when keeped amount not exceeded" do
|
534
540
|
stub_global_max_audits(3) do
|
535
541
|
user = create_versions(2)
|
536
|
-
user.update(name:
|
542
|
+
user.update(name: "John")
|
537
543
|
expect(user.audits.pluck(:version)).to eq([1, 2, 3])
|
538
544
|
end
|
539
545
|
end
|
540
546
|
|
541
547
|
it "should delete old extra audits after introducing limit" do
|
542
548
|
stub_global_max_audits(nil) do
|
543
|
-
user = Models::ActiveRecord::User.create!(name:
|
544
|
-
user.update!(name:
|
545
|
-
user.update!(name:
|
549
|
+
user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
|
550
|
+
user.update!(name: "Foobar")
|
551
|
+
user.update!(name: "Awesome", username: "keepers")
|
546
552
|
user.update!(activated: true)
|
547
553
|
|
548
554
|
Audited.max_audits = 3
|
549
555
|
Models::ActiveRecord::User.send(:normalize_audited_options)
|
550
|
-
user.update!(favourite_device:
|
556
|
+
user.update!(favourite_device: "Android Phone")
|
551
557
|
audits = user.audits
|
552
558
|
|
553
559
|
expect(audits.count).to eq(3)
|
554
|
-
expect(audits[0].audited_changes).to include({
|
555
|
-
expect(audits[1].audited_changes).to eq({
|
556
|
-
expect(audits[2].audited_changes).to eq({
|
560
|
+
expect(audits[0].audited_changes).to include({"name" => ["Foobar", "Awesome"], "username" => ["brandon", "keepers"]})
|
561
|
+
expect(audits[1].audited_changes).to eq({"activated" => [nil, true]})
|
562
|
+
expect(audits[2].audited_changes).to eq({"favourite_device" => [nil, "Android Phone"]})
|
557
563
|
end
|
558
564
|
end
|
559
565
|
|
560
566
|
it "should add comment line for combined audit" do
|
561
567
|
stub_global_max_audits(2) do
|
562
|
-
user = Models::ActiveRecord::User.create!(name:
|
563
|
-
user.update(name:
|
564
|
-
user.update(name:
|
568
|
+
user = Models::ActiveRecord::User.create!(name: "Foobar 1")
|
569
|
+
user.update(name: "Foobar 2", audit_comment: "First audit comment")
|
570
|
+
user.update(name: "Foobar 3", audit_comment: "Second audit comment")
|
565
571
|
expect(user.audits.first.comment).to match(/First audit comment.+is the result of multiple/m)
|
566
572
|
end
|
567
573
|
end
|
@@ -581,10 +587,10 @@ describe Audited::Auditor do
|
|
581
587
|
end
|
582
588
|
|
583
589
|
describe "revisions" do
|
584
|
-
let(
|
590
|
+
let(:user) { create_versions }
|
585
591
|
|
586
592
|
it "should return an Array of Users" do
|
587
|
-
expect(user.revisions).to be_a_kind_of(
|
593
|
+
expect(user.revisions).to be_a_kind_of(Array)
|
588
594
|
user.revisions.each { |version| expect(version).to be_a_kind_of Models::ActiveRecord::User }
|
589
595
|
end
|
590
596
|
|
@@ -593,38 +599,38 @@ describe Audited::Auditor do
|
|
593
599
|
end
|
594
600
|
|
595
601
|
it "should have one revision for each audit" do
|
596
|
-
expect(user.audits.size).to eql(
|
602
|
+
expect(user.audits.size).to eql(user.revisions.size)
|
597
603
|
end
|
598
604
|
|
599
605
|
it "should set the attributes for each revision" do
|
600
|
-
u = Models::ActiveRecord::User.create(name:
|
601
|
-
u.update! name:
|
602
|
-
u.update! name:
|
606
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
607
|
+
u.update! name: "Foobar"
|
608
|
+
u.update! name: "Awesome", username: "keepers"
|
603
609
|
|
604
610
|
expect(u.revisions.size).to eql(3)
|
605
611
|
|
606
|
-
expect(u.revisions[0].name).to eql(
|
607
|
-
expect(u.revisions[0].username).to eql(
|
612
|
+
expect(u.revisions[0].name).to eql("Brandon")
|
613
|
+
expect(u.revisions[0].username).to eql("brandon")
|
608
614
|
|
609
|
-
expect(u.revisions[1].name).to eql(
|
610
|
-
expect(u.revisions[1].username).to eql(
|
615
|
+
expect(u.revisions[1].name).to eql("Foobar")
|
616
|
+
expect(u.revisions[1].username).to eql("brandon")
|
611
617
|
|
612
|
-
expect(u.revisions[2].name).to eql(
|
613
|
-
expect(u.revisions[2].username).to eql(
|
618
|
+
expect(u.revisions[2].name).to eql("Awesome")
|
619
|
+
expect(u.revisions[2].username).to eql("keepers")
|
614
620
|
end
|
615
621
|
|
616
622
|
it "access to only recent revisions" do
|
617
|
-
u = Models::ActiveRecord::User.create(name:
|
618
|
-
u.update! name:
|
619
|
-
u.update! name:
|
623
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
624
|
+
u.update! name: "Foobar"
|
625
|
+
u.update! name: "Awesome", username: "keepers"
|
620
626
|
|
621
627
|
expect(u.revisions(2).size).to eq(2)
|
622
628
|
|
623
|
-
expect(u.revisions(2)[0].name).to eq(
|
624
|
-
expect(u.revisions(2)[0].username).to eq(
|
629
|
+
expect(u.revisions(2)[0].name).to eq("Foobar")
|
630
|
+
expect(u.revisions(2)[0].username).to eq("brandon")
|
625
631
|
|
626
|
-
expect(u.revisions(2)[1].name).to eq(
|
627
|
-
expect(u.revisions(2)[1].username).to eq(
|
632
|
+
expect(u.revisions(2)[1].name).to eq("Awesome")
|
633
|
+
expect(u.revisions(2)[1].username).to eq("keepers")
|
628
634
|
end
|
629
635
|
|
630
636
|
it "should be empty if no audits exist" do
|
@@ -633,13 +639,13 @@ describe Audited::Auditor do
|
|
633
639
|
end
|
634
640
|
|
635
641
|
it "should ignore attributes that have been deleted" do
|
636
|
-
user.audits.last.update! audited_changes: {old_attribute:
|
642
|
+
user.audits.last.update! audited_changes: {old_attribute: "old value"}
|
637
643
|
expect { user.revisions }.to_not raise_error
|
638
644
|
end
|
639
645
|
end
|
640
646
|
|
641
647
|
describe "revisions" do
|
642
|
-
let(
|
648
|
+
let(:user) { create_versions(5) }
|
643
649
|
|
644
650
|
it "should maintain identity" do
|
645
651
|
expect(user.revision(1)).to eq(user)
|
@@ -647,15 +653,15 @@ describe Audited::Auditor do
|
|
647
653
|
|
648
654
|
it "should find the given revision" do
|
649
655
|
revision = user.revision(3)
|
650
|
-
expect(revision).to be_a_kind_of(
|
656
|
+
expect(revision).to be_a_kind_of(Models::ActiveRecord::User)
|
651
657
|
expect(revision.audit_version).to eq(3)
|
652
|
-
expect(revision.name).to eq(
|
658
|
+
expect(revision.name).to eq("Foobar 3")
|
653
659
|
end
|
654
660
|
|
655
661
|
it "should find the previous revision with :previous" do
|
656
662
|
revision = user.revision(:previous)
|
657
663
|
expect(revision.audit_version).to eq(4)
|
658
|
-
#expect(revision).to eq(user.revision(4))
|
664
|
+
# expect(revision).to eq(user.revision(4))
|
659
665
|
expect(revision.attributes).to eq(user.revision(4).attributes)
|
660
666
|
end
|
661
667
|
|
@@ -666,7 +672,7 @@ describe Audited::Auditor do
|
|
666
672
|
end
|
667
673
|
|
668
674
|
it "should be able to set protected attributes" do
|
669
|
-
u = Models::ActiveRecord::User.create(name:
|
675
|
+
u = Models::ActiveRecord::User.create(name: "Brandon")
|
670
676
|
u.update_attribute :logins, 1
|
671
677
|
u.update_attribute :logins, 2
|
672
678
|
|
@@ -676,23 +682,23 @@ describe Audited::Auditor do
|
|
676
682
|
end
|
677
683
|
|
678
684
|
it "should set attributes directly" do
|
679
|
-
u = Models::ActiveRecord::User.create(name:
|
680
|
-
expect(u.revision(1).name).to eq(
|
685
|
+
u = Models::ActiveRecord::User.create(name: "<Joe>")
|
686
|
+
expect(u.revision(1).name).to eq("<Joe>")
|
681
687
|
end
|
682
688
|
|
683
689
|
it "should set the attributes for each revision" do
|
684
|
-
u = Models::ActiveRecord::User.create(name:
|
685
|
-
u.update! name:
|
686
|
-
u.update! name:
|
690
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
691
|
+
u.update! name: "Foobar"
|
692
|
+
u.update! name: "Awesome", username: "keepers"
|
687
693
|
|
688
|
-
expect(u.revision(3).name).to eq(
|
689
|
-
expect(u.revision(3).username).to eq(
|
694
|
+
expect(u.revision(3).name).to eq("Awesome")
|
695
|
+
expect(u.revision(3).username).to eq("keepers")
|
690
696
|
|
691
|
-
expect(u.revision(2).name).to eq(
|
692
|
-
expect(u.revision(2).username).to eq(
|
697
|
+
expect(u.revision(2).name).to eq("Foobar")
|
698
|
+
expect(u.revision(2).username).to eq("brandon")
|
693
699
|
|
694
|
-
expect(u.revision(1).name).to eq(
|
695
|
-
expect(u.revision(1).username).to eq(
|
700
|
+
expect(u.revision(1).name).to eq("Brandon")
|
701
|
+
expect(u.revision(1).username).to eq("brandon")
|
696
702
|
end
|
697
703
|
|
698
704
|
it "should correctly restore revision with enum" do
|
@@ -723,34 +729,42 @@ describe Audited::Auditor do
|
|
723
729
|
it "should record new audit when saving revision" do
|
724
730
|
expect {
|
725
731
|
user.revision(1).save!
|
726
|
-
}.to change(
|
732
|
+
}.to change(user.audits, :count).by(1)
|
727
733
|
end
|
728
734
|
|
729
735
|
it "should re-insert destroyed records" do
|
730
736
|
user.destroy
|
731
737
|
expect {
|
732
738
|
user.revision(1).save!
|
733
|
-
}.to change(
|
739
|
+
}.to change(Models::ActiveRecord::User, :count).by(1)
|
734
740
|
end
|
735
741
|
|
736
742
|
it "should return nil for values greater than the number of revisions" do
|
737
743
|
expect(user.revision(user.revisions.count + 1)).to be_nil
|
738
744
|
end
|
745
|
+
|
746
|
+
it "should work with array attributes" do
|
747
|
+
u = Models::ActiveRecord::User.create!(phone_numbers: ["+1 800-444-4444"])
|
748
|
+
u.update!(phone_numbers: ["+1 804-222-1111", "+1 317 222-2222"])
|
749
|
+
|
750
|
+
expect(u.revision(0).phone_numbers).to eq(["+1 804-222-1111", "+1 317 222-2222"])
|
751
|
+
expect(u.revision(1).phone_numbers).to eq(["+1 800-444-4444"])
|
752
|
+
end
|
739
753
|
end
|
740
754
|
|
741
755
|
describe "revision_at" do
|
742
|
-
let(
|
756
|
+
let(:user) { create_user }
|
743
757
|
|
744
758
|
it "should find the latest revision before the given time" do
|
745
759
|
audit = user.audits.first
|
746
760
|
audit.created_at = 1.hour.ago
|
747
761
|
audit.save!
|
748
|
-
user.update! name:
|
749
|
-
expect(user.revision_at(
|
762
|
+
user.update! name: "updated"
|
763
|
+
expect(user.revision_at(2.minutes.ago).audit_version).to eq(1)
|
750
764
|
end
|
751
765
|
|
752
766
|
it "should be nil if given a time before audits" do
|
753
|
-
expect(user.revision_at(
|
767
|
+
expect(user.revision_at(1.week.ago)).to be_nil
|
754
768
|
end
|
755
769
|
end
|
756
770
|
|
@@ -766,6 +780,19 @@ describe Audited::Auditor do
|
|
766
780
|
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
767
781
|
end
|
768
782
|
|
783
|
+
it "should return audits for STI classes" do
|
784
|
+
# Where parent is STI
|
785
|
+
sti_company = Models::ActiveRecord::Company::STICompany.create!
|
786
|
+
sti_company.update!(name: "Collective Idea")
|
787
|
+
expect(sti_company.own_and_associated_audits).to match_array(sti_company.audits)
|
788
|
+
|
789
|
+
# Where associated is STI
|
790
|
+
owner = Models::ActiveRecord::Owner.create!
|
791
|
+
company = owner.companies.create! type: "Models::ActiveRecord::OwnedCompany::STICompany"
|
792
|
+
company.update!(name: "Collective Idea")
|
793
|
+
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
794
|
+
end
|
795
|
+
|
769
796
|
it "should order audits by creation time" do
|
770
797
|
owner = Models::ActiveRecord::Owner.create!
|
771
798
|
first_audit = owner.audits.first
|
@@ -784,19 +811,32 @@ describe Audited::Auditor do
|
|
784
811
|
describe "without auditing" do
|
785
812
|
it "should not save an audit when calling #save_without_auditing" do
|
786
813
|
expect {
|
787
|
-
u = Models::ActiveRecord::User.new(name:
|
814
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
788
815
|
expect(u.save_without_auditing).to eq(true)
|
789
|
-
}.to_not change(
|
816
|
+
}.to_not change(Audited::Audit, :count)
|
790
817
|
end
|
791
818
|
|
792
819
|
it "should not save an audit inside of the #without_auditing block" do
|
793
820
|
expect {
|
794
|
-
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(
|
795
|
-
}.to_not change(
|
821
|
+
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
822
|
+
}.to_not change(Audited::Audit, :count)
|
823
|
+
end
|
824
|
+
|
825
|
+
context "when global audits are disabled" do
|
826
|
+
it "should re-enable class audits after #without_auditing block" do
|
827
|
+
Audited.auditing_enabled = false
|
828
|
+
Models::ActiveRecord::User.without_auditing {}
|
829
|
+
Audited.auditing_enabled = true
|
830
|
+
expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
|
831
|
+
end
|
796
832
|
end
|
797
833
|
|
798
834
|
it "should reset auditing status even it raises an exception" do
|
799
|
-
|
835
|
+
begin
|
836
|
+
Models::ActiveRecord::User.without_auditing { raise }
|
837
|
+
rescue
|
838
|
+
nil
|
839
|
+
end
|
800
840
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
801
841
|
end
|
802
842
|
|
@@ -807,7 +847,7 @@ describe Audited::Auditor do
|
|
807
847
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
808
848
|
Models::ActiveRecord::User.without_auditing do
|
809
849
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
810
|
-
Models::ActiveRecord::User.create!(
|
850
|
+
Models::ActiveRecord::User.create!(name: "Bart")
|
811
851
|
sleep 1
|
812
852
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
813
853
|
end
|
@@ -817,13 +857,13 @@ describe Audited::Auditor do
|
|
817
857
|
t2 = Thread.new do
|
818
858
|
sleep 0.5
|
819
859
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
820
|
-
Models::ActiveRecord::User.create!(
|
860
|
+
Models::ActiveRecord::User.create!(name: "Lisa")
|
821
861
|
end
|
822
862
|
t1.join
|
823
863
|
t2.join
|
824
864
|
|
825
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
826
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
865
|
+
expect(Models::ActiveRecord::User.find_by_name("Bart").audits.count).to eq(0)
|
866
|
+
expect(Models::ActiveRecord::User.find_by_name("Lisa").audits.count).to eq(1)
|
827
867
|
end
|
828
868
|
|
829
869
|
it "should not save an audit when auditing is globally disabled" do
|
@@ -837,7 +877,7 @@ describe Audited::Auditor do
|
|
837
877
|
Audited.auditing_enabled = true
|
838
878
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
839
879
|
|
840
|
-
user.update!(name:
|
880
|
+
user.update!(name: "Test")
|
841
881
|
expect(user.audits.count).to eq(1)
|
842
882
|
Models::ActiveRecord::User.enable_auditing
|
843
883
|
end
|
@@ -846,24 +886,37 @@ describe Audited::Auditor do
|
|
846
886
|
describe "with auditing" do
|
847
887
|
it "should save an audit when calling #save_with_auditing" do
|
848
888
|
expect {
|
849
|
-
u = Models::ActiveRecord::User.new(name:
|
889
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
850
890
|
Models::ActiveRecord::User.auditing_enabled = false
|
851
891
|
expect(u.save_with_auditing).to eq(true)
|
852
892
|
Models::ActiveRecord::User.auditing_enabled = true
|
853
|
-
}.to change(
|
893
|
+
}.to change(Audited::Audit, :count).by(1)
|
854
894
|
end
|
855
895
|
|
856
896
|
it "should save an audit inside of the #with_auditing block" do
|
857
897
|
expect {
|
858
898
|
Models::ActiveRecord::User.auditing_enabled = false
|
859
|
-
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(
|
899
|
+
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
860
900
|
Models::ActiveRecord::User.auditing_enabled = true
|
861
|
-
}.to change(
|
901
|
+
}.to change(Audited::Audit, :count).by(1)
|
902
|
+
end
|
903
|
+
|
904
|
+
context "when global audits are disabled" do
|
905
|
+
it "should re-enable class audits after #with_auditing block" do
|
906
|
+
Audited.auditing_enabled = false
|
907
|
+
Models::ActiveRecord::User.with_auditing {}
|
908
|
+
Audited.auditing_enabled = true
|
909
|
+
expect(Models::ActiveRecord::User.auditing_enabled).to eql(true)
|
910
|
+
end
|
862
911
|
end
|
863
912
|
|
864
913
|
it "should reset auditing status even it raises an exception" do
|
865
914
|
Models::ActiveRecord::User.disable_auditing
|
866
|
-
|
915
|
+
begin
|
916
|
+
Models::ActiveRecord::User.with_auditing { raise }
|
917
|
+
rescue
|
918
|
+
nil
|
919
|
+
end
|
867
920
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
868
921
|
Models::ActiveRecord::User.enable_auditing
|
869
922
|
end
|
@@ -877,7 +930,7 @@ describe Audited::Auditor do
|
|
877
930
|
Models::ActiveRecord::User.with_auditing do
|
878
931
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
879
932
|
|
880
|
-
Models::ActiveRecord::User.create!(
|
933
|
+
Models::ActiveRecord::User.create!(name: "Shaggy")
|
881
934
|
sleep 1
|
882
935
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
883
936
|
end
|
@@ -889,74 +942,81 @@ describe Audited::Auditor do
|
|
889
942
|
sleep 0.5
|
890
943
|
Models::ActiveRecord::User.disable_auditing
|
891
944
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
892
|
-
Models::ActiveRecord::User.create!(
|
945
|
+
Models::ActiveRecord::User.create!(name: "Scooby")
|
893
946
|
Models::ActiveRecord::User.enable_auditing
|
894
947
|
end
|
895
948
|
t1.join
|
896
949
|
t2.join
|
897
950
|
|
898
951
|
Models::ActiveRecord::User.enable_auditing
|
899
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
900
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
952
|
+
expect(Models::ActiveRecord::User.find_by_name("Shaggy").audits.count).to eq(1)
|
953
|
+
expect(Models::ActiveRecord::User.find_by_name("Scooby").audits.count).to eq(0)
|
901
954
|
end
|
902
955
|
end
|
903
956
|
|
904
957
|
describe "comment required" do
|
905
|
-
|
906
958
|
describe "on create" do
|
907
959
|
it "should not validate when audit_comment is not supplied when initialized" do
|
908
|
-
expect(Models::ActiveRecord::CommentRequiredUser.new(name:
|
960
|
+
expect(Models::ActiveRecord::CommentRequiredUser.new(name: "Foo")).not_to be_valid
|
909
961
|
end
|
910
962
|
|
911
963
|
it "should not validate when audit_comment is not supplied trying to create" do
|
912
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
964
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).not_to be_valid
|
913
965
|
end
|
914
966
|
|
915
967
|
it "should validate when audit_comment is supplied" do
|
916
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
968
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo", audit_comment: "Create")).to be_valid
|
917
969
|
end
|
918
970
|
|
919
971
|
it "should validate when audit_comment is not supplied, and creating is not being audited" do
|
920
|
-
expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name:
|
921
|
-
expect(Models::ActiveRecord::OnDestroyCommentRequiredUser.create(name:
|
972
|
+
expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name: "Foo")).to be_valid
|
973
|
+
expect(Models::ActiveRecord::OnDestroyCommentRequiredUser.create(name: "Foo")).to be_valid
|
922
974
|
end
|
923
975
|
|
924
976
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
925
977
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
926
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
978
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
|
927
979
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
928
980
|
end
|
981
|
+
|
982
|
+
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
983
|
+
expect(Models::ActiveRecord::CommentRequiredUser.new(password: "Foo")).to be_valid
|
984
|
+
end
|
929
985
|
end
|
930
986
|
|
931
987
|
describe "on update" do
|
932
|
-
let(
|
933
|
-
let(
|
934
|
-
let(
|
988
|
+
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
989
|
+
let(:on_create_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
990
|
+
let(:on_destroy_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
935
991
|
|
936
992
|
it "should not validate when audit_comment is not supplied" do
|
937
|
-
expect(user.update(name:
|
993
|
+
expect(user.update(name: "Test")).to eq(false)
|
938
994
|
end
|
939
995
|
|
940
996
|
it "should validate when audit_comment is not supplied, and updating is not being audited" do
|
941
|
-
expect(on_create_user.update(name:
|
942
|
-
expect(on_destroy_user.update(name:
|
997
|
+
expect(on_create_user.update(name: "Test")).to eq(true)
|
998
|
+
expect(on_destroy_user.update(name: "Test")).to eq(true)
|
943
999
|
end
|
944
1000
|
|
945
1001
|
it "should validate when audit_comment is supplied" do
|
946
|
-
expect(user.update(name:
|
1002
|
+
expect(user.update(name: "Test", audit_comment: "Update")).to eq(true)
|
947
1003
|
end
|
948
1004
|
|
949
1005
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
950
1006
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
951
|
-
expect(user.update(name:
|
1007
|
+
expect(user.update(name: "Test")).to eq(true)
|
952
1008
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
953
1009
|
end
|
1010
|
+
|
1011
|
+
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
1012
|
+
expect(user.update(password: "Test")).to eq(true)
|
1013
|
+
end
|
954
1014
|
end
|
955
1015
|
|
956
1016
|
describe "on destroy" do
|
957
|
-
let(
|
958
|
-
let(
|
959
|
-
let(
|
1017
|
+
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
1018
|
+
let(:on_create_user) { Models::ActiveRecord::OnCreateCommentRequiredUser.create!(audit_comment: "Create") }
|
1019
|
+
let(:on_update_user) { Models::ActiveRecord::OnUpdateCommentRequiredUser.create }
|
960
1020
|
|
961
1021
|
it "should not validate when audit_comment is not supplied" do
|
962
1022
|
expect(user.destroy).to eq(false)
|
@@ -978,41 +1038,38 @@ describe Audited::Auditor do
|
|
978
1038
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
979
1039
|
end
|
980
1040
|
end
|
981
|
-
|
982
1041
|
end
|
983
1042
|
|
984
1043
|
describe "no update with comment only" do
|
985
|
-
let(
|
1044
|
+
let(:user) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
|
986
1045
|
|
987
1046
|
it "does not create an audit when only an audit_comment is present" do
|
988
1047
|
user.audit_comment = "Comment"
|
989
|
-
expect { user.save! }.to_not change(
|
1048
|
+
expect { user.save! }.to_not change(Audited::Audit, :count)
|
990
1049
|
end
|
991
|
-
|
992
1050
|
end
|
993
1051
|
|
994
1052
|
describe "attr_protected and attr_accessible" do
|
995
|
-
|
996
1053
|
it "should not raise error when attr_accessible is set and protected is false" do
|
997
1054
|
expect {
|
998
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1055
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
999
1056
|
}.to_not raise_error
|
1000
1057
|
end
|
1001
1058
|
|
1002
1059
|
it "should not rause an error when attr_accessible is declared before audited" do
|
1003
1060
|
expect {
|
1004
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1061
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
1005
1062
|
}.to_not raise_error
|
1006
1063
|
end
|
1007
1064
|
end
|
1008
1065
|
|
1009
1066
|
describe "audit_as" do
|
1010
|
-
let(
|
1067
|
+
let(:user) { Models::ActiveRecord::User.create name: "Testing" }
|
1011
1068
|
|
1012
1069
|
it "should record user objects" do
|
1013
|
-
Models::ActiveRecord::Company.audit_as(
|
1014
|
-
company = Models::ActiveRecord::Company.create name:
|
1015
|
-
company.update! name:
|
1070
|
+
Models::ActiveRecord::Company.audit_as(user) do
|
1071
|
+
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1072
|
+
company.update! name: "The Auditors"
|
1016
1073
|
|
1017
1074
|
company.audits.each do |audit|
|
1018
1075
|
expect(audit.user).to eq(user)
|
@@ -1021,9 +1078,9 @@ describe Audited::Auditor do
|
|
1021
1078
|
end
|
1022
1079
|
|
1023
1080
|
it "should record usernames" do
|
1024
|
-
Models::ActiveRecord::Company.audit_as(
|
1025
|
-
company = Models::ActiveRecord::Company.create name:
|
1026
|
-
company.update! name:
|
1081
|
+
Models::ActiveRecord::Company.audit_as(user.name) do
|
1082
|
+
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1083
|
+
company.update! name: "The Auditors"
|
1027
1084
|
|
1028
1085
|
company.audits.each do |audit|
|
1029
1086
|
expect(audit.user).to eq(user.name)
|
@@ -1033,7 +1090,7 @@ describe Audited::Auditor do
|
|
1033
1090
|
end
|
1034
1091
|
|
1035
1092
|
describe "after_audit" do
|
1036
|
-
let(
|
1093
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1037
1094
|
|
1038
1095
|
it "should invoke after_audit callback on create" do
|
1039
1096
|
expect(user.bogus_attr).to be_nil
|
@@ -1043,7 +1100,7 @@ describe Audited::Auditor do
|
|
1043
1100
|
end
|
1044
1101
|
|
1045
1102
|
describe "around_audit" do
|
1046
|
-
let(
|
1103
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1047
1104
|
|
1048
1105
|
it "should invoke around_audit callback on create" do
|
1049
1106
|
expect(user.around_attr).to be_nil
|
@@ -1054,13 +1111,13 @@ describe Audited::Auditor do
|
|
1054
1111
|
|
1055
1112
|
describe "STI auditing" do
|
1056
1113
|
it "should correctly disable auditing when using STI" do
|
1057
|
-
company = Models::ActiveRecord::Company::STICompany.create name:
|
1114
|
+
company = Models::ActiveRecord::Company::STICompany.create name: "The auditors"
|
1058
1115
|
expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
|
1059
1116
|
expect {
|
1060
1117
|
Models::ActiveRecord::Company.auditing_enabled = false
|
1061
|
-
company.update! name:
|
1118
|
+
company.update! name: "STI auditors"
|
1062
1119
|
Models::ActiveRecord::Company.auditing_enabled = true
|
1063
|
-
}.to_not change(
|
1120
|
+
}.to_not change(Audited::Audit, :count)
|
1064
1121
|
end
|
1065
1122
|
end
|
1066
1123
|
end
|