github_api 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,9 +20,8 @@ describe Github::Repos::Watching do
20
20
  end
21
21
 
22
22
  it "should yield iterator if block given" do
23
- pending
24
- block = lambda { ['a', 'b', 'c'] }
25
- github.repos.watchers(user, repo, &block)
23
+ github.repos.should_receive(:watchers).with(user, repo).and_yield('github')
24
+ github.repos.watchers(user, repo) { |param| 'github' }
26
25
  end
27
26
 
28
27
  it "should get the resources" do
@@ -36,6 +35,11 @@ describe Github::Repos::Watching do
36
35
  watchers.should have(1).items
37
36
  end
38
37
 
38
+ it "should return result of mash type" do
39
+ watchers = github.repos.watchers user, repo
40
+ watchers.first.should be_a Hashie::Mash
41
+ end
42
+
39
43
  it "should get watcher information" do
40
44
  watchers = github.repos.watchers(user, repo)
41
45
  watchers.first.login.should == 'octocat'
@@ -3,36 +3,569 @@ require 'spec_helper'
3
3
  describe Github::Repos do
4
4
 
5
5
  let(:github) { Github.new }
6
- let(:repo) { mock('object').as_null_object }
6
+ let(:user) { 'peter-murach' }
7
+ let(:repo) { 'github' }
7
8
 
8
- before do
9
- github.stub(:repos).and_return(repo)
10
- end
9
+ describe "branches" do
10
+ context "resource found" do
11
+ before do
12
+ stub_get("/repos/#{user}/#{repo}/branches").
13
+ to_return(:body => fixture('repos/branches.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
14
+ end
15
+
16
+ it "should raise error when no user/repo parameters" do
17
+ github.user, github.repo = nil, nil
18
+ expect {
19
+ github.repos.branches
20
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
21
+ end
22
+
23
+ it "should raise error when no repository" do
24
+ github.user, github.repo = nil, nil
25
+ expect {
26
+ github.repos.branches user
27
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
28
+ end
29
+
30
+ it "should find resources" do
31
+ github.repos.branches user, repo
32
+ a_get("/repos/#{user}/#{repo}/branches").should have_been_made
33
+ end
11
34
 
12
- context "branches" do
35
+ it "should return array of resources" do
36
+ branches = github.repos.branches user, repo
37
+ branches.should be_an Array
38
+ branches.should have(1).items
39
+ end
13
40
 
14
- before do
15
- @branches = []
16
- repo.stub(:branches).and_return([])
41
+ it "should get branch information" do
42
+ branches = github.repos.branches user, repo
43
+ branches.first.name.should == 'master'
44
+ end
45
+
46
+ it "should yield to a block" do
47
+ github.repos.should_receive(:branches).with(user, repo).and_yield('web')
48
+ github.repos.branches(user, repo) { |param| 'web'}
49
+ end
17
50
  end
18
51
 
19
- it "should raise error when no user" do
20
- pending
21
- expect {
22
- Github.new.repos.branches
23
- }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
52
+ context "resource not found" do
53
+ before do
54
+ stub_get("/repos/#{user}/#{repo}/branches").
55
+ to_return(:body => fixture('repos/branches.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
56
+
57
+ end
58
+
59
+ it "should fail to get resource" do
60
+ expect {
61
+ github.repos.branches user, repo
62
+ }.to raise_error(Github::ResourceNotFound)
63
+ end
24
64
  end
65
+ end # branches
66
+
67
+ describe "contributors" do
68
+ context "resource found" do
69
+ before do
70
+ stub_get("/repos/#{user}/#{repo}/contributors").
71
+ to_return(:body => fixture('repos/contributors.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
72
+ end
25
73
 
26
- it "should raise error when no repo" do
27
- pending
28
- expect {
29
- Github.new(:user => 'peter-murach').repos.branches
30
- }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
74
+ it "should raise error when no user/repo parameters" do
75
+ github.user, github.repo = nil, nil
76
+ expect {
77
+ github.repos.contributors
78
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
79
+ end
80
+
81
+ it "should raise error when no repository" do
82
+ github.user, github.repo = nil, nil
83
+ expect {
84
+ github.repos.contributors user
85
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
86
+ end
87
+
88
+ it "should find resources" do
89
+ github.repos.contributors user, repo
90
+ a_get("/repos/#{user}/#{repo}/contributors").should have_been_made
91
+ end
92
+
93
+ it "should return array of resources" do
94
+ contributors = github.repos.contributors user, repo
95
+ contributors.should be_an Array
96
+ contributors.should have(1).items
97
+ end
98
+
99
+ it "should get branch information" do
100
+ contributors = github.repos.contributors user, repo
101
+ contributors.first.login.should == 'octocat'
102
+ end
103
+
104
+ it "should yield to a block" do
105
+ github.repos.should_receive(:contributors).with(user, repo).and_yield('web')
106
+ github.repos.contributors(user, repo) { |param| 'web'}
107
+ end
31
108
  end
32
109
 
33
- it "should list all branches" do
34
- pending
35
- github.repos.should_receive(:branches).and_return(@branches)
110
+ context "resource not found" do
111
+ before do
112
+ stub_get("/repos/#{user}/#{repo}/contributors").
113
+ to_return(:body => fixture('repos/contributors.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
114
+
115
+ end
116
+
117
+ it "should fail to get resource" do
118
+ expect {
119
+ github.repos.contributors user, repo
120
+ }.to raise_error(Github::ResourceNotFound)
121
+ end
122
+ end
123
+ end # contributors
124
+
125
+ describe "create_repo" do
126
+ let(:inputs) { {:name => 'web', :description => "This is your first repo", :homepage => "https://github.com", :public => true, :has_issues => true, :has_wiki => true}}
127
+
128
+ context "resource created successfully for the authenticated user" do
129
+ before do
130
+ github.user = nil
131
+ github.oauth_token = OAUTH_TOKEN
132
+ stub_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
133
+ to_return(:body => fixture('repos/repo.json'), :status => 201,:headers => {:content_type => "application/json; charset=utf-8"} )
134
+ end
135
+
136
+ after do
137
+ github.user, github.oauth_token = nil, nil
138
+ end
139
+
140
+ it "should faile to create resource if 'name' inputs is missing" do
141
+ expect {
142
+ github.repos.create_repo inputs.except(:name)
143
+ }.to raise_error(ArgumentError)
144
+ end
145
+
146
+ it "should create resource" do
147
+ github.repos.create_repo inputs
148
+ a_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).should have_been_made
149
+ end
150
+
151
+ it "should return the resource" do
152
+ repository = github.repos.create_repo inputs
153
+ repository.name.should == 'Hello-World'
154
+ end
155
+
156
+ it "should return mash type" do
157
+ repository = github.repos.create_repo inputs
158
+ repository.should be_a Hashie::Mash
159
+ end
160
+ end
161
+
162
+ context "resource created for the authenticated user belonging to organization" do
163
+ let(:org) { '37signals' }
164
+ before do
165
+ github.user = nil
166
+ github.oauth_token = OAUTH_TOKEN
167
+ stub_post("/orgs/#{org}/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
168
+ to_return(:body => fixture('repos/repo.json'), :status => 201,:headers => {:content_type => "application/json; charset=utf-8"} )
169
+ end
170
+
171
+ after do
172
+ github.user, github.oauth_token = nil, nil
173
+ end
174
+
175
+ it "should get the resource" do
176
+ github.repos.create_repo inputs.merge(:org => org)
177
+ a_post("/orgs/#{org}/repos?access_token=#{OAUTH_TOKEN}").with(inputs).should have_been_made
178
+ end
179
+ end
180
+
181
+ context "failed to create" do
182
+ before do
183
+ github.user = nil
184
+ github.oauth_token = OAUTH_TOKEN
185
+ stub_post("/user/repos?access_token=#{OAUTH_TOKEN}").with(inputs).
186
+ to_return(:body => '', :status => 404,:headers => {:content_type => "application/json; charset=utf-8"} )
187
+ end
188
+
189
+ after do
190
+ github.user, github.oauth_token = nil, nil
191
+ end
192
+
193
+ it "should faile to retrieve resource" do
194
+ expect {
195
+ github.repos.create_repo inputs
196
+ }.to raise_error(Github::ResourceNotFound)
197
+ end
36
198
  end
37
199
  end
38
- end
200
+
201
+ describe "edit_repo" do
202
+ let(:inputs) { {:name => 'web', :description => "This is your first repo", :homepage => "https://github.com", :public => true, :has_issues => true, :has_wiki => true}}
203
+
204
+ context "resource edited successfully" do
205
+ before do
206
+ stub_patch("/repos/#{user}/#{repo}").with(inputs).
207
+ to_return(:body => fixture("repos/repo.json"), :status => 200, :headers => { :content_type => "application/json; charset=utf-8"})
208
+ end
209
+
210
+ it "should fail to edit without 'user/repo' parameters" do
211
+ github.user, github.repo = nil, nil
212
+ expect { github.repos.edit_repo }.to raise_error(ArgumentError)
213
+ end
214
+
215
+ it "should fail to edit resource without 'name' parameter" do
216
+ expect{
217
+ github.repos.edit_hook user, repo, inputs.except(:name)
218
+ }.to raise_error(ArgumentError)
219
+ end
220
+
221
+ it "should edit the resource" do
222
+ github.repos.edit_repo user, repo, inputs
223
+ a_patch("/repos/#{user}/#{repo}").with(inputs).should have_been_made
224
+ end
225
+
226
+ it "should return resource" do
227
+ repository = github.repos.edit_repo user, repo, inputs
228
+ repository.should be_a Hashie::Mash
229
+ end
230
+
231
+ it "should be able to retrieve information" do
232
+ repository = github.repos.edit_repo user, repo, inputs
233
+ repository.name.should == 'Hello-World'
234
+ end
235
+
236
+ end
237
+
238
+ context "failed to edit resource" do
239
+ before do
240
+ stub_patch("/repos/#{user}/#{repo}").with(inputs).
241
+ to_return(:body => fixture("repos/repo.json"), :status => 404, :headers => { :content_type => "application/json; charset=utf-8"})
242
+
243
+ end
244
+
245
+ it "should fail to find resource" do
246
+ expect {
247
+ github.repos.edit_repo user, repo, inputs
248
+ }.to raise_error(Github::ResourceNotFound)
249
+ end
250
+ end
251
+
252
+ end # edit_repo
253
+
254
+ describe "get_repo" do
255
+ context "resource found" do
256
+ before do
257
+ stub_get("/repos/#{user}/#{repo}").
258
+ to_return(:body => fixture('repos/repo.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
259
+ end
260
+
261
+ it "should raise error when no user/repo parameters" do
262
+ github.user, github.repo = nil, nil
263
+ expect {
264
+ github.repos.get_repo
265
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
266
+ end
267
+
268
+ it "should raise error when no repository" do
269
+ github.user, github.repo = nil, nil
270
+ expect {
271
+ github.repos.get_repo user
272
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
273
+ end
274
+
275
+ it "should find resources" do
276
+ github.repos.get_repo user, repo
277
+ a_get("/repos/#{user}/#{repo}").should have_been_made
278
+ end
279
+
280
+ it "should return repository mash" do
281
+ repository = github.repos.get_repo user, repo
282
+ repository.should be_a Hashie::Mash
283
+ end
284
+
285
+ it "should get repository information" do
286
+ repository = github.repos.get_repo user, repo
287
+ repository.name.should == 'Hello-World'
288
+ end
289
+ end
290
+
291
+ context "resource not found" do
292
+ before do
293
+ stub_get("/repos/#{user}/#{repo}").
294
+ to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
295
+ end
296
+
297
+ it "should fail to get resource" do
298
+ expect {
299
+ github.repos.get_repo user, repo
300
+ }.to raise_error(Github::ResourceNotFound)
301
+ end
302
+
303
+ end
304
+ end # get_repo
305
+
306
+ describe "languages" do
307
+ context "resource found" do
308
+ before do
309
+ stub_get("/repos/#{user}/#{repo}/languages").
310
+ to_return(:body => fixture('repos/languages.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
311
+ end
312
+
313
+ it "should raise error when no user/repo parameters" do
314
+ github.user, github.repo = nil, nil
315
+ expect {
316
+ github.repos.languages
317
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
318
+ end
319
+
320
+ it "should raise error when no repository" do
321
+ github.user, github.repo = nil, nil
322
+ expect {
323
+ github.repos.languages user
324
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
325
+ end
326
+
327
+ it "should find resources" do
328
+ github.repos.languages user, repo
329
+ a_get("/repos/#{user}/#{repo}/languages").should have_been_made
330
+ end
331
+
332
+ it "should return hash of languages" do
333
+ languages = github.repos.languages user, repo
334
+ languages.should be_an Hash
335
+ languages.should have(2).keys
336
+ end
337
+
338
+ it "should get language information" do
339
+ languages = github.repos.languages user, repo
340
+ languages.keys.first.should == 'Ruby'
341
+ end
342
+
343
+ it "should yield to a block" do
344
+ github.repos.should_receive(:languages).with(user, repo).and_yield('web')
345
+ github.repos.languages(user, repo) { |param| 'web'}
346
+ end
347
+ end
348
+
349
+ context "resource not found" do
350
+ before do
351
+ stub_get("/repos/#{user}/#{repo}/languages").
352
+ to_return(:body => '', :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
353
+
354
+ end
355
+
356
+ it "should fail to get resource" do
357
+ expect {
358
+ github.repos.languages user, repo
359
+ }.to raise_error(Github::ResourceNotFound)
360
+ end
361
+ end
362
+ end # languages
363
+
364
+ describe "repos" do
365
+ context "resource found for authenticated user" do
366
+ before do
367
+ github.user = nil
368
+ github.oauth_token = OAUTH_TOKEN
369
+ stub_get("/user/repos?access_token=#{OAUTH_TOKEN}").
370
+ to_return(:body => fixture('repos/repos.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
371
+ end
372
+
373
+ after do
374
+ github.oauth_token = nil
375
+ github.user, github.repo = nil, nil
376
+ end
377
+
378
+ it "should faile if user unauthenticated" do
379
+ github.oauth_token = nil
380
+ stub_get("/user/repos").
381
+ to_return(:body => '', :status => 401,:headers => {:content_type => "application/json; charset=utf-8"} )
382
+ expect { github.repos.repos}.to raise_error(Github::Unauthorised)
383
+ end
384
+
385
+ it "should get the resources" do
386
+ github.repos.repos
387
+ a_get("/user/repos?access_token=#{OAUTH_TOKEN}").should have_been_made
388
+ end
389
+
390
+ it "should return array of resources" do
391
+ repositories = github.repos.repos
392
+ repositories.should be_an Array
393
+ repositories.should have(1).items
394
+ end
395
+
396
+ it "should get resource information" do
397
+ repositories = github.repos.repos
398
+ repositories.first.name.should == 'Hello-World'
399
+ end
400
+
401
+ it "should yield repositories to a block" do
402
+ github.repos.should_receive(:repos).and_yield('octocat')
403
+ github.repos.repos { |repo| 'octocat' }
404
+ end
405
+ end
406
+
407
+ context "resource found for organization" do
408
+ let(:org) { '37signals' }
409
+
410
+ before do
411
+ github.user = nil
412
+ github.oauth_token = nil
413
+ stub_get("/orgs/#{org}/repos").
414
+ to_return(:body => fixture('repos/repos.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
415
+ end
416
+
417
+ it "should get the resources" do
418
+ github.repos.repos :org => org
419
+ a_get("/orgs/#{org}/repos").should have_been_made
420
+ end
421
+
422
+ end
423
+
424
+ context "resource found for organization" do
425
+ before do
426
+ stub_get("/users/#{user}/repos").
427
+ to_return(:body => fixture('repos/repos.json'), :status => 200,:headers => {:content_type => "application/json; charset=utf-8"} )
428
+ end
429
+
430
+ it "should get the resources" do
431
+ github.repos.repos :user => user
432
+ a_get("/users/#{user}/repos").should have_been_made
433
+ end
434
+ end
435
+
436
+ context "rosource not found for authenticated user" do
437
+ before do
438
+ github.user = nil
439
+ github.oauth_token = OAUTH_TOKEN
440
+ stub_get("/user/repos?access_token=#{OAUTH_TOKEN}").
441
+ to_return(:body => '', :status => 404,:headers => {:content_type => "application/json; charset=utf-8"} )
442
+ end
443
+
444
+ after do
445
+ github.oauth_token = nil
446
+ github.user, github.repo = nil, nil
447
+ end
448
+
449
+ it "fail to find resources" do
450
+ expect { github.repos.repos }.to raise_error(Github::ResourceNotFound)
451
+ end
452
+ end
453
+ end # repos
454
+
455
+ describe "tags" do
456
+ context "resource found" do
457
+ before do
458
+ stub_get("/repos/#{user}/#{repo}/tags").
459
+ to_return(:body => fixture('repos/tags.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
460
+ end
461
+
462
+ it "should raise error when no user/repo parameters" do
463
+ github.user, github.repo = nil, nil
464
+ expect {
465
+ github.repos.tags
466
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
467
+ end
468
+
469
+ it "should raise error when no repository" do
470
+ github.user, github.repo = nil, nil
471
+ expect {
472
+ github.repos.tags user
473
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
474
+ end
475
+
476
+ it "should find resources" do
477
+ github.repos.tags user, repo
478
+ a_get("/repos/#{user}/#{repo}/tags").should have_been_made
479
+ end
480
+
481
+ it "should return array of resources" do
482
+ tags = github.repos.tags user, repo
483
+ tags.should be_an Array
484
+ tags.should have(1).items
485
+ end
486
+
487
+ it "should get tag information" do
488
+ tags = github.repos.tags user, repo
489
+ tags.first.name.should == 'v0.1'
490
+ end
491
+
492
+ it "should yield to a block" do
493
+ github.repos.should_receive(:tags).with(user, repo).and_yield('web')
494
+ github.repos.tags(user, repo) { |param| 'web'}
495
+ end
496
+ end
497
+
498
+ context "resource not found" do
499
+ before do
500
+ stub_get("/repos/#{user}/#{repo}/tags").
501
+ to_return(:body => fixture('repos/branches.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
502
+
503
+ end
504
+
505
+ it "should fail to get resource" do
506
+ expect {
507
+ github.repos.tags user, repo
508
+ }.to raise_error(Github::ResourceNotFound)
509
+ end
510
+ end
511
+ end #tags
512
+
513
+ describe "teams" do
514
+ context "resource found" do
515
+ before do
516
+ stub_get("/repos/#{user}/#{repo}/teams").
517
+ to_return(:body => fixture('repos/teams.json'), :status => 200, :headers => {:content_type => "application/json; charset=utf-8"})
518
+ end
519
+
520
+ it "should raise error when no user/repo parameters" do
521
+ github.user, github.repo = nil, nil
522
+ expect {
523
+ github.repos.teams
524
+ }.to raise_error(ArgumentError, /\[user\] parameter cannot be nil/)
525
+ end
526
+
527
+ it "should raise error when no repository" do
528
+ github.user, github.repo = nil, nil
529
+ expect {
530
+ github.repos.teams user
531
+ }.to raise_error(ArgumentError, /\[repo\] parameter cannot be nil/)
532
+ end
533
+
534
+ it "should find resources" do
535
+ github.repos.teams user, repo
536
+ a_get("/repos/#{user}/#{repo}/teams").should have_been_made
537
+ end
538
+
539
+ it "should return array of resources" do
540
+ teams = github.repos.teams user, repo
541
+ teams.should be_an Array
542
+ teams.should have(1).items
543
+ end
544
+
545
+ it "should get branch information" do
546
+ teams = github.repos.teams user, repo
547
+ teams.first.name.should == 'Owners'
548
+ end
549
+
550
+ it "should yield to a block" do
551
+ github.repos.should_receive(:teams).with(user, repo).and_yield('web')
552
+ github.repos.teams(user, repo) { |param| 'web'}
553
+ end
554
+ end
555
+
556
+ context "resource not found" do
557
+ before do
558
+ stub_get("/repos/#{user}/#{repo}/teams").
559
+ to_return(:body => fixture('repos/teams.json'), :status => 404, :headers => {:content_type => "application/json; charset=utf-8"})
560
+
561
+ end
562
+
563
+ it "should fail to get resource" do
564
+ expect {
565
+ github.repos.teams user, repo
566
+ }.to raise_error(Github::ResourceNotFound)
567
+ end
568
+ end
569
+ end # teams
570
+
571
+ end # Github::Repos