#85 ✓resolved
kgiszczak

problems with passing remote_ip to created session

Reported by kgiszczak | April 9th, 2009 @ 01:28 PM

Hi, Ben

class Authlogic::TestCase::MockRequest stubs 'remote_ip' method and always returns "1.1.1.1", like this:


module Authlogic
  module TestCase
    class MockRequest # :nodoc:
      def request_method
        nil
      end
      
      def remote_ip
        "1.1.1.1"
      end
    end
  end
end

I can't test my admin authentication mechanism becouse it depends on ip address(admins can only login from authorized ips). My test looked like this:


context "on GET to :index" do
  setup do
    @request.env['REMOTE_ADDR'] = "127.0.0.1"
    AdminSession.create(Factory(:admin))
    get :index
  end

  should_respond_with :success
end

This worked in previous versions of authlogic. Now this test fails becouse admin session is always created with address returned by stubbed method.

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson April 9th, 2009 @ 01:42 PM

    • State changed from “new” to “resolved”

    Ok, I pushed out a change, update from the repo. This should be fixed.

  • kgiszczak

    kgiszczak April 9th, 2009 @ 02:17 PM

    Hi,

    I'm not shure but it looks like class Authlogic::TestCase::MockController(the one you made changes) isn't used in test, instead tests uses class Authlogic::TestCase::ControllerAdapter. When I changed 'request' method to looks like the one from MockController:

    
    def request
      @request ||= MockRequest.new(controller)
    end
    

    , I get this errors:

    
    NameError: uninitialized constant Authlogic::TestCase::MockRequest::Hahs
        app/models/admin_session.rb:12:in `check_connection_from_authorized_ip'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/proxy/create.rb:5:in `result'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:293:in `run'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:237:in `create'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:268:in `send'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl/factory.rb:268:in `default_strategy'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-factory_girl-1.2.0/lib/factory_girl.rb:20:in `Factory'
        /test/functional/home_controller_test.rb:29:in `__bind_1239300577_856972'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:271:in `call'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:271:in `run_current_setup_blocks'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:270:in `each'
        /Library/Ruby/Gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:270:in `run_current_setup_blocks'
    
  • kgiszczak

    kgiszczak April 10th, 2009 @ 05:58 AM

    Hi,

    I've found out what's going on. First of all class Authlogic::TestCase::MockController isn't used in tests. I made changes to class Authlogic::TestCase::ControllerAdapter. It looks like this:

    
    module Authlogic
      module TestCase
        # Adapts authlogic to work with the @request object when testing. This way Authlogic can set cookies and what not before
        # a request is made, ultimately letting you log in users in functional tests.
        class ControllerAdapter < ControllerAdapters::AbstractAdapter
          def authenticate_with_http_basic(&block)
          end
          
          def cookies
            new_cookies = {}
            super.each do |key, value|
              new_cookies[key] = value[:value]
            end
            new_cookies
          end
          
          def cookie_domain
            nil
          end
          
          def request
            @request ||= MockRequest.new(controller)
          end
          
          def request_content_type
            request.format.to_s
          end
        end
      end
    end
    

    The next thing which was incorrect was typo - you used Hahs instead of Hash(I haven't noticed it earlier). So after updating 'remote_ip' method in class MockRequest to this:

    
    def remote_ip
      (controller && controller.respond_to?(:env) && controller.env.is_a?(Hash) && controller.env['REMOTE_ADDR']) || "1.1.1.1"
    end
    

    all tests pass perfectly.

  • kgiszczak

    kgiszczak April 10th, 2009 @ 07:02 AM

    I've just noticed that you released new version with this changes. Big thanks!

  • kgiszczak

    kgiszczak April 10th, 2009 @ 07:07 AM

    One more request. Can you add 'accept_language' method to class MockRequest. I get this error now:

    
    NoMethodError: undefined method `accept_language' for #<Authlogic::TestCase::MockRequest:0x34519d0>
    

    Thanks!

  • Leah Bayly

    Leah Bayly July 10th, 2019 @ 02:20 AM

    Problems are going to passing through the remote function on this that was good among the created session. I have to get the solution that was at edubirdie uk reviews this will have the all dormers that will subscribe among of them it was useful for me.

  • mj

    mj July 17th, 2019 @ 01:42 AM

    Good information. thank you for sharing information,رزرو هتل رم I want to introduce you to the site for traveling. رزرو هتل زوریخ On-line reservation of the user's flight ticket at different points and having a mobile Internet connection can reserve the desired ticket. رزرو هتل ژنو I would love to share articles and receive articles from this author. رزرو هتل کراچی Please visit our site. رزرو هتل لس آنجلس good luck

  • rosstaylor505

    rosstaylor505 January 1st, 2020 @ 03:13 AM

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    https://diigo.com/0g9ott

  • 123VEGA

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database.

People watching this ticket

Pages