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.
@@ -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
@@ -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
@@ -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
-
@@ -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
@@ -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