forked from rubygems/rubygems.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_command_test.rb
240 lines (203 loc) · 6.72 KB
/
webhook_command_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require 'helper'
class WebhookCommandTest < CommandTest
context "webhooking" do
setup do
@gem = "foo"
@api = "https://rubygems.org/api/v1/web_hooks"
@url = "http://example.com/hook"
@command = Gem::Commands::WebhookCommand.new
stub(@command).say
end
%w[-a --add -r --remove -f --fire].each do |option|
should "raise an error with no URL with #{option}" do
assert_raise OptionParser::MissingArgument do
@command.handle_options([@gem, option])
end
end
end
context "adding a specific hook" do
setup do
stub_api_key("key")
stub_request(:post, @api).to_return(:body => "Success!")
@command.handle_options([@gem, "-a", @url])
@command.execute
end
should "say hook was added" do
assert_received(@command) do |command|
command.say("Adding webhook...")
command.say("Success!")
end
end
should "post to api" do
# webmock doesn't pass body params on correctly :[
assert_requested(:post, @api,
:times => 1)
assert_requested(:post, @api,
:headers => { 'Authorization' => 'key' })
end
end
context "adding a global hook" do
setup do
stub_api_key("key")
stub_request(:post, @api).to_return(:body => "Success!")
@command.handle_options(["-g", "-a", @url])
@command.execute
end
should "say hook was added" do
assert_received(@command) do |command|
command.say("Adding webhook...")
command.say("Success!")
end
end
should "post to api" do
# webmock doesn't pass body params on correctly :[
assert_requested(:post, @api,
:times => 1)
assert_requested(:post, @api,
:headers => { 'Authorization' => 'key' })
end
end
context "listing hooks with some available" do
setup do
stub_api_key("key")
stub_request(:get, "#{@api}.yaml").to_return :body => <<EOF
---
foo:
- url: http://foogemhook.com
failure_count: 0
all gems:
- url: http://allgemshook.com
failure_count: 0
EOF
end
should "list all hooks" do
@command.handle_options([])
@command.execute
assert_said(@command, "all gems:")
assert_said(@command, "- http://allgemshook.com")
assert_said(@command, "foo:")
assert_said(@command, "- http://foogemhook.com")
end
should "list only specific hooks" do
@command.handle_options(['foo'])
@command.execute
assert_said(@command, "foo:")
assert_said(@command, "- http://foogemhook.com")
assert_never_said(@command, "all gems:")
assert_never_said(@command, "- http://allgemshook.com")
end
should "list only global hooks" do
@command.handle_options(['-g'])
@command.execute
assert_said(@command, "all gems:")
assert_said(@command, "- http://allgemshook.com")
assert_never_said(@command, "foo:")
assert_never_said(@command, "- http://foogemhook.com")
end
should "send get to api" do
@command.handle_options([])
@command.execute
# webmock doesn't pass body params on correctly :[
assert_requested(:get, "#{@api}.yaml",
:times => 1)
assert_requested(:get, "#{@api}.yaml",
:headers => { 'Authorization' => 'key' })
end
end
context "listing hooks with none available" do
setup do
stub_api_key("key")
stub_request(:get, "#{@api}.yaml").to_return(:body => "{}")
@command.handle_options([])
@command.execute
end
should "list hooks" do
assert_received(@command) do |command|
command.say("You haven't added any webhooks yet.")
end
end
end
context "listing hooks with a json error" do
setup do
stub(@command).terminate_interaction
stub_api_key("key")
stub_request(:get, "#{@api}.yaml").to_return(:body => "fubar")
@command.handle_options([])
@command.execute
end
should "dump out with error message" do
assert_received(@command) do |command|
command.say("There was a problem parsing the data:")
end
end
should "terminate interaction" do
assert_received(@command) do |command|
command.terminate_interaction
end
end
end
context "removing specific hooks" do
setup do
stub_api_key("key")
stub_request(:delete, "#{@api}/remove").to_return(:body => "Success!")
@command.handle_options([@gem, "-r", @url])
@command.execute
end
should "say hook was removed" do
assert_received(@command) do |command|
command.say("Removing webhook...")
command.say("Success!")
end
end
should "send delete to api" do
# webmock doesn't pass body params on correctly :[
assert_requested(:delete, "#{@api}/remove",
:times => 1)
assert_requested(:delete, "#{@api}/remove",
:headers => { 'Authorization' => 'key' })
end
end
context "removing global hooks" do
setup do
stub_api_key("key")
stub_request(:delete, "#{@api}/remove").to_return(:body => "Success!")
@command.handle_options(["-g", "-r", @url])
@command.execute
end
should "say hook was removed" do
assert_received(@command) do |command|
command.say("Removing webhook...")
command.say("Success!")
end
end
should "send delete to api" do
# webmock doesn't pass body params on correctly :[
assert_requested(:delete, "#{@api}/remove",
:times => 1)
assert_requested(:delete, "#{@api}/remove",
:headers => { 'Authorization' => 'key' })
end
end
context "test firing hooks" do
setup do
stub_api_key("key")
stub_request(:post, "#{@api}/fire").to_return(:body => "Success!")
@command.handle_options([@gem, "-f", @url])
@command.execute
end
should "say hook was fired" do
assert_received(@command) do |command|
command.say("Test firing webhook...")
command.say("Success!")
end
end
should "send post to api" do
# webmock doesn't pass body params on correctly :[
assert_requested(:post, "#{@api}/fire",
:times => 1)
assert_requested(:post, "#{@api}/fire",
:headers => { 'Authorization' => 'key' })
end
end
end
end