File: request_test.rb

package info (click to toggle)
ruby-web-console 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ruby: 1,496; javascript: 497; sh: 19; makefile: 4
file content (68 lines) | stat: -rw-r--r-- 1,951 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

require "test_helper"

module WebConsole
  class RequestTest < ActiveSupport::TestCase
    setup do
      Request.stubs(:permissions).returns(IPAddr.new("127.0.0.1"))
    end

    test "#permitted? is falsy for not allowed IPs" do
      req = request("http://example.com", "REMOTE_ADDR" => "0.0.0.0")

      assert_not req.permitted?
    end

    test "#permitted? is truthy for allowed IPs" do
      req = request("http://example.com", "REMOTE_ADDR" => "127.0.0.1")

      assert req.permitted?
    end

    test "#permitted? is truthy for allowed IPs via allowed proxies" do
      req = request("http://example.com", "REMOTE_ADDR" => "127.0.0.1", "HTTP_X_FORWARDED_FOR" => "127.0.0.0")

      assert req.permitted?
    end

    test "#permitted? is falsy for not allowed IPs via allowed proxies" do
      req = request("http://example.com", "REMOTE_ADDR" => "127.0.0.1", "HTTP_X_FORWARDED_FOR" => "0.0.0.0")

      assert_not req.permitted?
    end

    test "#permitted? is falsy for lying not allowed IPs via allowed proxies" do
      req = request("http://example.com", "REMOTE_ADDR" => "127.0.0.1", "HTTP_X_FORWARDED_FOR" => "10.0.0.0, 127.0.0.0")

      assert_not req.permitted?
    end

    test "#permitted? is falsy for allowed IPs via not allowed proxies" do
      req = request("http://example.com", "REMOTE_ADDR" => "10.0.0.0", "HTTP_X_FORWARDED_FOR" => "127.0.0.0")

      assert_not req.permitted?
    end

    test "#permitted? is falsy for spoofed IPs" do
      req = request("http://example.com", "HTTP_CLIENT_IP" => "127.0.0.1", "HTTP_X_FORWARDED_FOR" => "127.0.0.0")

      assert_not req.permitted?
    end

    private

      def request(*args)
        Request.new(mock_env(*args))
      end

      def mock_env(*args)
        Rack::MockRequest.env_for(*args)
      end

      def xhr(*args)
        args[1]["HTTP_X_REQUESTED_WITH"] ||= "XMLHttpRequest"
        request(*args)
      end
  end
end