audited 4.9.0 → 5.0.2
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/ci.yml +115 -0
- data/.standard.yml +5 -0
- data/Appraisals +22 -16
- data/CHANGELOG.md +68 -1
- data/Gemfile +1 -1
- data/README.md +49 -11
- data/Rakefile +6 -6
- data/gemfiles/rails60.gemfile +1 -1
- data/gemfiles/rails61.gemfile +10 -0
- data/gemfiles/rails70.gemfile +10 -0
- data/lib/audited/audit.rb +36 -28
- data/lib/audited/auditor.rb +69 -43
- 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 +25 -8
- 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 +69 -47
- data/spec/audited/auditor_spec.rb +310 -246
- data/spec/audited/sweeper_spec.rb +19 -18
- data/spec/audited_spec.rb +18 -0
- data/spec/audited_spec_helpers.rb +7 -7
- data/spec/rails_app/app/assets/config/manifest.js +2 -0
- data/spec/rails_app/config/application.rb +3 -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 +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 +31 -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 +26 -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 -30
- data/.rubocop.yml +0 -25
- data/.travis.yml +0 -58
- 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
|
6
51
|
|
52
|
+
class InclusiveCompany2 < ::ActiveRecord::Base
|
53
|
+
self.table_name = "companies"
|
54
|
+
audited unless: proc { false }
|
55
|
+
end
|
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,10 +198,43 @@ 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
|
205
|
+
redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
|
206
|
+
user = Models::ActiveRecord::UserRedactedPassword.create(password: "password")
|
207
|
+
user.save!
|
208
|
+
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
209
|
+
user.password = "new_password"
|
210
|
+
user.save!
|
211
|
+
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should redact columns specified in 'redacted' option when there are multiple specified" do
|
215
|
+
redacted = Audited::Auditor::AuditedInstanceMethods::REDACTED
|
216
|
+
user =
|
217
|
+
Models::ActiveRecord::UserMultipleRedactedAttributes.create(
|
218
|
+
password: "password",
|
219
|
+
ssn: 123456789
|
220
|
+
)
|
221
|
+
user.save!
|
222
|
+
expect(user.audits.last.audited_changes["password"]).to eq(redacted)
|
223
|
+
expect(user.audits.last.audited_changes["ssn"]).to eq(redacted)
|
224
|
+
user.password = "new_password"
|
225
|
+
user.ssn = 987654321
|
226
|
+
user.save!
|
227
|
+
expect(user.audits.last.audited_changes["password"]).to eq([redacted, redacted])
|
228
|
+
expect(user.audits.last.audited_changes["ssn"]).to eq([redacted, redacted])
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should redact columns in 'redacted' column with custom option" do
|
232
|
+
user = Models::ActiveRecord::UserRedactedPasswordCustomRedaction.create(password: "password")
|
233
|
+
user.save!
|
234
|
+
expect(user.audits.last.audited_changes["password"]).to eq(["My", "Custom", "Value", 7])
|
235
|
+
end
|
236
|
+
|
237
|
+
if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
|
216
238
|
describe "'json' and 'jsonb' audited_changes column type" do
|
217
239
|
let(:migrations_path) { SPEC_ROOT.join("support/active_record/postgres") }
|
218
240
|
|
@@ -249,16 +271,16 @@ describe Audited::Auditor do
|
|
249
271
|
it "should allow mass assignment of all unprotected attributes" do
|
250
272
|
yesterday = 1.day.ago
|
251
273
|
|
252
|
-
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name:
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
274
|
+
u = Models::ActiveRecord::NoAttributeProtectionUser.new(name: "name",
|
275
|
+
username: "username",
|
276
|
+
password: "password",
|
277
|
+
activated: true,
|
278
|
+
suspended_at: yesterday,
|
279
|
+
logins: 2)
|
258
280
|
|
259
|
-
expect(u.name).to eq(
|
260
|
-
expect(u.username).to eq(
|
261
|
-
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")
|
262
284
|
expect(u.activated).to eq(true)
|
263
285
|
expect(u.suspended_at.to_i).to eq(yesterday.to_i)
|
264
286
|
expect(u.logins).to eq(2)
|
@@ -266,12 +288,12 @@ describe Audited::Auditor do
|
|
266
288
|
end
|
267
289
|
|
268
290
|
describe "on create" do
|
269
|
-
let(
|
291
|
+
let(:user) { create_user status: :reliable, audit_comment: "Create" }
|
270
292
|
|
271
293
|
it "should change the audit count" do
|
272
294
|
expect {
|
273
295
|
user
|
274
|
-
}.to change(
|
296
|
+
}.to change(Audited::Audit, :count).by(1)
|
275
297
|
end
|
276
298
|
|
277
299
|
it "should create associated audit" do
|
@@ -279,7 +301,7 @@ describe Audited::Auditor do
|
|
279
301
|
end
|
280
302
|
|
281
303
|
it "should set the action to create" do
|
282
|
-
expect(user.audits.first.action).to eq(
|
304
|
+
expect(user.audits.first.action).to eq("create")
|
283
305
|
expect(Audited::Audit.creates.order(:id).last).to eq(user.audits.first)
|
284
306
|
expect(user.audits.creates.count).to eq(1)
|
285
307
|
expect(user.audits.updates.count).to eq(0)
|
@@ -294,46 +316,55 @@ describe Audited::Auditor do
|
|
294
316
|
expect(user.audits.first.audited_changes["status"]).to eq(1)
|
295
317
|
end
|
296
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
|
+
|
297
328
|
it "should store comment" do
|
298
|
-
expect(user.audits.first.comment).to eq(
|
329
|
+
expect(user.audits.first.comment).to eq("Create")
|
299
330
|
end
|
300
331
|
|
301
332
|
it "should not audit an attribute which is excepted if specified on create or destroy" do
|
302
|
-
on_create_destroy_except_name = Models::ActiveRecord::OnCreateDestroyExceptName.create(name:
|
303
|
-
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)
|
304
335
|
end
|
305
336
|
|
306
337
|
it "should not save an audit if only specified on update/destroy" do
|
307
338
|
expect {
|
308
|
-
Models::ActiveRecord::OnUpdateDestroy.create!(
|
309
|
-
}.to_not change(
|
339
|
+
Models::ActiveRecord::OnUpdateDestroy.create!(name: "Bart")
|
340
|
+
}.to_not change(Audited::Audit, :count)
|
310
341
|
end
|
311
342
|
end
|
312
343
|
|
313
344
|
describe "on update" do
|
314
345
|
before do
|
315
|
-
@user = create_user(
|
346
|
+
@user = create_user(name: "Brandon", status: :active, audit_comment: "Update")
|
316
347
|
end
|
317
348
|
|
318
349
|
it "should save an audit" do
|
319
350
|
expect {
|
320
351
|
@user.update_attribute(:name, "Someone")
|
321
|
-
}.to change(
|
352
|
+
}.to change(Audited::Audit, :count).by(1)
|
322
353
|
expect {
|
323
354
|
@user.update_attribute(:name, "Someone else")
|
324
|
-
}.to change(
|
355
|
+
}.to change(Audited::Audit, :count).by(1)
|
325
356
|
end
|
326
357
|
|
327
358
|
it "should set the action to 'update'" do
|
328
|
-
@user.update! name:
|
329
|
-
expect(@user.audits.last.action).to eq(
|
359
|
+
@user.update! name: "Changed"
|
360
|
+
expect(@user.audits.last.action).to eq("update")
|
330
361
|
expect(Audited::Audit.updates.order(:id).last).to eq(@user.audits.last)
|
331
362
|
expect(@user.audits.updates.last).to eq(@user.audits.last)
|
332
363
|
end
|
333
364
|
|
334
365
|
it "should store the changed attributes" do
|
335
|
-
@user.update! name:
|
336
|
-
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"]})
|
337
368
|
end
|
338
369
|
|
339
370
|
it "should store changed enum values" do
|
@@ -342,35 +373,35 @@ describe Audited::Auditor do
|
|
342
373
|
end
|
343
374
|
|
344
375
|
it "should store audit comment" do
|
345
|
-
expect(@user.audits.last.comment).to eq(
|
376
|
+
expect(@user.audits.last.comment).to eq("Update")
|
346
377
|
end
|
347
378
|
|
348
379
|
it "should not save an audit if only specified on create/destroy" do
|
349
|
-
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(
|
380
|
+
on_create_destroy = Models::ActiveRecord::OnCreateDestroy.create(name: "Bart")
|
350
381
|
expect {
|
351
|
-
on_create_destroy.update! name:
|
352
|
-
}.to_not change(
|
382
|
+
on_create_destroy.update! name: "Changed"
|
383
|
+
}.to_not change(Audited::Audit, :count)
|
353
384
|
end
|
354
385
|
|
355
386
|
it "should not save an audit if the value doesn't change after type casting" do
|
356
387
|
@user.update! logins: 0, activated: true
|
357
|
-
expect { @user.update_attribute :logins,
|
358
|
-
expect { @user.update_attribute :activated, 1 }.to_not change(
|
359
|
-
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)
|
360
391
|
end
|
361
392
|
|
362
393
|
describe "with no dirty changes" do
|
363
394
|
it "does not create an audit if the record is not changed" do
|
364
395
|
expect {
|
365
396
|
@user.save!
|
366
|
-
}.to_not change(
|
397
|
+
}.to_not change(Audited::Audit, :count)
|
367
398
|
end
|
368
399
|
|
369
400
|
it "creates an audit when an audit comment is present" do
|
370
401
|
expect {
|
371
402
|
@user.audit_comment = "Comment"
|
372
403
|
@user.save!
|
373
|
-
}.to change(
|
404
|
+
}.to change(Audited::Audit, :count)
|
374
405
|
end
|
375
406
|
end
|
376
407
|
end
|
@@ -383,7 +414,7 @@ describe Audited::Auditor do
|
|
383
414
|
it "should save an audit" do
|
384
415
|
expect {
|
385
416
|
@user.destroy
|
386
|
-
}.to change(
|
417
|
+
}.to change(Audited::Audit, :count)
|
387
418
|
|
388
419
|
expect(@user.audits.size).to eq(2)
|
389
420
|
end
|
@@ -391,7 +422,7 @@ describe Audited::Auditor do
|
|
391
422
|
it "should set the action to 'destroy'" do
|
392
423
|
@user.destroy
|
393
424
|
|
394
|
-
expect(@user.audits.last.action).to eq(
|
425
|
+
expect(@user.audits.last.action).to eq("destroy")
|
395
426
|
expect(Audited::Audit.destroys.order(:id).last).to eq(@user.audits.last)
|
396
427
|
expect(@user.audits.destroys.last).to eq(@user.audits.last)
|
397
428
|
end
|
@@ -416,11 +447,11 @@ describe Audited::Auditor do
|
|
416
447
|
end
|
417
448
|
|
418
449
|
it "should not save an audit if only specified on create/update" do
|
419
|
-
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(
|
450
|
+
on_create_update = Models::ActiveRecord::OnCreateUpdate.create!(name: "Bart")
|
420
451
|
|
421
452
|
expect {
|
422
453
|
on_create_update.destroy
|
423
|
-
}.to_not change(
|
454
|
+
}.to_not change(Audited::Audit, :count)
|
424
455
|
end
|
425
456
|
|
426
457
|
it "should audit dependent destructions" do
|
@@ -429,9 +460,9 @@ describe Audited::Auditor do
|
|
429
460
|
|
430
461
|
expect {
|
431
462
|
owner.destroy
|
432
|
-
}.to change(
|
463
|
+
}.to change(Audited::Audit, :count)
|
433
464
|
|
434
|
-
expect(company.audits.map { |a| a.action }).to eq([
|
465
|
+
expect(company.audits.map { |a| a.action }).to eq(["create", "destroy"])
|
435
466
|
end
|
436
467
|
end
|
437
468
|
|
@@ -443,20 +474,20 @@ describe Audited::Auditor do
|
|
443
474
|
user.destroy
|
444
475
|
}.to_not raise_error
|
445
476
|
|
446
|
-
expect(
|
477
|
+
expect(user.audits).to be_empty
|
447
478
|
end
|
448
479
|
end
|
449
480
|
|
450
481
|
describe "associated with" do
|
451
|
-
let(:owner) { Models::ActiveRecord::Owner.create(name:
|
452
|
-
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) }
|
453
484
|
|
454
485
|
it "should record the associated object on create" do
|
455
486
|
expect(owned_company.audits.first.associated).to eq(owner)
|
456
487
|
end
|
457
488
|
|
458
489
|
it "should store the associated object on update" do
|
459
|
-
owned_company.update_attribute(:name,
|
490
|
+
owned_company.update_attribute(:name, "The Auditors")
|
460
491
|
expect(owned_company.audits.last.associated).to eq(owner)
|
461
492
|
end
|
462
493
|
|
@@ -467,8 +498,8 @@ describe Audited::Auditor do
|
|
467
498
|
end
|
468
499
|
|
469
500
|
describe "has associated audits" do
|
470
|
-
let!(:owner) { Models::ActiveRecord::Owner.create!(name:
|
471
|
-
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) }
|
472
503
|
|
473
504
|
it "should list the associated audits" do
|
474
505
|
expect(owner.associated_audits.length).to eq(1)
|
@@ -492,7 +523,7 @@ describe Audited::Auditor do
|
|
492
523
|
it "should delete old audits when keeped amount exceeded" do
|
493
524
|
stub_global_max_audits(2) do
|
494
525
|
user = create_versions(2)
|
495
|
-
user.update(name:
|
526
|
+
user.update(name: "John")
|
496
527
|
expect(user.audits.pluck(:version)).to eq([2, 3])
|
497
528
|
end
|
498
529
|
end
|
@@ -500,35 +531,35 @@ describe Audited::Auditor do
|
|
500
531
|
it "should not delete old audits when keeped amount not exceeded" do
|
501
532
|
stub_global_max_audits(3) do
|
502
533
|
user = create_versions(2)
|
503
|
-
user.update(name:
|
534
|
+
user.update(name: "John")
|
504
535
|
expect(user.audits.pluck(:version)).to eq([1, 2, 3])
|
505
536
|
end
|
506
537
|
end
|
507
538
|
|
508
539
|
it "should delete old extra audits after introducing limit" do
|
509
540
|
stub_global_max_audits(nil) do
|
510
|
-
user = Models::ActiveRecord::User.create!(name:
|
511
|
-
user.update!(name:
|
512
|
-
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")
|
513
544
|
user.update!(activated: true)
|
514
545
|
|
515
546
|
Audited.max_audits = 3
|
516
547
|
Models::ActiveRecord::User.send(:normalize_audited_options)
|
517
|
-
user.update!(favourite_device:
|
548
|
+
user.update!(favourite_device: "Android Phone")
|
518
549
|
audits = user.audits
|
519
550
|
|
520
551
|
expect(audits.count).to eq(3)
|
521
|
-
expect(audits[0].audited_changes).to include({
|
522
|
-
expect(audits[1].audited_changes).to eq({
|
523
|
-
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"]})
|
524
555
|
end
|
525
556
|
end
|
526
557
|
|
527
558
|
it "should add comment line for combined audit" do
|
528
559
|
stub_global_max_audits(2) do
|
529
|
-
user = Models::ActiveRecord::User.create!(name:
|
530
|
-
user.update(name:
|
531
|
-
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")
|
532
563
|
expect(user.audits.first.comment).to match(/First audit comment.+is the result of multiple/m)
|
533
564
|
end
|
534
565
|
end
|
@@ -548,10 +579,10 @@ describe Audited::Auditor do
|
|
548
579
|
end
|
549
580
|
|
550
581
|
describe "revisions" do
|
551
|
-
let(
|
582
|
+
let(:user) { create_versions }
|
552
583
|
|
553
584
|
it "should return an Array of Users" do
|
554
|
-
expect(user.revisions).to be_a_kind_of(
|
585
|
+
expect(user.revisions).to be_a_kind_of(Array)
|
555
586
|
user.revisions.each { |version| expect(version).to be_a_kind_of Models::ActiveRecord::User }
|
556
587
|
end
|
557
588
|
|
@@ -560,38 +591,38 @@ describe Audited::Auditor do
|
|
560
591
|
end
|
561
592
|
|
562
593
|
it "should have one revision for each audit" do
|
563
|
-
expect(user.audits.size).to eql(
|
594
|
+
expect(user.audits.size).to eql(user.revisions.size)
|
564
595
|
end
|
565
596
|
|
566
597
|
it "should set the attributes for each revision" do
|
567
|
-
u = Models::ActiveRecord::User.create(name:
|
568
|
-
u.update! name:
|
569
|
-
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"
|
570
601
|
|
571
602
|
expect(u.revisions.size).to eql(3)
|
572
603
|
|
573
|
-
expect(u.revisions[0].name).to eql(
|
574
|
-
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")
|
575
606
|
|
576
|
-
expect(u.revisions[1].name).to eql(
|
577
|
-
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")
|
578
609
|
|
579
|
-
expect(u.revisions[2].name).to eql(
|
580
|
-
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")
|
581
612
|
end
|
582
613
|
|
583
614
|
it "access to only recent revisions" do
|
584
|
-
u = Models::ActiveRecord::User.create(name:
|
585
|
-
u.update! name:
|
586
|
-
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"
|
587
618
|
|
588
619
|
expect(u.revisions(2).size).to eq(2)
|
589
620
|
|
590
|
-
expect(u.revisions(2)[0].name).to eq(
|
591
|
-
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")
|
592
623
|
|
593
|
-
expect(u.revisions(2)[1].name).to eq(
|
594
|
-
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")
|
595
626
|
end
|
596
627
|
|
597
628
|
it "should be empty if no audits exist" do
|
@@ -600,13 +631,13 @@ describe Audited::Auditor do
|
|
600
631
|
end
|
601
632
|
|
602
633
|
it "should ignore attributes that have been deleted" do
|
603
|
-
user.audits.last.update! audited_changes: {old_attribute:
|
634
|
+
user.audits.last.update! audited_changes: {old_attribute: "old value"}
|
604
635
|
expect { user.revisions }.to_not raise_error
|
605
636
|
end
|
606
637
|
end
|
607
638
|
|
608
639
|
describe "revisions" do
|
609
|
-
let(
|
640
|
+
let(:user) { create_versions(5) }
|
610
641
|
|
611
642
|
it "should maintain identity" do
|
612
643
|
expect(user.revision(1)).to eq(user)
|
@@ -614,15 +645,15 @@ describe Audited::Auditor do
|
|
614
645
|
|
615
646
|
it "should find the given revision" do
|
616
647
|
revision = user.revision(3)
|
617
|
-
expect(revision).to be_a_kind_of(
|
648
|
+
expect(revision).to be_a_kind_of(Models::ActiveRecord::User)
|
618
649
|
expect(revision.audit_version).to eq(3)
|
619
|
-
expect(revision.name).to eq(
|
650
|
+
expect(revision.name).to eq("Foobar 3")
|
620
651
|
end
|
621
652
|
|
622
653
|
it "should find the previous revision with :previous" do
|
623
654
|
revision = user.revision(:previous)
|
624
655
|
expect(revision.audit_version).to eq(4)
|
625
|
-
#expect(revision).to eq(user.revision(4))
|
656
|
+
# expect(revision).to eq(user.revision(4))
|
626
657
|
expect(revision.attributes).to eq(user.revision(4).attributes)
|
627
658
|
end
|
628
659
|
|
@@ -633,7 +664,7 @@ describe Audited::Auditor do
|
|
633
664
|
end
|
634
665
|
|
635
666
|
it "should be able to set protected attributes" do
|
636
|
-
u = Models::ActiveRecord::User.create(name:
|
667
|
+
u = Models::ActiveRecord::User.create(name: "Brandon")
|
637
668
|
u.update_attribute :logins, 1
|
638
669
|
u.update_attribute :logins, 2
|
639
670
|
|
@@ -643,23 +674,23 @@ describe Audited::Auditor do
|
|
643
674
|
end
|
644
675
|
|
645
676
|
it "should set attributes directly" do
|
646
|
-
u = Models::ActiveRecord::User.create(name:
|
647
|
-
expect(u.revision(1).name).to eq(
|
677
|
+
u = Models::ActiveRecord::User.create(name: "<Joe>")
|
678
|
+
expect(u.revision(1).name).to eq("<Joe>")
|
648
679
|
end
|
649
680
|
|
650
681
|
it "should set the attributes for each revision" do
|
651
|
-
u = Models::ActiveRecord::User.create(name:
|
652
|
-
u.update! name:
|
653
|
-
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"
|
654
685
|
|
655
|
-
expect(u.revision(3).name).to eq(
|
656
|
-
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")
|
657
688
|
|
658
|
-
expect(u.revision(2).name).to eq(
|
659
|
-
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")
|
660
691
|
|
661
|
-
expect(u.revision(1).name).to eq(
|
662
|
-
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")
|
663
694
|
end
|
664
695
|
|
665
696
|
it "should correctly restore revision with enum" do
|
@@ -690,34 +721,42 @@ describe Audited::Auditor do
|
|
690
721
|
it "should record new audit when saving revision" do
|
691
722
|
expect {
|
692
723
|
user.revision(1).save!
|
693
|
-
}.to change(
|
724
|
+
}.to change(user.audits, :count).by(1)
|
694
725
|
end
|
695
726
|
|
696
727
|
it "should re-insert destroyed records" do
|
697
728
|
user.destroy
|
698
729
|
expect {
|
699
730
|
user.revision(1).save!
|
700
|
-
}.to change(
|
731
|
+
}.to change(Models::ActiveRecord::User, :count).by(1)
|
701
732
|
end
|
702
733
|
|
703
734
|
it "should return nil for values greater than the number of revisions" do
|
704
735
|
expect(user.revision(user.revisions.count + 1)).to be_nil
|
705
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
|
706
745
|
end
|
707
746
|
|
708
747
|
describe "revision_at" do
|
709
|
-
let(
|
748
|
+
let(:user) { create_user }
|
710
749
|
|
711
750
|
it "should find the latest revision before the given time" do
|
712
751
|
audit = user.audits.first
|
713
752
|
audit.created_at = 1.hour.ago
|
714
753
|
audit.save!
|
715
|
-
user.update! name:
|
716
|
-
expect(user.revision_at(
|
754
|
+
user.update! name: "updated"
|
755
|
+
expect(user.revision_at(2.minutes.ago).audit_version).to eq(1)
|
717
756
|
end
|
718
757
|
|
719
758
|
it "should be nil if given a time before audits" do
|
720
|
-
expect(user.revision_at(
|
759
|
+
expect(user.revision_at(1.week.ago)).to be_nil
|
721
760
|
end
|
722
761
|
end
|
723
762
|
|
@@ -733,6 +772,19 @@ describe Audited::Auditor do
|
|
733
772
|
expect(owner.own_and_associated_audits).to match_array(owner.audits + company.audits)
|
734
773
|
end
|
735
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
|
+
|
736
788
|
it "should order audits by creation time" do
|
737
789
|
owner = Models::ActiveRecord::Owner.create!
|
738
790
|
first_audit = owner.audits.first
|
@@ -751,19 +803,23 @@ describe Audited::Auditor do
|
|
751
803
|
describe "without auditing" do
|
752
804
|
it "should not save an audit when calling #save_without_auditing" do
|
753
805
|
expect {
|
754
|
-
u = Models::ActiveRecord::User.new(name:
|
806
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
755
807
|
expect(u.save_without_auditing).to eq(true)
|
756
|
-
}.to_not change(
|
808
|
+
}.to_not change(Audited::Audit, :count)
|
757
809
|
end
|
758
810
|
|
759
811
|
it "should not save an audit inside of the #without_auditing block" do
|
760
812
|
expect {
|
761
|
-
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(
|
762
|
-
}.to_not change(
|
813
|
+
Models::ActiveRecord::User.without_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
814
|
+
}.to_not change(Audited::Audit, :count)
|
763
815
|
end
|
764
816
|
|
765
817
|
it "should reset auditing status even it raises an exception" do
|
766
|
-
|
818
|
+
begin
|
819
|
+
Models::ActiveRecord::User.without_auditing { raise }
|
820
|
+
rescue
|
821
|
+
nil
|
822
|
+
end
|
767
823
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
768
824
|
end
|
769
825
|
|
@@ -774,7 +830,7 @@ describe Audited::Auditor do
|
|
774
830
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
775
831
|
Models::ActiveRecord::User.without_auditing do
|
776
832
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
777
|
-
Models::ActiveRecord::User.create!(
|
833
|
+
Models::ActiveRecord::User.create!(name: "Bart")
|
778
834
|
sleep 1
|
779
835
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
780
836
|
end
|
@@ -784,13 +840,13 @@ describe Audited::Auditor do
|
|
784
840
|
t2 = Thread.new do
|
785
841
|
sleep 0.5
|
786
842
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
787
|
-
Models::ActiveRecord::User.create!(
|
843
|
+
Models::ActiveRecord::User.create!(name: "Lisa")
|
788
844
|
end
|
789
845
|
t1.join
|
790
846
|
t2.join
|
791
847
|
|
792
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
793
|
-
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)
|
794
850
|
end
|
795
851
|
|
796
852
|
it "should not save an audit when auditing is globally disabled" do
|
@@ -804,7 +860,7 @@ describe Audited::Auditor do
|
|
804
860
|
Audited.auditing_enabled = true
|
805
861
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
806
862
|
|
807
|
-
user.update!(name:
|
863
|
+
user.update!(name: "Test")
|
808
864
|
expect(user.audits.count).to eq(1)
|
809
865
|
Models::ActiveRecord::User.enable_auditing
|
810
866
|
end
|
@@ -813,24 +869,28 @@ describe Audited::Auditor do
|
|
813
869
|
describe "with auditing" do
|
814
870
|
it "should save an audit when calling #save_with_auditing" do
|
815
871
|
expect {
|
816
|
-
u = Models::ActiveRecord::User.new(name:
|
872
|
+
u = Models::ActiveRecord::User.new(name: "Brandon")
|
817
873
|
Models::ActiveRecord::User.auditing_enabled = false
|
818
874
|
expect(u.save_with_auditing).to eq(true)
|
819
875
|
Models::ActiveRecord::User.auditing_enabled = true
|
820
|
-
}.to change(
|
876
|
+
}.to change(Audited::Audit, :count).by(1)
|
821
877
|
end
|
822
878
|
|
823
879
|
it "should save an audit inside of the #with_auditing block" do
|
824
880
|
expect {
|
825
881
|
Models::ActiveRecord::User.auditing_enabled = false
|
826
|
-
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(
|
882
|
+
Models::ActiveRecord::User.with_auditing { Models::ActiveRecord::User.create!(name: "Brandon") }
|
827
883
|
Models::ActiveRecord::User.auditing_enabled = true
|
828
|
-
}.to change(
|
884
|
+
}.to change(Audited::Audit, :count).by(1)
|
829
885
|
end
|
830
886
|
|
831
887
|
it "should reset auditing status even it raises an exception" do
|
832
888
|
Models::ActiveRecord::User.disable_auditing
|
833
|
-
|
889
|
+
begin
|
890
|
+
Models::ActiveRecord::User.with_auditing { raise }
|
891
|
+
rescue
|
892
|
+
nil
|
893
|
+
end
|
834
894
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
835
895
|
Models::ActiveRecord::User.enable_auditing
|
836
896
|
end
|
@@ -844,7 +904,7 @@ describe Audited::Auditor do
|
|
844
904
|
Models::ActiveRecord::User.with_auditing do
|
845
905
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
846
906
|
|
847
|
-
Models::ActiveRecord::User.create!(
|
907
|
+
Models::ActiveRecord::User.create!(name: "Shaggy")
|
848
908
|
sleep 1
|
849
909
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(true)
|
850
910
|
end
|
@@ -856,74 +916,81 @@ describe Audited::Auditor do
|
|
856
916
|
sleep 0.5
|
857
917
|
Models::ActiveRecord::User.disable_auditing
|
858
918
|
expect(Models::ActiveRecord::User.auditing_enabled).to eq(false)
|
859
|
-
Models::ActiveRecord::User.create!(
|
919
|
+
Models::ActiveRecord::User.create!(name: "Scooby")
|
860
920
|
Models::ActiveRecord::User.enable_auditing
|
861
921
|
end
|
862
922
|
t1.join
|
863
923
|
t2.join
|
864
924
|
|
865
925
|
Models::ActiveRecord::User.enable_auditing
|
866
|
-
expect(Models::ActiveRecord::User.find_by_name(
|
867
|
-
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)
|
868
928
|
end
|
869
929
|
end
|
870
930
|
|
871
931
|
describe "comment required" do
|
872
|
-
|
873
932
|
describe "on create" do
|
874
933
|
it "should not validate when audit_comment is not supplied when initialized" do
|
875
|
-
expect(Models::ActiveRecord::CommentRequiredUser.new(name:
|
934
|
+
expect(Models::ActiveRecord::CommentRequiredUser.new(name: "Foo")).not_to be_valid
|
876
935
|
end
|
877
936
|
|
878
937
|
it "should not validate when audit_comment is not supplied trying to create" do
|
879
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
938
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).not_to be_valid
|
880
939
|
end
|
881
940
|
|
882
941
|
it "should validate when audit_comment is supplied" do
|
883
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
942
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo", audit_comment: "Create")).to be_valid
|
884
943
|
end
|
885
944
|
|
886
945
|
it "should validate when audit_comment is not supplied, and creating is not being audited" do
|
887
|
-
expect(Models::ActiveRecord::OnUpdateCommentRequiredUser.create(name:
|
888
|
-
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
|
889
948
|
end
|
890
949
|
|
891
950
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
892
951
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
893
|
-
expect(Models::ActiveRecord::CommentRequiredUser.create(name:
|
952
|
+
expect(Models::ActiveRecord::CommentRequiredUser.create(name: "Foo")).to be_valid
|
894
953
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
895
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
|
896
959
|
end
|
897
960
|
|
898
961
|
describe "on update" do
|
899
|
-
let(
|
900
|
-
let(
|
901
|
-
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 }
|
902
965
|
|
903
966
|
it "should not validate when audit_comment is not supplied" do
|
904
|
-
expect(user.update(name:
|
967
|
+
expect(user.update(name: "Test")).to eq(false)
|
905
968
|
end
|
906
969
|
|
907
970
|
it "should validate when audit_comment is not supplied, and updating is not being audited" do
|
908
|
-
expect(on_create_user.update(name:
|
909
|
-
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)
|
910
973
|
end
|
911
974
|
|
912
975
|
it "should validate when audit_comment is supplied" do
|
913
|
-
expect(user.update(name:
|
976
|
+
expect(user.update(name: "Test", audit_comment: "Update")).to eq(true)
|
914
977
|
end
|
915
978
|
|
916
979
|
it "should validate when audit_comment is not supplied, and auditing is disabled" do
|
917
980
|
Models::ActiveRecord::CommentRequiredUser.disable_auditing
|
918
|
-
expect(user.update(name:
|
981
|
+
expect(user.update(name: "Test")).to eq(true)
|
919
982
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
920
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
|
921
988
|
end
|
922
989
|
|
923
990
|
describe "on destroy" do
|
924
|
-
let(
|
925
|
-
let(
|
926
|
-
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 }
|
927
994
|
|
928
995
|
it "should not validate when audit_comment is not supplied" do
|
929
996
|
expect(user.destroy).to eq(false)
|
@@ -945,41 +1012,38 @@ describe Audited::Auditor do
|
|
945
1012
|
Models::ActiveRecord::CommentRequiredUser.enable_auditing
|
946
1013
|
end
|
947
1014
|
end
|
948
|
-
|
949
1015
|
end
|
950
1016
|
|
951
1017
|
describe "no update with comment only" do
|
952
|
-
let(
|
1018
|
+
let(:user) { Models::ActiveRecord::NoUpdateWithCommentOnlyUser.create }
|
953
1019
|
|
954
1020
|
it "does not create an audit when only an audit_comment is present" do
|
955
1021
|
user.audit_comment = "Comment"
|
956
|
-
expect { user.save! }.to_not change(
|
1022
|
+
expect { user.save! }.to_not change(Audited::Audit, :count)
|
957
1023
|
end
|
958
|
-
|
959
1024
|
end
|
960
1025
|
|
961
1026
|
describe "attr_protected and attr_accessible" do
|
962
|
-
|
963
1027
|
it "should not raise error when attr_accessible is set and protected is false" do
|
964
1028
|
expect {
|
965
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1029
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
966
1030
|
}.to_not raise_error
|
967
1031
|
end
|
968
1032
|
|
969
1033
|
it "should not rause an error when attr_accessible is declared before audited" do
|
970
1034
|
expect {
|
971
|
-
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name:
|
1035
|
+
Models::ActiveRecord::AccessibleAfterDeclarationUser.new(name: "No fail!")
|
972
1036
|
}.to_not raise_error
|
973
1037
|
end
|
974
1038
|
end
|
975
1039
|
|
976
1040
|
describe "audit_as" do
|
977
|
-
let(
|
1041
|
+
let(:user) { Models::ActiveRecord::User.create name: "Testing" }
|
978
1042
|
|
979
1043
|
it "should record user objects" do
|
980
|
-
Models::ActiveRecord::Company.audit_as(
|
981
|
-
company = Models::ActiveRecord::Company.create name:
|
982
|
-
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"
|
983
1047
|
|
984
1048
|
company.audits.each do |audit|
|
985
1049
|
expect(audit.user).to eq(user)
|
@@ -988,9 +1052,9 @@ describe Audited::Auditor do
|
|
988
1052
|
end
|
989
1053
|
|
990
1054
|
it "should record usernames" do
|
991
|
-
Models::ActiveRecord::Company.audit_as(
|
992
|
-
company = Models::ActiveRecord::Company.create name:
|
993
|
-
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"
|
994
1058
|
|
995
1059
|
company.audits.each do |audit|
|
996
1060
|
expect(audit.user).to eq(user.name)
|
@@ -1000,7 +1064,7 @@ describe Audited::Auditor do
|
|
1000
1064
|
end
|
1001
1065
|
|
1002
1066
|
describe "after_audit" do
|
1003
|
-
let(
|
1067
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1004
1068
|
|
1005
1069
|
it "should invoke after_audit callback on create" do
|
1006
1070
|
expect(user.bogus_attr).to be_nil
|
@@ -1010,7 +1074,7 @@ describe Audited::Auditor do
|
|
1010
1074
|
end
|
1011
1075
|
|
1012
1076
|
describe "around_audit" do
|
1013
|
-
let(
|
1077
|
+
let(:user) { Models::ActiveRecord::UserWithAfterAudit.new }
|
1014
1078
|
|
1015
1079
|
it "should invoke around_audit callback on create" do
|
1016
1080
|
expect(user.around_attr).to be_nil
|
@@ -1021,13 +1085,13 @@ describe Audited::Auditor do
|
|
1021
1085
|
|
1022
1086
|
describe "STI auditing" do
|
1023
1087
|
it "should correctly disable auditing when using STI" do
|
1024
|
-
company = Models::ActiveRecord::Company::STICompany.create name:
|
1088
|
+
company = Models::ActiveRecord::Company::STICompany.create name: "The auditors"
|
1025
1089
|
expect(company.type).to eq("Models::ActiveRecord::Company::STICompany")
|
1026
1090
|
expect {
|
1027
1091
|
Models::ActiveRecord::Company.auditing_enabled = false
|
1028
|
-
company.update! name:
|
1092
|
+
company.update! name: "STI auditors"
|
1029
1093
|
Models::ActiveRecord::Company.auditing_enabled = true
|
1030
|
-
}.to_not change(
|
1094
|
+
}.to_not change(Audited::Audit, :count)
|
1031
1095
|
end
|
1032
1096
|
end
|
1033
1097
|
end
|