<s> """<STR_LIT>""" <EOL> import abc <EOL> import json <EOL> import os <EOL> import re <EOL> import urlparse <EOL> from grr import gui <EOL> from grr . gui import api_auth_manager <EOL> from grr . gui import api_call_router_without_checks <EOL> from grr . gui import http_api <EOL> from grr . lib import test_lib <EOL> from grr . lib import utils <EOL> DOCUMENT_ROOT = os . path . join ( os . path . dirname ( gui . __file__ ) , "<STR_LIT>" ) <EOL> class NullAPIAuthorizationManager ( <EOL> api_auth_manager . APIAuthorizationManager ) : <EOL> """<STR_LIT>""" <EOL> def Initialize ( self ) : <EOL> pass <EOL> def GetRouterForUser ( self , unused_username ) : <EOL> return api_call_router_without_checks . ApiCallRouterWithoutChecks ( ) <EOL> class ApiCallHandlerRegressionTest ( test_lib . GRRBaseTest ) : <EOL> """<STR_LIT>""" <EOL> __abstract = True <EOL> handler = None <EOL> def setUp ( self ) : <EOL> super ( ApiCallHandlerRegressionTest , self ) . setUp ( ) <EOL> self . checks = [ ] <EOL> def NoAuthorizationChecks ( self ) : <EOL> return utils . Stubber ( api_auth_manager , "<STR_LIT>" , <EOL> NullAPIAuthorizationManager ( ) ) <EOL> def Check ( self , method , url , payload = None , replace = None ) : <EOL> """<STR_LIT>""" <EOL> parsed_url = urlparse . urlparse ( url ) <EOL> request = utils . DataObject ( method = method , <EOL> scheme = "<STR_LIT:http>" , <EOL> path = parsed_url . path , <EOL> environ = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> } , <EOL> user = "<STR_LIT:test>" ) <EOL> request . META = { <EOL> "<STR_LIT>" : "<STR_LIT:application/json>" <EOL> } <EOL> if method == "<STR_LIT:GET>" : <EOL> request . GET = dict ( urlparse . parse_qsl ( parsed_url . query ) ) <EOL> elif method == "<STR_LIT:POST>" : <EOL> request . body = json . dumps ( payload or "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % method ) <EOL> with self . NoAuthorizationChecks ( ) : <EOL> http_response = http_api . RenderHttpResponse ( request ) <EOL> content = http_response . content <EOL> xssi_token = "<STR_LIT>" <EOL> if content . startswith ( xssi_token ) : <EOL> content = content [ len ( xssi_token ) : ] <EOL> regex = re . compile ( r'<STR_LIT>' , <EOL> re . DOTALL ) <EOL> content = regex . sub ( '<STR_LIT>' , content ) <EOL> if replace : <EOL> if hasattr ( replace , "<STR_LIT>" ) : <EOL> replace = replace ( ) <EOL> for substr , repl in replace . items ( ) : <EOL> if hasattr ( substr , "<STR_LIT>" ) : <EOL> content = substr . sub ( repl , content ) <EOL> url = substr . sub ( repl , url ) <EOL> else : <EOL> content = content . replace ( substr , repl ) <EOL> url = url . replace ( substr , repl ) <EOL> parsed_content = json . loads ( content ) <EOL> check_result = dict ( method = method , url = url , <EOL> test_class = self . __class__ . __name__ , <EOL> response = parsed_content ) <EOL> if payload : <EOL> check_result [ "<STR_LIT>" ] = payload <EOL> stripped_content = http_api . HttpRequestHandler . StripTypeInfo ( parsed_content ) <EOL> if parsed_content != stripped_content : <EOL> check_result [ "<STR_LIT>" ] = stripped_content <EOL> self . checks . append ( check_result ) <EOL> @ abc . abstractmethod <EOL> def Run ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def testForRegression ( self ) : <EOL> """<STR_LIT>""" <EOL> self . maxDiff = <NUM_LIT> <EOL> with open ( os . path . join ( <EOL> DOCUMENT_ROOT , "<STR_LIT>" ) ) as fd : <EOL> prev_data = json . load ( fd ) <EOL> checks = prev_data [ self . handler ] <EOL> relevant_checks = [ ] <EOL> for check in checks : <EOL> if check [ "<STR_LIT>" ] == self . __class__ . __name__ : <EOL> relevant_checks . append ( check ) <EOL> self . Run ( ) <EOL> self . assertTrue ( self . checks ) <EOL> checks_str = json . dumps ( self . checks , indent = <NUM_LIT:2> , sort_keys = True , <EOL> separators = ( "<STR_LIT:U+002C>" , "<STR_LIT>" ) ) <EOL> prev_checks_str = json . dumps ( relevant_checks , indent = <NUM_LIT:2> , sort_keys = True , <EOL> separators = ( "<STR_LIT:U+002C>" , "<STR_LIT>" ) ) <EOL> self . assertMultiLineEqual ( prev_checks_str , checks_str ) </s>
