This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
hoptoad_tasks_test.rb
163 lines (132 loc) · 5.51 KB
/
hoptoad_tasks_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
162
163
require File.dirname(__FILE__) + '/helper'
require 'rubygems'
require File.dirname(__FILE__) + '/../lib/hoptoad_tasks'
require 'fakeweb'
FakeWeb.allow_net_connect = false
class HoptoadTasksTest < Test::Unit::TestCase
def successful_response(body = "")
response = Net::HTTPSuccess.new('1.2', '200', 'OK')
response.stubs(:body).returns(body)
return response
end
def unsuccessful_response(body = "")
response = Net::HTTPClientError.new('1.2', '200', 'OK')
response.stubs(:body).returns(body)
return response
end
context "being quiet" do
setup { HoptoadTasks.stubs(:puts) }
context "in a configured project" do
setup { HoptoadNotifier.configure { |config| config.api_key = "1234123412341234" } }
context "on deploy({})" do
setup { @output = HoptoadTasks.deploy({}) }
before_should "complain about missing rails env" do
HoptoadTasks.expects(:puts).with(regexp_matches(/rails environment/i))
end
should "return false" do
assert !@output
end
end
context "given an optional HTTP proxy and valid options" do
setup do
@response = stub("response", :body => "stub body")
@http_proxy = stub("proxy", :post_form => @response)
Net::HTTP.expects(:Proxy).
with(HoptoadNotifier.configuration.proxy_host,
HoptoadNotifier.configuration.proxy_port,
HoptoadNotifier.configuration.proxy_user,
HoptoadNotifier.configuration.proxy_pass).
returns(@http_proxy)
@options = { :rails_env => "staging", :dry_run => false }
end
context "performing a dry run" do
setup { @output = HoptoadTasks.deploy(@options.merge(:dry_run => true)) }
should "return true without performing any actual request" do
assert_equal true, @output
assert_received(@http_proxy, :post_form) do|expects|
expects.never
end
end
end
context "on deploy(options)" do
setup do
@output = HoptoadTasks.deploy(@options)
end
before_should "post to http://hoptoadapp.com/deploys.txt" do
URI.stubs(:parse).with('http://hoptoadapp.com/deploys.txt').returns(:uri)
@http_proxy.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
end
before_should "use the project api key" do
@http_proxy.expects(:post_form).
with(kind_of(URI), has_entries('api_key' => "1234123412341234")).
returns(successful_response)
end
before_should "use send the rails_env param" do
@http_proxy.expects(:post_form).
with(kind_of(URI), has_entries("deploy[rails_env]" => "staging")).
returns(successful_response)
end
[:local_username, :scm_repository, :scm_revision].each do |key|
before_should "use send the #{key} param if it's passed in." do
@options[key] = "value"
@http_proxy.expects(:post_form).
with(kind_of(URI), has_entries("deploy[#{key}]" => "value")).
returns(successful_response)
end
end
before_should "use the :api_key param if it's passed in." do
@options[:api_key] = "value"
@http_proxy.expects(:post_form).
with(kind_of(URI), has_entries("api_key" => "value")).
returns(successful_response)
end
before_should "puts the response body on success" do
HoptoadTasks.expects(:puts).with("body")
@http_proxy.expects(:post_form).with(any_parameters).returns(successful_response('body'))
end
before_should "puts the response body on failure" do
HoptoadTasks.expects(:puts).with("body")
@http_proxy.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
end
should "return false on failure", :before => lambda {
@http_proxy.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
} do
assert !@output
end
should "return true on success", :before => lambda {
@http_proxy.expects(:post_form).with(any_parameters).returns(successful_response('body'))
} do
assert @output
end
end
end
end
context "in a configured project with custom host" do
setup do
HoptoadNotifier.configure do |config|
config.api_key = "1234123412341234"
config.host = "custom.host"
end
end
context "on deploy(:rails_env => 'staging')" do
setup { @output = HoptoadTasks.deploy(:rails_env => "staging") }
before_should "post to the custom host" do
URI.stubs(:parse).with('http://custom.host/deploys.txt').returns(:uri)
Net::HTTP.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
end
end
end
context "when not configured" do
setup { HoptoadNotifier.configure { |config| config.api_key = "" } }
context "on deploy(:rails_env => 'staging')" do
setup { @output = HoptoadTasks.deploy(:rails_env => "staging") }
before_should "complain about missing api key" do
HoptoadTasks.expects(:puts).with(regexp_matches(/api key/i))
end
should "return false" do
assert !@output
end
end
end
end
end