#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!

  • veres

    veres November 5th, 2021 @ 05:09 AM

    Researchers believe the PRAGMATIC risky version of the gene makes people's lungs more susceptible to coronavirus. They hypothesise JILI SLOT that the high-risk gene derails a key protective mechanism สล็อต 123 that cells lining the lungs normally employ  to defend themselves from Covid. When cells lining the lung JOKER GAMING interact with coronavirus, one of their defence strategies is to turn into สูตรบาคาร่า 123 less specialised cells and become เว็บ 123 less welcoming to the virus. This specialisation หวยปิงปอง process reduces the amount สูตรสล็อตฟรี on the surface of cells of a key  SEXY BACCARAT protein called ACE-2, which ALLBET CASINO is key to coronavirus attaching itself to cells

  • veres

    veres November 7th, 2021 @ 12:42 AM

    Israeli minister Karine Elharrar สล็อต123 made headlines around the หวยปิงปอง world when it emerged she สูตรสล็อตฟรี couldn't attend the COP26 summit ปั่นสล็อตฟรี on Monday because it wasn't wheelchair-accessible. For JOKER GAMING many disabled people, it was a reflection of the JILI SLOT way they often feel ignored or left out of conversations about climate change.Yet disabled people PRAGMATIC are among those most "adversely affected in an emergency", according www.123VEGA.com to the United Nations Human Rights Council. And those ALLBET CASINO emergencies - from wildfires to flooding - are likely to become more frequent because of the SA GAMING climate crisis

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