<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import requests <EOL> import subprocess <EOL> from urlparse import urlparse <EOL> import lxml . html as html <EOL> import client <EOL> import server <EOL> import server_test <EOL> import ga4gh . datamodel as datamodel <EOL> import tests . paths as paths <EOL> def getClientKey ( server_url , username , password ) : <EOL> """<STR_LIT>""" <EOL> session = requests . session ( ) <EOL> loginPage = session . get ( "<STR_LIT>" . format ( server_url ) , verify = False ) <EOL> loginTree = html . fromstring ( loginPage . text ) <EOL> inputTags = loginTree . iterdescendants ( '<STR_LIT:input>' ) <EOL> state = ( tag for tag in inputTags if tag . name == '<STR_LIT:state>' ) . next ( ) . value <EOL> data = { <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT:password>' : password , <EOL> '<STR_LIT:state>' : state <EOL> } <EOL> opLocation = urlparse ( loginPage . url ) . netloc <EOL> nextUrl = '<STR_LIT>' . format ( opLocation ) <EOL> keyPage = session . post ( nextUrl , data , verify = False ) <EOL> keyTree = html . fromstring ( keyPage . text ) <EOL> sessionTokenTags = keyTree . find_class ( "<STR_LIT>" ) <EOL> if len ( sessionTokenTags ) > <NUM_LIT:0> : <EOL> sessionKey = keyTree . find_class ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . text_content ( ) <EOL> else : <EOL> raise StopIteration <EOL> return sessionKey <EOL> class TestOidc ( server_test . ServerTestClass ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def otherSetup ( cls ) : <EOL> cls . simulatedVariantSetId = datamodel . VariantSetCompoundId . obfuscate ( <EOL> paths . simulatedVariantSetId ) <EOL> requests . packages . urllib3 . disable_warnings ( ) <EOL> cls . opServer = server . OidcOpServerForTesting ( ) <EOL> cls . opServer . start ( ) <EOL> @ classmethod <EOL> def otherTeardown ( cls ) : <EOL> cls . opServer . shutdown ( ) <EOL> @ classmethod <EOL> def getServer ( cls ) : <EOL> return server . Ga4ghServerForTesting ( useOidc = True ) <EOL> def testOidc ( self ) : <EOL> serverUrl = self . server . getUrl ( ) <EOL> key = getClientKey ( serverUrl , '<STR_LIT>' , '<STR_LIT>' ) <EOL> test_client = client . ClientForTesting ( <EOL> serverUrl , flags = "<STR_LIT>" . format ( key ) ) <EOL> self . runVariantsRequest ( test_client ) <EOL> test_client . cleanup ( ) <EOL> def testOidcBadLoginPassword ( self ) : <EOL> serverUrl = self . server . getUrl ( ) <EOL> with self . assertRaises ( StopIteration ) : <EOL> getClientKey ( serverUrl , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def testOidcBadLoginKey ( self ) : <EOL> serverUrl = self . server . getUrl ( ) <EOL> test_client = client . ClientForTesting ( <EOL> serverUrl , flags = "<STR_LIT>" . format ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( subprocess . CalledProcessError ) : <EOL> test_client . runCommand ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( self . simulatedVariantSetId ) , <EOL> debugOnFail = False ) <EOL> test_client . cleanup ( ) <EOL> def testMultipleOidcClients ( self ) : <EOL> serverUrl = self . server . getUrl ( ) <EOL> key = getClientKey ( serverUrl , '<STR_LIT>' , '<STR_LIT>' ) <EOL> key2 = getClientKey ( serverUrl , '<STR_LIT>' , '<STR_LIT>' ) <EOL> client1 = client . ClientForTesting ( <EOL> serverUrl , flags = "<STR_LIT>" . format ( key ) ) <EOL> client2 = client . ClientForTesting ( <EOL> serverUrl , flags = "<STR_LIT>" . format ( key2 ) ) <EOL> self . runVariantsRequest ( client1 ) <EOL> self . runVariantsRequest ( client2 ) <EOL> client1 . cleanup ( ) <EOL> client2 . cleanup ( ) <EOL> def runVariantsRequest ( self , client ) : <EOL> self . runClientCmd ( <EOL> client , <EOL> "<STR_LIT>" . format ( <EOL> self . simulatedVariantSetId ) ) </s>
