Permalink
Browse files

Send auth parameters along with the request.

  • Loading branch information...
radar committed Aug 24, 2009
1 parent bd127ec commit 4707eb9ac4f19049ca82c606060306204369338f
Showing with 58 additions and 61 deletions.
  1. +1 −0 lib/octopi.rb
  2. +24 −25 lib/octopi/api.rb
  3. +15 −18 test/api_test.rb
  4. +11 −3 test/authenticated_test.rb
  5. +1 −0 test/commit_test.rb
  6. +0 −15 test/octopi_test.rb
  7. +1 −0 test/test_helper.rb
  8. +5 −0 test_auth.rb
View
@@ -48,6 +48,7 @@ def authenticated_with(options, &block)
end
begin
User.find(options[:login])
+ # If the user cannot see themselves then they are not logged in, tell them so
rescue Octopi::NotFound
raise Octopi::InvalidLogin
end
View
@@ -16,6 +16,10 @@ class AnonymousApi < Api
def read_only?
true
end
+
+ def auth_parameters
+ { }
+ end
end
class AuthApi < Api
@@ -26,6 +30,10 @@ class AuthApi < Api
def read_only?
false
end
+
+ def auth_parameters
+ { :login => Api.me.login, :token => Api.me.token }
+ end
end
# This is the real API class.
@@ -86,14 +94,16 @@ def save(resource_path, data)
#still can't figure out on what format values are expected
post("#{resource_path}", { :query => data })
end
+
- def find(path, result_key, resource_id, klass=nil, cache=true)
+ def find(path, result_key, resource_id, klass=nil, cache=true)
result = get(path, { :id => resource_id, :cache => cache }, klass)
result
end
def find_all(path, result_key, query, klass=nil, cache=true)
+ { :query => query, :id => query, :cache => cache }
result = get(path, { :query => query, :id => query, :cache => cache }, klass)
result[result_key]
end
@@ -105,9 +115,8 @@ def get_raw(path, params, klass=nil)
def get(path, params = {}, klass=nil, format = :yaml)
@@retries = 0
begin
- trace "GET [#{format}]", "/#{format}#{path}", params
- submit(path, params, klass, format) do |path, params, format|
- self.class.get "/#{format}#{path}", :format => format
+ submit(path, params, klass, format) do |path, params, format, query|
+ self.class.get "/#{format}#{path}", { :format => format, :query => query }
end
rescue RetryableAPIError => e
if @@retries < MAX_RETRIES
@@ -126,8 +135,8 @@ def post(path, params = {}, klass=nil, format = :yaml)
@@retries = 0
begin
trace "POST", "/#{format}#{path}", params
- submit(path, params, klass, format) do |path, params, format|
- resp = self.class.post "/#{format}#{path}", { :body => params, :format => format }
+ submit(path, params, klass, format) do |path, params, format, query|
+ resp = self.class.post "/#{format}#{path}", { :body => params, :format => format, :query => query }
resp
end
rescue RetryableAPIError => e
@@ -145,6 +154,8 @@ def post(path, params = {}, klass=nil, format = :yaml)
private
def submit(path, params = {}, klass=nil, format = :yaml, &block)
+ # Merge in the parameters required to do authorized stuff.
+ params = params.merge(auth_parameters)
# Ergh. Ugly way to do this. Find a better one!
cache = params.delete(:cache)
cache = true if cache.nil?
@@ -154,36 +165,24 @@ def submit(path, params = {}, klass=nil, format = :yaml, &block)
path = path.gsub(":#{k.to_s}", v)
end
end
- query = login ? { :login => login, :token => token } : {}
- query.merge!(params)
- # Left here for debugging purposes. Handy sometimes.
- # puts "#{self.class.base_uri}/yaml/#{path}"
+ # Work out query parameters
+ query = []
+ params.each { |k, v| query << "#{k}=#{v}" }
begin
- $requests ||= 0
key = "#{Api.api.class.to_s}:#{path}"
resp = if cache
APICache.get(key, :cache => 61) do
- $requests += 1
- yield(path, query.merge(params), format)
+ yield(path, params, format, query)
end
else
- $requests += 1
- yield(path, query.merge(params), format)
+ yield(path, params, format, query)
end
rescue Net::HTTPBadResponse
raise RetryableAPIError
- end
-
- if @trace_level == "curl"
- query_trace = []
- query.each_pair { |k,v| query_trace << "-F '#{k}=#{v}'" }
- puts "===== [curl version]"
- puts "curl #{query_trace.join(" ")} #{self.class.base_uri}/#{format}#{path}"
- puts "===================="
- end
-
+ end
raise RetryableAPIError, resp.code.to_i if RETRYABLE_STATUS.include? resp.code.to_i
+ # puts resp.code.inspect
raise NotFound, klass || self.class if resp.code.to_i == 404
raise APIError,
"GitHub returned status #{resp.code}" unless resp.code.to_i == 200
View
@@ -39,23 +39,20 @@ def setup
end
context "keys" do
- should "not be able to see keys if not authenticated" do
- exception = assert_raise AuthenticationRequired do
- Api.me.keys
- end
-
- assert_equal "To view keys, you must be authenticated", exception.message
- end
-
- should "have some keys" do
- auth do
- keys = Api.me.keys
- assert keys.is_a?(KeySet)
- assert_equal 2, keys.size
- end
- end
+ should "not be able to see keys if not authenticated" do
+ exception = assert_raise AuthenticationRequired do
+ Api.me.keys
+ end
+
+ assert_equal "To view keys, you must be authenticated", exception.message
+ end
+
+ should "have some keys" do
+ auth do
+ keys = Api.me.keys
+ assert keys.is_a?(KeySet)
+ assert_equal 2, keys.size
+ end
+ end
end
-
-
-
end
View
@@ -8,7 +8,7 @@ def setup
context "Authenticating" do
should "be possible with username and password" do
- authenticated_with(:user => "fcoury", :password => "yruocf") do
+ authenticated_with(:login => "fcoury", :password => "yruocf") do
assert_equal "6417354635233fbddf66de798c030f9f", Api.api.token
assert_not_nil User.find("fcoury")
end
@@ -25,7 +25,15 @@ def setup
assert_not_nil User.find("fcoury")
end
end
+
+ should "be denied access when specifying an invalid token and login combination" do
+ FakeWeb.clean_registry
+ FakeWeb.register_uri(:get, "http://github.com/api/v2/yaml/user/show/fcoury", :status => ["404", "Not Found"])
+ assert_raise InvalidLogin do
+ authenticated_with :login => "fcoury", :token => "ba7bf2d7f0ebc073d3874dda887b18ae" do
+ # Just blank will do us fine.
+ end
+ end
+ end
end
-
-
end
View
@@ -30,6 +30,7 @@ def setup
assert_equal 30, commits.size
end
+ # Tests issue #28
should "be able to find commits in a private repository" do
auth do
commits = Commit.find_all(:user => "fcoury", :repository => "rboard")
View
@@ -1,15 +0,0 @@
-# Tests for octopi core functionality go here.
-require File.join(File.dirname(__FILE__), 'test_helper')
-
-class OctopiTest < Test::Unit::TestCase
- include Octopi
-
- def setup
- fake_everything
- end
-
- def default_test
- assert true
- end
-
-end
View
@@ -44,6 +44,7 @@ def yaml_api
end
def fake_everything
+ FakeWeb.clean_registry
# helper variables to make things shorter.
sha = "f6609209c3ac0badd004512d318bfaa508ea10ae"
fake_sha = "ea3cd978650417470535f3a4725b6b5042a6ab59"
View
@@ -0,0 +1,5 @@
+require 'lib/octopi'
+include Octopi
+authenticated_with :token => "ba7bf2d7f0edc07373873dda887b18ae", :login => "radar" do
+ User.find("radar")
+end

0 comments on commit 4707eb9

Please sign in to comment.