forked from airbrake/airbrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_methods_test.rb
161 lines (128 loc) · 4.11 KB
/
controller_methods_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require File.expand_path '../helper', __FILE__
require 'airbrake/rails/controller_methods'
class TestController
include Airbrake::Rails::ControllerMethods
def params; {}; end
def session; nil; end
def request
OpenStruct.new(:port=> 80, :protocol => 'http://', host: 'example.com', :fullpath => '/', :env => [])
end
end
class NilUserTestController < TestController
def current_user
nil
end
end
class CurrentUserTestController < TestController
def current_user
OpenStruct.new(:id => 123, :name => 'tape')
end
end
class CurrentMemberTestController < TestController
def current_member
OpenStruct.new(:id => 321, :name => 'mamba')
end
end
class NoSessionTestController < TestController
def session
nil
end
end
class ControllerMethodsTest < Test::Unit::TestCase
include DefinesConstants
context "#airbrake_current_user" do
context "without a logged in user" do
setup do
NilClass.class_eval do
@@called = false
def self.called
!! @@called
end
def id
@@called = true
end
end
@controller = NilUserTestController.new
end
should "not call #id on NilClass" do
@controller.send(:airbrake_current_user)
assert_equal false, NilClass.called
end
end
context "with a logged in User" do
teardown do
Object.__send__(:remove_const, :ActiveRecord) if defined?(ActiveRecord)
Object.__send__(:remove_const, :POOL) if defined?(POOL)
end
should 'include user info in the data sent to Ab' do
Airbrake.configuration.user_attributes = %w(id)
controller = CurrentUserTestController.new
ab_data = controller.airbrake_request_data
assert_equal( {:id => 123}, ab_data[:user])
end
should 'include more info if asked to, discarding unknown attributes' do
Airbrake.configuration.user_attributes = %w(id name collar-size)
controller = CurrentUserTestController.new
ab_data = controller.airbrake_request_data
assert_equal( {:id => 123, :name => 'tape'}, ab_data[:user])
end
should 'work with a "current_member" method too' do
Airbrake.configuration.user_attributes = %w(id)
controller = CurrentMemberTestController.new
ab_data = controller.airbrake_request_data
assert_equal( {:id => 321}, ab_data[:user])
end
should "release DB connections" do
::POOL = Object.new
module ::ActiveRecord; class Base; def self.connection_pool; ::POOL; end; end; end
::POOL.expects(:release_connection)
CurrentUserTestController.new.airbrake_request_data
end
end
end
context '#airbrake_session_data' do
setup do
@controller = NoSessionTestController.new
end
should 'not call session if no session' do
no_session = @controller.send(:airbrake_session_data)
assert_equal no_session, {:session => 'no session found'}
end
end
context "Rails 3" do
setup do
@controller = NilUserTestController.new
::Rails = Object.new
::Rails.stubs(:version).returns("3.2.17")
end
should "respond to rails_3_or_4? with true" do
assert @controller.send(:rails_3_or_4?)
end
should "call filter_rails3_parameters" do
hash = {:a => "b"}
filtered_hash = {:c => "d"}
@controller.expects(:filter_rails3_parameters).with(hash).
returns(filtered_hash)
assert_equal filtered_hash,
@controller.send(:airbrake_filter_if_filtering, hash)
end
end
context "Rails 4.x" do
setup do
@controller = TestController.new
::Rails = Object.new
::Rails.stubs(:version).returns("4.5.6.7")
end
should 'be true when running Rails 4.x' do
assert @controller.send(:rails_3_or_4?)
end
should "call filter_rails3_parameters" do
hash = {:a => "b"}
filtered_hash = {:c => "d"}
@controller.expects(:filter_rails3_parameters).with(hash).
returns(filtered_hash)
assert_equal filtered_hash,
@controller.send(:airbrake_filter_if_filtering, hash)
end
end
end