
    Y@                     F   d 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mZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  ej,                  dgej.                        Z ej,                  dgd      Z G d dej4                        Zy)zTests for the reauth module.    N)http_client)urllib)client)
challenges)reauth)errors)reauth_creds)_reauth_client)Oauth2WithReauthCredentials)model)u2fstatus)specr   c                   4   e Zd ZdZdZdZdZdZdZd Z	 G d d	e
      Zd"dZd Zd Zd Zd Zd Zd Z ej(                  di       d        Z ej(                  dd      d        Zd Zd Zd Zd Zd Zd Zd Zd Z ej(                  dd
       d!        Zy
)#
ReauthTesta0  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                 
   t        t        j                  j                  |d               }	 t	        j
                  |d         }d|v r|d   n|d   }|| j                  k(  r?|j                  d      t        j                  k(  rt        t	        j                  ddi      fS |t        j                  dz   k(  r!dt	        j                  d	d
dddi dgd      fS |t        j                  dz   k(  r| j                  d|j                  d             | j                  d|j                  d             |j                  di       j                  d      | j                   k(  rYdt	        j                  d	ddddddt#        j$                  dj'                  d            j)                  d      dgddgd      fS dt	        j                  dd
dddi dgd      fS |t        j                  d z   k(  rg| j                  d|j                  d             | j                  d|j                  d             dt	        j                  d!d"| j*                  d#      fS t-        d$j/                  ||d               # t        $ r i }Y <w xY w)%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_effectz$ReauthTest._request_mock_side_effect;   s   , &,,((89	jj0G  %fUmDG D&&&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5c6&>JL 	LU  	G	s   I3 3JJc                       e Zd Zd Zy)ReauthTest._U2FInterfaceMockc                     t        j                  t         j                  j                  |d      }t        j                  dj	                         dj	                         |      S )Nsome_origin
key_handleresp)r   
ClientDataTYP_AUTHENTICATIONSignResponserI   )rN   unused_app_idr3   unused_registered_keysclient_datas        rS   Authenticatez)ReauthTest._U2FInterfaceMock.Authenticate   sM    **  33K %%l&9&9&;V]]_kZZ    N)__name__
__module____qualname__ra    rb   rS   _U2FInterfaceMockrV      s    	[rb   rg   Nc                     t         j                  j                  d      t        j                  d      t        j                  || j                  | j                  d| j                  |      S )NSK_SIGNING_PLUGINzunset SK_SIGNING_PLUGIN.some_refresh_tokenscopes)
osenvironrA   unittestSkipTestr   get_rapt_token	client_idclient_secretr@   )rN   request_mockrl   s      rS   _call_reauthzReauthTest._call_reauth   s]    ::>>-.:##$>??$$NN  	rb   c                     t        j                  |i |}| j                  |j                         |j	                         S N)mockpatch
addCleanupstopstart)rN   rO   rP   patchers       rS   
StartPatchzReauthTest.StartPatch   s1    **d-f-%}}rb   c                     | j                  d      | _        | j                         | j                  _        | j                  d      | _        | j
                  | j                  _        | j                  d      | _        d | j                  _        y )Nzpyu2f.u2f.GetLocalU2FInterfacezgetpass.getpassz	sys.stdinc                       y)NTrf   rf   rb   rS   <lambda>z"ReauthTest.setUp.<locals>.<lambda>   s    $rb   )r~   u2f_local_interface_mockrg   return_valuegetpass_mockr   is_interactive_mockisattyrN   s    rS   setUpzReauthTest.setUp   sr    (,,).%595K5K5M%%2 OO,=>)-)>)>&#'??;#? *6  'rb   c                    t        j                  d| j                        5 }| j                  |      }| j	                  | j
                  |       | j	                  d|j                         d d d        y # 1 sw Y   y xY wNhttplib2.Http.requestside_effect   rx   ry   rT   ru   rF   rK   
call_countrN   rt   reauth_results      rS   testPassAndGnubbyReauthz"ReauthTest.testPassAndGnubbyReauth   sk    ZZ/&*&D&DFIU --l;MT__m<Q 7 78	F F Fs   A
A55A>c                    t        j                  d| j                        5 }| j                  |ddg      }| j	                  | j
                  |       | j	                  d|j                         d d d        y # 1 sw Y   y xY w)Nr   r   z&https://www.googleapis.com/auth/scope1z&https://www.googleapis.com/auth/scope2r   r   r   s      rS   testPassWithScopeszReauthTest.testPassWithScopes   sz    ZZ/&*&D&DFIU --l88=: ;M T__m<Q 7 78F F Fs   AA88Bc                 ^   t        j                  d| j                        5 }d | j                  _        d| j
                  g| j                  _        | j                  |      }| j                  | j                  |       | j                  d|j                         d d d        y # 1 sw Y   y xY w)Nr   r   bad_pass   )rx   ry   rT   r   r   r   r   ru   rF   rK   r   r   s      rS   (testIncorrectPassThenPassAndGnubbyReauthz3ReauthTest.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                    t        j                  d| j                        5 }d | j                  _        | j                  t        j                        5  | j                  |      }d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   c                       y)NFrf   rf   rb   rS   r   z4ReauthTest.testNonInteractiveError.<locals>.<lambda>   s    erb   )	rx   ry   rT   r   r   assertRaisesr   ReauthUnattendedErrorru   )rN   rt   unused_reauth_results      rS   testNonInteractiveErrorz"ReauthTest.testNonInteractiveError   sz    ZZ/&*&D&DFIU.;D$$+""6#?#?@'+'8'8'F$ AF F A@F Fs#   2BA7&B7B 	<BBz-google_reauth.challenges.AVAILABLE_CHALLENGESc                 j   t        j                  d| j                        5 }| j                  t        j
                        5  | j                  |      }| j                  | j                  |       | j                  d|j                         d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   
rx   ry   rT   r   r   ReauthFailErrorru   rF   rK   r   r   s      rS   testChallengeNotSupportedz$ReauthTest.testChallengeNotSupported       ZZ/&*&D&DFIU""6#9#9: $ 1 1, ?  -@  L$;$;< ;F F::F F$    B)A
BB)B&	"B))B2z>google_reauth.challenges.PasswordChallenge.is_locally_eligibleFc                 j   t        j                  d| j                        5 }| j                  t        j
                        5  | j                  |      }| j                  | j                  |       | j                  d|j                         d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr   r   r   s      rS   testChallengeNotEligiblez#ReauthTest.testChallengeNotEligible   r   r   c           	      ~     fd}t        j                  d|      5 } j                  t        j                        5  t        j                  | j                   j                  d j                         d d d         j                  d|j                         d d d        y # 1 sw Y   .xY w# 1 sw Y   y xY w)Nc                      d|v r|d   n| d   }|j                   k(  rfS t        dj                  ||d               )Nr   r   r9   r   )r@   rL   rM   )rO   rP   r   contentresponserN   s      rS   r   z7ReauthTest.accessTokenRefreshError.<locals>.side_effect   sT    #(F?&-QCt)))((299#vf~NP Prb   r   r   rj   r    )rx   ry   r   r   HttpAccessTokenRefreshErrorr   refresh_access_tokenrr   rs   r@   rF   r   rN   r   r   r   rt   s   ```  rS   accessTokenRefreshErrorz"ReauthTest.accessTokenRefreshError   s    	P ZZ/&136B""6#E#EF++ NN&&(&&( G Q 7 783 3FF3 3#    B38B':$B3'B0	,B33B<c                     | j                  t        d       | j                  t        d       | j                  t        t        j                  ddi             y )Nfooerrornon_reauth_error)r   rC   _error_responser=   rD   r   s    rS   testAccessTokenRefreshErrorz&ReauthTest.testAccessTokenRefreshError  sI    $$\59$$_e<$$_djj'B) 7* 	+rb   c           	      ~     fd}t        j                  d|      5 } j                  t        j                        5  t        j                  | j                   j                  d j                         d d d         j                  d|j                         d d d        y # 1 sw Y   .xY w# 1 sw Y   y xY w)Nc                  p   t        t        j                  j                  |d               }	 t	        j
                  |d         }d|v r|d   n| d   }|j                  k(  r&|j                  d      t        j                  k(  rfS t        dj                  ||d               # t        $ r i }Y nw xY w)Nr   r   r   r   r9   )r:   r   r;   r<   r=   r>   r?   r@   rA   r   rB   rL   rM   )rO   rP   rQ   rR   r   r   r   rN   s        rS   r   z6ReauthTest.reauthAccessTokenError.<locals>.side_effect  s    fll,,VF^<=B**VF^4 $)F?&-QC***F$8$88((299#vf~NP P  s   B' 'B54B5r   r   rj   r    )rx   ry   r   r   ReauthAccessTokenRefreshErrorr   rq   rr   rs   r@   rF   r   r   s   ```  rS   reauthAccessTokenErrorz!ReauthTest.reauthAccessTokenError  s    	P ZZ/&136B""6#G#GH%% NN&&(&&( I Q 7 783 3HH3 3r   c                     | j                  t        d       | j                  t        t        j                  i              | j                  t        t        j                  ddi             y )Nr   r   z
some error)r   rC   r=   rD   r   r   s    rS   testReauthAccessTokenErrorz%ReauthTest.testReauthAccessTokenError.  sL    ##L%8##L$**R.A##TZZ,(?@	Brb   c                     | j                  dt        j                  i              | j                  dt        j                  ddi             | j                  dt        j                  ddd             y )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)rF   r   _get_refresh_error_messager   s    rS   testResponseErrorMessagez#ReauthTest.testResponseErrorMessage4  sq    ,--b1	3.--w8M.NO	Q@---%8/: ;	<rb   c           	      z     fd}t        j                  d|      5 } j                  t        j                        5 }t        j                  | j                   j                  d j                         d d d         j                  d|j                         d d d        y # 1 sw Y   .xY w# 1 sw Y   y xY w)Nc                      d|v r|d   n| d   }|j                   k(  rt        t        j                  ddi      fS |t        j
                  dz   k(  rd fS y )Nr   r   r   r   r   r@   rC   r=   rD   r
   rE   )rO   rP   r   r   rN   s      rS   r   z2ReauthTest.getChallengesError.<locals>.side_effect?  sp    #(F?&-QC***#TZZ"$=1? &@ @ @ ~11H<=W}$ >rb   r   r   rj   r,   )rx   ry   r   r   ReauthAPIErrorr   rq   rr   rs   r@   rF   r   )rN   r   r   rt   es   ``   rS   getChallengesErrorzReauthTest.getChallengesError>  s    		% ZZ/&136B""6#8#89Q%% NN&&(&&( : Q 7 783 3993 3s#    B1 8B%8$B1%B.	*B11B:c                     | j                  t        j                  ddi             | j                  t        j                  dddii             y )Nr   ERRORr   messagez
Some error)r   r=   rD   r   s    rS   testGetChallengesErrorz!ReauthTest.testGetChallengesErrorU  sF    

Hg+> ?@

i.,0 !1 	2rb   c           	      R     fd} j                  d      }||_         j                  t        j                        5 }t        j                  | j                   j                  d j                  d        d d d         j                  d|j                         y # 1 sw Y   &xY w)Nc            	          d|v r|d   n| d   }|j                   k(  rt        t        j                  ddi      fS |t        j
                  dz   k(  r d t        j                  dddd	d
dgd      fS y )Nr   r   r   r   r   r   r   	NOT_READYr    r!   )r   r"   r#   r%   r   )rO   rP   r   rN   s      rS   r   z>ReauthTest.testChallangeNotActivatedError.<locals>.side_effect[  s    #(F?&-QC***#TZZ"$=1? &@ @ @ ~11H<=TZZ2!/"-'()3$5 #6)7 8 8 8 >rb   r   rj   rk      )r~   r   r   r   r   r   rq   rr   rs   r@   rF   r   )rN   r   rt   r   s   `   rS   testChallangeNotActivatedErrorz)ReauthTest.testChallangeNotActivatedErrorZ  s    	8" '>?#. v556!!!""$"" 7 	L334 76s   :BB&zDgoogle_reauth.challenges.SecurityKeyChallenge.obtain_challenge_input)r   c                 2   t        j                  d| j                        5 }| j                  t        j
                        5  | j                  |      }d d d        | j                  d|j                         d d d        y # 1 sw Y   .xY w# 1 sw Y   y xY w)Nr   r      )	rx   ry   rT   r   r   r   ru   rF   r   )rN   challenge_mockrt   r   s       rS   testRetryOnNoUserInputz!ReauthTest.testRetryOnNoUserInputx  s}    ZZ/&*&D&DFIU""6#9#9: $ 1 1, ? ;Q 7 78	F F::F Fs#    BB$BB
	BBrw   ) rc   rd   re   __doc__rK   r   r@   rr   rs   rT   objectrg   ru   r~   r   r   r   r   r   rx   ry   r   r   r   r   r   r   r   r   r   r   r   rf   rb   rS   r   r   ,   s     1J))MI!MdLP[F [		7999G TZZ?D= E= TZZPRWX= Y=9$+90B<9.2
5< TZZVgkl9 m9rb   r   )r   rG   r=   rm   ro   rx   	six.movesr   r   oauth2clientr   google_reauthr   r   r   r	   r
   google_reauth.reauth_credsr   pyu2fr   r   MockOKrC   r   TestCaser   rf   rb   rS   <module>r      s|    #   	   !   $     & ( B   tyyxj@$))(D9R9"" R9rb   