audited 4.10.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of audited might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.standard.yml +5 -0
- data/.travis.yml +25 -21
- data/Appraisals +10 -18
- data/CHANGELOG.md +29 -1
- data/Gemfile +1 -1
- data/README.md +16 -3
- data/Rakefile +6 -6
- data/lib/audited-rspec.rb +3 -1
- data/lib/audited.rb +25 -8
- data/lib/audited/audit.rb +24 -25
- data/lib/audited/auditor.rb +45 -39
- 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/generators/audited/install_generator.rb +9 -7
- data/lib/generators/audited/migration.rb +2 -0
- 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 +67 -45
- data/spec/audited/auditor_spec.rb +284 -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 +3 -3
- data/spec/rails_app/config/environment.rb +1 -1
- data/spec/rails_app/config/environments/test.rb +5 -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 +16 -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 +20 -22
- data/.rubocop.yml +0 -25
- 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,22 @@ 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])
|
246
235
|
end
|
247
236
|
|
248
|
-
if ActiveRecord::Base.connection.adapter_name ==
|
237
|
+
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
249
238
|
describe "'json' and 'jsonb' audited_changes column type" do
|
250
239
|
let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
|
251
240
|
|
@@ -282,16 +271,16 @@ describe Audited::Auditor do
|
|
282
271
|
it "should allow mass assignment of all unprotected attributes" do
|
283
272
|
yesterday = 1.day.ago
|
284
273
|
|
285
|
-
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name:
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
274
|
+
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name: "name",
|
275
|
+
username: "username",
|
276
|
+
password: "password",
|
277
|
+
activated: true,
|
278
|
+
suspended_at: yesterday,
|
279
|
+
logins: 2)
|
291
280
|
|
292
|
-
expect(u.name).to eq(
|
293
|
-
expect(u.username).to eq(
|
294
|
-
expect(u.password).to eq(
|
281
|
+
expect(u.name).to eq("name")
|
282
|
+
expect(u.username).to eq("username")
|
283
|
+
expect(u.password).to eq("password")
|
295
284
|
expect(u.activated).to eq(true)
|
296
285
|
expect(u.suspended_at.to_i).to eq(yesterday.to_i)
|
297
286
|
expect(u.logins).to eq(2)
|
@@ -299,12 +288,12 @@ describe Audited::Auditor do
|
|
299
288
|
end
|
300
289
|
|
301
290
|
describe "on create" do
|
302
|
-
let(
|
291
|
+
let(:user) { create_user status: :reliable, audit_comment: "Create" }
|
303
292
|
|
304
293
|
it "should change the audit count" do
|
305
294
|
expect {
|
306
295
|
user
|
307
|
-
}.to change(
|
296
|
+
}.to change(Audited::Audit, :count).by(1)
|
308
297
|
end
|
309
298
|
|
310
299
|
it "should create associated audit" do
|
@@ -312,7 +301,7 @@ describe Audited::Auditor do
|
|
312
301
|
end
|
313
302
|
|
314
303
|
it "should set the action to create" do
|
315
|
-
expect(user.audits.first.action).to eq(
|
304
|
+
expect(user.audits.first.action).to eq("create")
|
316
305
|
expect(Audited::Audit.creates.order(:id).last).to eq(user.audits.first)
|
317
306
|
expect(user.audits.creates.count).to eq(1)
|
318
307
|
expect(user.audits.updates.count).to eq(0)
|
@@ -327,46 +316,55 @@ describe Audited::Auditor do
|
|
327
316
|
expect(user.audits.first.audited_changes["status"]).to eq(1)
|
328
317
|
end
|
329
318
|
|
319
|
+
context "when store_synthesized_enums is set to true" do
|
320
|
+
before { Audited.store_synthesized_enums = true }
|
321
|
+
after { Audited.store_synthesized_enums = false }
|
322
|
+
|
323
|
+
it "should store enum value as Rails synthesized value" do
|
324
|
+
expect(user.audits.first.audited_changes["status"]).to eq("reliable")
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
330
328
|
it "should store comment" do
|
331
|
-
expect(user.audits.first.comment).to eq(
|
329
|
+
expect(user.audits.first.comment).to eq("Create")
|
332
330
|
end
|
333
331
|
|
334
332
|
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| [
|
333
|
+
on_create_destroy_except_name = Models::ActiveRecord::OnCreateDestroyExceptName.create(name: "Bart")
|
334
|
+
expect(on_create_destroy_except_name.audits.first.audited_changes.keys.any? { |col| ["name"].include? col }).to eq(false)
|
337
335
|
end
|
338
336
|
|
339
337
|
it "should not save an audit if only specified on update/destroy" do
|
340
338
|
expect {
|
341
|
-
Models::ActiveRecord::OnUpdateDestroy.create!(
|
342
|
-
}.to_not change(
|
339
|
+
Models::ActiveRecord::OnUpdateDestroy.create!(name: "Bart")
|
340
|
+
}.to_not change(Audited::Audit, :count)
|
343
341
|
end
|
344
342
|
end
|
345
343
|
|
346
344
|
describe "on update" do
|
347
345
|
before do
|
348
|
-
@user = create_user(
|
346
|
+
@user = create_user(name: "Brandon", status: :active, audit_comment: "Update")
|
349
347
|
end
|
350
348
|
|
351
349
|
it "should save an audit" do
|
352
350
|
expect {
|
353
351
|
@user.update_attribute(:name, "Someone")
|
354
|
-
}.to change(
|
352
|
+
}.to change(Audited::Audit, :count).by(1)
|
355
353
|
expect {
|
356
354
|
@user.update_attribute(:name, "Someone else")
|
357
|
-
}.to change(
|
355
|
+
}.to change(Audited::Audit, :count).by(1)
|
358
356
|
end
|
359
357
|
|
360
358
|
it "should set the action to 'update'" do
|
361
|
-
@user.update! name:
|
362
|
-
expect(@user.audits.last.action).to eq(
|
359
|
+
@user.update! name: "Changed"
|
360
|
+
expect(@user.audits.last.action).to eq("update")
|
363
361
|
expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
|
364
362
|
expect(@user.audits.updates.last).to eq(@user.audits.last)
|
365
363
|
end
|
366
364
|
|
367
365
|
it "should store the changed attributes" do
|
368
|
-
@user.update! name:
|
369
|
-
expect(@user.audits.last.audited_changes).to eq({
|
366
|
+
@user.update! name: "Changed"
|
367
|
+
expect(@user.audits.last.audited_changes).to eq({"name" => ["Brandon", "Changed"]})
|
370
368
|
end
|
371
369
|
|
372
370
|
it "should store changed enum values" do
|
@@ -375,35 +373,35 @@ describe Audited::Auditor do
|
|
375
373
|
end
|
376
374
|
|
377
375
|
it "should store audit comment" do
|
378
|
-
expect(@user.audits.last.comment).to eq(
|
376
|
+
expect(@user.audits.last.comment).to eq("Update")
|
379
377
|
end
|
380
378
|
|
381
379
|
it "should not save an audit if only specified on create/destroy" do
|
382
|
-
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(
|
380
|
+
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(name: "Bart")
|
383
381
|
expect {
|
384
|
-
on_create_destroy.update! name:
|
385
|
-
}.to_not change(
|
382
|
+
on_create_destroy.update! name: "Changed"
|
383
|
+
}.to_not change(Audited::Audit, :count)
|
386
384
|
end
|
387
385
|
|
388
386
|
it "should not save an audit if the value doesn't change after type casting" do
|
389
387
|
@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,
|
388
|
+
expect { @user.update_attribute :logins, "0" }.to_not change(Audited::Audit, :count)
|
389
|
+
expect { @user.update_attribute :activated, 1 }.to_not change(Audited::Audit, :count)
|
390
|
+
expect { @user.update_attribute :activated, "1" }.to_not change(Audited::Audit, :count)
|
393
391
|
end
|
394
392
|
|
395
393
|
describe "with no dirty changes" do
|
396
394
|
it "does not create an audit if the record is not changed" do
|
397
395
|
expect {
|
398
396
|
@user.save!
|
399
|
-
}.to_not change(
|
397
|
+
}.to_not change(Audited::Audit, :count)
|
400
398
|
end
|
401
399
|
|
402
400
|
it "creates an audit when an audit comment is present" do
|
403
401
|
expect {
|
404
402
|
@user.audit_comment = "Comment"
|
405
403
|
@user.save!
|
406
|
-
}.to change(
|
404
|
+
}.to change(Audited::Audit, :count)
|
407
405
|
end
|
408
406
|
end
|
409
407
|
end
|
@@ -416,7 +414,7 @@ describe Audited::Auditor do
|
|
416
414
|
it "should save an audit" do
|
417
415
|
expect {
|
418
416
|
@user.destroy
|
419
|
-
}.to change(
|
417
|
+
}.to change(Audited::Audit, :count)
|
420
418
|
|
421
419
|
expect(@user.audits.size).to eq(2)
|
422
420
|
end
|
@@ -424,7 +422,7 @@ describe Audited::Auditor do
|
|
424
422
|
it "should set the action to 'destroy'" do
|
425
423
|
@user.destroy
|
426
424
|
|
427
|
-
expect(@user.audits.last.action).to eq(
|
425
|
+
expect(@user.audits.last.action).to eq("destroy")
|
428
426
|
expect(Audited::Audit.destroys.order(:id).last).to eq(@user.audits.last)
|
429
427
|
expect(@user.audits.destroys.last).to eq(@user.audits.last)
|
430
428
|
end
|
@@ -449,11 +447,11 @@ describe Audited::Auditor do
|
|
449
447
|
end
|
450
448
|
|
451
449
|
it "should not save an audit if only specified on create/update" do
|
452
|
-
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(
|
450
|
+
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(name: "Bart")
|
453
451
|
|
454
452
|
expect {
|
455
453
|
on_create_update.destroy
|
456
|
-
}.to_not change(
|
454
|
+
}.to_not change(Audited::Audit, :count)
|
457
455
|
end
|
458
456
|
|
459
457
|
it "should audit dependent destructions" do
|
@@ -462,9 +460,9 @@ describe Audited::Auditor do
|
|
462
460
|
|
463
461
|
expect {
|
464
462
|
owner.destroy
|
465
|
-
}.to change(
|
463
|
+
}.to change(Audited::Audit, :count)
|
466
464
|
|
467
|
-
expect(company.audits.map { |a| a.action }).to eq([
|
465
|
+
expect(company.audits.map { |a| a.action }).to eq(["create", "destroy"])
|
468
466
|
end
|
469
467
|
end
|
470
468
|
|
@@ -476,20 +474,20 @@ describe Audited::Auditor do
|
|
476
474
|
user.destroy
|
477
475
|
}.to_not raise_error
|
478
476
|
|
479
|
-
expect(
|
477
|
+
expect(user.audits).to be_empty
|
480
478
|
end
|
481
479
|
end
|
482
480
|
|
483
481
|
describe "associated with" do
|
484
|
-
let(:owner) { Models::ActiveRecord::Owner.create(name:
|
485
|
-
let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name:
|
482
|
+
let(:owner) { Models::ActiveRecord::Owner.create(name: "Models::ActiveRecord::Owner") }
|
483
|
+
let(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
486
484
|
|
487
485
|
it "should record the associated object on create" do
|
488
486
|
expect(owned_company.audits.first.associated).to eq(owner)
|
489
487
|
end
|
490
488
|
|
491
489
|
it "should store the associated object on update" do
|
492
|
-
owned_company.update_attribute(:name,
|
490
|
+
owned_company.update_attribute(:name, "The Auditors")
|
493
491
|
expect(owned_company.audits.last.associated).to eq(owner)
|
494
492
|
end
|
495
493
|
|
@@ -500,8 +498,8 @@ describe Audited::Auditor do
|
|
500
498
|
end
|
501
499
|
|
502
500
|
describe "has associated audits" do
|
503
|
-
let!(:owner) { Models::ActiveRecord::Owner.create!(name:
|
504
|
-
let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name:
|
501
|
+
let!(:owner) { Models::ActiveRecord::Owner.create!(name: "Models::ActiveRecord::Owner") }
|
502
|
+
let!(:owned_company) { Models::ActiveRecord::OwnedCompany.create!(name: "The auditors", owner: owner) }
|
505
503
|
|
506
504
|
it "should list the associated audits" do
|
507
505
|
expect(owner.associated_audits.length).to eq(1)
|
@@ -525,7 +523,7 @@ describe Audited::Auditor do
|
|
525
523
|
it "should delete old audits when keeped amount exceeded" do
|
526
524
|
stub_global_max_audits(2) do
|
527
525
|
user = create_versions(2)
|
528
|
-
user.update(name:
|
526
|
+
user.update(name: "John")
|
529
527
|
expect(user.audits.pluck(:version)).to eq([2, 3])
|
530
528
|
end
|
531
529
|
end
|
@@ -533,35 +531,35 @@ describe Audited::Auditor do
|
|
533
531
|
it "should not delete old audits when keeped amount not exceeded" do
|
534
532
|
stub_global_max_audits(3) do
|
535
533
|
user = create_versions(2)
|
536
|
-
user.update(name:
|
534
|
+
user.update(name: "John")
|
537
535
|
expect(user.audits.pluck(:version)).to eq([1, 2, 3])
|
538
536
|
end
|
539
537
|
end
|
540
538
|
|
541
539
|
it "should delete old extra audits after introducing limit" do
|
542
540
|
stub_global_max_audits(nil) do
|
543
|
-
user = Models::ActiveRecord::User.create!(name:
|
544
|
-
user.update!(name:
|
545
|
-
user.update!(name:
|
541
|
+
user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
|
542
|
+
user.update!(name: "Foobar")
|
543
|
+
user.update!(name: "Awesome", username: "keepers")
|
546
544
|
user.update!(activated: true)
|
547
545
|
|
548
546
|
Audited.max_audits = 3
|
549
547
|
Models::ActiveRecord::User.send(:normalize_audited_options)
|
550
|
-
user.update!(favourite_device:
|
548
|
+
user.update!(favourite_device: "Android Phone")
|
551
549
|
audits = user.audits
|
552
550
|
|
553
551
|
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({
|
552
|
+
expect(audits[0].audited_changes).to include({"name" => ["Foobar", "Awesome"], "username" => ["brandon", "keepers"]})
|
553
|
+
expect(audits[1].audited_changes).to eq({"activated" => [nil, true]})
|
554
|
+
expect(audits[2].audited_changes).to eq({"favourite_device" => [nil, "Android Phone"]})
|
557
555
|
end
|
558
556
|
end
|
559
557
|
|
560
558
|
it "should add comment line for combined audit" do
|
561
559
|
stub_global_max_audits(2) do
|
562
|
-
user = Models::ActiveRecord::User.create!(name:
|
563
|
-
user.update(name:
|
564
|
-
user.update(name:
|
560
|
+
user = Models::ActiveRecord::User.create!(name: "Foobar 1")
|
561
|
+
user.update(name: "Foobar 2", audit_comment: "First audit comment")
|
562
|
+
user.update(name: "Foobar 3", audit_comment: "Second audit comment")
|
565
563
|
expect(user.audits.first.comment).to match(/First audit comment.+is the result of multiple/m)
|
566
564
|
end
|
567
565
|
end
|
@@ -581,10 +579,10 @@ describe Audited::Auditor do
|
|
581
579
|
end
|
582
580
|
|
583
581
|
describe "revisions" do
|
584
|
-
let(
|
582
|
+
let(:user) { create_versions }
|
585
583
|
|
586
584
|
it "should return an Array of Users" do
|
587
|
-
expect(user.revisions).to be_a_kind_of(
|
585
|
+
expect(user.revisions).to be_a_kind_of(Array)
|
588
586
|
user.revisions.each { |version| expect(version).to be_a_kind_of Models::ActiveRecord::User }
|
589
587
|
end
|
590
588
|
|
@@ -593,38 +591,38 @@ describe Audited::Auditor do
|
|
593
591
|
end
|
594
592
|
|
595
593
|
it "should have one revision for each audit" do
|
596
|
-
expect(user.audits.size).to eql(
|
594
|
+
expect(user.audits.size).to eql(user.revisions.size)
|
597
595
|
end
|
598
596
|
|
599
597
|
it "should set the attributes for each revision" do
|
600
|
-
u = Models::ActiveRecord::User.create(name:
|
601
|
-
u.update! name:
|
602
|
-
u.update! name:
|
598
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
599
|
+
u.update! name: "Foobar"
|
600
|
+
u.update! name: "Awesome", username: "keepers"
|
603
601
|
|
604
602
|
expect(u.revisions.size).to eql(3)
|
605
603
|
|
606
|
-
expect(u.revisions[0].name).to eql(
|
607
|
-
expect(u.revisions[0].username).to eql(
|
604
|
+
expect(u.revisions[0].name).to eql("Brandon")
|
605
|
+
expect(u.revisions[0].username).to eql("brandon")
|
608
606
|
|
609
|
-
expect(u.revisions[1].name).to eql(
|
610
|
-
expect(u.revisions[1].username).to eql(
|
607
|
+
expect(u.revisions[1].name).to eql("Foobar")
|
608
|
+
expect(u.revisions[1].username).to eql("brandon")
|
611
609
|
|
612
|
-
expect(u.revisions[2].name).to eql(
|
613
|
-
expect(u.revisions[2].username).to eql(
|
610
|
+
expect(u.revisions[2].name).to eql("Awesome")
|
611
|
+
expect(u.revisions[2].username).to eql("keepers")
|
614
612
|
end
|
615
613
|
|
616
614
|
it "access to only recent revisions" do
|
617
|
-
u = Models::ActiveRecord::User.create(name:
|
618
|
-
u.update! name:
|
619
|
-
u.update! name:
|
615
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
616
|
+
u.update! name: "Foobar"
|
617
|
+
u.update! name: "Awesome", username: "keepers"
|
620
618
|
|
621
619
|
expect(u.revisions(2).size).to eq(2)
|
622
620
|
|
623
|
-
expect(u.revisions(2)[0].name).to eq(
|
624
|
-
expect(u.revisions(2)[0].username).to eq(
|
621
|
+
expect(u.revisions(2)[0].name).to eq("Foobar")
|
622
|
+
expect(u.revisions(2)[0].username).to eq("brandon")
|
625
623
|
|
626
|
-
expect(u.revisions(2)[1].name).to eq(
|
627
|
-
expect(u.revisions(2)[1].username).to eq(
|
624
|
+
expect(u.revisions(2)[1].name).to eq("Awesome")
|
625
|
+
expect(u.revisions(2)[1].username).to eq("keepers")
|
628
626
|
end
|
629
627
|
|
630
628
|
it "should be empty if no audits exist" do
|
@@ -633,13 +631,13 @@ describe Audited::Auditor do
|
|
633
631
|
end
|
634
632
|
|
635
633
|
it "should ignore attributes that have been deleted" do
|
636
|
-
user.audits.last.update! audited_changes: {old_attribute:
|
634
|
+
user.audits.last.update! audited_changes: {old_attribute: "old value"}
|
637
635
|
expect { user.revisions }.to_not raise_error
|
638
636
|
end
|
639
637
|
end
|
640
638
|
|
641
639
|
describe "revisions" do
|
642
|
-
let(
|
640
|
+
let(:user) { create_versions(5) }
|
643
641
|
|
644
642
|
it "should maintain identity" do
|
645
643
|
expect(user.revision(1)).to eq(user)
|
@@ -647,15 +645,15 @@ describe Audited::Auditor do
|
|
647
645
|
|
648
646
|
it "should find the given revision" do
|
649
647
|
revision = user.revision(3)
|
650
|
-
expect(revision).to be_a_kind_of(
|
648
|
+
expect(revision).to be_a_kind_of(Models::ActiveRecord::User)
|
651
649
|
expect(revision.audit_version).to eq(3)
|
652
|
-
expect(revision.name).to eq(
|
650
|
+
expect(revision.name).to eq("Foobar 3")
|
653
651
|
end
|
654
652
|
|
655
653
|
it "should find the previous revision with :previous" do
|
656
654
|
revision = user.revision(:previous)
|
657
655
|
expect(revision.audit_version).to eq(4)
|
658
|
-
#expect(revision).to eq(user.revision(4))
|
656
|
+
# expect(revision).to eq(user.revision(4))
|
659
657
|
expect(revision.attributes).to eq(user.revision(4).attributes)
|
660
658
|
end
|
661
659
|
|
@@ -666,7 +664,7 @@ describe Audited::Auditor do
|
|
666
664
|
end
|
667
665
|
|
668
666
|
it "should be able to set protected attributes" do
|
669
|
-
u = Models::ActiveRecord::User.create(name:
|
667
|
+
u = Models::ActiveRecord::User.create(name: "Brandon")
|
670
668
|
u.update_attribute :logins, 1
|
671
669
|
u.update_attribute :logins, 2
|
672
670
|
|
@@ -676,23 +674,23 @@ describe Audited::Auditor do
|
|
676
674
|
end
|
677
675
|
|
678
676
|
it "should set attributes directly" do
|
679
|
-
u = Models::ActiveRecord::User.create(name:
|
680
|
-
expect(u.revision(1).name).to eq(
|
677
|
+
u = Models::ActiveRecord::User.create(name: "<Joe>")
|
678
|
+
expect(u.revision(1).name).to eq("<Joe>")
|
681
679
|
end
|
682
680
|
|
683
681
|
it "should set the attributes for each revision" do
|
684
|
-
u = Models::ActiveRecord::User.create(name:
|
685
|
-
u.update! name:
|
686
|
-
u.update! name:
|
682
|
+
u = Models::ActiveRecord::User.create(name: "Brandon", username: "brandon")
|
683
|
+
u.update! name: "Foobar"
|
684
|
+
u.update! name: "Awesome", username: "keepers"
|
687
685
|
|
688
|
-
expect(u.revision(3).name).to eq(
|
689
|
-
expect(u.revision(3).username).to eq(
|
686
|
+
expect(u.revision(3).name).to eq("Awesome")
|
687
|
+
expect(u.revision(3).username).to eq("keepers")
|
690
688
|
|
691
|
-
expect(u.revision(2).name).to eq(
|
692
|
-
expect(u.revision(2).username).to eq(
|
689
|
+
expect(u.revision(2).name).to eq("Foobar")
|
690
|
+
expect(u.revision(2).username).to eq("brandon")
|
693
691
|
|
694
|
-
expect(u.revision(1).name).to eq(
|
695
|
-
expect(u.revision(1).username).to eq(
|
692
|
+
expect(u.revision(1).name).to eq("Brandon")
|
693
|
+
expect(u.revision(1).username).to eq("brandon")
|
696
694
|
end
|
697
695
|
|
698
696
|
it "should correctly restore revision with enum" do
|
@@ -723,34 +721,42 @@ describe Audited::Auditor do
|
|
723
721
|
it "should record new audit when saving revision" do
|
724
722
|
expect {
|
725
723
|
user.revision(1).save!
|
726
|
-
}.to change(
|
724
|
+
}.to change(user.audits, :count).by(1)
|
727
725
|
end
|
728
726
|
|
729
727
|
it "should re-insert destroyed records" do
|
730
728
|
user.destroy
|
731
729
|
expect {
|
732
730
|
user.revision(1).save!
|
733
|
-
}.to change(
|
731
|
+
}.to change(Models::ActiveRecord::User, :count).by(1)
|
734
732
|
end
|
735
733
|
|
736
734
|
it "should return nil for values greater than the number of revisions" do
|
737
735
|
expect(user.revision(user.revisions.count + 1)).to be_nil
|
738
736
|
end
|
737
|
+
|
738
|
+
it "should work with array attributes" do
|
739
|
+
u = Models::ActiveRecord::User.create!(phone_numbers: ["+1 800-444-4444"])
|
740
|
+
u.update!(phone_numbers: ["+1 804-222-1111", "+1 317 222-2222"])
|
741
|
+
|
742
|
+
expect(u.revision(0).phone_numbers).to eq(["+1 804-222-1111", "+1 317 222-2222"])
|
743
|
+
expect(u.revision(1).phone_numbers).to eq(["+1 800-444-4444"])
|
744
|
+
end
|
739
745
|
end
|
740
746
|
|
741
747
|
describe "revision_at" do
|
742
|
-
let(
|
748
|
+
let(:user) { create_user }
|
743
749
|
|
744
750
|
it "should find the latest revision before the given time" do
|
745
751
|
audit = user.audits.first
|
746
752
|
audit.created_at = 1.hour.ago
|
747
753
|
audit.save!
|
748
|
-
user.update! name:
|
749
|
-
expect(user.revision_at(
|
754
|
+
user.update! name: "updated"
|
755
|
+
expect(user.revision_at(2.minutes.ago).audit_version).to eq(1)
|
750
756
|
end
|
751
757
|
|
752
758
|
it "should be nil if given a time before audits" do
|
753
|
-
expect(user.revision_at(
|
759
|
+
expect(user.revision_at(1.week.ago)).to be_nil
|
754
760
|
end
|
755
761
|
end
|
756
762
|
|
@@ -766,6 +772,19 @@ describe Audited::Auditor do
|
|
766
772
|
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
767
773
|
end
|
768
774
|
|
775
|
+
it "should return audits for STI classes" do
|
776
|
+
# Where parent is STI
|
777
|
+
sti_company = Models::ActiveRecord::Company::STICompany.create!
|
778
|
+
sti_company.update!(name: "Collective Idea")
|
779
|
+
expect(sti_company.own_and_associated_audits).to match_array(sti_company.audits)
|
780
|
+
|
781
|
+
# Where associated is STI
|
782
|
+
owner = Models::ActiveRecord::Owner.create!
|
783
|
+
company = owner.companies.create! type: "Models::ActiveRecord::OwnedCompany::STICompany"
|
784
|
+
company.update!(name: "Collective Idea")
|
785
|
+
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
786
|
+
end
|
787
|
+
|
769
788
|
it "should order audits by creation time" do
|
770
789
|
owner = Models::ActiveRecord::Owner.create!
|
771
790
|
first_audit = owner.audits.first
|
@@ -784,19 +803,23 @@ describe Audited::Auditor do
|
|
784
803
|
describe "without auditing" do
|
785
804
|
it "should not save an audit when calling #save_without_auditing" do
|
786
805
|
expect {
|
787
|
-
u = Models::ActiveRecord::User.new(name:
|
806
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
788
807
|
expect(u.save_without_auditing).to eq(true)
|
789
|
-
}.to_not change(
|
808
|
+
}.to_not change(Audited::Audit, :count)
|
790
809
|
end
|
791
810
|
|
792
811
|
it "should not save an audit inside of the #without_auditing block" do
|
793
812
|
expect {
|
794
|
-
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(
|
795
|
-
}.to_not change(
|
813
|
+
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
814
|
+
}.to_not change(Audited::Audit, :count)
|
796
815
|
end
|
797
816
|
|
798
817
|
it "should reset auditing status even it raises an exception" do
|
799
|
-
|
818
|
+
begin
|
819
|
+
Models::ActiveRecord::User.without_auditing { raise }
|
820
|
+
rescue
|
821
|
+
nil
|
822
|
+
end
|
800
823
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
801
824
|
end
|
802
825
|
|
@@ -807,7 +830,7 @@ describe Audited::Auditor do
|
|
807
830
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
808
831
|
Models::ActiveRecord::User.without_auditing do
|
809
832
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
810
|
-
Models::ActiveRecord::User.create!(
|
833
|
+
Models::ActiveRecord::User.create!(name: "Bart")
|
811
834
|
sleep 1
|
812
835
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
813
836
|
end
|
@@ -817,13 +840,13 @@ describe Audited::Auditor do
|
|
817
840
|
t2 = Thread.new do
|
818
841
|
sleep 0.5
|
819
842
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
820
|
-
Models::ActiveRecord::User.create!(
|
843
|
+
Models::ActiveRecord::User.create!(name: "Lisa")
|
821
844
|
end
|
822
845
|
t1.join
|
823
846
|
t2.join
|
824
847
|
|
825
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
826
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
848
|
+
expect(Models::ActiveRecord::User.find_by_name("Bart").audits.count).to eq(0)
|
849
|
+
expect(Models::ActiveRecord::User.find_by_name("Lisa").audits.count).to eq(1)
|
827
850
|
end
|
828
851
|
|
829
852
|
it "should not save an audit when auditing is globally disabled" do
|
@@ -837,7 +860,7 @@ describe Audited::Auditor do
|
|
837
860
|
Audited.auditing_enabled = true
|
838
861
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
839
862
|
|
840
|
-
user.update!(name:
|
863
|
+
user.update!(name: "Test")
|
841
864
|
expect(user.audits.count).to eq(1)
|
842
865
|
Models::ActiveRecord::User.enable_auditing
|
843
866
|
end
|
@@ -846,24 +869,28 @@ describe Audited::Auditor do
|
|
846
869
|
describe "with auditing" do
|
847
870
|
it "should save an audit when calling #save_with_auditing" do
|
848
871
|
expect {
|
849
|
-
u = Models::ActiveRecord::User.new(name:
|
872
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
850
873
|
Models::ActiveRecord::User.auditing_enabled = false
|
851
874
|
expect(u.save_with_auditing).to eq(true)
|
852
875
|
Models::ActiveRecord::User.auditing_enabled = true
|
853
|
-
}.to change(
|
876
|
+
}.to change(Audited::Audit, :count).by(1)
|
854
877
|
end
|
855
878
|
|
856
879
|
it "should save an audit inside of the #with_auditing block" do
|
857
880
|
expect {
|
858
881
|
Models::ActiveRecord::User.auditing_enabled = false
|
859
|
-
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(
|
882
|
+
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
860
883
|
Models::ActiveRecord::User.auditing_enabled = true
|
861
|
-
}.to change(
|
884
|
+
}.to change(Audited::Audit, :count).by(1)
|
862
885
|
end
|
863
886
|
|
864
887
|
it "should reset auditing status even it raises an exception" do
|
865
888
|
Models::ActiveRecord::User.disable_auditing
|
866
|
-
|
889
|
+
begin
|
890
|
+
Models::ActiveRecord::User.with_auditing { raise }
|
891
|
+
rescue
|
892
|
+
nil
|
893
|
+
end
|
867
894
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
868
895
|
Models::ActiveRecord::User.enable_auditing
|
869
896
|
end
|
@@ -877,7 +904,7 @@ describe Audited::Auditor do
|
|
877
904
|
Models::ActiveRecord::User.with_auditing do
|
878
905
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
879
906
|
|
880
|
-
Models::ActiveRecord::User.create!(
|
907
|
+
Models::ActiveRecord::User.create!(name: "Shaggy")
|
881
908
|
sleep 1
|
882
909
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
883
910
|
end
|
@@ -889,74 +916,81 @@ describe Audited::Auditor do
|
|
889
916
|
sleep 0.5
|
890
917
|
Models::ActiveRecord::User.disable_auditing
|
891
918
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
892
|
-
Models::ActiveRecord::User.create!(
|
919
|
+
Models::ActiveRecord::User.create!(name: "Scooby")
|
893
920
|
Models::ActiveRecord::User.enable_auditing
|
894
921
|
end
|
895
922
|
t1.join
|
896
923
|
t2.join
|
897
924
|
|
898
925
|
Models::ActiveRecord::User.enable_auditing
|
899
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
900
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
926
|
+
expect(Models::ActiveRecord::User.find_by_name("Shaggy").audits.count).to eq(1)
|
927
|
+
expect(Models::ActiveRecord::User.find_by_name("Scooby").audits.count).to eq(0)
|
901
928
|
end
|
902
929
|
end
|
903
930
|
|
904
931
|
describe "comment required" do
|
905
|
-
|
906
932
|
describe "on create" do
|
907
933
|
it "should not validate when audit_comment is not supplied when initialized" do
|
908
|
-
expect(Models::ActiveRecord::CommentRequiredUser.new(name:
|
934
|
+
expect(Models::ActiveRecord::CommentRequiredUser.new(name: "Foo")).not_to be_valid
|
909
935
|
end
|
910
936
|
|
911
937
|
it "should not validate when audit_comment is not supplied trying to create" do
|
912
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
938
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).not_to be_valid
|
913
939
|
end
|
914
940
|
|
915
941
|
it "should validate when audit_comment is supplied" do
|
916
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
942
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo", audit_comment: "Create")).to be_valid
|
917
943
|
end
|
918
944
|
|
919
945
|
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:
|
946
|
+
expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name: "Foo")).to be_valid
|
947
|
+
expect(Models::ActiveRecord::OnDestroyCommentRequiredUser.create(name: "Foo")).to be_valid
|
922
948
|
end
|
923
949
|
|
924
950
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
925
951
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
926
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
952
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
|
927
953
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
928
954
|
end
|
955
|
+
|
956
|
+
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
957
|
+
expect(Models::ActiveRecord::CommentRequiredUser.new(password: "Foo")).to be_valid
|
958
|
+
end
|
929
959
|
end
|
930
960
|
|
931
961
|
describe "on update" do
|
932
|
-
let(
|
933
|
-
let(
|
934
|
-
let(
|
962
|
+
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
963
|
+
let(:on_create_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
964
|
+
let(:on_destroy_user) { Models::ActiveRecord::OnDestroyCommentRequiredUser.create }
|
935
965
|
|
936
966
|
it "should not validate when audit_comment is not supplied" do
|
937
|
-
expect(user.update(name:
|
967
|
+
expect(user.update(name: "Test")).to eq(false)
|
938
968
|
end
|
939
969
|
|
940
970
|
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:
|
971
|
+
expect(on_create_user.update(name: "Test")).to eq(true)
|
972
|
+
expect(on_destroy_user.update(name: "Test")).to eq(true)
|
943
973
|
end
|
944
974
|
|
945
975
|
it "should validate when audit_comment is supplied" do
|
946
|
-
expect(user.update(name:
|
976
|
+
expect(user.update(name: "Test", audit_comment: "Update")).to eq(true)
|
947
977
|
end
|
948
978
|
|
949
979
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
950
980
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
951
|
-
expect(user.update(name:
|
981
|
+
expect(user.update(name: "Test")).to eq(true)
|
952
982
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
953
983
|
end
|
984
|
+
|
985
|
+
it "should validate when audit_comment is not supplied, and only excluded attributes changed" do
|
986
|
+
expect(user.update(password: "Test")).to eq(true)
|
987
|
+
end
|
954
988
|
end
|
955
989
|
|
956
990
|
describe "on destroy" do
|
957
|
-
let(
|
958
|
-
let(
|
959
|
-
let(
|
991
|
+
let(:user) { Models::ActiveRecord::CommentRequiredUser.create!(audit_comment: "Create") }
|
992
|
+
let(:on_create_user) { Models::ActiveRecord::OnCreateCommentRequiredUser.create!(audit_comment: "Create") }
|
993
|
+
let(:on_update_user) { Models::ActiveRecord::OnUpdateCommentRequiredUser.create }
|
960
994
|
|
961
995
|
it "should not validate when audit_comment is not supplied" do
|
962
996
|
expect(user.destroy).to eq(false)
|
@@ -978,41 +1012,38 @@ describe Audited::Auditor do
|
|
978
1012
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
979
1013
|
end
|
980
1014
|
end
|
981
|
-
|
982
1015
|
end
|
983
1016
|
|
984
1017
|
describe "no update with comment only" do
|
985
|
-
let(
|
1018
|
+
let(:user) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
|
986
1019
|
|
987
1020
|
it "does not create an audit when only an audit_comment is present" do
|
988
1021
|
user.audit_comment = "Comment"
|
989
|
-
expect { user.save! }.to_not change(
|
1022
|
+
expect { user.save! }.to_not change(Audited::Audit, :count)
|
990
1023
|
end
|
991
|
-
|
992
1024
|
end
|
993
1025
|
|
994
1026
|
describe "attr_protected and attr_accessible" do
|
995
|
-
|
996
1027
|
it "should not raise error when attr_accessible is set and protected is false" do
|
997
1028
|
expect {
|
998
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1029
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
999
1030
|
}.to_not raise_error
|
1000
1031
|
end
|
1001
1032
|
|
1002
1033
|
it "should not rause an error when attr_accessible is declared before audited" do
|
1003
1034
|
expect {
|
1004
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1035
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
1005
1036
|
}.to_not raise_error
|
1006
1037
|
end
|
1007
1038
|
end
|
1008
1039
|
|
1009
1040
|
describe "audit_as" do
|
1010
|
-
let(
|
1041
|
+
let(:user) { Models::ActiveRecord::User.create name: "Testing" }
|
1011
1042
|
|
1012
1043
|
it "should record user objects" do
|
1013
|
-
Models::ActiveRecord::Company.audit_as(
|
1014
|
-
company = Models::ActiveRecord::Company.create name:
|
1015
|
-
company.update! name:
|
1044
|
+
Models::ActiveRecord::Company.audit_as(user) do
|
1045
|
+
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1046
|
+
company.update! name: "The Auditors"
|
1016
1047
|
|
1017
1048
|
company.audits.each do |audit|
|
1018
1049
|
expect(audit.user).to eq(user)
|
@@ -1021,9 +1052,9 @@ describe Audited::Auditor do
|
|
1021
1052
|
end
|
1022
1053
|
|
1023
1054
|
it "should record usernames" do
|
1024
|
-
Models::ActiveRecord::Company.audit_as(
|
1025
|
-
company = Models::ActiveRecord::Company.create name:
|
1026
|
-
company.update! name:
|
1055
|
+
Models::ActiveRecord::Company.audit_as(user.name) do
|
1056
|
+
company = Models::ActiveRecord::Company.create name: "The auditors"
|
1057
|
+
company.update! name: "The Auditors"
|
1027
1058
|
|
1028
1059
|
company.audits.each do |audit|
|
1029
1060
|
expect(audit.user).to eq(user.name)
|
@@ -1033,7 +1064,7 @@ describe Audited::Auditor do
|
|
1033
1064
|
end
|
1034
1065
|
|
1035
1066
|
describe "after_audit" do
|
1036
|
-
let(
|
1067
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1037
1068
|
|
1038
1069
|
it "should invoke after_audit callback on create" do
|
1039
1070
|
expect(user.bogus_attr).to be_nil
|
@@ -1043,7 +1074,7 @@ describe Audited::Auditor do
|
|
1043
1074
|
end
|
1044
1075
|
|
1045
1076
|
describe "around_audit" do
|
1046
|
-
let(
|
1077
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1047
1078
|
|
1048
1079
|
it "should invoke around_audit callback on create" do
|
1049
1080
|
expect(user.around_attr).to be_nil
|
@@ -1054,13 +1085,13 @@ describe Audited::Auditor do
|
|
1054
1085
|
|
1055
1086
|
describe "STI auditing" do
|
1056
1087
|
it "should correctly disable auditing when using STI" do
|
1057
|
-
company = Models::ActiveRecord::Company::STICompany.create name:
|
1088
|
+
company = Models::ActiveRecord::Company::STICompany.create name: "The auditors"
|
1058
1089
|
expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
|
1059
1090
|
expect {
|
1060
1091
|
Models::ActiveRecord::Company.auditing_enabled = false
|
1061
|
-
company.update! name:
|
1092
|
+
company.update! name: "STI auditors"
|
1062
1093
|
Models::ActiveRecord::Company.auditing_enabled = true
|
1063
|
-
}.to_not change(
|
1094
|
+
}.to_not change(Audited::Audit, :count)
|
1064
1095
|
end
|
1065
1096
|
end
|
1066
1097
|
end
|