geoloqi 0.9.26 → 0.9.27
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +0 -2
- data/lib/geoloqi.rb +6 -6
- data/lib/geoloqi/session.rb +11 -11
- data/lib/geoloqi/version.rb +1 -1
- data/spec/env.rb +23 -0
- data/spec/geoloqi_api_error_spec.rb +11 -0
- data/spec/geoloqi_config.rb +46 -0
- data/spec/geoloqi_session_spec.rb +279 -0
- data/spec/geoloqi_spec.rb +9 -394
- metadata +5 -1
data/lib/geoloqi.rb
CHANGED
@@ -32,16 +32,16 @@ module Geoloqi
|
|
32
32
|
@@config = Config.new opts
|
33
33
|
end
|
34
34
|
|
35
|
-
def get(access_token, path, args={})
|
36
|
-
run :get, access_token, path, args
|
35
|
+
def get(access_token, path, args={}, headers={})
|
36
|
+
run :get, access_token, path, args, headers
|
37
37
|
end
|
38
38
|
|
39
|
-
def post(access_token, path, args={})
|
40
|
-
run :post, access_token, path, args
|
39
|
+
def post(access_token, path, args={}, headers={})
|
40
|
+
run :post, access_token, path, args, headers
|
41
41
|
end
|
42
42
|
|
43
|
-
def run(meth, access_token, path, args={})
|
44
|
-
Session.new(:access_token => access_token).run meth, path, args
|
43
|
+
def run(meth, access_token, path, args={}, headers={})
|
44
|
+
Session.new(:access_token => access_token).run meth, path, args, headers
|
45
45
|
end
|
46
46
|
|
47
47
|
def authorize_url(client_id=nil, redirect_uri=@@config.redirect_uri, opts={})
|
data/lib/geoloqi/session.rb
CHANGED
@@ -30,20 +30,20 @@ module Geoloqi
|
|
30
30
|
Geoloqi.authorize_url @config.client_id, redirect_uri, opts
|
31
31
|
end
|
32
32
|
|
33
|
-
def get(path, query=nil)
|
34
|
-
run :get, path, query
|
33
|
+
def get(path, query=nil, headers={})
|
34
|
+
run :get, path, query, headers
|
35
35
|
end
|
36
36
|
|
37
|
-
def post(path, query=nil)
|
38
|
-
run :post, path, query
|
37
|
+
def post(path, query=nil, headers={})
|
38
|
+
run :post, path, query, headers
|
39
39
|
end
|
40
40
|
|
41
|
-
def run(meth, path, query=nil)
|
41
|
+
def run(meth, path, query=nil, headers={})
|
42
42
|
renew_access_token! if auth[:expires_at] && Time.rfc2822(auth[:expires_at]) <= Time.now && !(path =~ /^\/?oauth\/token$/)
|
43
43
|
retry_attempt = 0
|
44
44
|
|
45
45
|
begin
|
46
|
-
response = execute meth, path, query
|
46
|
+
response = execute meth, path, query, headers
|
47
47
|
hash = JSON.parse response.body
|
48
48
|
raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
|
49
49
|
rescue Geoloqi::ApiError
|
@@ -61,11 +61,11 @@ module Geoloqi
|
|
61
61
|
@config.use_hashie_mash ? Hashie::Mash.new(hash) : hash
|
62
62
|
end
|
63
63
|
|
64
|
-
def execute(meth, path, query=nil)
|
64
|
+
def execute(meth, path, query=nil, headers={})
|
65
65
|
query = Rack::Utils.parse_query query if query.is_a?(String)
|
66
66
|
raw = @connection.send(meth) do |req|
|
67
67
|
req.url "/#{Geoloqi.api_version.to_s}/#{path.gsub(/^\//, '')}"
|
68
|
-
req.headers = headers
|
68
|
+
req.headers = headers.merge! default_headers
|
69
69
|
|
70
70
|
if query
|
71
71
|
meth == :get ? req.params = query : req.body = query.to_json
|
@@ -74,8 +74,8 @@ module Geoloqi
|
|
74
74
|
|
75
75
|
if @config.logger
|
76
76
|
@config.logger.print "### Geoloqi::Session - #{meth.to_s.upcase} #{path}"
|
77
|
-
@config.logger.print Rack::Utils.build_query
|
78
|
-
@config.logger.puts "\
|
77
|
+
@config.logger.print "?#{Rack::Utils.build_query query}" unless query.nil?
|
78
|
+
@config.logger.puts "\n### Status: #{raw.status}\n### Headers: #{raw.headers.inspect}\n### Body: #{raw.body}"
|
79
79
|
end
|
80
80
|
|
81
81
|
Response.new raw.status, raw.headers, raw.body
|
@@ -109,7 +109,7 @@ module Geoloqi
|
|
109
109
|
expires_in.to_i.zero? ? nil : ((Time.now + expires_in.to_i)-5).rfc2822
|
110
110
|
end
|
111
111
|
|
112
|
-
def
|
112
|
+
def default_headers
|
113
113
|
headers = {'Content-Type' => 'application/json', 'User-Agent' => "geoloqi-ruby #{Geoloqi.version}", 'Accept' => 'application/json'}
|
114
114
|
headers['Authorization'] = "OAuth #{access_token}" if access_token
|
115
115
|
headers
|
data/lib/geoloqi/version.rb
CHANGED
data/spec/env.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Bundler.setup
|
2
|
+
require 'rubygems'
|
3
|
+
require './lib/geoloqi.rb'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'wrong'
|
6
|
+
require 'wrong/adapters/minitest'
|
7
|
+
require 'webmock'
|
8
|
+
|
9
|
+
CLIENT_ID = 'client_id1234'
|
10
|
+
CLIENT_SECRET = 'client_secret1234'
|
11
|
+
ACCESS_TOKEN = 'access_token1234'
|
12
|
+
|
13
|
+
def auth_headers(access_token='access_token1234')
|
14
|
+
{'Content-Type' => 'application/json',
|
15
|
+
'User-Agent' => "geoloqi-ruby #{Geoloqi.version}",
|
16
|
+
'Accept' => 'application/json',
|
17
|
+
'Authorization' => "OAuth #{access_token}"}
|
18
|
+
end
|
19
|
+
|
20
|
+
def api_url(path); "#{Geoloqi.api_url}/#{Geoloqi.api_version}/#{path}" end
|
21
|
+
|
22
|
+
Wrong.config.alias_assert :expect
|
23
|
+
include WebMock::API
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join File.dirname(__FILE__), 'env.rb'
|
2
|
+
|
3
|
+
describe Geoloqi::ApiError do
|
4
|
+
it 'throws exception properly and allows drill-down of message' do
|
5
|
+
error = Geoloqi::ApiError.new 405, 'not_enough_cats', 'not enough cats to complete this request'
|
6
|
+
expect { error.status == 405 }
|
7
|
+
expect { error.type == 'not_enough_cats' }
|
8
|
+
expect { error.reason == 'not enough cats to complete this request' }
|
9
|
+
expect { error.message == "#{error.type} - #{error.reason} (405)" }
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.join File.dirname(__FILE__), 'env.rb'
|
2
|
+
|
3
|
+
describe Geoloqi::Config do
|
4
|
+
describe 'with redirect_uri' do
|
5
|
+
it 'returns authorize url' do
|
6
|
+
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :redirect_uri => 'http://blah.blah/test'
|
7
|
+
authorize_url = Geoloqi.authorize_url 'test'
|
8
|
+
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
9
|
+
'response_type=code&'+
|
10
|
+
"client_id=#{Rack::Utils.escape 'test'}&"+
|
11
|
+
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'displays log information if logger is provided' do
|
16
|
+
stub_request(:get, api_url('account/username?cats=lol')).
|
17
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
18
|
+
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
19
|
+
|
20
|
+
io = StringIO.new
|
21
|
+
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
|
22
|
+
|
23
|
+
Geoloqi.get ACCESS_TOKEN, 'account/username', :cats => 'lol'
|
24
|
+
expect { io.string =~ /Geoloqi::Session/ }
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'displays log information if logger is provided and query is nil' do
|
28
|
+
stub_request(:get, api_url('account/username')).
|
29
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
30
|
+
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
31
|
+
|
32
|
+
io = StringIO.new
|
33
|
+
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
|
34
|
+
|
35
|
+
Geoloqi.get ACCESS_TOKEN, 'account/username'
|
36
|
+
expect { io.string =~ /Geoloqi::Session/ }
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'correctly checks booleans for client_id and client_secret' do
|
40
|
+
[:client_id, :client_secret].each do |k|
|
41
|
+
expect { Geoloqi.config(k => '').send("#{k}?") == false }
|
42
|
+
expect { Geoloqi.config(k => nil).send("#{k}?") == false }
|
43
|
+
expect { Geoloqi.config(k => 'lol').send("#{k}?") == true }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
require File.join File.dirname(__FILE__), 'env.rb'
|
2
|
+
|
3
|
+
describe Geoloqi::Session do
|
4
|
+
describe 'with nothing passed' do
|
5
|
+
before do
|
6
|
+
@session = Geoloqi::Session.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should not find access token' do
|
10
|
+
expect { !@session.access_token? }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'with access token and throw exceptions not set' do
|
15
|
+
it 'should throw api error exception' do
|
16
|
+
stub_request(:get, api_url('badmethodcall')).
|
17
|
+
with(:headers => auth_headers).
|
18
|
+
to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
|
19
|
+
|
20
|
+
expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('badmethodcall')}.class == Geoloqi::ApiError }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'with access token and throw exceptions false' do
|
25
|
+
before do
|
26
|
+
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:throw_exceptions => false}
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should not throw api error exception' do
|
30
|
+
stub_request(:get, api_url('badmethodcall')).
|
31
|
+
with(:headers => auth_headers).
|
32
|
+
to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
|
33
|
+
|
34
|
+
response = @session.get 'badmethodcall'
|
35
|
+
expect {response['error'] == 'not_found'}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'with access token and hashie mash' do
|
40
|
+
before do
|
41
|
+
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:use_hashie_mash => true}
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should respond to method calls in addition to hash' do
|
45
|
+
stub_request(:get, api_url('account/username')).
|
46
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
47
|
+
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
48
|
+
|
49
|
+
response = @session.get 'account/username'
|
50
|
+
expect { response['username'] == 'bulbasaurrulzok' }
|
51
|
+
expect { response.username == 'bulbasaurrulzok' }
|
52
|
+
expect { response[:username] == 'bulbasaurrulzok' }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'with access token and no config' do
|
57
|
+
before do
|
58
|
+
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'throws an exception on a hard request error' do
|
62
|
+
stub_request(:get, api_url('crashing_method')).
|
63
|
+
with(:headers => auth_headers).
|
64
|
+
to_return(:status => 500, :body => 'Something broke hard!')
|
65
|
+
|
66
|
+
expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.class == Geoloqi::Error }
|
67
|
+
expect {
|
68
|
+
rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.message ==
|
69
|
+
"API returned invalid JSON. Status: 500 Body: Something broke hard!"
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'successfully makes call with array' do
|
74
|
+
stub_request(:post, api_url('play_record_at_geoloqi_hq')).
|
75
|
+
with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json).
|
76
|
+
to_return(:body => {'result' => 'ok'}.to_json)
|
77
|
+
|
78
|
+
expect { @session.post('play_record_at_geoloqi_hq', [{:artist => 'Television'}])['result'] == 'ok' }
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'successfully makes call to api' do
|
82
|
+
stub_request(:get, api_url('layer/info/Gx')).
|
83
|
+
with(:headers => auth_headers).
|
84
|
+
to_return(:status => 200, :body => {'layer_id' => 'Gx'}.to_json)
|
85
|
+
|
86
|
+
%w{/layer/info/Gx layer/info/Gx}.each do |path|
|
87
|
+
expect { @session.get(path)['layer_id'] == 'Gx' }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'location/history' do
|
92
|
+
before do
|
93
|
+
stub_request(:get, api_url('location/history?count=2')).
|
94
|
+
with(:headers => auth_headers).
|
95
|
+
to_return(:status => 200, :body => {:points => [1,2]}.to_json)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'makes a location/history call with get and hash params' do
|
99
|
+
expect { @session.get('location/history', :count => 2)['points'].count == 2 }
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'makes a location/history call with get and query string directly in path' do
|
103
|
+
expect { @session.get('location/history?count=2')['points'].count == 2 }
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'makes a location/history call with get and query string params' do
|
107
|
+
expect { @session.get('location/history', 'count=2')['points'].count == 2 }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe 'with oauth id, secret, and access token via Geoloqi::Config' do
|
113
|
+
it 'should load config' do
|
114
|
+
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN,
|
115
|
+
:config => Geoloqi::Config.new(:client_id => CLIENT_ID,
|
116
|
+
:client_secret => CLIENT_SECRET)
|
117
|
+
expect { @session.config.client_id == CLIENT_ID }
|
118
|
+
expect { @session.config.client_secret == CLIENT_SECRET }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe 'with- client id, client secret, and access token via direct hash' do
|
123
|
+
before do
|
124
|
+
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN,
|
125
|
+
:config => {:client_id => CLIENT_ID,
|
126
|
+
:client_secret => CLIENT_SECRET}
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should return access token' do
|
130
|
+
expect { @session.access_token == ACCESS_TOKEN }
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should recognize access token exists' do
|
134
|
+
expect { @session.access_token? }
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'gets authorize url' do
|
138
|
+
authorize_url = @session.authorize_url 'http://blah.blah/test'
|
139
|
+
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
140
|
+
"response_type=code&"+
|
141
|
+
"client_id=#{Rack::Utils.escape CLIENT_ID}&"+
|
142
|
+
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'gets authorize url with scope' do
|
146
|
+
authorize_url = @session.authorize_url 'http://blah.blah/test', :scope => 'party_hard'
|
147
|
+
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
148
|
+
"response_type=code&"+
|
149
|
+
"client_id=#{Rack::Utils.escape CLIENT_ID}&"+
|
150
|
+
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}&"+
|
151
|
+
"scope=party_hard" }
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'with bunk access token' do
|
156
|
+
before do
|
157
|
+
@session = Geoloqi::Session.new :access_token => 'hey brah whats up let me in its cool 8)'
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'fails with an exception' do
|
161
|
+
stub_request(:post, api_url('message/send')).
|
162
|
+
with(:headers => auth_headers('hey brah whats up let me in its cool 8)')).
|
163
|
+
to_return(:status => 401, :body => {'error' => 'invalid_token'}.to_json)
|
164
|
+
|
165
|
+
begin
|
166
|
+
@session.post 'message/send'
|
167
|
+
rescue Exception => e
|
168
|
+
expect { e.class == Geoloqi::ApiError }
|
169
|
+
expect { e.status == 401 }
|
170
|
+
expect { e.type == 'invalid_token' }
|
171
|
+
expect { e.message == 'invalid_token (401)' }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe 'with config' do
|
177
|
+
before do
|
178
|
+
@session = Geoloqi::Session.new :config => {:client_id => CLIENT_ID, :client_secret => CLIENT_SECRET}
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'retrieves auth with mock' do
|
182
|
+
stub_request(:post, api_url('oauth/token')).
|
183
|
+
with(:body => {:client_id => CLIENT_ID,
|
184
|
+
:client_secret => CLIENT_SECRET,
|
185
|
+
:grant_type => "authorization_code",
|
186
|
+
:code => "1234",
|
187
|
+
:redirect_uri => "http://example.com"}.to_json).
|
188
|
+
to_return(:body => {:access_token => 'access_token1234',
|
189
|
+
:scope => nil,
|
190
|
+
:expires_in => '86400',
|
191
|
+
:refresh_token => 'refresh_token1234'}.to_json)
|
192
|
+
|
193
|
+
response = @session.get_auth '1234', 'http://example.com'
|
194
|
+
|
195
|
+
{:access_token => 'access_token1234',
|
196
|
+
:scope => nil,
|
197
|
+
:expires_in => '86400',
|
198
|
+
:refresh_token => 'refresh_token1234'}.each do |k,v|
|
199
|
+
expect { response[k] == v }
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'does not refresh when never expires' do
|
204
|
+
stub_request(:post, api_url('oauth/token')).
|
205
|
+
with(:body => {:client_id => CLIENT_ID,
|
206
|
+
:client_secret => CLIENT_SECRET,
|
207
|
+
:grant_type => "authorization_code",
|
208
|
+
:code => "1234",
|
209
|
+
:redirect_uri => "http://neverexpires.example.com/"}.to_json).
|
210
|
+
to_return(:body => {:access_token => 'access_token1234',
|
211
|
+
:scope => nil,
|
212
|
+
:expires_in => '0',
|
213
|
+
:refresh_token => 'never_expires'}.to_json)
|
214
|
+
|
215
|
+
stub_request(:get, api_url('account/username')).
|
216
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
217
|
+
to_return(:body => {:username => 'bulbasaurrulzok'}.to_json)
|
218
|
+
|
219
|
+
response = @session.get_auth '1234', 'http://neverexpires.example.com/'
|
220
|
+
|
221
|
+
expect { @session.auth[:expires_in] == '0' }
|
222
|
+
expect { @session.auth[:expires_at].nil? }
|
223
|
+
|
224
|
+
response = @session.get 'account/username'
|
225
|
+
|
226
|
+
expect { @session.auth[:access_token] == 'access_token1234' }
|
227
|
+
expect { response['username'] == 'bulbasaurrulzok' }
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'does not attempt to refresh for auth code expire' do
|
231
|
+
stub_request(:post, api_url('oauth/token')).
|
232
|
+
with(:body => {:client_id => CLIENT_ID,
|
233
|
+
:client_secret => CLIENT_SECRET,
|
234
|
+
:grant_type => "authorization_code",
|
235
|
+
:code => "1234",
|
236
|
+
:redirect_uri => "http://expired_code.example.com/"}.to_json).
|
237
|
+
to_return(:body => {:access_token => 'access_token1234',
|
238
|
+
:scope => nil,
|
239
|
+
:expires_in => '0',
|
240
|
+
:refresh_token => 'never_expires'}.to_json)
|
241
|
+
|
242
|
+
stub_request(:get, api_url('account/username?code=1234')).
|
243
|
+
with(:headers => auth_headers).
|
244
|
+
to_return(:status => 200, :body => {:points => [1,2]}.to_json)
|
245
|
+
|
246
|
+
# FINISH IMPLEMENTING
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
describe 'with config and expired auth' do
|
251
|
+
before do
|
252
|
+
@session = Geoloqi::Session.new :config => {:client_id => CLIENT_ID, :client_secret => CLIENT_SECRET},
|
253
|
+
:auth => { :access_token => 'access_token1234',
|
254
|
+
:scope => nil,
|
255
|
+
:expires_in => '86400',
|
256
|
+
:expires_at => Time.at(0).rfc2822,
|
257
|
+
:refresh_token => 'refresh_token1234' }
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'retrieves new access token and retries query if expired' do
|
261
|
+
stub_request(:post, api_url('oauth/token')).
|
262
|
+
with(:body => {:client_id => CLIENT_ID,
|
263
|
+
:client_secret => CLIENT_SECRET,
|
264
|
+
:grant_type => "refresh_token",
|
265
|
+
:refresh_token => "refresh_token1234"}.to_json).
|
266
|
+
to_return(:body => {:access_token => 'access_token4567',
|
267
|
+
:scope => nil,
|
268
|
+
:expires_in => '5000',
|
269
|
+
:refresh_token => 'refresh_token4567'}.to_json)
|
270
|
+
|
271
|
+
stub_request(:get, api_url('account/username')).
|
272
|
+
with(:headers => {'Authorization'=>'OAuth access_token4567'}).
|
273
|
+
to_return(:body => {'username' => 'pikachu4lyfe'}.to_json)
|
274
|
+
|
275
|
+
@session.get 'account/username'
|
276
|
+
expect { @session.auth[:access_token] == 'access_token4567' }
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
data/spec/geoloqi_spec.rb
CHANGED
@@ -1,26 +1,4 @@
|
|
1
|
-
|
2
|
-
require 'rubygems'
|
3
|
-
require './lib/geoloqi.rb'
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require 'wrong'
|
6
|
-
require 'wrong/adapters/minitest'
|
7
|
-
require 'webmock'
|
8
|
-
|
9
|
-
CLIENT_ID = 'client_id1234'
|
10
|
-
CLIENT_SECRET = 'client_secret1234'
|
11
|
-
ACCESS_TOKEN = 'access_token1234'
|
12
|
-
|
13
|
-
def auth_headers(access_token='access_token1234')
|
14
|
-
{'Content-Type' => 'application/json',
|
15
|
-
'User-Agent' => "geoloqi-ruby #{Geoloqi.version}",
|
16
|
-
'Accept' => 'application/json',
|
17
|
-
'Authorization' => "OAuth #{access_token}"}
|
18
|
-
end
|
19
|
-
|
20
|
-
def api_url(path); "#{Geoloqi.api_url}/#{Geoloqi.api_version}/#{path}" end
|
21
|
-
|
22
|
-
Wrong.config.alias_assert :expect
|
23
|
-
include WebMock::API
|
1
|
+
require File.join File.dirname(__FILE__), 'env.rb'
|
24
2
|
|
25
3
|
describe Geoloqi do
|
26
4
|
it 'reads geoloqi config' do
|
@@ -51,383 +29,20 @@ describe Geoloqi do
|
|
51
29
|
|
52
30
|
it 'makes get request' do
|
53
31
|
stub_request(:get, "https://api.geoloqi.com/1/quick_get?lol=cats").
|
54
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
32
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234', 'Special' => 'header'}).
|
55
33
|
to_return(:body => {:result => 'ok'}.to_json)
|
56
|
-
|
57
|
-
response = Geoloqi.get ACCESS_TOKEN, '/quick_get', :lol => 'cats'
|
34
|
+
|
35
|
+
response = Geoloqi.get ACCESS_TOKEN, '/quick_get', {:lol => 'cats'}, 'Special' => 'header'
|
58
36
|
expect { response['result'] == 'ok' }
|
59
37
|
end
|
60
|
-
|
38
|
+
|
61
39
|
it 'makes post request' do
|
62
40
|
stub_request(:post, "https://api.geoloqi.com/1/quick_post").
|
63
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'},
|
41
|
+
with(:headers => {'Authorization'=>'OAuth access_token1234', 'Special' => 'header'},
|
64
42
|
:body => {:lol => 'dogs'}.to_json).
|
65
43
|
to_return(:body => {:result => 'ok'}.to_json)
|
66
|
-
|
67
|
-
response = Geoloqi.post ACCESS_TOKEN, '/quick_post', :lol => 'dogs'
|
68
|
-
expect { response['result'] == 'ok' }
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe Geoloqi::ApiError do
|
73
|
-
it 'throws exception properly and allows drill-down of message' do
|
74
|
-
error = Geoloqi::ApiError.new 405, 'not_enough_cats', 'not enough cats to complete this request'
|
75
|
-
expect { error.status == 405 }
|
76
|
-
expect { error.type == 'not_enough_cats' }
|
77
|
-
expect { error.reason == 'not enough cats to complete this request' }
|
78
|
-
expect { error.message == "#{error.type} - #{error.reason} (405)" }
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe Geoloqi::Config do
|
83
|
-
describe 'with redirect_uri' do
|
84
|
-
it 'returns authorize url' do
|
85
|
-
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :redirect_uri => 'http://blah.blah/test'
|
86
|
-
authorize_url = Geoloqi.authorize_url 'test'
|
87
|
-
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
88
|
-
'response_type=code&'+
|
89
|
-
"client_id=#{Rack::Utils.escape 'test'}&"+
|
90
|
-
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
it 'displays log information if logger is provided' do
|
95
|
-
stub_request(:get, api_url('account/username?cats=lol')).
|
96
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
97
|
-
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
98
|
-
|
99
|
-
io = StringIO.new
|
100
|
-
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
|
101
|
-
|
102
|
-
Geoloqi.get ACCESS_TOKEN, 'account/username', :cats => 'lol'
|
103
|
-
expect { io.string =~ /Geoloqi::Session/ }
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'displays log information if logger is provided and query is nil' do
|
107
|
-
stub_request(:get, api_url('account/username')).
|
108
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
109
|
-
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
110
|
-
|
111
|
-
io = StringIO.new
|
112
|
-
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
|
113
|
-
|
114
|
-
Geoloqi.get ACCESS_TOKEN, 'account/username'
|
115
|
-
expect { io.string =~ /Geoloqi::Session/ }
|
116
|
-
end
|
117
|
-
|
118
|
-
|
119
|
-
it 'correctly checks booleans for client_id and client_secret' do
|
120
|
-
[:client_id, :client_secret].each do |k|
|
121
|
-
expect { Geoloqi.config(k => '').send("#{k}?") == false }
|
122
|
-
expect { Geoloqi.config(k => nil).send("#{k}?") == false }
|
123
|
-
expect { Geoloqi.config(k => 'lol').send("#{k}?") == true }
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
describe Geoloqi::Session do
|
129
|
-
describe 'with nothing passed' do
|
130
|
-
before do
|
131
|
-
@session = Geoloqi::Session.new
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'should not find access token' do
|
135
|
-
expect { !@session.access_token? }
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe 'with access token and throw exceptions not set' do
|
140
|
-
it 'should throw api error exception' do
|
141
|
-
stub_request(:get, api_url('badmethodcall')).
|
142
|
-
with(:headers => auth_headers).
|
143
|
-
to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
|
144
|
-
|
145
|
-
expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('badmethodcall')}.class == Geoloqi::ApiError }
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe 'with access token and throw exceptions false' do
|
150
|
-
before do
|
151
|
-
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:throw_exceptions => false}
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'should not throw api error exception' do
|
155
|
-
stub_request(:get, api_url('badmethodcall')).
|
156
|
-
with(:headers => auth_headers).
|
157
|
-
to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
|
158
|
-
|
159
|
-
response = @session.get 'badmethodcall'
|
160
|
-
expect {response['error'] == 'not_found'}
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
describe 'with access token and hashie mash' do
|
165
|
-
before do
|
166
|
-
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:use_hashie_mash => true}
|
167
|
-
end
|
168
|
-
|
169
|
-
it 'should respond to method calls in addition to hash' do
|
170
|
-
stub_request(:get, api_url('account/username')).
|
171
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
172
|
-
to_return(:body => {'username' => 'bulbasaurrulzok'}.to_json)
|
173
|
-
|
174
|
-
response = @session.get 'account/username'
|
175
|
-
expect { response['username'] == 'bulbasaurrulzok' }
|
176
|
-
expect { response.username == 'bulbasaurrulzok' }
|
177
|
-
expect { response[:username] == 'bulbasaurrulzok' }
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
describe 'with access token and no config' do
|
182
|
-
before do
|
183
|
-
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN
|
184
|
-
end
|
185
|
-
|
186
|
-
it 'throws an exception on a hard request error' do
|
187
|
-
stub_request(:get, api_url('crashing_method')).
|
188
|
-
with(:headers => auth_headers).
|
189
|
-
to_return(:status => 500, :body => 'Something broke hard!')
|
190
|
-
|
191
|
-
expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.class == Geoloqi::Error }
|
192
|
-
expect {
|
193
|
-
rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.message ==
|
194
|
-
"API returned invalid JSON. Status: 500 Body: Something broke hard!"
|
195
|
-
}
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'successfully makes mock call with array' do
|
199
|
-
stub_request(:post, api_url('play_record_at_geoloqi_hq')).
|
200
|
-
with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json).
|
201
|
-
to_return(:body => {'result' => 'ok'}.to_json)
|
202
|
-
|
203
|
-
expect { @session.post('play_record_at_geoloqi_hq', [{:artist => 'Television'}])['result'] == 'ok' }
|
204
|
-
end
|
205
|
-
|
206
|
-
it 'successfully makes call to api with forward slash' do
|
207
|
-
stub_request(:get, api_url('layer/info/Gx')).
|
208
|
-
with(:headers => auth_headers).
|
209
|
-
to_return(:status => 200, :body => {'layer_id' => 'Gx'}.to_json)
|
210
|
-
|
211
|
-
expect { @session.get('/layer/info/Gx')['layer_id'] == 'Gx' }
|
212
|
-
end
|
213
|
-
|
214
|
-
it 'successfully makes call to api without forward slash' do
|
215
|
-
stub_request(:get, api_url('layer/info/Gx')).
|
216
|
-
with(:headers => auth_headers).
|
217
|
-
to_return(:status => 200, :body => {'layer_id' => 'Gx'}.to_json)
|
218
|
-
|
219
|
-
expect { @session.get('layer/info/Gx')['layer_id'] == 'Gx' }
|
220
|
-
end
|
221
44
|
|
222
|
-
|
223
|
-
|
224
|
-
with(:headers => auth_headers, :body => {:name => 'Test Layer'}.to_json).
|
225
|
-
to_return(:status => 200, :body => {:layer_id => 'layer_id1234'}.to_json)
|
226
|
-
|
227
|
-
stub_request(:get, api_url('layer/info/layer_id1234')).
|
228
|
-
with(:headers => auth_headers).
|
229
|
-
to_return(:status => 200, :body => {:name => 'Test Layer'}.to_json)
|
230
|
-
|
231
|
-
stub_request(:post, api_url('layer/delete/layer_id1234')).
|
232
|
-
with(:headers => auth_headers).
|
233
|
-
to_return(:status => 200, :body => {'result' => 'ok'}.to_json)
|
234
|
-
|
235
|
-
layer_id = @session.post('/layer/create', :name => 'Test Layer')['layer_id']
|
236
|
-
layer_info = @session.get "/layer/info/#{layer_id}"
|
237
|
-
layer_delete = @session.post "/layer/delete/#{layer_id}"
|
238
|
-
|
239
|
-
expect { layer_id.is_a?(String) }
|
240
|
-
expect { !layer_id.empty? }
|
241
|
-
expect { layer_info['name'] == 'Test Layer' }
|
242
|
-
expect { layer_delete['result'] == 'ok' }
|
243
|
-
end
|
244
|
-
|
245
|
-
describe 'location/history' do
|
246
|
-
before do
|
247
|
-
stub_request(:get, api_url('location/history?count=2')).
|
248
|
-
with(:headers => auth_headers).
|
249
|
-
to_return(:status => 200, :body => {:points => [1,2]}.to_json)
|
250
|
-
end
|
251
|
-
|
252
|
-
it 'makes a location/history call with get and hash params' do
|
253
|
-
expect { @session.get('location/history', :count => 2)['points'].count == 2 }
|
254
|
-
end
|
255
|
-
|
256
|
-
it 'makes a location/history call with get and query string directly in path' do
|
257
|
-
expect { @session.get('location/history?count=2')['points'].count == 2 }
|
258
|
-
end
|
259
|
-
|
260
|
-
it 'makes a location/history call with get and query string params' do
|
261
|
-
expect { @session.get('location/history', 'count=2')['points'].count == 2 }
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
describe 'with oauth id, secret, and access token via Geoloqi::Config' do
|
267
|
-
it 'should load config' do
|
268
|
-
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN,
|
269
|
-
:config => Geoloqi::Config.new(:client_id => CLIENT_ID,
|
270
|
-
:client_secret => CLIENT_SECRET)
|
271
|
-
expect { @session.config.client_id == CLIENT_ID }
|
272
|
-
expect { @session.config.client_secret == CLIENT_SECRET }
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
describe 'with client id, client secret, and access token via direct hash' do
|
277
|
-
before do
|
278
|
-
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN,
|
279
|
-
:config => {:client_id => CLIENT_ID,
|
280
|
-
:client_secret => CLIENT_SECRET}
|
281
|
-
end
|
282
|
-
|
283
|
-
it 'should return access token' do
|
284
|
-
expect { @session.access_token == ACCESS_TOKEN }
|
285
|
-
end
|
286
|
-
|
287
|
-
it 'should recognize access token exists' do
|
288
|
-
expect { @session.access_token? }
|
289
|
-
end
|
290
|
-
|
291
|
-
it 'gets authorize url' do
|
292
|
-
authorize_url = @session.authorize_url 'http://blah.blah/test'
|
293
|
-
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
294
|
-
"response_type=code&"+
|
295
|
-
"client_id=#{Rack::Utils.escape CLIENT_ID}&"+
|
296
|
-
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
297
|
-
end
|
298
|
-
|
299
|
-
it 'gets authorize url with scope' do
|
300
|
-
authorize_url = @session.authorize_url 'http://blah.blah/test', :scope => 'party_hard'
|
301
|
-
expect { authorize_url == "#{Geoloqi.oauth_url}?"+
|
302
|
-
"response_type=code&"+
|
303
|
-
"client_id=#{Rack::Utils.escape CLIENT_ID}&"+
|
304
|
-
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}&"+
|
305
|
-
"scope=party_hard" }
|
306
|
-
end
|
307
|
-
end
|
308
|
-
|
309
|
-
describe 'with bunk access token' do
|
310
|
-
before do
|
311
|
-
@session = Geoloqi::Session.new :access_token => 'hey brah whats up let me in its cool 8)'
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'fails with an exception' do
|
315
|
-
stub_request(:post, api_url('message/send')).
|
316
|
-
with(:headers => auth_headers('hey brah whats up let me in its cool 8)')).
|
317
|
-
to_return(:status => 401, :body => {'error' => 'invalid_token'}.to_json)
|
318
|
-
|
319
|
-
begin
|
320
|
-
@session.post 'message/send'
|
321
|
-
rescue Exception => e
|
322
|
-
expect { e.class == Geoloqi::ApiError }
|
323
|
-
expect { e.status == 401 }
|
324
|
-
expect { e.type == 'invalid_token' }
|
325
|
-
expect { e.message == 'invalid_token (401)' }
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
describe 'with config' do
|
331
|
-
before do
|
332
|
-
@session = Geoloqi::Session.new :config => {:client_id => CLIENT_ID, :client_secret => CLIENT_SECRET}
|
333
|
-
end
|
334
|
-
|
335
|
-
it 'retrieves auth with mock' do
|
336
|
-
stub_request(:post, api_url('oauth/token')).
|
337
|
-
with(:body => {:client_id => CLIENT_ID,
|
338
|
-
:client_secret => CLIENT_SECRET,
|
339
|
-
:grant_type => "authorization_code",
|
340
|
-
:code => "1234",
|
341
|
-
:redirect_uri => "http://example.com"}.to_json).
|
342
|
-
to_return(:body => {:access_token => 'access_token1234',
|
343
|
-
:scope => nil,
|
344
|
-
:expires_in => '86400',
|
345
|
-
:refresh_token => 'refresh_token1234'}.to_json)
|
346
|
-
|
347
|
-
response = @session.get_auth '1234', 'http://example.com'
|
348
|
-
|
349
|
-
{:access_token => 'access_token1234',
|
350
|
-
:scope => nil,
|
351
|
-
:expires_in => '86400',
|
352
|
-
:refresh_token => 'refresh_token1234'}.each do |k,v|
|
353
|
-
expect { response[k] == v }
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
it 'does not refresh when never expires' do
|
358
|
-
stub_request(:post, api_url('oauth/token')).
|
359
|
-
with(:body => {:client_id => CLIENT_ID,
|
360
|
-
:client_secret => CLIENT_SECRET,
|
361
|
-
:grant_type => "authorization_code",
|
362
|
-
:code => "1234",
|
363
|
-
:redirect_uri => "http://neverexpires.example.com/"}.to_json).
|
364
|
-
to_return(:body => {:access_token => 'access_token1234',
|
365
|
-
:scope => nil,
|
366
|
-
:expires_in => '0',
|
367
|
-
:refresh_token => 'never_expires'}.to_json)
|
368
|
-
|
369
|
-
stub_request(:get, api_url('account/username')).
|
370
|
-
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
371
|
-
to_return(:body => {:username => 'bulbasaurrulzok'}.to_json)
|
372
|
-
|
373
|
-
response = @session.get_auth '1234', 'http://neverexpires.example.com/'
|
374
|
-
|
375
|
-
expect { @session.auth[:expires_in] == '0' }
|
376
|
-
expect { @session.auth[:expires_at].nil? }
|
377
|
-
|
378
|
-
response = @session.get 'account/username'
|
379
|
-
|
380
|
-
expect { @session.auth[:access_token] == 'access_token1234' }
|
381
|
-
expect { response['username'] == 'bulbasaurrulzok' }
|
382
|
-
end
|
383
|
-
|
384
|
-
it 'does not attempt to refresh for auth code expire' do
|
385
|
-
stub_request(:post, api_url('oauth/token')).
|
386
|
-
with(:body => {:client_id => CLIENT_ID,
|
387
|
-
:client_secret => CLIENT_SECRET,
|
388
|
-
:grant_type => "authorization_code",
|
389
|
-
:code => "1234",
|
390
|
-
:redirect_uri => "http://expired_code.example.com/"}.to_json).
|
391
|
-
to_return(:body => {:access_token => 'access_token1234',
|
392
|
-
:scope => nil,
|
393
|
-
:expires_in => '0',
|
394
|
-
:refresh_token => 'never_expires'}.to_json)
|
395
|
-
|
396
|
-
stub_request(:get, api_url('account/username?code=1234')).
|
397
|
-
with(:headers => auth_headers).
|
398
|
-
to_return(:status => 200, :body => {:points => [1,2]}.to_json)
|
399
|
-
|
400
|
-
# FINISH IMPLEMENTING
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
describe 'with config and expired auth' do
|
405
|
-
before do
|
406
|
-
@session = Geoloqi::Session.new :config => {:client_id => CLIENT_ID, :client_secret => CLIENT_SECRET},
|
407
|
-
:auth => { :access_token => 'access_token1234',
|
408
|
-
:scope => nil,
|
409
|
-
:expires_in => '86400',
|
410
|
-
:expires_at => Time.at(0).rfc2822,
|
411
|
-
:refresh_token => 'refresh_token1234' }
|
412
|
-
end
|
413
|
-
|
414
|
-
it 'retrieves new access token and retries query if expired' do
|
415
|
-
stub_request(:post, api_url('oauth/token')).
|
416
|
-
with(:body => {:client_id => CLIENT_ID,
|
417
|
-
:client_secret => CLIENT_SECRET,
|
418
|
-
:grant_type => "refresh_token",
|
419
|
-
:refresh_token => "refresh_token1234"}.to_json).
|
420
|
-
to_return(:body => {:access_token => 'access_token4567',
|
421
|
-
:scope => nil,
|
422
|
-
:expires_in => '5000',
|
423
|
-
:refresh_token => 'refresh_token4567'}.to_json)
|
424
|
-
|
425
|
-
stub_request(:get, api_url('account/username')).
|
426
|
-
with(:headers => {'Authorization'=>'OAuth access_token4567'}).
|
427
|
-
to_return(:body => {'username' => 'pikachu4lyfe'}.to_json)
|
428
|
-
|
429
|
-
@session.get 'account/username'
|
430
|
-
expect { @session.auth[:access_token] == 'access_token4567' }
|
431
|
-
end
|
45
|
+
response = Geoloqi.post ACCESS_TOKEN, '/quick_post', {:lol => 'dogs'}, 'Special' => 'header'
|
46
|
+
expect { response['result'] == 'ok' }
|
432
47
|
end
|
433
|
-
end
|
48
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: geoloqi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.9.
|
5
|
+
version: 0.9.27
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kyle Drake
|
@@ -118,6 +118,10 @@ files:
|
|
118
118
|
- lib/geoloqi/session.rb
|
119
119
|
- lib/geoloqi/version.rb
|
120
120
|
- license.txt
|
121
|
+
- spec/env.rb
|
122
|
+
- spec/geoloqi_api_error_spec.rb
|
123
|
+
- spec/geoloqi_config.rb
|
124
|
+
- spec/geoloqi_session_spec.rb
|
121
125
|
- spec/geoloqi_spec.rb
|
122
126
|
has_rdoc: true
|
123
127
|
homepage: http://github.com/geoloqi/geoloqi-ruby
|