Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

new test that fails

  • Loading branch information...
commit 102f9d4d90defba87db2b3b15ced374b493f1a08 1 parent a1a5464
Pedro Del Gallego authored December 17, 2009
1  examples/login-app.rb
@@ -9,6 +9,7 @@
9 9
 use Rack::Rpx, :port => '9393',
10 10
                :api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
11 11
                :callback_path => '/login_completed'
  12
+
12 13
  
13 14
 helpers do
14 15
   include Rack::Rpx::Methods
47  examples/login-hooks-sapp.rb
... ...
@@ -0,0 +1,47 @@
  1
+require 'rubygems'
  2
+require 'sinatra'
  3
+require 'rack'
  4
+require 'haml'
  5
+require 'lib/rack-rpx'
  6
+
  7
+use Rack::Session::Cookie
  8
+ 
  9
+use Rack::Rpx, :port => '9393',
  10
+               :api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
  11
+               :callback_path => '/login_completed'
  12
+ 
  13
+helpers do
  14
+  include Rack::Rpx::Methods
  15
+end
  16
+
  17
+class Rack::Rpx
  18
+  def login(env)
  19
+    puts "logging in ..."   
  20
+    env['rack.session']['user']= Rack::Rpx.credentials @req.params['token']
  21
+  end
  22
+  
  23
+  def logout(env)
  24
+    puts "logging out ..."
  25
+    env['rack.session'].delete 'user'
  26
+  end  
  27
+end
  28
+
  29
+get "/" do
  30
+  "our current session =>  #{session.inspect}, params => #{params.inspect}<br/>, #{env.inspect}"
  31
+end
  32
+
  33
+get "/login" do
  34
+  haml :login
  35
+end
  36
+
  37
+post "/login_completed" do  
  38
+  "our current #{session.inspect}"
  39
+end
  40
+
  41
+get '/logout' do
  42
+  "our current #{session.inspect}"
  43
+end
  44
+
  45
+
  46
+
  47
+
75  lib/rack-rpx.rb
@@ -6,19 +6,24 @@ module Rack #:nodoc:
6 6
   # Note: this *requires* that a Rack::Session middleware be enabled
7 7
   #
8 8
   class Rpx
  9
+    RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info"
  10
+    # Raised if an incompatible session is being used.
  11
+    class NoSession        < RuntimeError; end
  12
+    class LoginFailedError < RuntimeError; end
  13
+    
9 14
     OPTIONS = {
10  
-      :login_path      => '/login',
11 15
       :callback_path   => '/login_completed',
  16
+      :logout_path     => '/logout', 
12 17
       :host            => 'localhost',
13 18
       :port            => '80',
14  
-      :rack_session    => 'rack.session'
  19
+      :rack_session    => 'rack.session',
  20
+      :name            => 'default'
15 21
     }
16  
-
  22
+    
17 23
     # Helper methods intended to be included in your Rails controller or 
18 24
     # in your Sinatra helpers block
19 25
     module Methods
20  
-      RPX_LOGIN_URL = "https://rpxnow.com/api/v2/auth_info"
21  
-      
  26
+
22 27
       # This is *the* method you want to call.
23 28
       #
24 29
       # After you're authorized and redirected back to your #redirect_to path, 
@@ -27,6 +32,20 @@ module Methods
27 32
       # 
28 33
       # You can use the token to make GET/POST/etc requests
29 34
       def get_credentials(token)
  35
+        Rack::Rpx.get_credentials(token)
  36
+      end
  37
+
  38
+      def login_widget_url(app_name=nil)
  39
+        "https://#{app_name}.rpxnow.com/openid/v2/signin?token_url=#{callback_url}"
  40
+      end      
  41
+      
  42
+      def callback_url
  43
+        "http://#{env['HTTP_HOST']}#{OPTIONS[:callback_path]}"
  44
+      end
  45
+    end
  46
+
  47
+    class << self
  48
+      def credentials(token)
30 49
         u = URI.parse(RPX_LOGIN_URL)
31 50
         req = Net::HTTP::Post.new(u.path)
32 51
         req.set_form_data({:token => token, :apiKey => OPTIONS[:api_key], :format => 'json', :extended => 'true'})
@@ -37,25 +56,45 @@ def get_credentials(token)
37 56
         raise LoginFailedError, 'Cannot log in. Try another account!' unless json['stat'] == 'ok'
38 57
         json
39 58
       end
40  
-
41  
-      def login_widget_url(app_name)
42  
-        "https://#{app_name}.rpxnow.com/openid/v2/signin?token_url=#{callback_url}"
  59
+    end
  60
+    
  61
+    def initialize app, *options
  62
+      @app = app
  63
+      OPTIONS.merge! options.pop
  64
+      OPTIONS.each do |k,v|
  65
+        Rack::Rpx.send(:define_method, k.to_s) {OPTIONS[k]}
43 66
       end
44 67
       
  68
+    end
  69
+    
  70
+    def call env      
  71
+      @req = Rack::Request.new env 
  72
+      raise NoSession, 'No compatible session.' unless env['rack.session']
45 73
       
46  
-      def callback_url
47  
-        "http://#{OPTIONS[:host]}:#{OPTIONS[:port]}#{OPTIONS[:callback_path]}"
48  
-      end
  74
+      if env['PATH_INFO'] ==  OPTIONS[:callback_path] &&  @req.post? then 
  75
+        token = @req.params["token"]
  76
+        set_credentials(env, token) if OPTIONS[:set_credentials] 
  77
+        login(env)
  78
+      elsif env['PATH_INFO'] == OPTIONS[:logout_path] then
  79
+        logout(env)
  80
+      end        
  81
+      @app.call(env)     
49 82
     end
50 83
 
51  
-    def initialize app, *args
52  
-      @app = app     
53  
-      arg_options = args.pop
54  
-      OPTIONS.merge! arg_options      
  84
+    def set_credentials(env, token)
  85
+      env['rack.session']['credentials'] = self.get_credentials(token)
55 86
     end
  87
+
  88
+    # This is the method that you should override if you want to
  89
+    # perform any operation just after the response from rpx now
  90
+    # 
  91
+    # You can use the token to make GET/POST/etc requests
  92
+    def login(env);  end
  93
+
  94
+
  95
+    # This is the method that you should override if you want to
  96
+    # perform any operation just before you reach the logout_path
  97
+    def logout(env); end
56 98
     
57  
-    def call env
58  
-      @app.call(env)     
59  
-    end    
60 99
   end 
61 100
 end
35  spec/rack-rpx_spec.rb
... ...
@@ -1,7 +1,36 @@
1 1
 require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
  2
+require 'rack/test'
2 3
 
3  
-describe "RackRpx" do
4  
-  it "fails" do
5  
-    fail "hey buddy, you should probably rename this file and start specing for real"
  4
+describe Rack::Rpx, "basics" do  
  5
+  include Rack::Test::Methods
  6
+
  7
+  before do
  8
+    @lambda  = lambda {|env| [200, {}, ["Hello World"]] }
  9
+  end
  10
+
  11
+  def app       ; @app      ; end
  12
+  def app=(app) ; @app= app ; end
  13
+  
  14
+
  15
+  # TODO remove OPTIONS const, is just horrible
  16
+  it 'should have a name' do
  17
+    app = Rack::Rpx.new @lambda, :key => 'b', :secret => 'c', :name => 'olokun'
  18
+    app.name.should == 'olokun'
  19
+  end
  20
+
  21
+  it 'name should default to "default"' do
  22
+    app= Rack::Rpx.new @lambda,  :key => 'b', :secret => 'c'
  23
+    app.name.should == 'default'
  24
+  end
  25
+
  26
+  it 'should be able to access an instance of Rack::Rpx by name from within Rack application' do
  27
+    app = Rack::Rpx lambda {|env| [200, {}, [ env['rack.oauth'].keys.inspect ]] }, :key => 'b', :secret => 'c'
  28
+    
  29
+    #rpx = Rack::Rpx.new @app, :key => 'b', :secret => 'c', :name => 'olokun'
  30
+    get "/"
  31
+    
  32
+    last_response.body.should contain("olokun")
  33
+    
6 34
   end
  35
+  
7 36
 end
27  spec/spec_helper.rb
... ...
@@ -1,26 +1,19 @@
1 1
 $LOAD_PATH.unshift(File.dirname(__FILE__))
2 2
 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  3
+require 'rubygems'
3 4
 require 'rack-rpx'
  5
+require 'rack/test'
4 6
 require 'spec'
5 7
 require 'spec/autorun'
6  
-require 'rackbox'
7  
-require File.dirname(__FILE__) + '/../lib/rack-rpx'
  8
+require 'spec/interop/test'
8 9
 
9  
-RackBox.app = lambda {}
10  
-Spec::Runner.configure do |config|
11  
-  config.use_blackbox = true  
12  
-end
13  
- 
14  
-# returns the path to a data file, eg. data_path(:foo) => 'spec/data/foo.yml'
15  
-def data_path name
16  
-  File.join File.dirname(__FILE__), 'data', "#{name}.yml"
17  
-end
18  
- 
19  
-# returns the string body of a file using data_path to get the path to the file
20  
-def data name
21  
-  File.read data_path(name)
22  
-end
23  
- 
  10
+# set test environment
  11
+# set :environment, :test
  12
+# set :run, false
  13
+# set :raise_errors, true
  14
+# set :logging, false
  15
+
  16
+# I dont know 
24 17
 def mock_request_token options = {}
25 18
   YAML.load data(:unauthorized_request_token).sub('AUTH_PATH', options[:authorize_path] || '/oauth/authorize')
26 19
 end
24  test/rack_rpx_test.rb
... ...
@@ -0,0 +1,24 @@
  1
+require File.expand_path(File.dirname(__FILE__) + '/test_helper')
  2
+require 'rack/test'
  3
+
  4
+
  5
+class RackRpxTest < Test::Unit::TestCase
  6
+  include Rack::Test::Methods
  7
+  
  8
+  def app
  9
+    app = Rack::Builder.app do
  10
+      use Rack::Rpx, :port => '9393',
  11
+        :api_key => '5b17163d199813f86e51fc3282ffc4298a40cc44',
  12
+        :callback_path => '/login_completed'
  13
+      lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
  14
+    end
  15
+  end
  16
+
  17
+  def test_redirect_logged_in_users_to_dashboard
  18
+    get "/"
  19
+
  20
+    assert_equal "http://localhost:9393/login_completed", last_request.url
  21
+    assert last_response.ok?
  22
+  end
  23
+
  24
+end
10  test/test_helper.rb
... ...
@@ -0,0 +1,10 @@
  1
+$LOAD_PATH.unshift(File.dirname(__FILE__))
  2
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  3
+require 'rubygems'
  4
+require 'rack-rpx'
  5
+require 'rack/test'
  6
+require 'spec'
  7
+require 'spec/autorun'
  8
+require 'spec/interop/test'
  9
+require 'test/spec'
  10
+

0 notes on commit 102f9d4

Please sign in to comment.
Something went wrong with that request. Please try again.