Permalink
Browse files

new test that fails

  • Loading branch information...
pedrodelgallego committed Dec 17, 2009
1 parent a1a5464 commit 102f9d4d90defba87db2b3b15ced374b493f1a08
Showing with 181 additions and 38 deletions.
  1. +1 −0 examples/login-app.rb
  2. +47 −0 examples/login-hooks-sapp.rb
  3. +57 −18 lib/rack-rpx.rb
  4. +32 −3 spec/rack-rpx_spec.rb
  5. +10 −17 spec/spec_helper.rb
  6. +24 −0 test/rack_rpx_test.rb
  7. +10 −0 test/test_helper.rb
View
@@ -9,6 +9,7 @@
use Rack::Rpx, :port => '9393',
:api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
:callback_path => '/login_completed'
+
helpers do
include Rack::Rpx::Methods
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'sinatra'
+require 'rack'
+require 'haml'
+require 'lib/rack-rpx'
+
+use Rack::Session::Cookie
+
+use Rack::Rpx, :port => '9393',
+ :api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
+ :callback_path => '/login_completed'
+
+helpers do
+ include Rack::Rpx::Methods
+end
+
+class Rack::Rpx
+ def login(env)
+ puts "logging in ..."
+ env['rack.session']['user']= Rack::Rpx.credentials @req.params['token']
+ end
+
+ def logout(env)
+ puts "logging out ..."
+ env['rack.session'].delete 'user'
+ end
+end
+
+get "/" do
+ "our current session => #{session.inspect}, params => #{params.inspect}<br/>, #{env.inspect}"
+end
+
+get "/login" do
+ haml :login
+end
+
+post "/login_completed" do
+ "our current #{session.inspect}"
+end
+
+get '/logout' do
+ "our current #{session.inspect}"
+end
+
+
+
+
View
@@ -6,19 +6,24 @@ module Rack #:nodoc:
# Note: this *requires* that a Rack::Session middleware be enabled
#
class Rpx
+ RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info"
+ # Raised if an incompatible session is being used.
+ class NoSession < RuntimeError; end
+ class LoginFailedError < RuntimeError; end
+
OPTIONS = {
- :login_path => '/login',
:callback_path => '/login_completed',
+ :logout_path => '/logout',
:host => 'localhost',
:port => '80',
- :rack_session => 'rack.session'
+ :rack_session => 'rack.session',
+ :name => 'default'
}
-
+
# Helper methods intended to be included in your Rails controller or
# in your Sinatra helpers block
module Methods
- RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info"
-
+
# This is *the* method you want to call.
#
# After you're authorized and redirected back to your #redirect_to path,
@@ -27,6 +32,20 @@ module Methods
#
# You can use the token to make GET/POST/etc requests
def get_credentials(token)
+ Rack::Rpx.get_credentials(token)
+ end
+
+ def login_widget_url(app_name=nil)
+ "https://#{app_name}.rpxnow.com/openid/v2/signin?token_url=#{callback_url}"
+ end
+
+ def callback_url
+ "http://#{env['HTTP_HOST']}#{OPTIONS[:callback_path]}"
+ end
+ end
+
+ class << self
+ def credentials(token)
u = URI.parse(RPX_LOGIN_URL)
req = Net::HTTP::Post.new(u.path)
req.set_form_data({:token => token, :apiKey => OPTIONS[:api_key], :format => 'json', :extended => 'true'})
@@ -37,25 +56,45 @@ def get_credentials(token)
raise LoginFailedError, 'Cannot log in. Try another account!' unless json['stat'] == 'ok'
json
end
-
- def login_widget_url(app_name)
- "https://#{app_name}.rpxnow.com/openid/v2/signin?token_url=#{callback_url}"
+ end
+
+ def initialize app, *options
+ @app = app
+ OPTIONS.merge! options.pop
+ OPTIONS.each do |k,v|
+ Rack::Rpx.send(:define_method, k.to_s) {OPTIONS[k]}
end
+ end
+
+ def call env
+ @req = Rack::Request.new env
+ raise NoSession, 'No compatible session.' unless env['rack.session']
- def callback_url
- "http://#{OPTIONS[:host]}:#{OPTIONS[:port]}#{OPTIONS[:callback_path]}"
- end
+ if env['PATH_INFO'] == OPTIONS[:callback_path] && @req.post? then
+ token = @req.params["token"]
+ set_credentials(env, token) if OPTIONS[:set_credentials]
+ login(env)
+ elsif env['PATH_INFO'] == OPTIONS[:logout_path] then
+ logout(env)
+ end
+ @app.call(env)
end
- def initialize app, *args
- @app = app
- arg_options = args.pop
- OPTIONS.merge! arg_options
+ def set_credentials(env, token)
+ env['rack.session']['credentials'] = self.get_credentials(token)
end
+
+ # This is the method that you should override if you want to
+ # perform any operation just after the response from rpx now
+ #
+ # You can use the token to make GET/POST/etc requests
+ def login(env); end
+
+
+ # This is the method that you should override if you want to
+ # perform any operation just before you reach the logout_path
+ def logout(env); end
- def call env
- @app.call(env)
- end
end
end
View
@@ -1,7 +1,36 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'rack/test'
-describe "RackRpx" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+describe Rack::Rpx, "basics" do
+ include Rack::Test::Methods
+
+ before do
+ @lambda = lambda {|env| [200, {}, ["Hello World"]] }
+ end
+
+ def app ; @app ; end
+ def app=(app) ; @app= app ; end
+
+
+ # TODO remove OPTIONS const, is just horrible
+ it 'should have a name' do
+ app = Rack::Rpx.new @lambda, :key => 'b', :secret => 'c', :name => 'olokun'
+ app.name.should == 'olokun'
+ end
+
+ it 'name should default to "default"' do
+ app= Rack::Rpx.new @lambda, :key => 'b', :secret => 'c'
+ app.name.should == 'default'
+ end
+
+ it 'should be able to access an instance of Rack::Rpx by name from within Rack application' do
+ app = Rack::Rpx lambda {|env| [200, {}, [ env['rack.oauth'].keys.inspect ]] }, :key => 'b', :secret => 'c'
+
+ #rpx = Rack::Rpx.new @app, :key => 'b', :secret => 'c', :name => 'olokun'
+ get "/"
+
+ last_response.body.should contain("olokun")
+
end
+
end
View
@@ -1,26 +1,19 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'rubygems'
require 'rack-rpx'
+require 'rack/test'
require 'spec'
require 'spec/autorun'
-require 'rackbox'
-require File.dirname(__FILE__) + '/../lib/rack-rpx'
+require 'spec/interop/test'
-RackBox.app = lambda {}
-Spec::Runner.configure do |config|
- config.use_blackbox = true
-end
-
-# returns the path to a data file, eg. data_path(:foo) => 'spec/data/foo.yml'
-def data_path name
- File.join File.dirname(__FILE__), 'data', "#{name}.yml"
-end
-
-# returns the string body of a file using data_path to get the path to the file
-def data name
- File.read data_path(name)
-end
-
+# set test environment
+# set :environment, :test
+# set :run, false
+# set :raise_errors, true
+# set :logging, false
+
+# I dont know
def mock_request_token options = {}
YAML.load data(:unauthorized_request_token).sub('AUTH_PATH', options[:authorize_path] || '/oauth/authorize')
end
View
@@ -0,0 +1,24 @@
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+require 'rack/test'
+
+
+class RackRpxTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ app = Rack::Builder.app do
+ use Rack::Rpx, :port => '9393',
+ :api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
+ :callback_path => '/login_completed'
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
+ end
+ end
+
+ def test_redirect_logged_in_users_to_dashboard
+ get "/"
+
+ assert_equal "http://localhost:9393/login_completed", last_request.url
+ assert last_response.ok?
+ end
+
+end
View
@@ -0,0 +1,10 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'rubygems'
+require 'rack-rpx'
+require 'rack/test'
+require 'spec'
+require 'spec/autorun'
+require 'spec/interop/test'
+require 'test/spec'
+

0 comments on commit 102f9d4

Please sign in to comment.