forked from piotrmurach/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepos_spec.rb
More file actions
637 lines (532 loc) · 20.6 KB
/
repos_spec.rb
File metadata and controls
637 lines (532 loc) · 20.6 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# encoding: utf-8
require 'spec_helper'
describe Github::Repos do
let(:github) { Github.new }
let(:user) { 'peter-murach' }
let(:repo) { 'github' }
after { reset_authentication_for github }
its(:collaborators) { should be_a Github::Repos::Collaborators }
its(:commits) { should be_a Github::Repos::Commits }
its(:downloads) { should be_a Github::Repos::Downloads }
its(:forks) { should be_a Github::Repos::Forks }
its(:hooks) { should be_a Github::Repos::Hooks }
its(:keys) { should be_a Github::Repos::Keys }
its(:watching) { should be_a Github::Repos::Watching }
its(:pubsubhubbub) { should be_a Github::Repos::PubSubHubbub }
describe "#branches" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/branches").
to_return(:body => fixture('repos/branches.json'),
:status => 200,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
expect {
github.repos.branches nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
expect {
github.repos.branches user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.branches user, repo
a_get("/repos/#{user}/#{repo}/branches").should have_been_made
end
it "should return array of resources" do
branches = github.repos.branches user, repo
branches.should be_an Array
branches.should have(1).items
end
it "should get branch information" do
branches = github.repos.branches user, repo
branches.first.name.should == 'master'
end
it "should yield to a block" do
block = lambda { |el| repo }
github.repos.should_receive(:branches).with(user, repo).and_yield repo
github.repos.branches(user, repo, &block)
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/branches").
to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.branches user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # branches
describe "#branch" do
let(:branch) { 'master' }
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/branches/#{branch}").
to_return(:body => fixture('repos/branch.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should find resources" do
github.repos.branch user, repo, branch
a_get("/repos/#{user}/#{repo}/branches/#{branch}").should have_been_made
end
it "should return repository mash" do
repo_branch = github.repos.branch user, repo, branch
repo_branch.should be_a Hashie::Mash
end
it "should get repository branch information" do
repo_branch = github.repos.branch user, repo, branch
repo_branch.name.should == 'master'
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/branches/#{branch}").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.branch user, repo, branch
}.to raise_error(Github::Error::NotFound)
end
end
end # branch
describe "contributors" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/contributors").
to_return(:body => fixture('repos/contributors.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
expect {
github.repos.contributors nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
expect {
github.repos.contributors user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.contributors user, repo
a_get("/repos/#{user}/#{repo}/contributors").should have_been_made
end
it "should return array of resources" do
contributors = github.repos.contributors user, repo
contributors.should be_an Array
contributors.should have(1).items
end
it "should get branch information" do
contributors = github.repos.contributors user, repo
contributors.first.login.should == 'octocat'
end
it "should yield to a block" do
github.repos.should_receive(:contributors).with(user, repo).and_yield('web')
github.repos.contributors(user, repo) { |param| 'web'}
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/contributors").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.contributors user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # contributors
describe "#create" do
let(:inputs) { {:name => 'web', :description => "This is your first repo", :homepage => "https://github.com", :public => true, :has_issues => true, :has_wiki => true}}
context "resource created successfully for the authenticated user" do
before do
github.oauth_token = OAUTH_TOKEN
stub_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
to_return(:body => fixture('repos/repo.json'), :status => 201,
:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "should faile to create resource if 'name' inputs is missing" do
expect {
github.repos.create inputs.except(:name)
}.to raise_error(Github::Error::RequiredParams)
end
it "should create resource" do
github.repos.create inputs
a_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).should have_been_made
end
it "should return the resource" do
repository = github.repos.create inputs
repository.name.should == 'Hello-World'
end
it "should return mash type" do
repository = github.repos.create inputs
repository.should be_a Hashie::Mash
end
end
context "resource created for the authenticated user belonging to organization" do
let(:org) { '37signals' }
before do
github.oauth_token = OAUTH_TOKEN
stub_post("/orgs/#{org}/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
to_return(:body => fixture('repos/repo.json'), :status => 201,:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "should get the resource" do
github.repos.create inputs.merge(:org => org)
a_post("/orgs/#{org}/repos?access_token=#{OAUTH_TOKEN}").with(inputs).should have_been_made
end
end
context "failed to create" do
before do
github.oauth_token = OAUTH_TOKEN
stub_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
to_return(:body => '', :status => 404,:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "should faile to retrieve resource" do
expect {
github.repos.create inputs
}.to raise_error(Github::Error::NotFound)
end
end
end
describe "#delete" do
before do
stub_delete("/repos/#{user}/#{repo}").
to_return(:body => '', :status => 204,
:headers => { :content_type => "application/json; charset=utf-8"})
end
it { should respond_to :remove }
it "should delete the resource successfully" do
github.repos.delete user, repo
a_delete("/repos/#{user}/#{repo}").should have_been_made
end
it "should fail to delete resource without 'user' parameter" do
expect{
github.repos.delete nil, repo
}.to raise_error(ArgumentError)
end
it "should fail to delete resource without 'repo' parameter" do
expect{
github.repos.delete user, nil
}.to raise_error(ArgumentError)
end
it "should fail to delete resource that is not found" do
stub_delete("/repos/#{user}/#{repo}").
to_return(:body => '', :status => 404,
:headers => { :content_type => "application/json; charset=utf-8"})
expect {
github.repos.delete user, repo
}.to raise_error(Github::Error::NotFound)
end
end # delete
describe "#edit" do
let(:inputs) do
{ :name => 'web',
:description => "This is your first repo",
:homepage => "https://github.com",
:private => false,
:has_issues => true,
:has_wiki => true }
end
context "resource edited successfully" do
before do
stub_patch("/repos/#{user}/#{repo}").with(inputs).
to_return(:body => fixture("repos/repo.json"), :status => 200,
:headers => { :content_type => "application/json; charset=utf-8"})
end
it "should fail to edit without 'user/repo' parameters" do
expect { github.repos.edit user, nil }.to raise_error(ArgumentError)
end
it "should fail to edit resource without 'name' parameter" do
expect{
github.repos.edit user, repo, inputs.except(:name)
}.to raise_error(Github::Error::RequiredParams)
end
it "should edit the resource" do
github.repos.edit user, repo, inputs
a_patch("/repos/#{user}/#{repo}").with(inputs).should have_been_made
end
it "should return resource" do
repository = github.repos.edit user, repo, inputs
repository.should be_a Hashie::Mash
end
it "should be able to retrieve information" do
repository = github.repos.edit user, repo, inputs
repository.name.should == 'Hello-World'
end
end
context "failed to edit resource" do
before do
stub_patch("/repos/#{user}/#{repo}").with(inputs).
to_return(:body => fixture("repos/repo.json"), :status => 404,
:headers => { :content_type => "application/json; charset=utf-8"})
end
it "should fail to find resource" do
expect {
github.repos.edit user, repo, inputs
}.to raise_error(Github::Error::NotFound)
end
end
end # edit
describe "#get" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}").
to_return(:body => fixture('repos/repo.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
# github.user, github.repo = nil, nil
expect {
github.repos.get nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
# github.user, github.repo = nil, nil
expect {
github.repos.get user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.get user, repo
a_get("/repos/#{user}/#{repo}").should have_been_made
end
it "should return repository mash" do
repository = github.repos.get user, repo
repository.should be_a Hashie::Mash
end
it "should get repository information" do
repository = github.repos.get user, repo
repository.name.should == 'Hello-World'
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.get user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # get
describe "#languages" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/languages").
to_return(:body => fixture('repos/languages.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
expect {
github.repos.languages nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
expect {
github.repos.languages user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.languages user, repo
a_get("/repos/#{user}/#{repo}/languages").should have_been_made
end
it "should return hash of languages" do
languages = github.repos.languages user, repo
languages.should be_an Hash
languages.should have(2).keys
end
it "should get language information" do
languages = github.repos.languages user, repo
languages.keys.first.should == 'Ruby'
end
it "should yield to a block" do
github.repos.should_receive(:languages).with(user, repo).and_yield('web')
github.repos.languages(user, repo) { |param| 'web'}
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/languages").
to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.languages user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # languages
describe "#list" do
context "resource found for authenticated user" do
before do
github.oauth_token = OAUTH_TOKEN
stub_get("/user/repos?access_token=#{OAUTH_TOKEN}").
to_return(:body => fixture('repos/repos.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
stub_get("/user/repos?access_token=#{OAUTH_TOKEN}&sort=pushed").
to_return(:body => fixture('repos/repos_sorted_by_pushed.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "fails if user is unauthenticated" do
github.oauth_token = nil
stub_get("/user/repos").
to_return(:body => '', :status => 401,:headers => {:content_type => "application/json; charset=utf-8"} )
expect { github.repos.list }.to raise_error(Github::Error::Unauthorized)
end
it "should get the resources" do
github.repos.list
a_get("/user/repos?access_token=#{OAUTH_TOKEN}").should have_been_made
end
it "should return array of resources" do
repositories = github.repos.list
repositories.should be_an Array
repositories.should have(1).items
end
it "should return array of resources sorted by pushed_at time" do
repositories = github.repos.list(:sort => 'pushed')
repositories.first.name.should == "Hello-World-2"
end
it "should get resource information" do
repositories = github.repos.list
repositories.first.name.should == 'Hello-World'
end
it "should yield repositories to a block" do
github.repos.should_receive(:list).and_yield('octocat')
github.repos.list { |repo| 'octocat' }
end
end
context "resource found for organization" do
let(:org) { '37signals' }
before do
stub_get("/orgs/#{org}/repos").
to_return(:body => fixture('repos/repos.json'), :status => 200,
:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "should get the resources" do
github.repos.list :org => org
a_get("/orgs/#{org}/repos").should have_been_made
end
end
context "resource found for organization" do
before do
stub_get("/users/#{user}/repos").
to_return(:body => fixture('repos/repos.json'), :status => 200,
:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "should get the resources" do
github.repos.list :user => user
a_get("/users/#{user}/repos").should have_been_made
end
end
context "rosource not found for authenticated user" do
before do
github.oauth_token = OAUTH_TOKEN
stub_get("/user/repos?access_token=#{OAUTH_TOKEN}").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"} )
end
it "fail to find resources" do
expect { github.repos.list }.to raise_error(Github::Error::NotFound)
end
end
end # list
describe "tags" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/tags").
to_return(:body => fixture('repos/tags.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
expect {
github.repos.tags nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
expect {
github.repos.tags user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.tags user, repo
a_get("/repos/#{user}/#{repo}/tags").should have_been_made
end
it "should return array of resources" do
tags = github.repos.tags user, repo
tags.should be_an Array
tags.should have(1).items
end
it "should get tag information" do
tags = github.repos.tags user, repo
tags.first.name.should == 'v0.1'
end
it "should yield to a block" do
github.repos.should_receive(:tags).with(user, repo).and_yield('web')
github.repos.tags(user, repo) { |param| 'web'}
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/tags").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.tags user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # tags
describe "#teams" do
context "resource found" do
before do
stub_get("/repos/#{user}/#{repo}/teams").
to_return(:body => fixture('repos/teams.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
end
it "should raise error when no user/repo parameters" do
expect {
github.repos.teams nil, repo
}.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
end
it "should raise error when no repository" do
expect {
github.repos.teams user, nil
}.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
end
it "should find resources" do
github.repos.teams user, repo
a_get("/repos/#{user}/#{repo}/teams").should have_been_made
end
it "should return array of resources" do
teams = github.repos.teams user, repo
teams.should be_an Array
teams.should have(1).items
end
it "should get branch information" do
teams = github.repos.teams user, repo
teams.first.name.should == 'Owners'
end
it "should yield to a block" do
github.repos.should_receive(:teams).with(user, repo).and_yield('web')
github.repos.teams(user, repo) { |param| 'web'}
end
end
context "resource not found" do
before do
stub_get("/repos/#{user}/#{repo}/teams").
to_return(:body => '', :status => 404,
:headers => {:content_type => "application/json; charset=utf-8"})
end
it "should fail to get resource" do
expect {
github.repos.teams user, repo
}.to raise_error(Github::Error::NotFound)
end
end
end # teams
end # Github::Repos