
    cI                        d Z eZddlmZ ddlZddlZddlZddlZddl	Z	ddl
Z
	 ddlmZ ddlZddlmZ ddlmZmZ ddlmZ ddlZddlmZ dd	lmZ dd
lmZmZmZmZmZm Z m!Z! ddlm"Z" dZ# G d de
jH                        Z% G d de
jH                        Z& G d de
jH                        Z' G d de
jH                        Z( G d de
jH                        Z) G d de
jH                        Z* G d de
jH                        Z+ G d de+      Z, G d de+      Z- G d d e
jH                        Z. G d! d"e
jH                        Z/y# e$ r
 ddlmZ Y $w xY w)#zTests for the Launchpad class.    )contextmanagerN)patch)ServiceRoot)AccessTokenCredentials)uris)	Launchpad)UnencryptedFileCredentialStore)assert_keyring_not_importedBadSaveKeyringfake_keyringFauxSocketModuleInMemoryKeyringNoNetworkAuthorizationEngineNoNetworkLaunchpad)KeyringCredentialStorehttp://api.example.com/c                       e Zd ZdZd Zy)TestResourceTypeClassesz<launchpadlib must know about restfulclient's resource types.c                     t         j                  j                         D ](  \  }}| j                  t        j                  |   |       * y N)r   RESOURCE_TYPE_CLASSESitemsassertEqualr	   )selfnameclss      C/usr/lib/python3/dist-packages/launchpadlib/tests/test_launchpad.pytest_resource_typesz+TestResourceTypeClasses.test_resource_typesB   sC     %::@@B 	IID#Y<<TBCH	I    N)__name__
__module____qualname____doc__r    r    r   r   r   ?   s    FIr    r   c                   \    e Zd ZdZd Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zy)TestNameLookupsz0Test the utility functions in the 'uris' module.c                 (    t        g d      | _        y )N)
production	qastagingstagingdogfooddevtest_devedge)sortedaliasesr   s    r   setUpzTestNameLookups.setUpL   s    

r    c              #   Z  K   t        j                  d      5 }t        j                  d       d  | j                  t	        |      d       |\  }| j                  t        |j                  t                     | j                  dt        |             d d d        y # 1 sw Y   y xY ww)NTrecordalways   zno longer exists)warningscatch_warningssimplefilterr   len
assertTrue
issubclasscategoryDeprecationWarningassertInstr)r   caughtwarnings      r   edge_deprecation_errorz&TestNameLookups.edge_deprecation_errorY   s      $$D1 	<V!!(+S[!,JWOOJw'7'79KLMMM,c'l;	< 	< 	<s   B+A=B	B+B($B+c                    | j                  t        t        j                  j	                               | j
                         | j                  t        t        j                  j	                               | j
                         y r   )r   r0   r   service_rootskeysr1   	web_rootsr2   s    r   test_short_namesz TestNameLookups.test_short_namesf   sP     2 2 7 7 9:DLLI 3 3 56Er    c                     | j                         5  | j                  t        j                  d      t        j                  d             d d d        y # 1 sw Y   y xY wNr/   r)   rE   r   r   lookup_service_rootr2   s    r   $test_edge_service_root_is_productionz4TestNameLookups.test_edge_service_root_is_productionk   sO     ((* 	((0((6	 	 	   9AAc                     | j                         5  | j                  t        j                  d      t        j                  d             d d d        y # 1 sw Y   y xY wrL   rE   r   r   lookup_web_rootr2   s    r    test_edge_web_root_is_productionz0TestNameLookups.test_edge_web_root_is_productiont   sO     ((* 	$$V,$$\2	 	 	rP   c                     | j                         5  | j                  t        j                  t        j                        t        j                  d             d d d        y # 1 sw Y   y xY wNr)   rE   r   r   rN   EDGE_SERVICE_ROOTr2   s    r   -test_edge_service_root_url_becomes_productionz=TestNameLookups.test_edge_service_root_url_becomes_production}   S    ((* 	(()?)?@((6	 	 	   AA!!A*c                     | j                         5  | j                  t        j                  t        j                        t        j                  d             d d d        y # 1 sw Y   y xY wrV   )rE   r   r   rS   EDGE_WEB_ROOTr2   s    r   )test_edge_web_root_url_becomes_productionz9TestNameLookups.test_edge_web_root_url_becomes_production   sS    ((* 	$$T%7%78$$\2	 	 	r[   c                     | j                         5  | j                  t        j                  t        j                        t        j                  d             d d d        y # 1 sw Y   y xY wrV   rW   r2   s    r   /test_top_level_edge_constant_becomes_productionz?TestNameLookups.test_top_level_edge_constant_becomes_production   rZ   r[   c                     | j                         5  | j                  t        j                  d      t        j                  d             d d d        y # 1 sw Y   y xY w)Nzhttps://api.edge.launchpad.net/r)   rM   r2   s    r   5test_edge_server_equivalent_string_becomes_productionzETestNameLookups.test_edge_server_equivalent_string_becomes_production   sN    ((* 	(()JK((6	 	 	rP   c                     | j                         5  | j                  t        j                  d      t        j                  d             d d d        y # 1 sw Y   y xY w)Nzhttps://edge.launchpad.net/r)   rR   r2   s    r   9test_edge_web_server_equivalent_string_becomes_productionzITestNameLookups.test_edge_web_server_equivalent_string_becomes_production   sN    ((* 	$$%BC$$\2	 	 	rP   c                    | j                         5  | j                  D ]8  }| j                  t        j                  |      t        j
                  |          : 	 ddd       | j                         5  | j                  D ]8  }| j                  t        j                  |      t        j                  |          : 	 ddd       d}| j                  t        j                  |      |       | j                  t        j                  |      |       d}| j                  t        t        j                  |       | j                  t        t        j                  |       y# 1 sw Y   xY w# 1 sw Y   xY w)z=Ensure that short service names turn into long service names.Nzhttp://some-other-server.comz	not-a-url)
rE   r1   r   r   rN   rG   rS   rI   assertRaises
ValueError)r   alias
other_root	not_a_urls       r   test_lookupszTestNameLookups.test_lookups   s2   
 ((* 	   ,,U3T5G5G5N	 ((* 	   ((/1F	 4
11*=zJ--j9:F  	*d&>&>	J*d&:&:IF+	 		 	s   AE2AE)E&)E2N)r!   r"   r#   r$   r3   r   rE   rJ   rO   rT   rY   r^   r`   rb   rd   rk   r%   r    r   r'   r'   I   sM    :
 
< 
<F
Gr    r'   c                       e Zd ZdZd Zy)"TestServiceNameWithEmbeddedVersiona3  Reject 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}t         j                  d   |z   }	 t        d d d ||       t        d      # t        $ r3}| j                  t        |      j                  d             Y d }~nd }~ww xY w|dz  }| j                  t        t        d d d ||       t        j                  }t         j                  d   |z   dz   }| j                  t        t        d d d |       y )Nzversion-foor+   )service_rootversionz*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   )r   rG   r	   AssertionErrorrg   r=   rB   
startswithrf   r   DEFAULT_VERSION)r   rp   rootedefault_versions        r   'test_service_name_with_embedded_versionzJTestServiceNameWithEmbeddedVersion.test_service_name_with_embedded_version   s      !!),w6	OdD$T7K !!MNN  	OOA!!& 	 	 	 	
 -<<!!),>D	4t$ 	 	
s   5 	A1)A,,A1N)r!   r"   r#   r$   ry   r%   r    r   rm   rm      s    &
r    rm   c                   (    e Zd ZdZd Zd Zd Zd Zy)#TestRequestTokenAuthorizationEnginez4Tests for the RequestTokenAuthorizationEngine class.c                 B    | j                  t        t        t               y r   rf   rg   r   SERVICE_ROOTr2   s    r   test_app_must_be_identifiedz?TestRequestTokenAuthorizationEngine.test_app_must_be_identified   s    4l	
r    c                 &    t        t        d       y Nr   application_namer   r~   r2   s    r   $test_application_name_identifies_appzHTestRequestTokenAuthorizationEngine.test_application_name_identifies_app   s    $\FKr    c                 &    t        t        d       y Nr   consumer_namer   r2   s    r   !test_consumer_name_identifies_appzETestRequestTokenAuthorizationEngine.test_consumer_name_identifies_app   s    $\Hr    c                     | j                  t        t        t        dd       | j                  t        t        t        dd       y )Nname1name2)r   r   r   r}   r2   s    r   #test_conflicting_app_identificationzGTestRequestTokenAuthorizationEngine.test_conflicting_app_identification  sH    ($! 	 	
 	(#  	 	
r    N)r!   r"   r#   r$   r   r   r   r   r%   r    r   r{   r{      s    >

LI
r    r{   c                       e Zd ZdZd Zd Zy)%TestLaunchpadLoginWithCredentialsFilez9Tests for Launchpad.login_with() with a credentials file.c                 <   t        j                         \  }}t        j                  d|      }t	        j
                  |      }| j                  |j                  j                  |j                  j                  j                         t        j                  |       y )Nnot important)r   credentials_file)tempfilemkstempr   
login_withr   load_from_pathr   consumerkeycredentialsosremove)r   ignorefilename	launchpadr   s        r   test_filenamez3TestLaunchpadLoginWithCredentialsFile.test_filename  s{    #++-&11,x
	 "00:  $$i&;&;&D&D&H&H	
 			(r    c                     t        j                         \  }}t               }| j                  t        t
        j                  d||       t        j                  |       y )Nr   )r   r   credential_store)	r   r   r   rf   rg   r   r   r   r   )r   r   r   stores       r   +test_cannot_specify_both_filename_and_storezQTestLaunchpadLoginWithCredentialsFile.test_cannot_specify_both_filename_and_store)  sS    #++-&()),%" 	 	
 			(r    N)r!   r"   r#   r$   r   r   r%   r    r   r   r     s    C
r    r   c                       e Zd Zd Zy)4TestLaunchpadLoginWithCredentialsFileFromEnvVariablec                    t        j                         \  }}|t        j                  d<   t	        j
                  d      }| j                  |j                  t               | j                  |j                  j                  |       t        j                  d       t        j                  d= | j                  t        j                  j                  d             t        j                  |       y )NLP_CREDENTIALS_FILEr   r   )r   r   r   environr   r   assertIsInstancer   r
   r   r   unsetenvassertIsNonegetr   )r   r   r   r   s       r   r   zBTestLaunchpadLoginWithCredentialsFileFromEnvVariable.test_filename:  s    #++-,4

()&11,
	 	&&(F	
 	33<<hG
)*JJ,-"**..)>?@
		(r    N)r!   r"   r#   r   r%   r    r   r   r   6  s    r    r   c                       e Zd ZdZd Zd Zy)KeyringTestz*Base class for tests that use the keyring.c                 R    t                t               t        j                  _        y r   )r   r   launchpadlibr   keyringr2   s    r   r3   zKeyringTest.setUpR  s    #% ,;+<  (r    c                 $    t         j                  `y r   )r   r   r   r2   s    r   tearDownzKeyringTest.tearDownY  s    $$,r    N)r!   r"   r#   r$   r3   r   r%   r    r   r   r   O  s    4=-r    r   c                        e Zd ZdZ fdZ fdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )TestLaunchpadLoginWithz!Tests for Launchpad.login_with().c                 \    t         t        |           t        j                         | _        y r   )superr   r3   r   mkdtemptemp_dirr   	__class__s    r   r3   zTestLaunchpadLoginWith.setUpa  s!    $d13 ((*r    c                 h    t         t        |           t        j                  | j
                         y r   )r   r   r   shutilrmtreer   r   s    r   r   zTestLaunchpadLoginWith.tearDowne  s!    $d46dmm$r    c                    t         j                  j                  | j                  d      }t	        j
                  dt        |       | j                  t         j                  j                  |             t         j                  j                  |d      }| j                  t         j                  j                  |             | j                  t         j                  j                  t         j                  j                  |d                   t         j                  j                  |d      }| j                  t         j                  j                  |             y )Nr   r   ro   launchpadlib_dirapi.example.comcacher   )
r   pathjoinr   r   r   r~   r=   isdirassertFalse)r   r   service_pathcredentials_paths       r   test_dirs_createdz(TestLaunchpadLoginWith.test_dirs_createdi  s     77<<~F%%%-	
 	&678ww||$46GHl34 	bggll<&IJK 77<<mD'789r    c                    t         j                  j                  | j                  d      }t        j                  |       t        j
                  |d       | j                  t         j                  j                  |             t        j                  |      }t        j                  |j                        }| j                  |t        j                  t        j                  z  t        j                  z         t        j                   dt"        |       t        j                  |      }t        j                  |j                        }| j%                  |t        j                  t        j                  z  t        j                  z         y )Nr   i  r   r   )r   r   r   r   mkdirchmodr=   r   statS_IMODEst_modeassertNotEqualS_IWRITES_IREADS_IEXECr   r   r~   r   r   r   statinfomodes       r   'test_dirs_created_are_changed_to_securez>TestLaunchpadLoginWith.test_dirs_created_are_changed_to_secure  s    77<<~F
!"
!5)&67877+,||H,,-D$--$,,">"MN%%%-	
 77+,||H,,-t}}t||;dllJKr    c                    t         j                  j                  | j                  d      }t	        j
                  dt        |       | j                  t         j                  j                  |             t        j                  |      }t        j                  |j                        }| j                  |t        j                  t        j                  z  t        j                  z         y )Nr   r   r   )r   r   r   r   r   r   r~   r=   r   r   r   r   r   r   r   r   r   s       r   test_dirs_created_are_securez3TestLaunchpadLoginWith.test_dirs_created_are_secure  s    77<<~F%%%-	

 	&67877+,||H,,-t}}t||;dllJKr    c                 H   t         j                  j                  | j                  d      }t	        j
                  dt        |d      }| j                  |j                  d   d       t	        j
                  dt        |d      }| j                  |j                  d   d       y )Nr   r   foo)ro   r   rp   rp   bar)	r   r   r   r   r   r   r~   r   passed_in_argsr   r   r   s      r   test_version_is_propagatedz1TestLaunchpadLoginWith.test_version_is_propagated  s     77<<~F&11%-	
	 	11)<eD '11%-	
	 	11)<eDr    c                    t         j                  j                  | j                  d      }t	        j
                  dt        |      }| j                  |j                  j                  j                  d       t	        j
                  dt        |      }| j                  |j                  j                  j                  d       y )Nr   zvery importantr   )r   r   r   r   r   r   r~   r   r   r   r   r   s      r   #test_application_name_is_propagatedz:TestLaunchpadLoginWith.test_application_name_is_propagated  s    
 77<<~F&11%-
	
 	!!**;;=M	
 '11%-
	
 	!!**;;=M	
r    c                     t        t        d      }t        j                  |       | j	                  |j
                  d       | j	                  |j                  d       y )Napplication nameauthorization_enginer8   )r   r~   r   r   r   request_tokens_obtainedaccess_tokens_obtainedr   engines     r   'test_authorization_engine_is_propagatedz>TestLaunchpadLoginWith.test_authorization_engine_is_propagated  sK     .l<NO%%6B77;66:r    c                 L    | j                  t        t        j                         y r   )rf   rg   r   r   r2   s    r   )test_login_with_must_identify_applicationz@TestLaunchpadLoginWith.test_login_with_must_identify_application  s     	*&8&C&CDr    c                 0    t        j                  d       y r   r   r   r2   s    r   r   z;TestLaunchpadLoginWith.test_application_name_identifies_app  s     	%%v>r    c                 0    t        j                  d       y r   r   r2   s    r   r   z8TestLaunchpadLoginWith.test_consumer_name_identifies_app  s     	%%F;r    c                 r    t        t        d      }| j                  t        t        j
                  d|       y);Catch an attempt to specify inconsistent application_names.zapplication name1zapplication name2r   Nr   r~   rf   rg   r   r   r   s     r   +test_inconsistent_application_name_rejectedzBTestLaunchpadLoginWith.test_inconsistent_application_name_rejected  s9    --
 	))!'	 	 	
r    c                 v    t        t        dd      }| j                  t        t        j
                  d|       y)r   Nconsumer_name1r   consumer_name2r   r   r   s     r   (test_inconsistent_consumer_name_rejectedz?TestLaunchpadLoginWith.test_inconsistent_consumer_name_rejected  s;    -$.>
 	))!'	 	 	
r    c                 ~    t        t        ddg      }| j                  t        t        j
                  dddg|       y)z=Catch an attempt to specify inconsistent allow_access_levels.r   FOOr   allow_access_levelsNBAR)r   r   r   r   r   s     r   .test_inconsistent_allow_access_levels_rejectedzETestLaunchpadLoginWith.test_inconsistent_allow_access_levels_rejected  sD    -

 	))$!&!' 	 	
r    c                 x    d }t        |      }d }| j                  t        t        j                  d||       y )Nc                       y r   r%   r%   r    r   	callback1zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback1      r    )credential_save_failedc                       y r   r%   r%   r    r   	callback2zRTestLaunchpadLoginWith.test_inconsistent_credential_save_failed.<locals>.callback2  r  r    app name)r   r  )r   rf   rg   r   r   )r   r  r   r  s       r   (test_inconsistent_credential_save_failedz?TestLaunchpadLoginWith.test_inconsistent_credential_save_failed  sB    	 'iH	 	))"#, 	 	
r    c                 B   t        j                  ddg      }| j                  |j                  j                  j
                  d       | j                  |j                  j                  j                  d        | j                  |j                  j                  dg       y )Nr   r   r   )	r   r   r   r   r   r   r   r   r   r   r   s     r   test_non_desktop_integrationz3TestLaunchpadLoginWith.test_non_desktop_integration)  s     '11$5'
	 	..77;;ZH..77HH$O**>>	
r    c                     t        j                  ddg      }| j                  |j                  j                  dg       y )Nr   r   )r   DESKTOP_INTEGRATION)r   r   r   r   r   r
  s     r   <test_desktop_integration_doesnt_happen_without_consumer_namezSTestLaunchpadLoginWith.test_desktop_integration_doesnt_happen_without_consumer_name6  s@    
 '11UG
	 	**>>"#	
r    c                 t   t               }t               }t        j                  d| j                  t        ||      }| j                  |j                  j                  j                  t        j                         | j                  |j                  j                  j                  d       | j                  |j                  j                  dg       t        t        t         j"                  j%                  | j                  dd      ||t        j&                        }| j                  |j(                  |       y )Nr  )r   ro   timeout
proxy_infor  r   r   )ro   r   r  r  rp   )objectr   r   r   r~   r   r   access_tokenr   r   ACCESS_TOKEN_KEYr   r   r   r   dictr   r   r   ru   r   )r   r  r  r   expected_argumentss        r   *test_no_credentials_creates_new_credentialzATestLaunchpadLoginWith.test_no_credentials_creates_new_credentialC  s    (X
&11!]]%!
	 	!!..22(99	
 	!!**;;Z	
 	**>>"#	
 "%'',,t}}.?I!&66
 	113EFr    c                    t        j                  d| j                  t              }| j	                  |j
                  j                  j                  d       | j	                  |j
                  j                  j                  d       t        j                  j                  | j                  ddd      }| j                  t        j                  j                  |             y)z)Test the anonymous login helper function.zanonymous access)r   ro    r   r   N)r   login_anonymouslyr   r~   r   r   r  r   secretr   r   r   r   exists)r   r   r   s      r   test_anonymous_loginz+TestLaunchpadLoginWith.test_anonymous_logine  s    &88!]]%
	
 	..;;??D..;;BBBG 77<<MM,m=O
 	(89:r    c                    t        j                  t         j                  j                  | j                  dd             t         j                  j                  | j                  ddd      }t        ddt        dd            }|j                  |       t               }t               }d}t        j                  d| j                  t        |||	      }t        t        |||t         j                  j                  | j                  dd
            }|j                         D ]&  \  }}	|j                  |   }
| j                  |
|	       ( y )Nr   r   r  zconsumer_secret:42zaccess_key:84zaccess_secret:168)consumer_secretr  r   )r   ro   r  r  rp   r   )ro   r  r  rp   r   )r   makedirsr   r   r   r   r   save_to_pathr  r   r   r~   r  r   r   r   )r   credentials_file_pathr   r  r  rp   r   r  r   expectedactuals              r   -test_existing_credentials_arguments_passed_onzDTestLaunchpadLoginWith.test_existing_credentials_arguments_passed_onu  s#    	GGLL(9=I	
 !#MM,mZ!
 "0$_6IJ

 	  !67(X
&11!]]%!
	 "%!'',,t}}.?I
 0557 	/MC--c2FVX.	/r    c           	         t         j                  j                  d      }| j                  t         j                  d<   t	        j
                  dt              }||t         j                  d<   nt         j                  d= |j                  d   }t         j                  j                  t         j                  j                  |dd            }| j                  |t         j                  j                  | j                  d             | j                  t         j                  j                  t         j                  j                  |dd                   y )NHOMEr  rr   r   z..z.launchpadlibr   )r   r   r   r   r   r   r~   r   r   abspathr   r   r=   r  )r   old_homer   	cache_dirr   s        r   test_None_launchpadlib_dirz1TestLaunchpadLoginWith.test_None_launchpadlib_dir  s     ::>>&)!]]

6&11\
	 !)BJJv

6",,W5	77??277<<	4+NObggll4==/J	
 	GGNN-/@'J	
r    c                 l   t        j                  dd      }| j                  |j                  d   d       t        j                  dt        j
                  d         }| j                  |j                  d   t        j
                  d          d}| j                  t        t         j                  dd       y )Nr  r+   ro   z"https://api.staging.launchpad.net/)r  zhttps://r   )r   r   r   r   r   rG   rf   rg   r
  s     r   test_short_service_namez.TestLaunchpadLoginWith.test_short_service_name  s    &11*iH	$$^40	
 '11**95
	 	$$^4y)	
 -	*55z5	
r    c                 2    t        j                  dd       y )Nr      )max_failed_attemptsr   r2   s    r   !test_max_failed_attempts_acceptedz8TestLaunchpadLoginWith.test_max_failed_attempts_accepted  s     	%%o1Mr    )r!   r"   r#   r$   r3   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r%  r+  r-  r1  __classcell__)r   s   @r   r   r   ^  s    ++%:.L&LE2
:;E
?
<





&

 GD; $/L
2
2Nr    r   c                       e Zd ZdZd Zd Zy)TestDeprecatedLoginMethodsz2Make sure the deprecated login methods still work.c                 b   t        j                  d      5 }t        j                  d       t        j                  dt               t	        j
                  ddd       | j                  t        |      d       | j                  |d	   j                  t               d d d        y # 1 sw Y   y xY w)
NTr5   r7   r   r   tokenr  r8   r   )
r9   r:   r;   PendingDeprecationWarningr   loginr   r<   r?   r@   r   rC   s     r   test_login_is_deprecatedz3TestDeprecatedLoginMethods.test_login_is_deprecated  s    $$D1 	EV!!(+!!(,EF$$Z(CS[!,VAY//1CD	E 	E 	Es   BB%%B.c                    t        j                  d      5 }t        j                  d       t        j                  dt               t        j                  dd       t        j                  d       | j                  t        |d   j                        d       | j                  |d   j                  t               d d d        y # 1 sw Y   y xY w)	NTr5   r7   r   z.*next release of cryptographyr   r   zThe 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.)r9   r:   r;   r7  filterwarningsr   get_token_and_loginr   rB   messager?   r@   r9  s     r   &test_get_token_and_login_is_deprecatedzATestDeprecatedLoginMethods.test_get_token_and_login_is_deprecated  s    $$D1 	EV!!(+!!(,EF##; 22:>F1I%%&L VAY//1CD	E 	E 	Es   B&CCN)r!   r"   r#   r$   r:  r?  r%   r    r   r4  r4    s    <EEr    r4  c                       e Zd Zd Zd Z ej                  ed ed             d        Z	 ej                  ed ed             d        Z
 ej                  ed ed             d	        Zy
)!TestCredenitialSaveFailedCallbackc                 p    t               t        j                  _        t	        j
                         | _        y r   r   r   r   socketr   r   r   r2   s    r   r3   z'TestCredenitialSaveFailedCallback.setUp  %     )9(:% ((*r    c                 t    t         t        j                  _         t        j                  | j
                         y r   rD  r   r   r   r   r   r2   s    r   r   z*TestCredenitialSaveFailedCallback.tearDown       (.%dmm$r    _is_sudoc                       yNFr%   r%   r    r   <lambda>z*TestCredenitialSaveFailedCallback.<lambda>       r    c                 $   g fd}t         j                  j                  | j                  d      }d}t	        t                     5  t        j                  d|||       | j                  t              d       d d d        y # 1 sw Y   y xY w)Nc                  (     j                  d        y r   )append)callback_calleds   r   callbackzPTestCredenitialSaveFailedCallback.test_credentials_save_failed.<locals>.callback  s     ""4(r    r   r   r   )ro   r   r  r8   )
r   r   r   r   r   r   r   r   r   r<   )r   rR  r   ro   rQ  s       @r   test_credentials_save_failedz>TestCredenitialSaveFailedCallback.test_credentials_save_failed   s    
 	)
 77<<~F0.*+ 	6)))!1'/	 S115	6 	6 	6s   5BBc                       yrK  r%   r%   r    r   rL  z*TestCredenitialSaveFailedCallback.<lambda>  rM  r    c                     t         j                  j                  | j                  d      }d}t	        t                     5  | j                  t        t        j                  d||       d d d        y # 1 sw Y   y xY wNr   r   r   r   )
r   r   r   r   r   r   rf   RuntimeErrorr   r   r   r   ro   s      r   :test_default_credentials_save_failed_is_to_raise_exceptionz\TestCredenitialSaveFailedCallback.test_default_credentials_save_failed_is_to_raise_exception  sj     77<<~F0.*+ 	"--)!1  	 	 	s    )A22A;c                       y)NTr%   r%   r    r   rL  z*TestCredenitialSaveFailedCallback.<lambda>&  rM  r    c                     t         j                  j                  | j                  d      }d}t	        t                     5  t        j                  d||       d d d        y # 1 sw Y   y xY wrV  )r   r   r   r   r   r   r   r   rX  s      r   >test_credentials_save_fail_under_sudo_does_not_raise_exceptionz`TestCredenitialSaveFailedCallback.test_credentials_save_fail_under_sudo_does_not_raise_exception&  sY     77<<~F0.*+ 	)))!1	 	 	s    A""A+N)r!   r"   r#   r3   r   r   r  r   staticmethodrS  rY  r\  r%   r    r   rA  rA    s    +% U\\$j,}2MN6 O6, U\\$j,}2MN O U\\$j,|2LM
 N
r    rA  c                       e Zd Zd Zd Z ej                  ed ed             d        Z	 ej                  ed ed             d        Z
y)	TestMultipleSitesc                 p    t               t        j                  _        t	        j
                         | _        y r   rC  r2   s    r   r3   zTestMultipleSites.setUp9  rE  r    c                 t    t         t        j                  _         t        j                  | j
                         y r   rG  r2   s    r   r   zTestMultipleSites.tearDown@  rH  r    rI  c                       yrK  r%   r%   r    r   rL  zTestMultipleSites.<lambda>D  rM  r    c                    t         j                  j                  | j                  d      }t	               }d}d}t        |      5  t        j                  |||      }|j                  j                  j                  }d d d        t        |j                  j                               d   d   }| j                  ||       | j                  |       | j                  ||dz   |z          y # 1 sw Y   oxY w)Nr   r   zSuper App 3000r   r   r8   @)r   r   r   r   r   r   r   r   r   r   r   listdatarH   rA   r   )r   r   r   ro   r   r   r   application_keys           r   "test_components_of_application_keyz4TestMultipleSites.test_components_of_application_keyD  s    77<<~F!#0+'" 	?*55 )!1I
 &11::>>M	? w||0023A6q9
 	lO4m_5
 	-#*=*LM'	? 	?s   9C++C4c                       yrK  r%   r%   r    r   rL  zTestMultipleSites.<lambda>_  rM  r    c                 J   t         j                  j                  | j                  d      }t	               }|j
                  rJ d       t        |      5  t        j                  dd|       t        j                  dd|       d d d        t        |j
                  j                               dk(  sJ t        |j
                  j                               d   d	   }t        |j
                  j                               d	   d	   }| j                  ||       y # 1 sw Y   xY w)
Nr   z$oops, a fresh keyring has data in itr   zhttp://alpha.example.com/r   zhttp://beta.example.com/   r   r8   )r   r   r   r   r   rf  r   r   r   r<   rH   re  r   )r   r   r   application_key_1application_key_2s        r   test_same_app_different_serversz1TestMultipleSites.test_same_app_different_servers_  s    77<<~F!#<<G!GG'" 	 ))"8!1
 ))"7!1	  7<<$$&'1,,, !2!2!45a8; !2!2!45a8;-/@A)	 	s   1DD"N)r!   r"   r#   r3   r   r   r  r   r]  rh  rn  r%   r    r   r_  r_  4  sd    
+% U\\$j,}2MNN ON4 U\\$j,}2MNB OBr    r_  )0r$   type__metaclass__
contextlibr   r   r   rD  r   r   unittestunittest.mockr   ImportErrormockr9   lazr.restfulclient.resourcer   launchpadlib.credentialsr   r   r   r   launchpadlib.launchpadr	   r
   launchpadlib.testing.helpersr   r   r   r   r   r   r   r   r~   TestCaser   r'   rm   r{   r   r   r   r   r4  rA  r_  r%   r    r   <module>r{     sK  " % % 	     #  3
   , C  
 )Ih// IqGh'' qGh5
):): 5
p 
(*;*;  
FH,=,= <8;L;L 2-(## -rN[ rNjE E<@(9(9 @FEB)) EBk  s   E   EE