auditor 1.0.0 → 2.0.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.
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/README.rdoc +58 -11
- data/Rakefile +33 -0
- data/auditor.gemspec +23 -0
- data/init.rb +1 -0
- data/lib/auditor.rb +16 -4
- data/lib/auditor/audit.rb +49 -2
- data/lib/auditor/auditable.rb +44 -0
- data/lib/auditor/config.rb +38 -0
- data/lib/auditor/recorder.rb +37 -36
- data/lib/auditor/spec_helpers.rb +6 -6
- data/lib/auditor/status.rb +60 -0
- data/lib/auditor/user.rb +5 -6
- data/lib/auditor/version.rb +2 -2
- data/lib/generators/auditor/migration/migration_generator.rb +4 -4
- data/lib/generators/auditor/migration/templates/migration.rb +8 -4
- data/spec/audit_spec.rb +63 -0
- data/spec/auditable_spec.rb +111 -0
- data/spec/config_spec.rb +49 -0
- data/spec/recorder_spec.rb +73 -107
- data/spec/spec_helper.rb +2 -26
- data/spec/status_spec.rb +18 -0
- data/spec/support/db_setup.rb +50 -0
- data/spec/support/transactional_specs.rb +17 -0
- data/spec/user_spec.rb +5 -5
- metadata +70 -34
- data/lib/auditor/config_parser.rb +0 -36
- data/lib/auditor/integration.rb +0 -49
- data/lib/auditor/model_audit.rb +0 -47
- data/lib/auditor/thread_local.rb +0 -18
- data/lib/auditor/thread_status.rb +0 -34
- data/spec/config_parser_spec.rb +0 -53
- data/spec/model_audit_spec.rb +0 -83
- data/spec/support/auditor_helpers.rb +0 -29
- data/spec/thread_local_spec.rb +0 -14
- data/spec/thread_status_spec.rb +0 -16
data/lib/auditor/thread_local.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Auditor
|
2
|
-
class ThreadLocal
|
3
|
-
|
4
|
-
def initialize(initial_value)
|
5
|
-
@thread_symbol = "#{rand}#{Time.now.to_f}"
|
6
|
-
set initial_value
|
7
|
-
end
|
8
|
-
|
9
|
-
def set(value)
|
10
|
-
Thread.current[@thread_symbol] = value
|
11
|
-
end
|
12
|
-
|
13
|
-
def get
|
14
|
-
Thread.current[@thread_symbol]
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'auditor/thread_local'
|
2
|
-
|
3
|
-
module Auditor
|
4
|
-
module ThreadStatus
|
5
|
-
|
6
|
-
def self.enabled?
|
7
|
-
status
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.disabled?
|
11
|
-
!status
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.enable
|
15
|
-
set_status true
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.disable
|
19
|
-
set_status false
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
def self.status
|
24
|
-
@status = Auditor::ThreadLocal.new(true) if @status.nil?
|
25
|
-
@status.get
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.set_status(status)
|
29
|
-
@status = Auditor::ThreadLocal.new(true) if @status.nil?
|
30
|
-
@status.set status
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
data/spec/config_parser_spec.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'auditor'
|
3
|
-
|
4
|
-
describe Auditor::ConfigParser do
|
5
|
-
|
6
|
-
describe 'Configuration' do
|
7
|
-
it "should parse actions and options from a config array" do
|
8
|
-
config = Auditor::ConfigParser.extract_config([:create, 'update', {:only => :username}])
|
9
|
-
config.should_not be_nil
|
10
|
-
config.should have(2).items
|
11
|
-
config[0].should =~ [:create, :update]
|
12
|
-
config[1].should == {:only => ["username"], :except => []}
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should parse actions and options from a config array when options are absent" do
|
16
|
-
config = Auditor::ConfigParser.extract_config([:create, 'update'])
|
17
|
-
config.should_not be_nil
|
18
|
-
config.should have(2).items
|
19
|
-
config[0].should =~ [:create, :update]
|
20
|
-
config[1].should == {:only => [], :except => []}
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should parse actions" do
|
24
|
-
config = Auditor::ConfigParser.extract_config([:create])
|
25
|
-
config.should_not be_nil
|
26
|
-
config.should have(2).items
|
27
|
-
config[0].should =~ [:create]
|
28
|
-
config[1].should == {:only => [], :except => []}
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
describe 'Configuration Validation' do
|
34
|
-
it "should raise a Auditor::Error if no action is specified" do
|
35
|
-
lambda {
|
36
|
-
Auditor::ConfigParser.instance_eval { validate_config([], {}) }
|
37
|
-
}.should raise_error(Auditor::Error)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should raise a Auditor::Error if an invalid action is specified" do
|
41
|
-
lambda {
|
42
|
-
Auditor::ConfigParser.instance_eval { validate_config([:create, :udate], {}) }
|
43
|
-
}.should raise_error(Auditor::Error)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should raise a Auditor::Error if both the except and only options are specified" do
|
47
|
-
lambda {
|
48
|
-
Auditor::ConfigParser.instance_eval { validate_config([:find], {:except => :ssn, :only => :username}) }
|
49
|
-
}.should raise_error(Auditor::Error)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
data/spec/model_audit_spec.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'auditor'
|
3
|
-
|
4
|
-
describe Auditor::ModelAudit do
|
5
|
-
|
6
|
-
before(:each) do
|
7
|
-
Auditor::User.current_user = (Class.new do
|
8
|
-
def id; 1 end
|
9
|
-
end).new
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'should audit find' do
|
13
|
-
c = new_model_class.instance_eval { audit(:find); self }
|
14
|
-
verify_standard_audits(c.new, :find)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should audit create' do
|
18
|
-
c = new_model_class.instance_eval { audit(:create); self }
|
19
|
-
verify_standard_audits(c.new, :create)
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should audit update' do
|
23
|
-
c = new_model_class.instance_eval { audit(:update); self }
|
24
|
-
verify_standard_audits(c.new, :update)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'should audit destroy' do
|
28
|
-
c = new_model_class.instance_eval { audit(:destroy); self }
|
29
|
-
verify_standard_audits(c.new, :destroy)
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'should allow multiple actions to be specified with one audit statment' do
|
33
|
-
c = new_model_class.instance_eval { audit(:create, :update); self }
|
34
|
-
verify_standard_audits(c.new, :create, :update)
|
35
|
-
|
36
|
-
c = new_model_class.instance_eval { audit(:create, :update, :destroy); self }
|
37
|
-
verify_standard_audits(c.new, :create, :update, :destroy)
|
38
|
-
|
39
|
-
c = new_model_class.instance_eval { audit(:create, :update, :destroy, :find); self }
|
40
|
-
verify_standard_audits(c.new, :create, :update, :destroy, :find)
|
41
|
-
end
|
42
|
-
|
43
|
-
def verify_standard_audits(instance, *audited_callbacks)
|
44
|
-
audited_callbacks.each do |action|
|
45
|
-
mock_auditor = mock('auditor')
|
46
|
-
Auditor::Recorder.should_receive(:new).and_return(mock_auditor)
|
47
|
-
mock_auditor.should_receive(:audit_before) unless action == :find
|
48
|
-
mock_auditor.should_receive(:audit_after)
|
49
|
-
instance.send action
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def new_model_class
|
54
|
-
Class.new(ActiveRecordMock) do
|
55
|
-
include Auditor::ModelAudit
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
class ActiveRecordMock
|
60
|
-
def id; 1 end
|
61
|
-
|
62
|
-
[:create, :update, :destroy, :find].each do |action|
|
63
|
-
define_method(action) do
|
64
|
-
send "before_#{action}".to_sym unless action == :find
|
65
|
-
send "after_#{action}".to_sym
|
66
|
-
end
|
67
|
-
|
68
|
-
metaclass = class << self; self end
|
69
|
-
metaclass.instance_eval do
|
70
|
-
unless action == :find
|
71
|
-
define_method("before_#{action}") do |method|
|
72
|
-
define_method("before_#{action}") { send method }
|
73
|
-
end
|
74
|
-
end
|
75
|
-
define_method("after_#{action}") do |method|
|
76
|
-
define_method("after_#{action}") { send method }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module AuditorHelpers
|
2
|
-
|
3
|
-
def current_user=(user)
|
4
|
-
Auditor::User.current_user = user
|
5
|
-
end
|
6
|
-
|
7
|
-
def current_user
|
8
|
-
Auditor::User.current_user
|
9
|
-
end
|
10
|
-
|
11
|
-
def clear_current_user
|
12
|
-
Auditor::User.current_user = nil
|
13
|
-
end
|
14
|
-
|
15
|
-
def verify_audit(audit, audited, user, action, edits_nil=false, message_nil=true)
|
16
|
-
audit.auditable_id.should == audited.id
|
17
|
-
audit.auditable_type.should == audited.class.name
|
18
|
-
audit.user_id.should == user.id
|
19
|
-
audit.user_type.should == user.class.name
|
20
|
-
audit.action.should == action.to_s
|
21
|
-
audit.message.should be_nil if message_nil
|
22
|
-
audit.message.should_not be_nil unless message_nil
|
23
|
-
audit.edits.should be_nil if edits_nil
|
24
|
-
audit.edits.should_not be_nil unless edits_nil
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
|
data/spec/thread_local_spec.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'auditor/thread_local'
|
3
|
-
|
4
|
-
describe Auditor::ThreadLocal do
|
5
|
-
it "should properly set and get thread-local variables" do
|
6
|
-
val = "val"
|
7
|
-
tl = Auditor::ThreadLocal.new(val)
|
8
|
-
tl.get.should == val
|
9
|
-
|
10
|
-
val2 = "val2"
|
11
|
-
tl.set val2
|
12
|
-
tl.get.should == val2
|
13
|
-
end
|
14
|
-
end
|
data/spec/thread_status_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
require 'auditor'
|
3
|
-
|
4
|
-
describe Auditor::ThreadStatus do
|
5
|
-
it "should be enabled if set to enabled" do
|
6
|
-
Auditor::ThreadStatus.enable
|
7
|
-
Auditor::ThreadStatus.should be_enabled
|
8
|
-
Auditor::ThreadStatus.should_not be_disabled
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should be disabled if set to disabled" do
|
12
|
-
Auditor::ThreadStatus.disable
|
13
|
-
Auditor::ThreadStatus.should_not be_enabled
|
14
|
-
Auditor::ThreadStatus.should be_disabled
|
15
|
-
end
|
16
|
-
end
|