22
33from auth .identification import IpBasedIdentification
44from auth .tornado_auth import TornadoAuth
5+ from model .trusted_ips import TrustedIpValidator
56from tests .test_utils import mock_object
67from utils import date_utils
78
@@ -13,7 +14,7 @@ def mock_request_handler(ip=None, x_forwarded_for=None, x_real_ip=None, saved_to
1314
1415 handler_mock .application = mock_object ()
1516 handler_mock .application .auth = TornadoAuth (None )
16- handler_mock .application .identification = IpBasedIdentification (['127.0.0.1' ], user_header_name )
17+ handler_mock .application .identification = IpBasedIdentification (TrustedIpValidator ( ['127.0.0.1' ]) , user_header_name )
1718
1819 handler_mock .request = mock_object ()
1920 handler_mock .request .headers = {}
@@ -55,22 +56,22 @@ def clear_cookie(key):
5556class IpIdentificationTest (unittest .TestCase ):
5657
5758 def test_localhost_ip_trusted_identification (self ):
58- identification = IpBasedIdentification (['127.0.0.1' ], None )
59+ identification = IpBasedIdentification (TrustedIpValidator ( ['127.0.0.1' ]) , None )
5960 id = identification .identify (mock_request_handler (ip = '127.0.0.1' ))
6061 self .assertEqual ('127.0.0.1' , id )
6162
6263 def test_some_ip_trusted_identification (self ):
63- identification = IpBasedIdentification (['192.168.21.13' ], None )
64+ identification = IpBasedIdentification (TrustedIpValidator ( ['192.168.21.13' ]) , None )
6465 id = identification .identify (mock_request_handler (ip = '192.168.21.13' ))
6566 self .assertEqual ('192.168.21.13' , id )
6667
6768 def test_ip_untrusted_identification (self ):
68- identification = IpBasedIdentification ([] , None )
69+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
6970 id = identification .identify (mock_request_handler (ip = '192.168.21.13' ))
7071 self .assertNotEqual ('192.168.21.13' , id )
7172
7273 def test_ip_untrusted_identification_for_different_connections (self ):
73- identification = IpBasedIdentification ([] , None )
74+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
7475
7576 ids = set ()
7677 for _ in range (0 , 100 ):
@@ -79,22 +80,22 @@ def test_ip_untrusted_identification_for_different_connections(self):
7980 self .assertEqual (100 , len (ids ))
8081
8182 def test_ip_untrusted_identification_same_connection (self ):
82- identification = IpBasedIdentification ([] , None )
83+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
8384
8485 request_handler = mock_request_handler (ip = '192.168.21.13' )
8586 id1 = identification .identify (request_handler )
8687 id2 = identification .identify (request_handler )
8788 self .assertEqual (id1 , id2 )
8889
8990 def test_proxied_ip_behind_trusted (self ):
90- identification = IpBasedIdentification (['127.0.0.1' ], None )
91+ identification = IpBasedIdentification (TrustedIpValidator ( ['127.0.0.1' ]) , None )
9192
9293 request_handler = mock_request_handler (ip = '127.0.0.1' , x_forwarded_for = '192.168.21.13' )
9394 id = identification .identify (request_handler )
9495 self .assertEqual ('192.168.21.13' , id )
9596
9697 def test_proxied_ip_behind_untrusted (self ):
97- identification = IpBasedIdentification ([] , None )
98+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
9899
99100 request_handler = mock_request_handler (ip = '127.0.0.1' , x_forwarded_for = '192.168.21.13' )
100101 id = identification .identify (request_handler )
@@ -104,9 +105,9 @@ def test_proxied_ip_behind_untrusted(self):
104105 def test_change_to_trusted (self ):
105106 request_handler = mock_request_handler (ip = '192.168.21.13' )
106107
107- old_id = IpBasedIdentification ([] , None ).identify (request_handler )
108+ old_id = IpBasedIdentification (TrustedIpValidator ([]) , None ).identify (request_handler )
108109
109- trusted_identification = IpBasedIdentification (['192.168.21.13' ], None )
110+ trusted_identification = IpBasedIdentification (TrustedIpValidator ( ['192.168.21.13' ]) , None )
110111 new_id = trusted_identification .identify (request_handler )
111112
112113 self .assertNotEqual (old_id , new_id )
@@ -116,10 +117,10 @@ def test_change_to_trusted(self):
116117 def test_change_to_untrusted (self ):
117118 request_handler = mock_request_handler (ip = '192.168.21.13' )
118119
119- trusted_identification = IpBasedIdentification (['192.168.21.13' ], None )
120+ trusted_identification = IpBasedIdentification (TrustedIpValidator ( ['192.168.21.13' ]) , None )
120121 old_id = trusted_identification .identify (request_handler )
121122
122- new_id = IpBasedIdentification ([] , None ).identify (request_handler )
123+ new_id = IpBasedIdentification (TrustedIpValidator ([]) , None ).identify (request_handler )
123124
124125 self .assertNotEqual (old_id , new_id )
125126 self .assertNotEqual (new_id , '192.168.21.13' )
@@ -128,7 +129,7 @@ def test_change_to_untrusted(self):
128129 def test_no_cookie_change_for_same_user (self ):
129130 request_handler = mock_request_handler (ip = '192.168.21.13' )
130131
131- identification = IpBasedIdentification ([] , None )
132+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
132133
133134 identification .identify (request_handler )
134135 cookie1 = request_handler .get_cookie (COOKIE_KEY )
@@ -140,7 +141,7 @@ def test_no_cookie_change_for_same_user(self):
140141 def test_refresh_old_cookie_with_same_id (self ):
141142 request_handler = mock_request_handler (ip = '192.168.21.13' )
142143
143- identification = IpBasedIdentification ([] , None )
144+ identification = IpBasedIdentification (TrustedIpValidator ([]) , None )
144145
145146 id = '1234567'
146147 token_expiry = str (date_utils .get_current_millis () + date_utils .days_to_ms (2 ))
@@ -157,7 +158,7 @@ def test_broken_token_structure(self):
157158 request_handler = mock_request_handler (ip = '192.168.21.13' )
158159 request_handler .set_secure_cookie (COOKIE_KEY , 'something' )
159160
160- IpBasedIdentification ([] , None ).identify (request_handler )
161+ IpBasedIdentification (TrustedIpValidator ([]) , None ).identify (request_handler )
161162
162163 new_token = request_handler .get_cookie (COOKIE_KEY )
163164
@@ -167,7 +168,7 @@ def test_broken_token_timestamp(self):
167168 request_handler = mock_request_handler (ip = '192.168.21.13' )
168169 request_handler .set_secure_cookie (COOKIE_KEY , 'something&hello' )
169170
170- id = IpBasedIdentification ([] , None ).identify (request_handler )
171+ id = IpBasedIdentification (TrustedIpValidator ([]) , None ).identify (request_handler )
171172
172173 new_token = request_handler .get_cookie (COOKIE_KEY )
173174
@@ -178,7 +179,7 @@ def test_old_token_timestamp(self):
178179 request_handler = mock_request_handler (ip = '192.168.21.13' )
179180 request_handler .set_secure_cookie (COOKIE_KEY , 'something&100000' )
180181
181- id = IpBasedIdentification ([] , None ).identify (request_handler )
182+ id = IpBasedIdentification (TrustedIpValidator ([]) , None ).identify (request_handler )
182183
183184 new_token = request_handler .get_cookie (COOKIE_KEY )
184185
0 commit comments