<s> import mock <EOL> import sys <EOL> from tornado import web <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT:..>' ) <EOL> from tinman import whitelist <EOL> class RequestMock ( object ) : <EOL> def __init__ ( self , remote_ip ) : <EOL> self . application = mock . Mock ( ) <EOL> self . application . settings = dict ( ) <EOL> self . request = mock . Mock ( ) <EOL> self . request . remote_ip = remote_ip <EOL> @ whitelist . whitelisted <EOL> def whitelisted_method ( self ) : <EOL> return True <EOL> @ whitelist . whitelisted ( "<STR_LIT>" ) <EOL> def whitelisted_specific ( self ) : <EOL> return True <EOL> class WhitelistTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . request = self . _get_request ( ) <EOL> def tearDown ( self ) : <EOL> del self . request <EOL> def _get_request ( self , ip_address = '<STR_LIT>' ) : <EOL> request = RequestMock ( ip_address ) <EOL> request . application . settings [ '<STR_LIT>' ] = [ '<STR_LIT>' ] <EOL> return request <EOL> def test_empty_whitelist ( self ) : <EOL> request = RequestMock ( '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , <EOL> request . whitelisted_method , <EOL> '<STR_LIT>' ) <EOL> def test_whitelisted_ip ( self ) : <EOL> self . assertTrue ( self . request . whitelisted_method ( ) , <EOL> '<STR_LIT>' ) <EOL> def test_non_whitelisted_ip ( self ) : <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertRaises ( web . HTTPError , <EOL> request . whitelisted_method , <EOL> '<STR_LIT>' ) <EOL> def test_specific_whitelisted_ip ( self ) : <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertTrue ( request . whitelisted_specific ( ) , <EOL> '<STR_LIT>' ) <EOL> def test_specific_non_whitelisted_ip ( self ) : <EOL> self . assertRaises ( web . HTTPError , <EOL> self . request . whitelisted_specific , <EOL> '<STR_LIT>' ) <EOL> def test_invalid_whitelisted_ip ( self ) : <EOL> try : <EOL> @ whitelist . whitelisted ( <NUM_LIT> ) <EOL> def whitelisted_invalid ( self ) : <EOL> return True <EOL> except ValueError : <EOL> return <EOL> assert False , '<STR_LIT>' </s>
