
    Y@                     :   S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  \R,                  " S/\R.                  S9r\R,                  " S/SS9r " S S\R4                  5      rg)zTests for the reauth module.    N)http_client)urllib)client)
challenges)reauth)errors)reauth_creds)_reauth_client)Oauth2WithReauthCredentials)model)u2fstatus)specr   c                   8   \ rS rSrSrSrSrSrSrSr	S r
 " S	 S
\5      rS$S jrS rS rS rS rS rS r\R*                  " S0 5      S 5       r\R*                  " SS5      S 5       rS rS rS rS rS rS rS rS r\R*                  " S SS!9S" 5       r S#r!g)%
ReauthTest,   a  This class contains tests for mocking reauth.

The tests here are a bit more verbose since we are trying to mock out U2F, and
present the reauth code some form of valid reply. This makes this test a bit
of an integration test, as opposed to testing every individual method by
itself.
encoded_proof_of_reauth_tokencorrect_passwordzhttp://some_url.comsome_idsome_secretc                 &   [        [        R                  R                  US   5      5      n [        R
                  " US   5      nSU;   a  US   OUS   nXPR                  :X  aA  UR                  S5      [        R                  :X  a  [        [        R                  " SS05      4$ U[        R                  S-   :X  a"  S[        R                  " S	S
SSS0 S./S.5      4$ U[        R                  S-   :X  a  U R                  SUR                  S5      5        U R                  SUR                  S5      5        UR                  S0 5      R                  S5      U R                   :X  a[  S[        R                  " S	SSSSSS["        R$                  " SR'                  S5      5      R)                  S5      S./S.S./S.5      4$ S[        R                  " SS
SSS0 S./S.5      4$ U[        R                  S -   :X  ah  U R                  SUR                  S5      5        U R                  SUR                  S5      5        S[        R                  " S!S"U R*                  S#.5      4$ [-        S$R/                  XRS   5      5      e! [         a    0 n GNIf = f)%a<  Helper function to respond with valid requests as if a real server.

This is the helper function for mocking HTTP calls. The calls that should
end up here are to the oauth2 API or to the reauth API. The order of ifs
tries to mimic the real order that the requests are expected, but we do not
enforce a particular order so it can be more general.

Args:
  *args: Every arg passed to a request.
  **kwargs: Every keyed arg passed to a request.

Returns:
  (str, str), Mocked (headers, content)

Raises:
  Exception: In case this function doesn't know how to mock a request.
bodyurir   scopeaccess_tokenaccess_token_for_reauth:startNCHALLENGE_REQUIREDsession_id_1READY   PASSWORD)r   challengeIdchallengeTypesecurityKeyr   	sessionIdr   z/session_id_1:continuer#   RESPONDactionproposalResponse
credentialsession_id_2   SECURITY_KEYsecurity_key_application_idsome_keysome_challengeascii)	keyHandle	challenge)applicationIdr   CHALLENGE_PENDINGz/session_id_2:continueAUTHENTICATEDsession_id_3)r   r'   encodedProofOfReauthTokenUnexpected call :/
URL {0}
{1})dictr   parse	parse_qsljsonloads
ValueErroroauth_api_urlgetr   _REAUTH_SCOPE_ok_responsedumpsr
   _REAUTH_APIassertEqualr   base64urlsafe_b64encodeencodedecode
rapt_token	Exceptionformat)selfargskwargsqpqp_jsonr   s         Eplatform/gsutil/third_party/google-reauth-python/tests/test_reauth.py_request_mock_side_effect$ReauthTest._request_mock_side_effect;   s   , &,,((89	jj0G  %fUmDG &&&VVG_ 4 44^=V,W!XXX >--89.+%#$%/#%	  	% 	 	 	 >--0HHIQM :;YH(=>.377E(() TZZ2!/")'()7-J-7-3-E-E$4$;$;G$D.FFLfWo, +(	$ #)   , TZZ1!/")'()3')	$ #	) 	 	 	 >--0HHIQM :;YH(=>)+-1__%    .55c&>JL 	LU  	G	s   J   JJc                       \ rS rSrS rSrg)ReauthTest._U2FInterfaceMock   c                     [         R                  " [         R                  R                  US5      n[         R                  " SR	                  5       SR	                  5       U5      $ )Nsome_origin
key_handleresp)r   
ClientDataTYP_AUTHENTICATIONSignResponserJ   )rO   unused_app_idr4   unused_registered_keysclient_datas        rT   Authenticate)ReauthTest._U2FInterfaceMock.Authenticate   sM    **  33K %%l&9&9&;V]]_kZZ     N)__name__
__module____qualname____firstlineno__rd   __static_attributes__rg   rf   rT   _U2FInterfaceMockrX      s    	[rf   rm   Nc           	          [         R                  R                  S5      b  [        R                  " S5      e[
        R                  " UU R                  U R                  SU R                  US9$ )NSK_SIGNING_PLUGINzunset SK_SIGNING_PLUGIN.some_refresh_tokenscopes)
osenvironrB   unittestSkipTestr   get_rapt_token	client_idclient_secretrA   )rO   request_mockrr   s      rT   _call_reauthReauthTest._call_reauth   s]    ::>>-.:##$>??$$NN  	rf   c                     [         R                  " U0 UD6nU R                  UR                  5        UR	                  5       $ N)mockpatch
addCleanupstopstart)rO   rP   rQ   patchers       rT   
StartPatchReauthTest.StartPatch   s1    **d-f-%}}rf   c                     U R                  S5      U l        U R                  5       U R                  l        U R                  S5      U l        U R
                  U R                  l        U R                  S5      U l        S U R                  l        g )Nzpyu2f.u2f.GetLocalU2FInterfacezgetpass.getpassz	sys.stdinc                      g)NTrg   rg   rf   rT   <lambda>"ReauthTest.setUp.<locals>.<lambda>   s    $rf   )r   u2f_local_interface_mockrm   return_valuegetpass_mockr   is_interactive_mockisattyrO   s    rT   setUpReauthTest.setUp   sr    (,,).%595K5K5M%%2 OO,=>)-)>)>&#'??;#? *6  'rf   c                    [         R                  " SU R                  S9 nU R                  U5      nU R	                  U R
                  U5        U R	                  SUR                  5        S S S 5        g ! , (       d  f       g = fNhttplib2.Http.requestside_effect   r   r   rU   r{   rG   rL   
call_countrO   rz   reauth_results      rT   testPassAndGnubbyReauth"ReauthTest.testPassAndGnubbyReauth   sk    ZZ/&*&D&DFIU --l;MT__m<Q 7 78	F F Fs   A
A33
Bc                    [         R                  " SU R                  S9 nU R                  USS/5      nU R	                  U R
                  U5        U R	                  SUR                  5        S S S 5        g ! , (       d  f       g = f)Nr   r   z&https://www.googleapis.com/auth/scope1z&https://www.googleapis.com/auth/scope2r   r   r   s      rT   testPassWithScopesReauthTest.testPassWithScopes   sz    ZZ/&*&D&DFIU --l88=: ;M T__m<Q 7 78F F Fs   AA66
Bc                 d   [         R                  " SU R                  S9 nS U R                  l        SU R
                  /U R                  l        U R                  U5      nU R                  U R                  U5        U R                  SUR                  5        S S S 5        g ! , (       d  f       g = f)Nr   r   bad_pass   )r   r   rU   r   r   r   r   r{   rG   rL   r   r   s      rT   (testIncorrectPassThenPassAndGnubbyReauth3ReauthTest.testIncorrectPassThenPassAndGnubbyReauth   s    ZZ/&*&D&DFIU-1D*-79N9N,OD) --l;MT__m<Q 7 78F F Fs   A8B!!
B/c                 .   [         R                  " SU R                  S9 nS U R                  l        U R                  [        R                  5         U R                  U5      nS S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)Nr   r   c                      g)NFrg   rg   rf   rT   r   4ReauthTest.testNonInteractiveError.<locals>.<lambda>   s    erf   )	r   r   rU   r   r   assertRaisesr   ReauthUnattendedErrorr{   )rO   rz   unused_reauth_results      rT   testNonInteractiveError"ReauthTest.testNonInteractiveError   sz    ZZ/&*&D&DFIU.;D$$+""6#?#?@'+'8'8'F$ AF F A@F Fs#   2BA5$B5
B	?B
Bz-google_reauth.challenges.AVAILABLE_CHALLENGESc                 z   [         R                  " SU R                  S9 nU R                  [        R
                  5         U R                  U5      nU R                  U R                  U5        U R                  SUR                  5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = fr   
r   r   rU   r   r   ReauthFailErrorr{   rG   rL   r   r   s      rT   testChallengeNotSupported$ReauthTest.testChallengeNotSupported       ZZ/&*&D&DFIU""6#9#9: $ 1 1, ?  -@  L$;$;< ;F F::F F$    B, A
B
B,
B)	%B,,
B:z>google_reauth.challenges.PasswordChallenge.is_locally_eligibleFc                 z   [         R                  " SU R                  S9 nU R                  [        R
                  5         U R                  U5      nU R                  U R                  U5        U R                  SUR                  5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = fr   r   r   s      rT   testChallengeNotEligible#ReauthTest.testChallengeNotEligible   r   r   c           	        ^ ^^ UUU 4S jn[         R                  " SUS9 nT R                  [        R                  5         [
        R                  " UT R                  T R                  ST R                  5        S S S 5        T R                  SUR                  5        S S S 5        g ! , (       d  f       N3= f! , (       d  f       g = f)Nc                     > SU;   a  US   OU S   nUTR                   :X  a  TT4$ [        SR                  X!S   5      5      e)Nr   r   r:   r   )rA   rM   rN   )rP   rQ   r   contentresponserO   s      rT   r   7ReauthTest.accessTokenRefreshError.<locals>.side_effect   sR    #(F?&-QCt)))((299#f~NP Prf   r   r   rp   r!   )r   r   r   r   HttpAccessTokenRefreshErrorr   refresh_access_tokenrx   ry   rA   rG   r   rO   r   r   r   rz   s   ```  rT   accessTokenRefreshError"ReauthTest.accessTokenRefreshError   s    	P ZZ/&136B""6#E#EF++ NN&&(&&( G Q 7 783 3FF3 3#    B89B':$B8'
B5	1B88
Cc                     U R                  [        S5        U R                  [        S5        U R                  [        [        R                  " SS05      5        g )Nfooerrornon_reauth_error)r   rD   _error_responser>   rE   r   s    rT   testAccessTokenRefreshError&ReauthTest.testAccessTokenRefreshError  sI    $$\59$$_e<$$_djj'B) 7* 	+rf   c           	        ^ ^^ UUU 4S jn[         R                  " SUS9 nT R                  [        R                  5         [
        R                  " UT R                  T R                  ST R                  5        S S S 5        T R                  SUR                  5        S S S 5        g ! , (       d  f       N3= f! , (       d  f       g = f)Nc                  z  > [        [        R                  R                  US   5      5      n [        R
                  " US   5      nSU;   a  US   OU S   nUTR                  :X  a'  UR                  S5      [        R                  :X  a  TT4$ [        SR                  XAS   5      5      e! [         a    0 n Nrf = f)Nr   r   r   r   r:   )r;   r   r<   r=   r>   r?   r@   rA   rB   r   rC   rM   rN   )rP   rQ   rR   rS   r   r   r   rO   s        rT   r   6ReauthTest.reauthAccessTokenError.<locals>.side_effect  s    fll,,VF^<=B**VF^4 $)F?&-QC***F$8$88((299#f~NP P  s   B+ +B:9B:r   r   rp   r!   )r   r   r   r   ReauthAccessTokenRefreshErrorr   rw   rx   ry   rA   rG   r   r   s   ```  rT   reauthAccessTokenError!ReauthTest.reauthAccessTokenError  s    	P ZZ/&136B""6#G#GH%% NN&&(&&( I Q 7 783 3HH3 3r   c                     U R                  [        S5        U R                  [        [        R                  " 0 5      5        U R                  [        [        R                  " SS05      5        g )Nr   r   z
some error)r   rD   r>   rE   r   r   s    rT   testReauthAccessTokenError%ReauthTest.testReauthAccessTokenError.  sL    ##L%8##L$**R.A##TZZ,(?@	Brf   c                     U R                  S[        R                  " 0 5      5        U R                  S[        R                  " SS05      5        U R                  S[        R                  " SSS.5      5        g )NzInvalid response.zThere was an error.r   z%There was an error: error descriptionzThere was an errorzerror description)r   error_description)rG   r   _get_refresh_error_messager   s    rT   testResponseErrorMessage#ReauthTest.testResponseErrorMessage4  sq    ,--b1	3.--w8M.NO	Q@---%8/: ;	<rf   c           	        ^ ^ UU 4S jn[         R                  " SUS9 nT R                  [        R                  5       n[
        R                  " UT R                  T R                  ST R                  5        S S S 5        T R                  SUR                  5        S S S 5        g ! , (       d  f       N3= f! , (       d  f       g = f)Nc                     > SU;   a  US   OU S   nUTR                   :X  a  [        [        R                  " SS05      4$ U[        R
                  S-   :X  a  S T4$ g )Nr   r   r   r   r   rA   rD   r>   rE   r
   rF   )rP   rQ   r   r   rO   s      rT   r   2ReauthTest.getChallengesError.<locals>.side_effect?  sp    #(F?&-QC***#TZZ"$=1? &@ @ @ ~11H<=W}$ >rf   r   r   rp   r-   )r   r   r   r   ReauthAPIErrorr   rw   rx   ry   rA   rG   r   )rO   r   r   rz   es   ``   rT   getChallengesErrorReauthTest.getChallengesError>  s    		% ZZ/&136B""6#8#89Q%% NN&&(&&( : Q 7 783 3993 3s"    B69B%8$B6%
B3	/B66
Cc                     U R                  [        R                  " SS05      5        U R                  [        R                  " SSS005      5        g )Nr   ERRORr   messagez
Some error)r   r>   rE   r   s    rT   testGetChallengesError!ReauthTest.testGetChallengesErrorU  sF    

Hg+> ?@

i.,0 !1 	2rf   c           
      X  ^  U 4S jnT R                  S5      nXl        T R                  [        R                  5       n[
        R                  " UT R                  T R                  ST R                  S S9  S S S 5        T R                  SUR                  5        g ! , (       d  f       N+= f)Nc            	         > SU;   a  US   OU S   nUTR                   :X  a  [        [        R                  " SS05      4$ U[        R
                  S-   :X  a!  S [        R                  " SSSS	S
S./S.5      4$ g )Nr   r   r   r   r   r   r   	NOT_READYr!   r"   )r   r#   r$   r&   r   )rP   rQ   r   rO   s      rT   r   >ReauthTest.testChallangeNotActivatedError.<locals>.side_effect[  s    #(F?&-QC***#TZZ"$=1? &@ @ @ ~11H<=TZZ2!/"-'()3$5 #6)7 8 8 8 >rf   r   rp   rq      )r   r   r   r   r   r   rw   rx   ry   rA   rG   r   )rO   r   rz   r   s   `   rT   testChallangeNotActivatedError)ReauthTest.testChallangeNotActivatedErrorZ  s    	8" '>?#. v556!!!""$"" 7 	L334 76s   8B
B)zDgoogle_reauth.challenges.SecurityKeyChallenge.obtain_challenge_input)r   c                 B   [         R                  " SU R                  S9 nU R                  [        R
                  5         U R                  U5      nS S S 5        U R                  SUR                  5        S S S 5        g ! , (       d  f       N3= f! , (       d  f       g = f)Nr   r      )	r   r   rU   r   r   r   r{   rG   r   )rO   challenge_mockrz   r   s       rT   testRetryOnNoUserInput!ReauthTest.testRetryOnNoUserInputx  s}    ZZ/&*&D&DFIU""6#9#9: $ 1 1, ? ;Q 7 78	F F::F Fs#    B A?$B?
B		B
B)r   r   r   r~   )"rh   ri   rj   rk   __doc__rL   r   rA   rx   ry   rU   objectrm   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rl   rg   rf   rT   r   r   ,   s     1J))MI!MdLP[F [		7999G 
ZZ?D= E= 
ZZPRWX= Y=9$+90B<9.2
5< 
ZZVgkl9 m9rf   r   )r   rH   r>   rs   ru   r   	six.movesr   r   oauth2clientr   google_reauthr   r   r   r	   r
   google_reauth.reauth_credsr   pyu2fr   r   MockOKrD   r   TestCaser   rg   rf   rT   <module>r      sx    #   	   !   $     & ( B   yyxj@))(D9R9"" R9rf   