audited 5.3.3 → 5.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Appraisals +33 -20
- data/CHANGELOG.md +52 -1
- data/README.md +11 -3
- data/Rakefile +1 -3
- data/audited.gemspec +38 -0
- data/lib/audited/audit.rb +5 -1
- data/lib/audited/auditor.rb +49 -14
- data/lib/audited/version.rb +1 -1
- data/lib/audited.rb +11 -4
- data/lib/generators/audited/migration.rb +1 -1
- metadata +26 -59
- data/.github/workflows/buildlight.yml +0 -15
- data/.github/workflows/ci.yml +0 -134
- data/.gitignore +0 -17
- data/.standard.yml +0 -5
- data/.yardopts +0 -3
- data/gemfiles/rails50.gemfile +0 -11
- data/gemfiles/rails51.gemfile +0 -11
- data/gemfiles/rails52.gemfile +0 -11
- data/gemfiles/rails60.gemfile +0 -10
- data/gemfiles/rails61.gemfile +0 -10
- data/gemfiles/rails70.gemfile +0 -10
- data/spec/audited/audit_spec.rb +0 -357
- data/spec/audited/auditor_spec.rb +0 -1227
- data/spec/audited/rspec_matchers_spec.rb +0 -69
- data/spec/audited/sweeper_spec.rb +0 -133
- data/spec/audited_spec.rb +0 -14
- data/spec/audited_spec_helpers.rb +0 -32
- data/spec/rails_app/app/assets/config/manifest.js +0 -2
- data/spec/rails_app/config/application.rb +0 -19
- data/spec/rails_app/config/database.yml +0 -26
- data/spec/rails_app/config/environment.rb +0 -5
- data/spec/rails_app/config/environments/test.rb +0 -52
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/rails_app/config/initializers/inflections.rb +0 -2
- data/spec/rails_app/config/initializers/secret_token.rb +0 -3
- data/spec/rails_app/config/routes.rb +0 -3
- data/spec/spec_helper.rb +0 -24
- data/spec/support/active_record/models.rb +0 -170
- data/spec/support/active_record/postgres/1_change_audited_changes_type_to_json.rb +0 -11
- data/spec/support/active_record/postgres/2_change_audited_changes_type_to_jsonb.rb +0 -11
- data/spec/support/active_record/schema.rb +0 -90
- data/test/db/version_1.rb +0 -17
- data/test/db/version_2.rb +0 -18
- data/test/db/version_3.rb +0 -18
- data/test/db/version_4.rb +0 -19
- data/test/db/version_5.rb +0 -17
- data/test/db/version_6.rb +0 -19
- data/test/install_generator_test.rb +0 -62
- data/test/test_helper.rb +0 -18
- data/test/upgrade_generator_test.rb +0 -97
data/.github/workflows/ci.yml
DELETED
@@ -1,134 +0,0 @@
|
|
1
|
-
name: CI
|
2
|
-
|
3
|
-
on:
|
4
|
-
- pull_request
|
5
|
-
- push
|
6
|
-
|
7
|
-
jobs:
|
8
|
-
build:
|
9
|
-
runs-on: ubuntu-latest
|
10
|
-
strategy:
|
11
|
-
fail-fast: false
|
12
|
-
matrix:
|
13
|
-
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2]
|
14
|
-
appraisal:
|
15
|
-
- rails50
|
16
|
-
- rails51
|
17
|
-
- rails52
|
18
|
-
- rails60
|
19
|
-
- rails61
|
20
|
-
- rails70
|
21
|
-
db: [POSTGRES, MYSQL, SQLITE]
|
22
|
-
exclude:
|
23
|
-
# MySQL has issues on Ruby 2.3
|
24
|
-
# https://github.com/ruby/setup-ruby/issues/150
|
25
|
-
- ruby: 2.3
|
26
|
-
db: MYSQL
|
27
|
-
|
28
|
-
# PostgreSQL is segfaulting on 2.3
|
29
|
-
# Doesn't seem worth solving.
|
30
|
-
- ruby: 2.3
|
31
|
-
db: POSTGRES
|
32
|
-
|
33
|
-
# Rails 5.0 supports Ruby 2.2-2.4
|
34
|
-
- appraisal: rails50
|
35
|
-
ruby: 2.5
|
36
|
-
- appraisal: rails50
|
37
|
-
ruby: 2.6
|
38
|
-
- appraisal: rails50
|
39
|
-
ruby: 2.7
|
40
|
-
- appraisal: rails50
|
41
|
-
ruby: 3.0
|
42
|
-
- appraisal: rails50
|
43
|
-
ruby: 3.1
|
44
|
-
- appraisal: rails50
|
45
|
-
ruby: 3.2
|
46
|
-
|
47
|
-
# Rails 5.1 supports Ruby 2.2-2.5
|
48
|
-
- appraisal: rails51
|
49
|
-
ruby: 2.6
|
50
|
-
- appraisal: rails51
|
51
|
-
ruby: 2.7
|
52
|
-
- appraisal: rails51
|
53
|
-
ruby: 3.0
|
54
|
-
- appraisal: rails51
|
55
|
-
ruby: 3.1
|
56
|
-
- appraisal: rails51
|
57
|
-
ruby: 3.2
|
58
|
-
|
59
|
-
# Rails 5.2 supports Ruby 2.2-2.5
|
60
|
-
- appraisal: rails52
|
61
|
-
ruby: 2.6
|
62
|
-
- appraisal: rails52
|
63
|
-
ruby: 2.7
|
64
|
-
- appraisal: rails52
|
65
|
-
ruby: 3.0
|
66
|
-
- appraisal: rails52
|
67
|
-
ruby: 3.1
|
68
|
-
- appraisal: rails52
|
69
|
-
ruby: 3.2
|
70
|
-
|
71
|
-
# Rails 6.0 supports Ruby 2.5-2.7
|
72
|
-
- appraisal: rails60
|
73
|
-
ruby: 2.3
|
74
|
-
- appraisal: rails60
|
75
|
-
ruby: 2.4
|
76
|
-
- appraisal: rails60
|
77
|
-
ruby: 3.0
|
78
|
-
- appraisal: rails60
|
79
|
-
ruby: 3.1
|
80
|
-
- appraisal: rails60
|
81
|
-
ruby: 3.2
|
82
|
-
|
83
|
-
# Rails 6.1 supports Ruby 2.5+
|
84
|
-
- appraisal: rails61
|
85
|
-
ruby: 2.3
|
86
|
-
- appraisal: rails61
|
87
|
-
ruby: 2.4
|
88
|
-
|
89
|
-
# Rails 7 supports Ruby 2.7+
|
90
|
-
- appraisal: rails70
|
91
|
-
ruby: 2.3
|
92
|
-
- appraisal: rails70
|
93
|
-
ruby: 2.4
|
94
|
-
- appraisal: rails70
|
95
|
-
ruby: 2.5
|
96
|
-
- appraisal: rails70
|
97
|
-
ruby: 2.6
|
98
|
-
|
99
|
-
services:
|
100
|
-
postgres:
|
101
|
-
image: postgres
|
102
|
-
env:
|
103
|
-
POSTGRES_USER: postgres
|
104
|
-
POSTGRES_PASSWORD: postgres
|
105
|
-
POSTGRES_DB: audited_test
|
106
|
-
ports:
|
107
|
-
- 5432:5432
|
108
|
-
# needed because the postgres container does not provide a healthcheck
|
109
|
-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
110
|
-
|
111
|
-
env:
|
112
|
-
DB_DATABASE: audited_test
|
113
|
-
DB_USER: root
|
114
|
-
DB_PASSWORD: 'root'
|
115
|
-
DB_HOST: localhost
|
116
|
-
|
117
|
-
steps:
|
118
|
-
- name: Setup MySQL
|
119
|
-
run: |
|
120
|
-
sudo /etc/init.d/mysql start
|
121
|
-
mysql -e 'CREATE DATABASE audited_test;' -uroot -proot
|
122
|
-
mysql -e 'SHOW DATABASES;' -uroot -proot
|
123
|
-
- uses: actions/checkout@v3
|
124
|
-
- name: Copy Gemfile
|
125
|
-
run: sed 's/\.\././' gemfiles/${{ matrix.appraisal }}.gemfile > Gemfile
|
126
|
-
- name: Set up Ruby ${{ matrix.ruby }}
|
127
|
-
uses: ruby/setup-ruby@v1
|
128
|
-
with:
|
129
|
-
ruby-version: ${{ matrix.ruby }}
|
130
|
-
bundler-cache: true
|
131
|
-
- name: Run tests
|
132
|
-
env:
|
133
|
-
DB: ${{ matrix.db }}
|
134
|
-
run: bundle exec rake
|
data/.gitignore
DELETED
data/.standard.yml
DELETED
data/.yardopts
DELETED
data/gemfiles/rails50.gemfile
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rails", "~> 5.0.0"
|
6
|
-
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
7
|
-
gem "pg", ">= 0.18", "< 2.0"
|
8
|
-
gem "sqlite3", "~> 1.3.6"
|
9
|
-
gem "psych", "~> 3.1"
|
10
|
-
|
11
|
-
gemspec name: "audited", path: "../"
|
data/gemfiles/rails51.gemfile
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rails", "~> 5.1.4"
|
6
|
-
gem "mysql2", ">= 0.3.18", "< 0.6.0"
|
7
|
-
gem "pg", ">= 0.18", "< 2.0"
|
8
|
-
gem "sqlite3", "~> 1.3.6"
|
9
|
-
gem "psych", "~> 3.1"
|
10
|
-
|
11
|
-
gemspec name: "audited", path: "../"
|
data/gemfiles/rails52.gemfile
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "rails", ">= 5.2.8.1", "< 5.3"
|
6
|
-
gem "mysql2", ">= 0.4.4", "< 0.6.0"
|
7
|
-
gem "pg", ">= 0.18", "< 2.0"
|
8
|
-
gem "sqlite3", "~> 1.3.6"
|
9
|
-
gem "psych", "~> 3.1"
|
10
|
-
|
11
|
-
gemspec name: "audited", path: "../"
|
data/gemfiles/rails60.gemfile
DELETED
data/gemfiles/rails61.gemfile
DELETED
data/gemfiles/rails70.gemfile
DELETED
data/spec/audited/audit_spec.rb
DELETED
@@ -1,357 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
SingleCov.covered! uncovered: 1 # Rails version check
|
4
|
-
|
5
|
-
class CustomAudit < Audited::Audit
|
6
|
-
def custom_method
|
7
|
-
"I'm custom!"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class TempModel1 < ::ActiveRecord::Base
|
12
|
-
self.table_name = :companies
|
13
|
-
end
|
14
|
-
|
15
|
-
class TempModel2 < ::ActiveRecord::Base
|
16
|
-
self.table_name = :companies
|
17
|
-
end
|
18
|
-
|
19
|
-
class Models::ActiveRecord::CustomUser < ::ActiveRecord::Base
|
20
|
-
end
|
21
|
-
|
22
|
-
class Models::ActiveRecord::CustomUserSubclass < Models::ActiveRecord::CustomUser
|
23
|
-
audited
|
24
|
-
end
|
25
|
-
|
26
|
-
describe Audited::Audit do
|
27
|
-
let(:user) { Models::ActiveRecord::User.new name: "Testing" }
|
28
|
-
|
29
|
-
describe "audit class" do
|
30
|
-
around(:example) do |example|
|
31
|
-
original_audit_class = Audited.audit_class
|
32
|
-
|
33
|
-
example.run
|
34
|
-
|
35
|
-
Audited.config { |config| config.audit_class = original_audit_class }
|
36
|
-
end
|
37
|
-
|
38
|
-
context "when a custom audit class is configured" do
|
39
|
-
it "should be used in place of #{described_class}" do
|
40
|
-
Audited.config { |config| config.audit_class = "CustomAudit" }
|
41
|
-
TempModel1.audited
|
42
|
-
|
43
|
-
record = TempModel1.create
|
44
|
-
|
45
|
-
audit = record.audits.first
|
46
|
-
expect(audit).to be_a CustomAudit
|
47
|
-
expect(audit.custom_method).to eq "I'm custom!"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "when a custom audit class is not configured" do
|
52
|
-
it "should default to #{described_class}" do
|
53
|
-
TempModel2.audited
|
54
|
-
|
55
|
-
record = TempModel2.create
|
56
|
-
|
57
|
-
audit = record.audits.first
|
58
|
-
expect(audit).to be_a Audited::Audit
|
59
|
-
expect(audit.respond_to?(:custom_method)).to be false
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe "#audited_changes" do
|
65
|
-
let(:audit) { Audited.audit_class.new }
|
66
|
-
|
67
|
-
it "can unserialize yaml from text columns" do
|
68
|
-
audit.audited_changes = {foo: "bar"}
|
69
|
-
expect(audit.audited_changes).to eq foo: "bar"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "does not unserialize from binary columns" do
|
73
|
-
allow(Audited::YAMLIfTextColumnType).to receive(:text_column?).and_return(false)
|
74
|
-
audit.audited_changes = {foo: "bar"}
|
75
|
-
expect(audit.audited_changes).to eq "{:foo=>\"bar\"}"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "#undo" do
|
80
|
-
let(:user) { Models::ActiveRecord::User.create(name: "John") }
|
81
|
-
|
82
|
-
it "undos changes" do
|
83
|
-
user.update_attribute(:name, "Joe")
|
84
|
-
user.audits.last.undo
|
85
|
-
user.reload
|
86
|
-
expect(user.name).to eq("John")
|
87
|
-
end
|
88
|
-
|
89
|
-
it "undos destroy" do
|
90
|
-
user.destroy
|
91
|
-
user.audits.last.undo
|
92
|
-
user = Models::ActiveRecord::User.find_by(name: "John")
|
93
|
-
expect(user.name).to eq("John")
|
94
|
-
end
|
95
|
-
|
96
|
-
it "undos creation" do
|
97
|
-
user # trigger create
|
98
|
-
expect { user.audits.last.undo }.to change(Models::ActiveRecord::User, :count).by(-1)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "fails when trying to undo unknown" do
|
102
|
-
audit = user.audits.last
|
103
|
-
audit.action = "oops"
|
104
|
-
expect { audit.undo }.to raise_error("invalid action given oops")
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "user=" do
|
109
|
-
it "should be able to set the user to a model object" do
|
110
|
-
subject.user = user
|
111
|
-
expect(subject.user).to eq(user)
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should be able to set the user to nil" do
|
115
|
-
subject.user_id = 1
|
116
|
-
subject.user_type = "Models::ActiveRecord::User"
|
117
|
-
subject.username = "joe"
|
118
|
-
|
119
|
-
subject.user = nil
|
120
|
-
|
121
|
-
expect(subject.user).to be_nil
|
122
|
-
expect(subject.user_id).to be_nil
|
123
|
-
expect(subject.user_type).to be_nil
|
124
|
-
expect(subject.username).to be_nil
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should be able to set the user to a string" do
|
128
|
-
subject.user = "test"
|
129
|
-
expect(subject.user).to eq("test")
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should clear model when setting to a string" do
|
133
|
-
subject.user = user
|
134
|
-
subject.user = "testing"
|
135
|
-
expect(subject.user_id).to be_nil
|
136
|
-
expect(subject.user_type).to be_nil
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should clear the username when setting to a model" do
|
140
|
-
subject.username = "test"
|
141
|
-
subject.user = user
|
142
|
-
expect(subject.username).to be_nil
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
describe "revision" do
|
147
|
-
it "should recreate attributes" do
|
148
|
-
user = Models::ActiveRecord::User.create name: "1"
|
149
|
-
5.times { |i| user.update_attribute :name, (i + 2).to_s }
|
150
|
-
|
151
|
-
user.audits.each do |audit|
|
152
|
-
expect(audit.revision.name).to eq(audit.version.to_s)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
it "should set protected attributes" do
|
157
|
-
u = Models::ActiveRecord::User.create(name: "Brandon")
|
158
|
-
u.update_attribute :logins, 1
|
159
|
-
u.update_attribute :logins, 2
|
160
|
-
|
161
|
-
expect(u.audits[2].revision.logins).to eq(2)
|
162
|
-
expect(u.audits[1].revision.logins).to eq(1)
|
163
|
-
expect(u.audits[0].revision.logins).to eq(0)
|
164
|
-
end
|
165
|
-
|
166
|
-
it "should bypass attribute assignment wrappers" do
|
167
|
-
u = Models::ActiveRecord::User.create(name: "<Joe>")
|
168
|
-
expect(u.audits.first.revision.name).to eq("<Joe>")
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should work for deleted records" do
|
172
|
-
user = Models::ActiveRecord::User.create name: "1"
|
173
|
-
user.destroy
|
174
|
-
revision = user.audits.last.revision
|
175
|
-
expect(revision.name).to eq(user.name)
|
176
|
-
expect(revision).to be_a_new_record
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe ".collection_cache_key" do
|
181
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
182
|
-
it "uses created at" do
|
183
|
-
Audited::Audit.delete_all
|
184
|
-
audit = Models::ActiveRecord::User.create(name: "John").audits.last
|
185
|
-
audit.update_columns(created_at: Time.zone.parse("2018-01-01"))
|
186
|
-
expect(Audited::Audit.collection_cache_key).to match(/-20180101\d+$/)
|
187
|
-
end
|
188
|
-
else
|
189
|
-
it "is not defined" do
|
190
|
-
expect { Audited::Audit.collection_cache_key }.to raise_error(NoMethodError)
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
describe ".assign_revision_attributes" do
|
196
|
-
it "dups when frozen" do
|
197
|
-
user.freeze
|
198
|
-
assigned = Audited::Audit.assign_revision_attributes(user, name: "Bar")
|
199
|
-
expect(assigned.name).to eq "Bar"
|
200
|
-
end
|
201
|
-
|
202
|
-
it "ignores unassignable attributes" do
|
203
|
-
assigned = Audited::Audit.assign_revision_attributes(user, oops: "Bar")
|
204
|
-
expect(assigned.name).to eq "Testing"
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
it "should set the version number on create" do
|
209
|
-
user = Models::ActiveRecord::User.create! name: "Set Version Number"
|
210
|
-
expect(user.audits.first.version).to eq(1)
|
211
|
-
user.update_attribute :name, "Set to 2"
|
212
|
-
expect(user.audits.reload.first.version).to eq(1)
|
213
|
-
expect(user.audits.reload.last.version).to eq(2)
|
214
|
-
user.destroy
|
215
|
-
expect(Audited::Audit.where(auditable_type: "Models::ActiveRecord::User", auditable_id: user.id).last.version).to eq(3)
|
216
|
-
end
|
217
|
-
|
218
|
-
it "should set the request uuid on create" do
|
219
|
-
user = Models::ActiveRecord::User.create! name: "Set Request UUID"
|
220
|
-
expect(user.audits.reload.first.request_uuid).not_to be_blank
|
221
|
-
end
|
222
|
-
|
223
|
-
describe "reconstruct_attributes" do
|
224
|
-
it "should work with the old way of storing just the new value" do
|
225
|
-
audits = Audited::Audit.reconstruct_attributes([Audited::Audit.new(audited_changes: {"attribute" => "value"})])
|
226
|
-
expect(audits["attribute"]).to eq("value")
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
describe "audited_classes" do
|
231
|
-
it "should include audited classes" do
|
232
|
-
expect(Audited::Audit.audited_classes).to include(Models::ActiveRecord::User)
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should include subclasses" do
|
236
|
-
expect(Audited::Audit.audited_classes).to include(Models::ActiveRecord::CustomUserSubclass)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
describe "new_attributes" do
|
241
|
-
it "should return the audited_changes without modification for create" do
|
242
|
-
new_attributes = Audited::Audit.new(audited_changes: {int: 1, array: [1]}, action: :create).new_attributes
|
243
|
-
expect(new_attributes).to eq({"int" => 1, "array" => [1]})
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should return a hash that contains the after values of each attribute" do
|
247
|
-
new_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [3, 4]}, action: :update).new_attributes
|
248
|
-
expect(new_attributes).to eq({"a" => 2, "b" => 4})
|
249
|
-
end
|
250
|
-
|
251
|
-
it "should return the audited_changes without modification for destroy" do
|
252
|
-
new_attributes = Audited::Audit.new(audited_changes: {int: 1, array: [1]}, action: :destroy).new_attributes
|
253
|
-
expect(new_attributes).to eq({"int" => 1, "array" => [1]})
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
describe "old_attributes" do
|
258
|
-
it "should return the audited_changes without modification for create" do
|
259
|
-
old_attributes = Audited::Audit.new(audited_changes: {int: 1, array: [1]}, action: :create).new_attributes
|
260
|
-
expect(old_attributes).to eq({"int" => 1, "array" => [1]})
|
261
|
-
end
|
262
|
-
|
263
|
-
it "should return a hash that contains the before values of each attribute" do
|
264
|
-
old_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [3, 4]}, action: :update).old_attributes
|
265
|
-
expect(old_attributes).to eq({"a" => 1, "b" => 3})
|
266
|
-
end
|
267
|
-
|
268
|
-
it "should return the audited_changes without modification for destroy" do
|
269
|
-
old_attributes = Audited::Audit.new(audited_changes: {int: 1, array: [1]}, action: :destroy).old_attributes
|
270
|
-
expect(old_attributes).to eq({"int" => 1, "array" => [1]})
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
describe "as_user" do
|
275
|
-
it "should record user objects" do
|
276
|
-
Audited::Audit.as_user(user) do
|
277
|
-
company = Models::ActiveRecord::Company.create name: "The auditors"
|
278
|
-
company.name = "The Auditors, Inc"
|
279
|
-
company.save
|
280
|
-
|
281
|
-
company.audits.each do |audit|
|
282
|
-
expect(audit.user).to eq(user)
|
283
|
-
end
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
it "should support nested as_user" do
|
288
|
-
Audited::Audit.as_user("sidekiq") do
|
289
|
-
company = Models::ActiveRecord::Company.create name: "The auditors"
|
290
|
-
company.name = "The Auditors, Inc"
|
291
|
-
company.save
|
292
|
-
expect(company.audits[-1].user).to eq("sidekiq")
|
293
|
-
|
294
|
-
Audited::Audit.as_user(user) do
|
295
|
-
company.name = "NEW Auditors, Inc"
|
296
|
-
company.save
|
297
|
-
expect(company.audits[-1].user).to eq(user)
|
298
|
-
end
|
299
|
-
|
300
|
-
company.name = "LAST Auditors, Inc"
|
301
|
-
company.save
|
302
|
-
expect(company.audits[-1].user).to eq("sidekiq")
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
it "should record usernames" do
|
307
|
-
Audited::Audit.as_user(user.name) do
|
308
|
-
company = Models::ActiveRecord::Company.create name: "The auditors"
|
309
|
-
company.name = "The Auditors, Inc"
|
310
|
-
company.save
|
311
|
-
|
312
|
-
company.audits.each do |audit|
|
313
|
-
expect(audit.username).to eq(user.name)
|
314
|
-
end
|
315
|
-
end
|
316
|
-
end
|
317
|
-
|
318
|
-
if ActiveRecord::Base.connection.adapter_name != "SQLite"
|
319
|
-
it "should be thread safe" do
|
320
|
-
expect(user.save).to eq(true)
|
321
|
-
|
322
|
-
t1 = Thread.new do
|
323
|
-
Audited::Audit.as_user(user) do
|
324
|
-
sleep 1
|
325
|
-
expect(Models::ActiveRecord::Company.create(name: "The Auditors, Inc").audits.first.user).to eq(user)
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
t2 = Thread.new do
|
330
|
-
Audited::Audit.as_user(user.name) do
|
331
|
-
expect(Models::ActiveRecord::Company.create(name: "The Competing Auditors, LLC").audits.first.username).to eq(user.name)
|
332
|
-
sleep 0.5
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
t1.join
|
337
|
-
t2.join
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
it "should return the value from the yield block" do
|
342
|
-
result = Audited::Audit.as_user("foo") do
|
343
|
-
42
|
344
|
-
end
|
345
|
-
expect(result).to eq(42)
|
346
|
-
end
|
347
|
-
|
348
|
-
it "should reset audited_user when the yield block raises an exception" do
|
349
|
-
expect {
|
350
|
-
Audited::Audit.as_user("foo") do
|
351
|
-
raise StandardError.new("expected")
|
352
|
-
end
|
353
|
-
}.to raise_exception("expected")
|
354
|
-
expect(Audited.store[:audited_user]).to be_nil
|
355
|
-
end
|
356
|
-
end
|
357
|
-
end
|