forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull_requests_spec.rb
More file actions
402 lines (337 loc) · 13.2 KB
/
pull_requests_spec.rb
File metadata and controls
402 lines (337 loc) · 13.2 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
require 'spec_helper'
describe Github::PullRequests do
let(:github) { Github.new }
let(:user) { 'peter-murach' }
let(:repo) { 'github' }
let(:pull_request_id) { 1 }
after { reset_authentication_for github }
describe "#list" do
it { should respond_to :all }
context 'resource found' do
let(:inputs) { { 'state'=> 'closed', 'unrelated' => true } }
before do
stub_get("/repos/#{user}/#{repo}/pulls").
with(:query => inputs.except('unrelated')).
to_return(:body => fixture('pull_requests/pull_requests.json'),
:status => 200,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "throws error if pull_request id not provided" do
expect { github.pull_requests.list nil}.to raise_error(ArgumentError)
end
it "should get the resources" do
github.pull_requests.list user, repo, inputs
a_get("/repos/#{user}/#{repo}/pulls").with(:query => inputs).should have_been_made
end
it "should return array of resources" do
pull_requests = github.pull_requests.list user, repo, inputs
pull_requests.should be_an Array
pull_requests.should have(1).items
end
it "should be a mash type" do
pull_requests = github.pull_requests.list user, repo, inputs
pull_requests.first.should be_a Hashie::Mash
end
it "should get pull request information" do
pull_requests = github.pull_requests.list user, repo, inputs
pull_requests.first.title.should == 'new-feature'
end
it "should yield to a block" do
github.pull_requests.should_receive(:list).with(user, repo).
and_yield('web')
github.pull_requests.list(user, repo) { |param| 'web' }
end
end
context 'resource not found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls").
to_return(:body => "", :status => [404, "Not Found"])
end
it "should return 404 with a message 'Not Found'" do
expect {
github.pull_requests.list user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # list
describe "#get" do
it { github.pull_requests.should respond_to :find }
context 'resource found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").
to_return(:body => fixture('pull_requests/pull_request.json'),
:status => 200,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource without pull_request id" do
expect { github.pull_requests.get nil }.to raise_error(ArgumentError)
end
it "should get the resource" do
github.pull_requests.get user, repo, pull_request_id
a_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").
should have_been_made
end
it "should get pull_request information" do
pull_request = github.pull_requests.get user, repo, pull_request_id
pull_request.number.should eq pull_request_id
pull_request.head.user.login.should == 'octocat'
end
it "should return mash" do
pull_request = github.pull_requests.get user, repo, pull_request_id
pull_request.should be_a Hashie::Mash
end
end
context 'resource not found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").
to_return(:body => fixture('pull_requests/pull_request.json'),
:status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to retrive resource" do
expect {
github.pull_requests.get user, repo, pull_request_id
}.to raise_error(Github::Error::NotFound)
end
end
end # get
describe "#create" do
let(:inputs) {
{
"title" => "Amazing new feature",
"body" => "Please pull this in!",
"head" => "octocat:new-feature",
"base" => "master",
"state" => "open",
'unrelated' => 'giberrish'
}
}
context "resouce created" do
before do
stub_post("/repos/#{user}/#{repo}/pulls").
with(inputs.except('unrelated')).
to_return(:body => fixture('pull_requests/pull_request.json'), :status => 201, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should create resource successfully" do
github.pull_requests.create user, repo, inputs
a_post("/repos/#{user}/#{repo}/pulls").with(inputs).should have_been_made
end
it "should return the resource" do
pull_request = github.pull_requests.create user, repo, inputs
pull_request.should be_a Hashie::Mash
end
it "should get the request information" do
pull_request = github.pull_requests.create user, repo, inputs
pull_request.title.should eql "new-feature"
end
end
context "failed to create resource" do
before do
stub_post("/repos/#{user}/#{repo}/pulls").with(inputs).
to_return(:body => fixture('pull_requests/pull_request.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should faile to retrieve resource" do
expect {
github.pull_requests.create user, repo, inputs
}.to raise_error(Github::Error::NotFound)
end
end
end # create
describe "#update" do
let(:inputs) {
{
"title" => "new title",
"body" => "updated body",
"state" => "open",
"unrelated" => true
}
}
context "resouce updateed" do
before do
stub_patch("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").
with(inputs.except('unrelated')).
to_return(:body => fixture('pull_requests/pull_request.json'),
:status => 201,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should create resource successfully" do
github.pull_requests.update user, repo, pull_request_id, inputs
a_patch("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").with(inputs).
should have_been_made
end
it "should return the resource" do
pull_request = github.pull_requests.update user, repo, pull_request_id, inputs
pull_request.should be_a Hashie::Mash
end
it "should get the pull_request information" do
pull_request = github.pull_requests.update user, repo, pull_request_id, inputs
pull_request.title.should == 'new-feature'
end
end
context "failed to create resource" do
before do
stub_patch("/repos/#{user}/#{repo}/pulls/#{pull_request_id}").
with(inputs).
to_return(:body => fixture('pull_requests/pull_request.json'),
:status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should faile to retrieve resource" do
expect {
github.pull_requests.update user, repo, pull_request_id, inputs
}.to raise_error(Github::Error::NotFound)
end
end
end # update
describe "#commits" do
context 'resource found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/commits").
to_return(:body => fixture('pull_requests/commits.json'),
:status => 200,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "throws error if pull_request_id not provided" do
expect {
github.pull_requests.commits user, repo, nil
}.to raise_error(ArgumentError)
end
it "should get the resources" do
github.pull_requests.commits user, repo, pull_request_id
a_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/commits").
should have_been_made
end
it "should return array of resources" do
pull_requests = github.pull_requests.commits user, repo, pull_request_id
pull_requests.should be_an Array
pull_requests.should have(1).items
end
it "should be a mash type" do
pull_requests = github.pull_requests.commits user, repo, pull_request_id
pull_requests.first.should be_a Hashie::Mash
end
it "should get pull request information" do
pull_requests = github.pull_requests.commits user, repo, pull_request_id
pull_requests.first.committer.name.should == 'Scott Chacon'
end
it "should yield to a block" do
github.pull_requests.should_receive(:commits).
with(user, repo, pull_request_id).and_yield('web')
github.pull_requests.commits(user, repo, pull_request_id) {|param| 'web' }
end
end
context 'resource not found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/commits").
to_return(:body => "", :status => [404, "Not Found"])
end
it "should return 404 with a message 'Not Found'" do
expect {
github.pull_requests.commits user, repo, pull_request_id
}.to raise_error(Github::Error::NotFound)
end
end
end # commits
describe "#files" do
it { github.pull_requests.should respond_to :files }
context 'resource found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/files").
to_return(:body => fixture('pull_requests/files.json'),
:status => 200,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should get the resources" do
github.pull_requests.files user, repo, pull_request_id
a_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/files").
should have_been_made
end
it "should return array of resources" do
pull_requests = github.pull_requests.files user, repo, pull_request_id
pull_requests.should be_an Array
pull_requests.should have(1).items
end
it "should be a mash type" do
pull_requests = github.pull_requests.files user, repo, pull_request_id
pull_requests.first.should be_a Hashie::Mash
end
it "should get pull request information" do
pull_requests = github.pull_requests.files user, repo, pull_request_id
pull_requests.first.filename.should == 'file1.txt'
end
it "should yield to a block" do
github.pull_requests.should_receive(:files).with(user, repo, pull_request_id).
and_yield('web')
github.pull_requests.files(user, repo, pull_request_id) { |param| 'web' }
end
end
context 'resource not found' do
before do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/files").
to_return(:body => "", :status => [404, "Not Found"])
end
it "should return 404 with a message 'Not Found'" do
expect {
github.pull_requests.files user, repo, pull_request_id
}.to raise_error(Github::Error::NotFound)
end
end
end # files
describe "#merged?" do
context "checks whetehr pull request has been merged" do
before do
github.oauth_token = nil
github.user = nil
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/merge").
to_return(:body => "", :status => 404, :headers => {:user_agent => github.user_agent})
end
it "should fail validation " do
expect {
github.pull_requests.merged?(nil, nil, pull_request_id)
}.to raise_error(ArgumentError)
end
it "should return false if resource not found" do
merged = github.pull_requests.merged? user, repo, pull_request_id
merged.should be_false
end
it "should return true if resoure found" do
stub_get("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/merge").
to_return(:body => "", :status => 200,
:headers => {:user_agent => github.user_agent})
merged = github.pull_requests.merged? user, repo, pull_request_id
merged.should be_true
end
end
end # merged?
describe "#merge" do
context 'successful' do
before do
stub_put("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/merge").
to_return(:body => fixture('pull_requests/merge_success.json'),
:status => 200,
:headers => {:user_agent => github.user_agent})
end
it 'performs request' do
github.pull_requests.merge user, repo, pull_request_id
a_put("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/merge").
should have_been_made
end
it 'response contains merge success flag' do
response = github.pull_requests.merge(user, repo, pull_request_id)
response.merged.should be_true
end
end
context 'cannot be performed' do
before do
stub_put("/repos/#{user}/#{repo}/pulls/#{pull_request_id}/merge").
to_return(:body => fixture('pull_requests/merge_failure.json'),
:status => 200,
:headers => {:user_agent => github.user_agent})
end
it 'response contains merge failure flag' do
response = github.pull_requests.merge(user, repo, pull_request_id)
response.merged.should be_false
end
end
end # merge
end # Github::PullRequests