<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import unittest <EOL> import socket <EOL> import mock <EOL> import urlparse <EOL> import logging <EOL> import mimetypes <EOL> import oic <EOL> import oic . oic . message as message <EOL> import ga4gh . frontend as frontend <EOL> import tests . utils as utils <EOL> RANDSTR = '<STR_LIT:abc>' <EOL> OICCODE = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> OICRESPONSE = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : RANDSTR , <EOL> '<STR_LIT:code>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' } <EOL> logging . captureWarnings ( True ) <EOL> def mockAuthorizationRequest ( self , request_args , state ) : <EOL> class mockResult : <EOL> def __init__ ( self ) : <EOL> self . url = None <EOL> result = mockResult ( ) <EOL> urlString = '<STR_LIT>' <EOL> result . url = urlString . format ( <EOL> request_args [ '<STR_LIT>' ] , state , request_args [ '<STR_LIT>' ] ) <EOL> return result <EOL> def mockRndstr ( size ) : <EOL> return RANDSTR <EOL> ARESP = { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT:state>' : RANDSTR , <EOL> '<STR_LIT:code>' : OICRESPONSE [ '<STR_LIT:code>' ] } <EOL> def mockParseResponse ( self , response_class , info , sformat ) : <EOL> response = message . AuthorizationResponse ( ) <EOL> response . from_dict ( ARESP ) <EOL> return response <EOL> ATR = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : RANDSTR , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : RANDSTR , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' } <EOL> def mockAccessTokenRequest ( self , scope , state , request_args ) : <EOL> response = message . AccessTokenResponse ( ) <EOL> response . from_dict ( ATR ) <EOL> return response <EOL> def mockAccessTokenRequestError ( self , scope , state , request_args ) : <EOL> return '<STR_LIT>' <EOL> class TestFrontendOidc ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> config = { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1.0> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : RANDSTR , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> frontend . reset ( ) <EOL> frontend . configure ( <EOL> baseConfig = "<STR_LIT>" , extraConfig = config , port = <NUM_LIT> ) <EOL> cls . app = frontend . app . test_client ( ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . app = None <EOL> def sendPostRequest ( self , path , request ) : <EOL> """<STR_LIT>""" <EOL> versionedPath = utils . applyVersion ( path ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : self . exampleUrl , <EOL> } <EOL> return self . app . post ( <EOL> versionedPath , headers = headers , <EOL> data = request . toJsonString ( ) ) <EOL> @ mock . patch ( '<STR_LIT>' , mockRndstr ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAuthorizationRequest ) <EOL> def testAuthorizationRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . app . get ( '<STR_LIT:/>' ) <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( <EOL> response . location ) <EOL> self . assertEqual ( netloc , '<STR_LIT>' ) <EOL> args = urlparse . parse_qs ( query , strict_parsing = True ) <EOL> self . assertEqual ( path , '<STR_LIT>' ) <EOL> self . assertEqual ( args [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> socket . gethostname ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda x : RANDSTR ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockParseResponse ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAccessTokenRequest ) <EOL> def testOidcCallbackSucceeds ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . format ( <EOL> oic . oauth2 . rndstr ( <NUM_LIT:0> ) , OICCODE <EOL> ) <EOL> with self . app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:state>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> sess [ '<STR_LIT>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> result = self . app . get ( url ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( result . location , '<STR_LIT>' . format ( <EOL> socket . gethostname ( ) ) ) <EOL> def testSessionKeyAllowsIndex ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app as app : <EOL> with app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:key>' ] = '<STR_LIT>' <EOL> app . application . tokenMap [ '<STR_LIT>' ] = RANDSTR <EOL> result = app . get ( '<STR_LIT:/>' ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( "<STR_LIT>" , result . mimetype ) <EOL> self . assertGreater ( len ( result . data ) , <NUM_LIT:0> ) <EOL> def testKeyParamAllowsIndex ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app as app : <EOL> app . application . tokenMap [ '<STR_LIT>' ] = RANDSTR <EOL> result = app . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( "<STR_LIT>" , result . mimetype ) <EOL> self . assertGreater ( len ( result . data ) , <NUM_LIT:0> ) <EOL> @ mock . patch ( '<STR_LIT>' , mockRndstr ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAuthorizationRequest ) <EOL> def testInvalidSessionKeyRedirects ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . app as app : <EOL> with app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:key>' ] = '<STR_LIT>' <EOL> result = app . get ( '<STR_LIT:/>' ) <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( <EOL> result . location ) <EOL> self . assertEqual ( netloc , '<STR_LIT>' ) <EOL> args = urlparse . parse_qs ( query , strict_parsing = True ) <EOL> self . assertEqual ( path , '<STR_LIT>' ) <EOL> self . assertEqual ( args [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> socket . gethostname ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , mockRndstr ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAuthorizationRequest ) <EOL> def testInvalidKeyParamIsUnauthorized ( self ) : <EOL> """<STR_LIT>""" <EOL> result = self . app . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT> ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda x : RANDSTR ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockParseResponse ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAccessTokenRequest ) <EOL> def testOidcCallbackBadNonce ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . format ( <EOL> oic . oauth2 . rndstr ( <NUM_LIT:0> ) , OICCODE <EOL> ) <EOL> with self . app as app : <EOL> with app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:state>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> sess [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> result = app . get ( url ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT> ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda x : RANDSTR ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockParseResponse ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAccessTokenRequest ) <EOL> def testOidcCallbackBadState ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . format ( <EOL> oic . oauth2 . rndstr ( <NUM_LIT:0> ) , OICCODE <EOL> ) <EOL> with self . app as app : <EOL> with app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:state>' ] = '<STR_LIT>' <EOL> sess [ '<STR_LIT>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> result = app . get ( url ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT> ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda x : RANDSTR ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockParseResponse ) <EOL> @ mock . patch ( '<STR_LIT>' , <EOL> mockAccessTokenRequestError ) <EOL> def testOidcCallbackBadTokenResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' . format ( <EOL> oic . oauth2 . rndstr ( <NUM_LIT:0> ) , OICCODE <EOL> ) <EOL> with self . app as app : <EOL> with app . session_transaction ( ) as sess : <EOL> sess [ '<STR_LIT:state>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> sess [ '<STR_LIT>' ] = oic . oauth2 . rndstr ( <NUM_LIT:0> ) <EOL> result = app . get ( url ) <EOL> self . assertEqual ( result . status_code , <NUM_LIT> ) <EOL> def testMimetype ( self ) : <EOL> """<STR_LIT>""" <EOL> content_type , encoding = mimetypes . guess_type ( "<STR_LIT>" ) <EOL> self . assertEqual ( content_type , "<STR_LIT:application/json>" ) </s>
