cIdZeZddlmZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddlZddlmZddlmZmZddlmZddlZddlmZdd lmZdd lmZmZmZmZmZm Z m!Z!dd lm"Z"d Z#Gd de jHZ%Gdde jHZ&Gdde jHZ'Gdde jHZ(Gdde jHZ)Gdde jHZ*Gdde jHZ+Gdde+Z,Gdde+Z-Gdd e jHZ.Gd!d"e jHZ/y#e $r ddlm Z Y$wxYw)#zTests for the Launchpad class.)contextmanagerN)patch) ServiceRoot) AccessToken Credentials)uris) Launchpad)UnencryptedFileCredentialStore)assert_keyring_not_importedBadSaveKeyring fake_keyringFauxSocketModuleInMemoryKeyringNoNetworkAuthorizationEngineNoNetworkLaunchpad)KeyringCredentialStorehttp://api.example.com/ceZdZdZdZy)TestResourceTypeClassesz&> J *d&:&:IF+     sAE2AE)E&)E2N)r!r"r#r$r3rrErJrOrTrYr^r`rbrdrkr%r rr'r'IsM:   < <F Gr r'ceZdZdZdZy)"TestServiceNameWithEmbeddedVersiona3Reject service roots that include the version at the end of the URL. If the service root is "http://api.launchpad.net/beta/" and the version is "beta", the launchpadlib constructor will raise an exception. This happens with scripts that were written against old versions of launchpadlib. The alternative is to try to silently fix it (the fix will eventually break as new versions of the web service are released) or to go ahead and make a request to http://api.launchpad.net/beta/beta/, and cause an unhelpful 404 error. c d}tjd|z} tddd||td#t$r3}|j t |jdYd}~nd}~wwxYw|dz }|jttddd||tj}tjd|zdz}|jttddd|y)Nz version-foor+) service_rootversionz*Expected a ValueError that was not thrown!zoIt looks like you're using a service root that incorporates the name of the web service version ("version-foo")/ro) rrGr AssertionErrorrgr=rB startswithrfrDEFAULT_VERSION)rrprootedefault_versions r'test_service_name_with_embedded_versionzJTestServiceNameWithEmbeddedVersion.test_service_name_with_embedded_versions !!),w6 O dD$T7 K!!MN N  OOA!!&             -<<!!),>D   4t$  s5 A1)A,,A1N)r!r"r#r$ryr%r rrmrms  & r rmc(eZdZdZdZdZdZdZy)#TestRequestTokenAuthorizationEnginez4Tests for the RequestTokenAuthorizationEngine class.cB|jtttyrrfrgr SERVICE_ROOTr2s rtest_app_must_be_identifiedz?TestRequestTokenAuthorizationEngine.test_app_must_be_identifieds  4l r c&ttdyNrapplication_namerr~r2s r$test_application_name_identifies_appzHTestRequestTokenAuthorizationEngine.test_application_name_identifies_apps $\FKr c&ttdyNr consumer_namerr2s r!test_consumer_name_identifies_appzETestRequestTokenAuthorizationEngine.test_consumer_name_identifies_apps $\Hr c|jtttdd|jtttddy)Nname1name2)rrrr}r2s r#test_conflicting_app_identificationzGTestRequestTokenAuthorizationEngine.test_conflicting_app_identificationsH   ( $!     ( #  r N)r!r"r#r$rrrrr%r rr{r{s> LI r r{ceZdZdZdZdZy)%TestLaunchpadLoginWithCredentialsFilez9Tests for Launchpad.login_with() with a credentials file.c<tj\}}tjd|}t j |}|j |jj|jjjtj|y)N not important)rcredentials_file) tempfilemkstempr login_withrload_from_pathrconsumerkey credentialsosremove)rignorefilename launchpadrs r test_filenamez3TestLaunchpadLoginWithCredentialsFile.test_filenames{#++-&11,x "00:    $ $i&;&;&D&D&H&H  (r ctj\}}t}|jtt j d||tj|y)Nr)rrcredential_store) rrrrfrgrrrr)rrrstores r+test_cannot_specify_both_filename_and_storezQTestLaunchpadLoginWithCredentialsFile.test_cannot_specify_both_filename_and_store)sS#++-&(    ) ),%"   (r N)r!r"r#r$rrr%r rrrsC  r rceZdZdZy)4TestLaunchpadLoginWithCredentialsFileFromEnvVariablectj\}}|tjd<t j d}|j |jt|j|jj|tjdtjd=|jtjjdtj|y)NLP_CREDENTIALS_FILErr)rrrenvironrrassertIsInstancerr rrunsetenv assertIsNonegetr)rrrrs rrzBTestLaunchpadLoginWithCredentialsFileFromEnvVariable.test_filename:s#++-,4 ()&11,    & &(F  33<?@ (r N)r!r"r#rr%r rrr6sr rceZdZdZdZdZy) KeyringTestz*Base class for tests that use the keyring.cRtttj_yr)r r launchpadlibrkeyringr2s rr3zKeyringTest.setUpRs#%,;+<   (r c$tj`yr)rrrr2s rtearDownzKeyringTest.tearDownYs  $ $ ,r N)r!r"r#r$r3rr%r rrrOs4=-r rceZdZdZfdZfdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZxZS)TestLaunchpadLoginWithz!Tests for Launchpad.login_with().c\tt| tj|_yr)superrr3rmkdtemptemp_dirr __class__s rr3zTestLaunchpadLoginWith.setUpas! $d13 ((* r chtt| tj|j yr)rrrshutilrmtreerrs rrzTestLaunchpadLoginWith.tearDownes! $d46 dmm$r ctjj|jd}t j dt ||jtjj|tjj|d}|jtjj||jtjjtjj|dtjj|d}|jtjj|y)Nrrrolaunchpadlib_dirapi.example.comcacher) rpathjoinrrrr~r=isdir assertFalse)rr service_pathcredentials_paths rtest_dirs_createdz(TestLaunchpadLoginWith.test_dirs_createdis77<< ~F%% %-  &678ww||$46GH   l34  bggll<&IJK77<< mD '789r ctjj|jd}tj|tj |d|j tjj|tj|}tj|j}|j|tjtjztjztj dt"|tj|}tj|j}|j%|tjtjztjzy)Nrirr)rrrrmkdirchmodr=rstatS_IMODEst_modeassertNotEqualS_IWRITES_IREADS_IEXECrrr~rrrstatinfomodes r'test_dirs_created_are_changed_to_securez>TestLaunchpadLoginWith.test_dirs_created_are_changed_to_secures77<< ~F !" !5)  &67877+,||H,,- D$--$,,">"MN%% %- 77+,||H,,- t}}t||;dllJKr ctjj|jd}t j dt ||jtjj|tj|}tj|j}|j|tjtjztjzy)Nrrr)rrrrrrr~r=rrrrrrrrrs rtest_dirs_created_are_securez3TestLaunchpadLoginWith.test_dirs_created_are_secures77<< ~F%% %-  &67877+,||H,,- t}}t||;dllJKr cHtjj|jd}t j dt |d}|j|jddt j dt |d}|j|jddy)Nrrfoo)rorrprpbar) rrrrrrr~rpassed_in_argsrrrs rtest_version_is_propagatedz1TestLaunchpadLoginWith.test_version_is_propagateds77<< ~F&11 %-  11)TestLaunchpadLoginWith.test_authorization_engine_is_propagatedsK.lr c0tjdyrrr2s rrz8TestLaunchpadLoginWith.test_consumer_name_identifies_apps %%F;r crttd}|jttj d|y);Catch an attempt to specify inconsistent application_names.zapplication name1zapplication name2rNrr~rfrgrrrs r+test_inconsistent_application_name_rejectedzBTestLaunchpadLoginWith.test_inconsistent_application_name_rejecteds9- -     ) ) !'  r cvttdd}|jttj d|y)rNconsumer_name1rconsumer_name2rrrs r(test_inconsistent_consumer_name_rejectedz?TestLaunchpadLoginWith.test_inconsistent_consumer_name_rejecteds;- $.>     ) ) !'  r c~ttddg}|jttj dddg|y)z=Catch an attempt to specify inconsistent allow_access_levels.rFOOrallow_access_levelsNBAR)rrrrrs r.test_inconsistent_allow_access_levels_rejectedzETestLaunchpadLoginWith.test_inconsistent_allow_access_levels_rejectedsD-       ) ) $!&!'  r cxd}t|}d}|jttjd||y)Ncyrr%r%r r callback1zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed..callback1 r )credential_save_failedcyrr%r%r r callback2zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed..callback2rr app name)rr)rrfrgrr)rrrrs r(test_inconsistent_credential_save_failedz?TestLaunchpadLoginWith.test_inconsistent_credential_save_failedsB 'iH     ) ) "#,  r cBtjddg}|j|jjj d|j|jjj d|j|jjdgy)Nrrr) rrrrrrrrrrrs rtest_non_desktop_integrationz3TestLaunchpadLoginWith.test_non_desktop_integration)s'11$5'  ..77;;ZH ..77HH$O   * * > > r ctjddg}|j|jjdgy)Nrr)rDESKTOP_INTEGRATION)rrrrrr s r > " # r ctt}t}tjd|jt||}|j |j jjtj|j |j jjd|j |jjdgttt j"j%|jdd||tj&}|j |j(|y)Nr)rrotimeout proxy_infor rr)rorrrrp)objectrrrr~rr access_tokenrrACCESS_TOKEN_KEYrrrrdictrrrrur)rrrrexpected_argumentss r*test_no_credentials_creates_new_credentialzATestLaunchpadLoginWith.test_no_credentials_creates_new_credentialCs(X &11 !]]%!     ! ! . . 2 2 ( 9 9    ! ! * * ; ;Z    * * > > " # "%'',,t}}.?I!&66   113EFr ctjd|jt}|j |j j jd|j |j j jdtjj|jddd}|jtjj|y)z)Test the anonymous login helper function.zanonymous access)rrorrN)rlogin_anonymouslyrr~rrrrsecretrrrrexists)rrrs rtest_anonymous_loginz+TestLaunchpadLoginWith.test_anonymous_logines&88 !]]% ..;;??D ..;;BBBG77<< MM,m=O  (89:r c tjtjj|jddtjj|jddd}t ddt dd}|j|t}t}d}tjd|jt||| }tt|||tjj|jdd  }|jD]&\}} |j|} |j| | (y) Nrrrzconsumer_secret:42z access_key:84zaccess_secret:168)consumer_secretrr)rrorrrpr)rorrrpr)rmakedirsrrrrr save_to_pathrrrr~rrrr) rcredentials_file_pathrrrrprrrexpectedactuals r-test_existing_credentials_arguments_passed_onzDTestLaunchpadLoginWith.test_existing_credentials_arguments_passed_onus# GGLL(9= I !# MM,mZ! " 0$_6IJ   !67(X &11 !]]%!  "%!'',,t}}.?I  0557 /MC--c2F   VX . /r c tjjd}|jtjd<t j dt }||tjd<ntjd=|jd}tjjtjj|dd}|j|tjj|jd|jtjjtjj|ddy)NHOMErrrrz..z .launchpadlibr)rrrrrrr~rrabspathrrr=r)rold_homer cache_dirrs rtest_None_launchpadlib_dirz1TestLaunchpadLoginWith.test_None_launchpadlib_dirs::>>&)!]] 6&11 \   !)BJJv  6",,W5 77??277<< 4+NO  bggll4==/J   GGNN -/@'J  r cltjdd}|j|jddtjdtj d}|j|jdtj dd}|j ttjddy)Nrr+roz"https://api.staging.launchpad.net/)rzhttps://r)rrrrrrGrfrgr s rtest_short_service_namez.TestLaunchpadLoginWith.test_short_service_names&11*iH    $ $^ 4 0 '11 **95    $ $^ 4   y ) -   *55z5 r c2tjddy)Nr)max_failed_attemptsrr2s r!test_max_failed_attempts_acceptedz8TestLaunchpadLoginWith.test_max_failed_attempts_accepteds %%o1Mr )r!r"r#r$r3rrrrrrrrrrrrrrr rrrr%r+r-r1 __classcell__)rs@rrr^s++%:.L& LE2 :;E ? <       &     GD; $/L 2 2Nr rceZdZdZdZdZy)TestDeprecatedLoginMethodsz2Make sure the deprecated login methods still work.cbtjd5}tjdtjdtt j ddd|j t|d|j |d jtdddy#1swYyxYw) NTr5r7rrtokenrr8r) r9r:r;PendingDeprecationWarningrloginrr<r?r@rrCs rtest_login_is_deprecatedz3TestDeprecatedLoginMethods.test_login_is_deprecateds  $ $D 1 EV  ! !( +  ! !(,E F  $ $Z( C   S[! ,   VAY//1C D  E E Es BB%%B.ctjd5}tjdtjdttjddt j d|jt|djd|j|djtdddy#1swYyxYw) NTr5r7rz.*next release of cryptographyrrzThe Launchpad.get_token_and_login() method is deprecated. You should use Launchpad.login_anonymous() for anonymous access and Launchpad.login_with() for all other purposes.) r9r:r;r7filterwarningsrget_token_and_loginrrBmessager?r@r9s r&test_get_token_and_login_is_deprecatedzATestDeprecatedLoginMethods.test_get_token_and_login_is_deprecateds  $ $D 1 EV  ! !( +  ! !(,E F  # #;   2 2: >   F1I%%&L    VAY//1C D E E Es B&CCN)r!r"r#r$r:r?r%r rr4r4s<EEr r4ceZdZdZdZej ededdZ ej ededdZ ej ededd Z y ) !TestCredenitialSaveFailedCallbackcpttj_t j |_yrrrrsocketrrrr2s rr3z'TestCredenitialSaveFailedCallback.setUp%)9(: % ((* r ctttj_tj|j yrrDrrrrrr2s rrz*TestCredenitialSaveFailedCallback.tearDown (. % dmm$r _is_sudocyNFr%r%r rz*TestCredenitialSaveFailedCallback.r c$gfd}tjj|jd}d}t t 5t jd||||jtddddy#1swYyxYw)Nc(jdyr)append)callback_calledsrcallbackzPTestCredenitialSaveFailedCallback.test_credentials_save_failed..callbacks  " "4 (r rrr)rorrr8) rrrrr r rrrr<)rrRrrorQs @rtest_credentials_save_failedz>TestCredenitialSaveFailedCallback.test_credentials_save_faileds  ) 77<< ~F0 .* + 6  ) ))!1'/     S11 5 6 6 6s 5BBcyrKr%r%r rrLz*TestCredenitialSaveFailedCallback.rMr ctjj|jd}d}t t 5|j ttjd||dddy#1swYyxYwNrrrr) rrrrr r rf RuntimeErrorrrrrros r:test_default_credentials_save_failed_is_to_raise_exceptionz\TestCredenitialSaveFailedCallback.test_default_credentials_save_failed_is_to_raise_exceptionsj77<< ~F0 .* +    "--)!1     s )A22A;cy)NTr%r%r rrLz*TestCredenitialSaveFailedCallback.&rMr ctjj|jd}d}t t 5t jd||dddy#1swYyxYwrV)rrrrr r rrrXs r>test_credentials_save_fail_under_sudo_does_not_raise_exceptionz`TestCredenitialSaveFailedCallback.test_credentials_save_fail_under_sudo_does_not_raise_exception&sY77<< ~F0 .* +   ) ))!1    s A""A+N) r!r"r#r3rrrr staticmethodrSrYr\r%r rrArAs+%U\\$j,}2MN6O6,U\\$j,}2MN O U\\$j,|2LM N r rAceZdZdZdZej ededdZ ej ededdZ y) TestMultipleSitescpttj_t j |_yrrCr2s rr3zTestMultipleSites.setUp9rEr ctttj_tj|j yrrGr2s rrzTestMultipleSites.tearDown@rHr rIcyrKr%r%r rrLzTestMultipleSites.DrMr ctjj|jd}t }d}d}t |5t j|||}|jjj}dddt|jjdd}|j|||j||j||dz|zy#1swYoxYw)NrrzSuper App 3000rrr8@)rrrrrr rrrrrlistdatarHrAr)rrrrorrrapplication_keys r"test_components_of_application_keyz4TestMultipleSites.test_components_of_application_keyDs77<< ~F!#0 + ' " ?*55 )!1I &11::>>M  ?w||0023A6q9 lO4 m_5 -#*= *LM' ? ?s 9C++C4cyrKr%r%r rrLzTestMultipleSites._rMr cJtjj|jd}t }|j rJdt |5tjdd|tjdd|dddt|j jdk(sJt|j jdd }t|j jd d }|j||y#1swYxYw) Nrz$oops, a fresh keyring has data in itrzhttp://alpha.example.com/rzhttp://beta.example.com/rr8) rrrrrrfr rrr<rHrer)rrrapplication_key_1application_key_2s rtest_same_app_different_serversz1TestMultipleSites.test_same_app_different_servers_s77<< ~F!#<<G!GG ' "   ) )"8!1   ) )"7!1   7<<$$&'1,,, !2!2!45a8; !2!2!45a8; -/@A)  s 1DD"N) r!r"r#r3rrrrr]rhrnr%r rr_r_4sd +%U\\$j,}2MNNON4U\\$j,}2MNBOBr r_)0r$type __metaclass__ contextlibrrrrDrrunittest unittest.mockr ImportErrormockr9lazr.restfulclient.resourcerlaunchpadlib.credentialsrrrrlaunchpadlib.launchpadr r launchpadlib.testing.helpersr r r rrrrrr~TestCaserr'rmr{rrrrr4rAr_r%r rr{sK"% % #3 ,C ) Ih//IqGh''qGh5 ):):5 p  (*;*;  FH,=,=<8;L;L2 -(## -rN[rNj EE<@(9(9@FEB))EBksE EE