ghee 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ghee::API::Orgs::Teams do
4
+ subject { Ghee.new(GH_AUTH) }
5
+
6
+ def should_be_a_team(team)
7
+ team['url'].should include('teams')
8
+ team['permission'].should_not be_nil
9
+ team['name'].should_not be_nil
10
+ end
11
+
12
+ describe "#orgs#teams" do
13
+ context "with a test team" do
14
+ before :all do
15
+ VCR.use_cassette "orgs.teams.create.test" do
16
+ @test_team = subject.orgs(GH_ORG).teams.create({
17
+ :name => "test_team"
18
+ })
19
+ @test_team.should_not be_nil
20
+ end
21
+ end
22
+ let(:test_team) {@test_team}
23
+
24
+ context "with a member test team" do
25
+ before :all do
26
+ VCR.use_cassette "orgs.teams.create.test_member" do
27
+ @member_team = subject.orgs(GH_ORG).teams.create({
28
+ :name => "test_team_with_members"
29
+ })
30
+ @member_team.should_not be_nil
31
+ end
32
+ end
33
+ after :all do
34
+ VCR.use_cassette "orgs.teams.destroy.test_member" do
35
+ subject.orgs(GH_ORG).teams(member_team["id"]).destroy
36
+ end
37
+ end
38
+ let(:member_team) {@member_team}
39
+ describe "#members" do
40
+ it "should return members" do
41
+ VCR.use_cassette "orgs(name).teams(id).members" do
42
+ members = subject.orgs.teams(member_team["id"]).members
43
+ members.size.should == 0
44
+ end
45
+ end
46
+ it "should add a member" do
47
+ VCR.use_cassette "orgs(name).teams(id).members.add" do
48
+ subject.orgs.teams(member_team["id"]).members.add(GH_USER).should be_true
49
+ members = subject.orgs.teams(member_team["id"]).members
50
+ members.first["login"].should == GH_USER
51
+ members.size.should > 0
52
+ end
53
+ end
54
+ it "should remove a member" do
55
+ VCR.use_cassette "orgs(name).teams(id).members.remove" do
56
+ subject.orgs.teams(member_team["id"]).members.remove(GH_USER).should be_true
57
+ members = subject.orgs.teams(member_team["id"]).members
58
+ members.size.should == 0
59
+ end
60
+ end
61
+ end
62
+ end
63
+ it "should return a team" do
64
+ VCR.use_cassette "orgs.teams" do
65
+ teams = subject.orgs(GH_ORG).teams
66
+ teams.size.should > 0
67
+ end
68
+ end
69
+
70
+ it "should patch the team" do
71
+ VCR.use_cassette "orgs.teams.patch" do
72
+ team = subject.orgs(GH_ORG).teams(test_team['id']).patch({
73
+ :name => "herpderp"
74
+ })
75
+ should_be_a_team team
76
+ team["name"].should == "herpderp"
77
+ end
78
+
79
+ end
80
+
81
+ it "should destroy the team" do
82
+ VCR.use_cassette "orgs.teams.destroy" do
83
+ team = subject.orgs(GH_ORG).teams(test_team['id']).destroy.should be_true
84
+ end
85
+ end
86
+ end
87
+
88
+ end
89
+ end
90
+
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ghee::API::Users do
4
+ subject { Ghee.new(GH_AUTH) }
5
+
6
+ describe "#user" do
7
+ it "should return authenticated user" do
8
+ VCR.use_cassette('user') do
9
+ user = subject.user
10
+ user['type'].should == 'User'
11
+ user['email'].should_not be_nil
12
+ user['created_at'].should_not be_nil
13
+ user['html_url'].should include('https://github.com/')
14
+ user['name'].should be_instance_of(String)
15
+ user['login'].size.should > 0
16
+ end
17
+ end
18
+
19
+ # not sure how to write this test so that it doesn't mess
20
+ # up users info, so I'm just going to take the bio and add
21
+ # a space to the end of it
22
+ describe "#patch" do
23
+ it "should patch the user" do
24
+ VCR.use_cassette('user.patch') do
25
+ before_user = subject.user
26
+ after_user = subject.user.patch({
27
+ :bio => "#{before_user['bio']} "
28
+ })
29
+ after_user['bio'].should == "#{before_user['bio']} "
30
+ end
31
+ end
32
+ end
33
+
34
+ # clearly this test is going to fail if the authenticated user isn't part of any organizations
35
+ describe "#orgs" do
36
+ it "should return list of orgs" do
37
+ VCR.use_cassette "user.orgs" do
38
+ orgs = subject.user.orgs
39
+ orgs.size.should > 0
40
+ orgs.first["url"].should include("https://api.github.com/orgs")
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "#repos :type => 'public'" do
46
+ it "should only return public repos" do
47
+ VCR.use_cassette "user.repos.public" do
48
+ repos = subject.user.repos :type => "public"
49
+ repos.size.should > 0
50
+ repos.path_prefix.should == "/user/repos"
51
+ repos.should be_instance_of(Array)
52
+ repos.each do |r|
53
+ r["private"].should be_false
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "#repos" do
60
+ it "should return list of repos" do
61
+ VCR.use_cassette "user.repos" do
62
+ repos = subject.user.repos
63
+ repos.size.should > 0
64
+ end
65
+ end
66
+
67
+ describe "#paginate" do
68
+ it "should limit the count to 10" do
69
+ VCR.use_cassette "users(github).repos.paginate" do
70
+ repos = subject.users("github").repos.paginate :page => 1, :per_page => 10
71
+ repos.size.should == 10
72
+ repos.current_page.should == 1
73
+ repos.next_page.should == 2
74
+ end
75
+ end
76
+ it "should return page 3" do
77
+ VCR.use_cassette "users(github).repos.paginate:page=>3" do
78
+ repos = subject.users("github").repos.paginate :page => 3, :per_page => 10
79
+ repos.size.should == 10
80
+ repos.current_page.should == 3
81
+ repos.next_page.should == 4
82
+ repos.prev_page.should == 2
83
+ repos.first_page.should == 1
84
+ # no consistent way to check last page
85
+ repos.last_page.should >= 4
86
+ end
87
+ end
88
+ end
89
+
90
+ end
91
+ end
92
+
93
+ describe "#users" do
94
+ it "should return given user" do
95
+ VCR.use_cassette('users') do
96
+ user = subject.users('jonmagic')
97
+ user['type'].should == 'User'
98
+ user['login'].should == 'jonmagic'
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ghee::Connection do
4
+ context "with an access_token" do
5
+ subject { Ghee::Connection.new(GH_AUTH) }
6
+
7
+ describe "#initialize" do
8
+ it "should set an instance variable for access token" do
9
+ subject.hash.should == GH_AUTH
10
+ end
11
+ end
12
+
13
+ describe "any request" do
14
+ let(:response) do
15
+ VCR.use_cassette('authorized_request') do
16
+ subject.get('/')
17
+ end
18
+ end
19
+
20
+ it "should return 204" do
21
+ response.status.should == 204
22
+ end
23
+
24
+ it "should parse the json response" do
25
+ response.body.should be_nil
26
+ end
27
+ end
28
+ end
29
+
30
+ context "without an access token" do
31
+ subject { Ghee::Connection.new }
32
+
33
+ describe "#initialize" do
34
+ it "should set an instance variable for access token" do
35
+ subject.hash.should == {}
36
+ end
37
+ end
38
+
39
+ describe "any request" do
40
+ let(:response) do
41
+ VCR.use_cassette('unauthorized_request') do
42
+ subject.get('/')
43
+ end
44
+ end
45
+ it "should return 204" do
46
+ response.status.should == 204
47
+ end
48
+
49
+ it "should parse the json response" do
50
+ response.body.should be_nil
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,207 @@
1
+ require 'spec_helper'
2
+
3
+ class FakeResponse
4
+ def body
5
+ "Bar!"
6
+ end
7
+ end
8
+
9
+ class PaginationResponse
10
+ def initialize(header = nil)
11
+ @header = header
12
+ end
13
+ def body
14
+ [1]
15
+ end
16
+ def headers
17
+ @header || {}
18
+ end
19
+
20
+ end
21
+ class IncrementPager
22
+ def initialize(times=2)
23
+ @times = times
24
+ @executed = 1
25
+ end
26
+ def headers
27
+
28
+ return {} if @executed == @times
29
+ @executed += 1
30
+ {"link" => "<https://api.github.com/users/rauhryan/repos?page=#{@executed}>; rel=\"next\""}
31
+ end
32
+ def body
33
+ [@executed]
34
+ end
35
+
36
+ end
37
+
38
+ describe "Pagination" do
39
+ describe "#all with many pages" do
40
+ before(:each) do
41
+ @connection = mock('connection')
42
+ @connection.stub!(:get).and_return(IncrementPager.new 2)
43
+ end
44
+
45
+ subject do
46
+ Ghee::ResourceProxy.new(@connection, '/foo')
47
+ end
48
+
49
+ it "should return count of 2" do
50
+ subject.all.size.should == 2
51
+ end
52
+ end
53
+ describe "#all with 1 page" do
54
+ before(:each) do
55
+ @connection = mock('connection')
56
+ @connection.stub!(:get).and_return(IncrementPager.new 1)
57
+ end
58
+
59
+ subject do
60
+ Ghee::ResourceProxy.new(@connection, '/foo')
61
+ end
62
+
63
+ it "should return count of 1" do
64
+ subject.all.size.should == 1
65
+ end
66
+ end
67
+
68
+
69
+ describe "past first page" do
70
+
71
+ before(:each) do
72
+ @connection = mock('connection')
73
+ header = {
74
+ "link" => '<https://api.github.com/users/rauhryan/repos?page=3&per_page=5&type=private>; rel="next", <https://api.github.com/users/rauhryan/repos?page=10&per_page=5&type=private>; rel="last", <https://api.github.com/users/rauhryan/repos?page=1&per_page=5&type=private>; rel="first", <https://api.github.com/users/rauhryan/repos?page=1&per_page=5&type=private>; rel="prev"'
75
+ }
76
+ @connection.stub!(:get).and_return(PaginationResponse.new header)
77
+ end
78
+
79
+ subject do
80
+ Ghee::ResourceProxy.new(@connection, '/foo')
81
+ end
82
+
83
+ describe "#paginate" do
84
+ before :each do
85
+ @proxy = subject.paginate :page => 2, :per_page => 5
86
+ end
87
+
88
+ it "should set per_page from the link header" do
89
+ @proxy.pagination[:next][:per_page].should == 5
90
+ end
91
+
92
+ it "should set next page to 3" do
93
+ @proxy.pagination[:next][:page].should == 3
94
+ end
95
+
96
+ it "should set prev page to 1" do
97
+ @proxy.pagination[:prev][:page].should == 1
98
+ end
99
+
100
+ it "should set first page to 1" do
101
+ @proxy.pagination[:first][:page].should == 1
102
+ end
103
+
104
+ it "should set last page to 10" do
105
+ @proxy.pagination[:last][:page].should == 10
106
+ end
107
+
108
+ it "should generate all the xxx_page methods" do
109
+ @proxy.next_page.should == 3
110
+ @proxy.prev_page.should == 1
111
+ @proxy.first_page.should == 1
112
+ @proxy.last_page.should == 10
113
+ end
114
+ end
115
+ end
116
+ describe "per_page is provided" do
117
+
118
+ before(:each) do
119
+ @connection = mock('connection')
120
+ header = {
121
+ "link" => '<https://api.github.com/user/repos?page=2&per_page=10>; rel="next", <https://api.github.com/user/repos?page=6&per_page=10>;
122
+ rel="last"'
123
+ }
124
+ @connection.stub!(:get).and_return(PaginationResponse.new header)
125
+ end
126
+
127
+ subject do
128
+ Ghee::ResourceProxy.new(@connection, '/foo')
129
+ end
130
+
131
+ describe "#paginate" do
132
+ before :each do
133
+ @proxy = subject.paginate :page => 1, :per_page => 10
134
+ end
135
+
136
+ it "should set per_page from the link header" do
137
+ @proxy.pagination[:next][:per_page].should == 10
138
+ end
139
+
140
+ it "should set next page to 2" do
141
+ @proxy.pagination[:next][:page].should == 2
142
+ end
143
+
144
+ it "should set last page to 6" do
145
+ @proxy.pagination[:last][:page].should == 6
146
+ end
147
+ end
148
+ end
149
+
150
+ describe "No header" do
151
+
152
+ before(:each) do
153
+ @connection = mock('connection')
154
+ @connection.stub!(:get).and_return(PaginationResponse.new)
155
+ end
156
+ subject do
157
+ Ghee::ResourceProxy.new(@connection, '/foo')
158
+ end
159
+
160
+ describe "#paginate" do
161
+ it "should set current page" do
162
+ proxy = subject.paginate :page => 1
163
+ proxy.current_page.should == 1
164
+ end
165
+ it "should pass through #method_missing" do
166
+ proxy = subject.paginate :page => 1
167
+ proxy.size.should == 1
168
+ end
169
+ it "should set total" do
170
+ proxy = subject.paginate :page => 1
171
+ proxy.total.should == 1
172
+ end
173
+ it "next page should be nil" do
174
+ proxy = subject.paginate :page => 1
175
+ proxy.next_page.should be_nil
176
+ end
177
+
178
+ end
179
+ end
180
+ end
181
+
182
+ describe Ghee::ResourceProxy do
183
+ before(:each) do
184
+ @connection = mock('connection')
185
+ @connection.stub!(:get).and_return(FakeResponse.new)
186
+ end
187
+
188
+ subject do
189
+ Ghee::ResourceProxy.new(@connection, '/foo')
190
+ end
191
+
192
+ describe "#initialize" do
193
+ it "should set connection" do
194
+ subject.instance_variable_get(:@connection).should == @connection
195
+ end
196
+
197
+ it "should set path_prefix" do
198
+ subject.instance_variable_get(:@path_prefix).should == "/foo"
199
+ end
200
+ end
201
+
202
+ describe "#method_missing" do
203
+ it "should pass message and args to target" do
204
+ subject.upcase.should == "BAR!"
205
+ end
206
+ end
207
+ end