Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11112: active_resouce_accepts_user_and_password.diff

File active_resouce_accepts_user_and_password.diff, 13.4 kB (added by ernesto.jimenez, 8 months ago)

Removed trailing spaces and fixing grammar errors

  • activeresource/test/authorization_test.rb

    old new  
    4545    assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1] 
    4646  end 
    4747   
     48  def test_authorization_header_explicitly_setting_username_and_password 
     49    @authenticated_conn = ActiveResource::Connection.new("http://@localhost") 
     50    @authenticated_conn.user = 'david' 
     51    @authenticated_conn.password = 'test123' 
     52    authorization_header = @authenticated_conn.send!(:authorization_header) 
     53    assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization'] 
     54    authorization = authorization_header["Authorization"].to_s.split 
     55 
     56    assert_equal "Basic", authorization[0] 
     57    assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1] 
     58  end 
     59 
     60  def test_authorization_header_explicitly_setting_username_but_no_password 
     61    @conn = ActiveResource::Connection.new("http://@localhost") 
     62    @conn.user = "david" 
     63    authorization_header = @conn.send!(:authorization_header) 
     64    authorization = authorization_header["Authorization"].to_s.split 
     65 
     66    assert_equal "Basic", authorization[0] 
     67    assert_equal ["david"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1] 
     68  end 
     69 
     70  def test_authorization_header_explicitly_setting_password_but_no_username 
     71    @conn = ActiveResource::Connection.new("http://@localhost") 
     72    @conn.password = "test123" 
     73    authorization_header = @conn.send!(:authorization_header) 
     74    authorization = authorization_header["Authorization"].to_s.split 
     75 
     76    assert_equal "Basic", authorization[0] 
     77    assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1] 
     78  end 
     79 
    4880  def test_get 
    4981    david = @authenticated_conn.get("/people/2.xml") 
    5082    assert_equal "David", david["name"] 
  • activeresource/test/base/custom_methods_test.rb

    old new  
    3232      mock.put    "/people/1/addresses/sort.xml?by=name", {}, nil, 204 
    3333      mock.post   "/people/1/addresses/new/link.xml", {}, { :street => '12345 Street' }.to_xml(:root => 'address'), 201, 'Location' => '/people/1/addresses/2.xml' 
    3434    end 
     35 
     36    Person.user = nil 
     37    Person.password = nil 
    3538  end   
    3639 
    3740  def teardown 
  • activeresource/test/base_test.rb

    old new  
    4242      mock.head   "/people/1/addresses/2.xml", {}, nil, 404 
    4343      mock.head   "/people/2/addresses/1.xml", {}, nil, 404 
    4444    end 
     45 
     46    Person.user = nil 
     47    Person.password = nil 
    4548  end 
    4649 
    4750 
     
    6871    assert_nil actor.site     
    6972  end 
    7073 
     74  def test_should_accept_setting_user 
     75    Forum.user = 'david' 
     76    assert_equal('david', Forum.user) 
     77    assert_equal('david', Forum.connection.user) 
     78  end 
     79 
     80  def test_should_accept_setting_password 
     81    Forum.password = 'test123' 
     82    assert_equal('test123', Forum.password) 
     83    assert_equal('test123', Forum.connection.password) 
     84  end 
     85 
     86  def test_user_variable_can_be_reset 
     87    actor = Class.new(ActiveResource::Base) 
     88    actor.site = 'http://cinema' 
     89    assert_nil actor.user 
     90    actor.user = 'username' 
     91    actor.user = nil 
     92    assert_nil actor.user 
     93    assert_nil actor.connection.user 
     94  end 
     95 
     96  def test_password_variable_can_be_reset 
     97    actor = Class.new(ActiveResource::Base) 
     98    actor.site = 'http://cinema' 
     99    assert_nil actor.password 
     100    actor.password = 'username' 
     101    actor.password = nil 
     102    assert_nil actor.password 
     103    assert_nil actor.connection.password 
     104  end 
     105 
    71106  def test_site_reader_uses_superclass_site_until_written 
    72107    # Superclass is Object so returns nil. 
    73108    assert_nil ActiveResource::Base.site 
     
    103138    apple = Class.new(fruit) 
    104139     
    105140    fruit.site = 'http://market' 
    106     assert_equal fruit.site, apple.site, 'subclass did not adopt changes to parent class' 
     141    assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' 
    107142     
    108143    fruit.site = 'http://supermarket' 
    109     assert_equal fruit.site, apple.site, 'subclass did not adopt changes to parent class'     
     144    assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class'     
    110145  end 
    111146   
     147  def test_user_reader_uses_superclass_user_until_written 
     148    # Superclass is Object so returns nil. 
     149    assert_nil ActiveResource::Base.user 
     150    assert_nil Class.new(ActiveResource::Base).user 
     151 
     152    # Subclass uses superclass user. 
     153    actor = Class.new(Person) 
     154    assert_equal Person.user, actor.user 
     155 
     156    # Changing subclass user doesn't change superclass user. 
     157    actor.user = 'david' 
     158    assert_not_equal Person.user, actor.user 
     159 
     160    # Changing superclass user doesn't overwrite subclass user. 
     161    Person.user = 'john' 
     162    assert_not_equal Person.user, actor.user 
     163 
     164    # Changing superclass user after subclassing changes subclass user. 
     165    jester = Class.new(actor) 
     166    actor.user = 'john.doe' 
     167    assert_equal actor.user, jester.user 
     168 
     169    # Subclasses are always equal to superclass user when not overridden 
     170    fruit = Class.new(ActiveResource::Base) 
     171    apple = Class.new(fruit) 
     172 
     173    fruit.user = 'manager' 
     174    assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class' 
     175 
     176    fruit.user = 'client' 
     177    assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class' 
     178  end 
     179 
     180  def test_password_reader_uses_superclass_password_until_written 
     181    # Superclass is Object so returns nil. 
     182    assert_nil ActiveResource::Base.password 
     183    assert_nil Class.new(ActiveResource::Base).password 
     184 
     185    # Subclass uses superclass password. 
     186    actor = Class.new(Person) 
     187    assert_equal Person.password, actor.password 
     188 
     189    # Changing subclass password doesn't change superclass password. 
     190    actor.password = 'secret' 
     191    assert_not_equal Person.password, actor.password 
     192 
     193    # Changing superclass password doesn't overwrite subclass password. 
     194    Person.password = 'super-secret' 
     195    assert_not_equal Person.password, actor.password 
     196 
     197    # Changing superclass password after subclassing changes subclass password. 
     198    jester = Class.new(actor) 
     199    actor.password = 'even-more-secret' 
     200    assert_equal actor.password, jester.password 
     201 
     202    # Subclasses are always equal to superclass password when not overridden 
     203    fruit = Class.new(ActiveResource::Base) 
     204    apple = Class.new(fruit) 
     205 
     206    fruit.password = 'mega-secret' 
     207    assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class' 
     208 
     209    fruit.password = 'ok-password' 
     210    assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class' 
     211  end 
     212 
    112213  def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects 
    113214    # Subclasses are always equal to superclass site when not overridden     
    114215    fruit = Class.new(ActiveResource::Base) 
     
    121222    assert_equal fruit.connection.site, apple.connection.site     
    122223  end 
    123224 
     225  def test_updating_baseclass_user_wipes_descendent_cached_connection_objects 
     226    # Subclasses are always equal to superclass site when not overridden 
     227    fruit = Class.new(ActiveResource::Base) 
     228    apple = Class.new(fruit) 
     229    fruit.site = 'http://market' 
     230 
     231    fruit.user = 'david' 
     232    assert_equal fruit.connection.user, apple.connection.user 
     233 
     234    fruit.user = 'john' 
     235    assert_equal fruit.connection.user, apple.connection.user 
     236  end 
     237 
     238  def test_updating_baseclass_password_wipes_descendent_cached_connection_objects 
     239    # Subclasses are always equal to superclass site when not overridden 
     240    fruit = Class.new(ActiveResource::Base) 
     241    apple = Class.new(fruit) 
     242    fruit.site = 'http://market' 
     243 
     244    fruit.password = 'secret' 
     245    assert_equal fruit.connection.password, apple.connection.password 
     246 
     247    fruit.password = 'supersecret' 
     248    assert_equal fruit.connection.password, apple.connection.password 
     249  end 
     250 
    124251  def test_collection_name 
    125252    assert_equal "people", Person.collection_name 
    126253  end 
  • activeresource/lib/active_resource/connection.rb

    old new  
    5555  # This class is used by ActiveResource::Base to interface with REST 
    5656  # services. 
    5757  class Connection 
    58     attr_reader :site 
     58    attr_reader :site, :user, :password 
    5959    attr_accessor :format 
    6060 
    6161    class << self 
     
    6868    # attribute to the URI for the remote resource service. 
    6969    def initialize(site, format = ActiveResource::Formats[:xml]) 
    7070      raise ArgumentError, 'Missing site URI' unless site 
     71      @user = @password = nil 
    7172      self.site = site 
    7273      self.format = format 
    7374    end 
     
    7576    # Set URI for remote service. 
    7677    def site=(site) 
    7778      @site = site.is_a?(URI) ? site : URI.parse(site) 
     79      @user = @site.user if @site.user 
     80      @password = @site.password if @site.password 
    7881    end 
    7982 
     83    # Set user for remote service. 
     84    def user=(user) 
     85      @user = user 
     86    end 
     87 
     88    # Set password for remote service. 
     89    def password=(password) 
     90      @password = password 
     91    end 
     92 
    8093    # Execute a GET request. 
    8194    # Used to get (find) resources. 
    8295    def get(path, headers = {}) 
     
    166179        authorization_header.update(default_header).update(headers) 
    167180      end 
    168181       
    169       # Sets authorization header; authentication information is pulled from credentials provided with site URI. 
     182      # Sets authorization header 
    170183      def authorization_header 
    171         (@site.user || @site.password ? { 'Authorization' => 'Basic ' + ["#{@site.user}:#{ @site.password}"].pack('m').delete("\r\n") } : {}) 
     184        (@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {}) 
    172185      end 
    173186 
    174187      def logger #:nodoc: 
  • activeresource/lib/active_resource/base.rb

    old new  
    8585  # == Authentication 
    8686  #  
    8787  # Many REST APIs will require authentication, usually in the form of basic 
    88   # HTTP authentication.  Authentication can be specified by putting the credentials 
    89   # in the +site+ variable of the Active Resource class you need to authenticate. 
     88  # HTTP authentication.  Authentication can be specified by: 
     89  # * putting the credentials in the +site+ variable. 
    9090  #  
    91   #   class Person < ActiveResource::Base 
    92   #     self.site = "http://ryan:password@api.people.com:3000/" 
    93   #   end 
     91  #    class Person < ActiveResource::Base 
     92  #      self.site = "http://ryan:password@api.people.com:3000/" 
     93  #    end 
    9494  #  
     95  # * defining +user+ and/or +password+ variables 
     96  #  
     97  #    class Person < ActiveResource::Base 
     98  #      self.site = "http://api.people.com:3000/" 
     99  #      self.user = "ryan" 
     100  #      self.password = "password" 
     101  #    end 
     102  #  
    95103  # For obvious security reasons, it is probably best if such services are available  
    96104  # over HTTPS. 
    97105  #  
     106  # Note: when specified in the +site+ variable, credentials such as email are not accepted 
     107  # because of restrictions from URI library. 
     108  #  
    98109  # == Errors & Validation 
    99110  # 
    100111  # Error handling and validation is handled in much the same manner as you're used to seeing in 
     
    175186      # The site variable is required ActiveResource's mapping to work. 
    176187      def site=(site) 
    177188        @connection = nil 
    178         @site = site.nil? ? nil : create_site_uri_from(site) 
     189        if site.nil? 
     190          @site = nil 
     191        else 
     192          @site = create_site_uri_from(site) 
     193          @user = @site.user if @site.user 
     194          @password = @site.password if @site.password 
     195        end 
    179196      end 
    180197 
     198      # Gets the user for REST HTTP authentication 
     199      def user 
     200        if defined?(@user) 
     201          @user 
     202        elsif superclass != Object && superclass.user 
     203          superclass.user.dup 
     204        end 
     205      end 
     206 
     207      # Sets the user for REST HTTP authentication 
     208      def user=(user) 
     209        @connection = nil 
     210        @user = user 
     211      end 
     212 
     213      # Gets the password for REST HTTP authentication 
     214      def password 
     215        if defined?(@password) 
     216          @password 
     217        elsif superclass != Object && superclass.password 
     218          superclass.password.dup 
     219        end 
     220      end 
     221 
     222      # Sets the password for REST HTTP authentication 
     223      def password=(password) 
     224        @connection = nil 
     225        @password = password 
     226      end 
     227 
    181228      # Sets the format that attributes are sent and received in from a mime type reference. Example: 
    182229      # 
    183230      #   Person.format = :json 
     
    206253      def connection(refresh = false) 
    207254        if defined?(@connection) || superclass == Object 
    208255          @connection = Connection.new(site, format) if refresh || @connection.nil? 
     256          @connection.user = user if user 
     257          @connection.password = password if password 
    209258          @connection 
    210259        else 
    211260          superclass.connection