繁体   English   中英

如何在Rails中测试多个IP地址的登录

[英]How to test login from multiple IP addresses in Rails

应客户要求,每次应用程序从不同的IP地址检测到同一用户的两个活动会话时,我必须实施发送通知电子邮件。 你是如何测试的?

创建集成测试test / integration / multiple_ip_test.rb

require 'test_helper'

@@default_ip = "127.0.0.1"

class ActionController::Request
  def remote_ip
    @@default_ip
  end
end

class MultipleIpTest < ActionDispatch::IntegrationTest
  fixtures :all

  test "send email notification if login from different ip address" do
    post_via_redirect login_path,
                      :user => {:username => "john", :password => "test"}
    assert_equal "/users/john", path

    reset!
    @@default_ip = "200.1.1.1"
    post_via_redirect login_path,
                      :user => {:username => "john", :password => "test"}
    assert_equal "/users/john", path
    assert_equal 1, ActionMailer::Base.deliveries.size
  end
end

集成测试看起来很像功能测试,但存在一些差异。 您无法使用@request更改原始IP地址。 这就是我必须打开ActionController::Request类并重新定义remote_ip方法的原因。

因为对post_via_redirect的响应总是200,而不是使用assert_response :redirect我使用URL来验证用户是否已成功登录。

reset!的电话reset! 有必要开始一个新的会议。

有关集成测试的介绍,请检查Rails指南中的测试,遗憾的是他们没有提到reset! 方法。

假设您正在使用某些框架进行登录,例如设计,以下命令将获取远程访问您的应用程序的计算机的IP:

request.remote_ip

您需要将他们使用的IP存储在模型中,然后您应该能够轻松地判断他们是否使用不同的IP进行访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM