hoptoad_notifier 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,237 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ gem 'jferris-mocha', '0.9.5.0.1241126838'
5
+
6
+ require 'shoulda'
7
+ require 'mocha'
8
+
9
+ $LOAD_PATH << File.join(File.dirname(__FILE__), *%w[.. vendor ginger lib])
10
+ require 'ginger'
11
+
12
+ require 'action_controller'
13
+ require 'action_controller/test_process'
14
+ require 'active_record'
15
+ require 'active_record/base'
16
+ require 'active_support'
17
+ require 'nokogiri'
18
+
19
+ require File.join(File.dirname(__FILE__), "..", "lib", "hoptoad_notifier")
20
+
21
+ begin require 'redgreen'; rescue LoadError; end
22
+
23
+ module TestMethods
24
+ def rescue_action e
25
+ raise e
26
+ end
27
+
28
+ def do_raise
29
+ raise "Hoptoad"
30
+ end
31
+
32
+ def do_not_raise
33
+ render :text => "Success"
34
+ end
35
+
36
+ def do_raise_ignored
37
+ raise ActiveRecord::RecordNotFound.new("404")
38
+ end
39
+
40
+ def do_raise_not_ignored
41
+ raise ActiveRecord::StatementInvalid.new("Statement invalid")
42
+ end
43
+
44
+ def manual_notify
45
+ notify_hoptoad(Exception.new)
46
+ render :text => "Success"
47
+ end
48
+
49
+ def manual_notify_ignored
50
+ notify_hoptoad(ActiveRecord::RecordNotFound.new("404"))
51
+ render :text => "Success"
52
+ end
53
+ end
54
+
55
+ class HoptoadController < ActionController::Base
56
+ include TestMethods
57
+ end
58
+
59
+ class Test::Unit::TestCase
60
+ def request(action = nil, method = :get, user_agent = nil, params = {})
61
+ @request = ActionController::TestRequest.new
62
+ @request.action = action ? action.to_s : ""
63
+
64
+ if user_agent
65
+ if @request.respond_to?(:user_agent=)
66
+ @request.user_agent = user_agent
67
+ else
68
+ @request.env["HTTP_USER_AGENT"] = user_agent
69
+ end
70
+ end
71
+ @request.query_parameters = @request.query_parameters.merge(params)
72
+ @response = ActionController::TestResponse.new
73
+ @controller.process(@request, @response)
74
+ end
75
+
76
+ # Borrowed from ActiveSupport 2.3.2
77
+ def assert_difference(expression, difference = 1, message = nil, &block)
78
+ b = block.send(:binding)
79
+ exps = Array.wrap(expression)
80
+ before = exps.map { |e| eval(e, b) }
81
+
82
+ yield
83
+
84
+ exps.each_with_index do |e, i|
85
+ error = "#{e.inspect} didn't change by #{difference}"
86
+ error = "#{message}.\n#{error}" if message
87
+ assert_equal(before[i] + difference, eval(e, b), error)
88
+ end
89
+ end
90
+
91
+ def assert_no_difference(expression, message = nil, &block)
92
+ assert_difference expression, 0, message, &block
93
+ end
94
+
95
+ def stub_sender
96
+ stub('sender', :send_to_hoptoad => nil)
97
+ end
98
+
99
+ def stub_sender!
100
+ HoptoadNotifier.sender = stub_sender
101
+ end
102
+
103
+ def stub_notice
104
+ stub('notice', :to_xml => 'some yaml', :ignore? => false)
105
+ end
106
+
107
+ def stub_notice!
108
+ returning stub_notice do |notice|
109
+ HoptoadNotifier::Notice.stubs(:new => notice)
110
+ end
111
+ end
112
+
113
+ def create_dummy
114
+ HoptoadNotifier::DummySender.new
115
+ end
116
+
117
+ def reset_config
118
+ HoptoadNotifier.configuration = nil
119
+ HoptoadNotifier.configure do |config|
120
+ config.api_key = 'abc123'
121
+ end
122
+ end
123
+
124
+ def clear_backtrace_filters
125
+ HoptoadNotifier.configuration.backtrace_filters.clear
126
+ end
127
+
128
+ def build_exception
129
+ raise
130
+ rescue => caught_exception
131
+ caught_exception
132
+ end
133
+
134
+ def build_notice_data(exception = nil)
135
+ exception ||= build_exception
136
+ {
137
+ :api_key => 'abc123',
138
+ :error_class => exception.class.name,
139
+ :error_message => "#{exception.class.name}: #{exception.message}",
140
+ :backtrace => exception.backtrace,
141
+ :environment => { 'PATH' => '/bin', 'REQUEST_URI' => '/users/1' },
142
+ :request => {
143
+ :params => { 'controller' => 'users', 'action' => 'show', 'id' => '1' },
144
+ :rails_root => '/path/to/application',
145
+ :url => "http://test.host/users/1"
146
+ },
147
+ :session => {
148
+ :key => '123abc',
149
+ :data => { 'user_id' => '5', 'flash' => { 'notice' => 'Logged in successfully' } }
150
+ }
151
+ }
152
+ end
153
+
154
+ def assert_caught_and_sent
155
+ assert !HoptoadNotifier.sender.collected.empty?
156
+ end
157
+
158
+ def assert_caught_and_not_sent
159
+ assert HoptoadNotifier.sender.collected.empty?
160
+ end
161
+
162
+ def assert_array_starts_with(expected, actual)
163
+ assert_respond_to actual, :to_ary
164
+ array = actual.to_ary.reverse
165
+ expected.reverse.each_with_index do |value, i|
166
+ assert_equal value, array[i]
167
+ end
168
+ end
169
+
170
+ def assert_valid_node(document, xpath, content)
171
+ nodes = document.xpath(xpath)
172
+ assert nodes.any?{|node| node.content == content },
173
+ "Expected xpath #{xpath} to have content #{content}, " +
174
+ "but found #{nodes.map { |n| n.content }} in #{nodes.size} matching nodes." +
175
+ "Document:\n#{document.to_s}"
176
+ end
177
+ end
178
+
179
+ module DefinesConstants
180
+ def setup
181
+ @defined_constants = []
182
+ end
183
+
184
+ def teardown
185
+ @defined_constants.each do |constant|
186
+ Object.__send__(:remove_const, constant)
187
+ end
188
+ end
189
+
190
+ def define_constant(name, value)
191
+ Object.const_set(name, value)
192
+ @defined_constants << name
193
+ end
194
+ end
195
+
196
+ # Also stolen from AS 2.3.2
197
+ class Array
198
+ # Wraps the object in an Array unless it's an Array. Converts the
199
+ # object to an Array using #to_ary if it implements that.
200
+ def self.wrap(object)
201
+ case object
202
+ when nil
203
+ []
204
+ when self
205
+ object
206
+ else
207
+ if object.respond_to?(:to_ary)
208
+ object.to_ary
209
+ else
210
+ [object]
211
+ end
212
+ end
213
+ end
214
+
215
+ end
216
+
217
+ class CollectingSender
218
+ attr_reader :collected
219
+
220
+ def initialize
221
+ @collected = []
222
+ end
223
+
224
+ def send_to_hoptoad(data)
225
+ @collected << data
226
+ end
227
+ end
228
+
229
+ class FakeLogger
230
+ def info(*args); end
231
+ def debug(*args); end
232
+ def warn(*args); end
233
+ def error(*args); end
234
+ def fatal(*args); end
235
+ end
236
+
237
+ RAILS_DEFAULT_LOGGER = FakeLogger.new
@@ -0,0 +1,138 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'rubygems'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/hoptoad_tasks'
5
+ require 'fakeweb'
6
+
7
+ FakeWeb.allow_net_connect = false
8
+
9
+ class HoptoadTasksTest < Test::Unit::TestCase
10
+ def successful_response(body = "")
11
+ response = Net::HTTPSuccess.new('1.2', '200', 'OK')
12
+ response.stubs(:body).returns(body)
13
+ return response
14
+ end
15
+
16
+ def unsuccessful_response(body = "")
17
+ response = Net::HTTPClientError.new('1.2', '200', 'OK')
18
+ response.stubs(:body).returns(body)
19
+ return response
20
+ end
21
+
22
+ context "being quiet" do
23
+ setup { HoptoadTasks.stubs(:puts) }
24
+
25
+ context "in a configured project" do
26
+ setup { HoptoadNotifier.configure { |config| config.api_key = "1234123412341234" } }
27
+
28
+ context "on deploy({})" do
29
+ setup { @output = HoptoadTasks.deploy({}) }
30
+
31
+ before_should "complain about missing rails env" do
32
+ HoptoadTasks.expects(:puts).with(regexp_matches(/rails environment/i))
33
+ end
34
+
35
+ should "return false" do
36
+ assert !@output
37
+ end
38
+ end
39
+
40
+ context "given valid options" do
41
+ setup { @options = {:rails_env => "staging"} }
42
+
43
+ context "on deploy(options)" do
44
+ setup { @output = HoptoadTasks.deploy(@options) }
45
+
46
+ before_should "post to http://hoptoadapp.com/deploys.txt" do
47
+ URI.stubs(:parse).with('http://hoptoadapp.com/deploys.txt').returns(:uri)
48
+ Net::HTTP.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
49
+ end
50
+
51
+ before_should "use the project api key" do
52
+ Net::HTTP.expects(:post_form).
53
+ with(kind_of(URI), has_entries('api_key' => "1234123412341234")).
54
+ returns(successful_response)
55
+ end
56
+
57
+ before_should "use send the rails_env param" do
58
+ Net::HTTP.expects(:post_form).
59
+ with(kind_of(URI), has_entries("deploy[rails_env]" => "staging")).
60
+ returns(successful_response)
61
+ end
62
+
63
+ [:local_username, :scm_repository, :scm_revision].each do |key|
64
+ before_should "use send the #{key} param if it's passed in." do
65
+ @options[key] = "value"
66
+ Net::HTTP.expects(:post_form).
67
+ with(kind_of(URI), has_entries("deploy[#{key}]" => "value")).
68
+ returns(successful_response)
69
+ end
70
+ end
71
+
72
+ before_should "use the :api_key param if it's passed in." do
73
+ @options[:api_key] = "value"
74
+ Net::HTTP.expects(:post_form).
75
+ with(kind_of(URI), has_entries("api_key" => "value")).
76
+ returns(successful_response)
77
+ end
78
+
79
+ before_should "puts the response body on success" do
80
+ HoptoadTasks.expects(:puts).with("body")
81
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(successful_response('body'))
82
+ end
83
+
84
+ before_should "puts the response body on failure" do
85
+ HoptoadTasks.expects(:puts).with("body")
86
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
87
+ end
88
+
89
+ should "return false on failure", :before => lambda {
90
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
91
+ } do
92
+ assert !@output
93
+ end
94
+
95
+ should "return true on success", :before => lambda {
96
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(successful_response('body'))
97
+ } do
98
+ assert @output
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ context "in a configured project with custom host" do
105
+ setup do
106
+ HoptoadNotifier.configure do |config|
107
+ config.api_key = "1234123412341234"
108
+ config.host = "custom.host"
109
+ end
110
+ end
111
+
112
+ context "on deploy(:rails_env => 'staging')" do
113
+ setup { @output = HoptoadTasks.deploy(:rails_env => "staging") }
114
+
115
+ before_should "post to the custom host" do
116
+ URI.stubs(:parse).with('http://custom.host/deploys.txt').returns(:uri)
117
+ Net::HTTP.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
118
+ end
119
+ end
120
+ end
121
+
122
+ context "when not configured" do
123
+ setup { HoptoadNotifier.configure { |config| config.api_key = "" } }
124
+
125
+ context "on deploy(:rails_env => 'staging')" do
126
+ setup { @output = HoptoadTasks.deploy(:rails_env => "staging") }
127
+
128
+ before_should "complain about missing api key" do
129
+ HoptoadTasks.expects(:puts).with(regexp_matches(/api key/i))
130
+ end
131
+
132
+ should "return false" do
133
+ assert !@output
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ def stub_http(response, body = nil)
5
+ response.stubs(:body => body) if body
6
+ @http = stub(:post => response,
7
+ :read_timeout= => nil,
8
+ :open_timeout= => nil,
9
+ :use_ssl= => nil)
10
+ Net::HTTP.stubs(:new).returns(@http)
11
+ end
12
+
13
+ def send_notice
14
+ HoptoadNotifier.sender.send_to_hoptoad('data')
15
+ end
16
+
17
+ def stub_verbose_log
18
+ HoptoadNotifier.stubs(:write_verbose_log)
19
+ end
20
+
21
+ def assert_logged(expected)
22
+ assert_received(HoptoadNotifier, :write_verbose_log) do |expect|
23
+ expect.with {|actual| actual =~ expected }
24
+ end
25
+ end
26
+
27
+ def assert_not_logged(expected)
28
+ assert_received(HoptoadNotifier, :write_verbose_log) do |expect|
29
+ expect.with {|actual| actual =~ expected }.never
30
+ end
31
+ end
32
+
33
+ def configure
34
+ HoptoadNotifier.configure { |config| }
35
+ end
36
+
37
+ should "report that notifier is ready when configured" do
38
+ stub_verbose_log
39
+ configure
40
+ assert_logged /Notifier (.*) ready/
41
+ end
42
+
43
+ should "not report that notifier is ready when internally configured" do
44
+ stub_verbose_log
45
+ HoptoadNotifier.configure(true) { |config| }
46
+ assert_not_logged /.*/
47
+ end
48
+
49
+ should "print environment info a successful notification without a body" do
50
+ reset_config
51
+ stub_verbose_log
52
+ stub_http(Net::HTTPSuccess)
53
+ send_notice
54
+ assert_logged /Environment Info:/
55
+ assert_not_logged /Response from Hoptoad:/
56
+ end
57
+
58
+ should "print environment info on a failed notification without a body" do
59
+ reset_config
60
+ stub_verbose_log
61
+ stub_http(Net::HTTPError)
62
+ send_notice
63
+ assert_logged /Environment Info:/
64
+ assert_not_logged /Response from Hoptoad:/
65
+ end
66
+
67
+ should "print environment info and response on a success with a body" do
68
+ reset_config
69
+ stub_verbose_log
70
+ stub_http(Net::HTTPSuccess, 'test')
71
+ send_notice
72
+ assert_logged /Environment Info:/
73
+ assert_logged /Response from Hoptoad:/
74
+ end
75
+
76
+ should "print environment info and response on a failure with a body" do
77
+ reset_config
78
+ stub_verbose_log
79
+ stub_http(Net::HTTPError, 'test')
80
+ send_notice
81
+ assert_logged /Environment Info:/
82
+ assert_logged /Response from Hoptoad:/
83
+ end
84
+
85
+ end