
    M/e3                     &   d 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	 ddl
mZ ddgZg d	Z G d
 dej                        Z G d dej                        Zedk(  r4 ej&                   ej(                  ej*                  dd egz                yy)z*Test :mod:`certbot._internal.display.obj`.    N)mock)errors)obj)util)FirstDescription1)SecondDescription2)tag1tag2tag3c                       e Zd ZdZ fdZ ej                  d      d        Zd Zd Z	d Z
d Z ej                  d	      d
        Zd Zd Zd Zd Zd Zd Zd Zd Z ej                  d      d        Z ej                  d      d        Z ej                  d      d        Z ej                  d      d        Zd Zd Z ej                  d      d        Zd Zd Zd Zd Zd Z d  Z! xZ"S )!FileOutputDisplayTestzTest stdout display.

    Most of this class has to deal with visual output.  In order to test how the
    functions look to a user, uncomment the test_visual function.

    c                     t         |           t        j                         | _        t        j                  | j                  d      | _        y )NF)supersetUpr   	MagicMockmock_stdoutdisplay_objFileDisplay	displayer)self	__class__s    J/usr/lib/python3/dist-packages/certbot/_internal/tests/display/obj_test.pyr   zFileOutputDisplayTest.setUp   s5    >>+$001A1A5I    $certbot._internal.display.obj.loggerc                     | j                   j                  dd       | j                  j                  j                  d   d   }d|v sJ |j
                  j                  dd       y )NmessageFr   Notifying user: %sr   notificationr   write	call_argsdebugassert_called_withr   mock_loggerstrings      r   test_notification_no_pausez0FileOutputDisplayTest.test_notification_no_pause   s\    ##Iu5!!''11!4Q7F""",,-A9Mr   c                     d}t        j                  |d      5  | j                  j                  dd       d d d        d| j                  j
                  j                  d   d   v sJ y # 1 sw Y   4xY w)N1certbot._internal.display.util.input_with_timeoutenterreturn_valuer   Tforce_interactiver   )r   patchr   r!   r   r"   r#   r   input_with_timeouts     r   test_notification_pausez-FileOutputDisplayTest.test_notification_pause$   ss    PZZ*A 	KNN''	T'J	K D,,22<<Q?BBBB	K 	Ks   A++A4c                     | j                  | j                  j                  d       | j                  j                  j
                  d   d   }d|v sJ y )Nr   r   )_force_noninteractiver   r!   r   r"   r#   r   r(   s     r    test_notification_noninteractivez6FileOutputDisplayTest.test_notification_noninteractive+   sK    ""4>>#>#>	J!!''11!4Q7F"""r   c                 |   | j                  | j                  j                  d       | j                  j                  j
                  d   d   }d|v sJ | j                  j                  sJ | j                  | j                  j                  d       | j                  j                  j
                  d   d   }d|v sJ y )Nr   r   message2)r6   r   r!   r   r"   r#   skipped_interactionr7   s     r   !test_notification_noninteractive2z7FileOutputDisplayTest.test_notification_noninteractive20   s     	""4>>#>#>	J!!''11!4Q7F"""~~1111""4>>#>#>
K!!''11!4Q7V###r   c                 x   ddl m} | j                  j                  ddd       | j                  j
                  j                  d   d   }|d|j                  z   k(  sJ | j                  j                  dd       | j                  j
                  j                  d   d   }d|v sJ d|j                  z   |v sJ y 	Nr   )osr   F)pausedecorater:   )r@   z- - - certbot.compatr?   r   r!   r   r"   r#   linesepr   r?   r(   s      r   test_notification_decorationz2FileOutputDisplayTest.test_notification_decoration=       %##IUU#K!!''11!4Q7RZZ////##Je#<!!''11!4Q76!!!BJJ&&000r   z<certbot._internal.display.obj.FileDisplay._get_valid_int_ansc                     t         j                  df|_        | j                  j	                  dt
        d      }|t         j                  dfk(  sJ y )N   r   Tr/   r   )display_utilOKr.   r   menuCHOICES)r   mock_ansrets      r   	test_menuzFileOutputDisplayTest.test_menuH   sG     ".! 4nn!!)W!M|****r   c                     d}| j                  | j                  j                  dt        |      }|t        j
                  |fk(  sJ y )Nr   msgdefault)r6   r   rL   rM   rJ   rK   )r   rT   results      r   test_menu_noninteractivez.FileOutputDisplayTest.test_menu_noninteractiveO   sG    ++NN , B,//73333r   c                     d}t        j                  |d      5  | j                  j                  dd      \  }}d d d        sJ t        j
                         y # 1 sw Y   !xY w)Nr+   cr-   r   Tr/   )r   r1   r   inputrJ   CANCEL)r   r3   code_s       r   test_input_cancelz'FileOutputDisplayTest.test_input_cancelU   sb    PZZ*= 	Nnn**9*MGD!	N (\(((t	N 	Ns   !AA$c                     d}t        j                  |d      5  | j                  j                  dd      \  }}d d d        t        j
                  k(  sJ dk(  sJ y # 1 sw Y   &xY w)Nr+   z
domain.comr-   r   Tr/   )r   r1   r   rY   rJ   rK   )r   r3   r[   input_s       r   test_input_normalz'FileOutputDisplayTest.test_input_normal\   so    PZZ*F 	S>>//	T/RLD&	S |&&&%%%		S 	Ss   !A  A)c                     d}| j                  | j                  j                  d|      \  }}|t        j                  k(  sJ ||k(  sJ y )Nfoor   rS   )r6   r   rY   rJ   rK   r   rT   r[   r_   s       r   test_input_noninteractivez/FileOutputDisplayTest.test_input_noninteractived   sS    11NN  )W 2 >f |&&&   r   c                     t        j                  t              5  | j                  | j                  j
                  dd       d d d        y # 1 sw Y   y xY w)Nr   --flagcli_flag)pytestraises	Exceptionr6   r   rY   r   s    r   test_input_assertion_failz/FileOutputDisplayTest.test_input_assertion_faill   sI     ]]9% 	[&&t~~';';YQY&Z	[ 	[ 	[s   )AAc                    t        j                  d      5  t        j                  t        j
                        5  | j                  | j                  j                  dd       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz&certbot.display.util.assert_valid_callrR   rf   rg   )	r   r1   ri   rj   r   Errorr6   r   rY   rl   s    r   test_input_assertion_fail2z0FileOutputDisplayTest.test_input_assertion_fail2r   sw    ZZ@A 	[v||, [**4>>+?+?QY*Z[	[ 	[[ [	[ 	[s"   $B )A4#B 4A=	9B  B	c                    d}t        j                  |d      5  | j                  j                  dd      sJ 	 d d d        t        j                  |d      5  | j                  j                  dd      sJ 	 d d d        t        j                  |ddg	      5  | j                  j                  dd      sJ 	 d d d        t        j                  |d
      5  | j                  j                  dd      rJ 	 d d d        t        j                  |ddg	      5  | j                  j                  dd      rJ 	 d d d        t        j                  |d      5  | j                  j                  ddd      sJ 	 d d d        y # 1 sw Y   PxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   }xY w# 1 sw Y   y xY w)Nr+   Yesr-   r   Tr/   ymaybeside_effectNocancelnarR   Agree)	yes_labelr0   )r   r1   r   yesnor2   s     r   
test_yesnoz FileOutputDisplayTest.test_yesnow   s   PZZ*? 	3>>''T ( 3 3 3	3 ZZ*= 	3>>''T ( 3 3 3	3 ZZ*#G 	3>>''T ( 3 3 3	3 ZZ*> 	3~~++T , 3 3 3 3	3 ZZ*3H 	3~~++T , 3 3 3 3	3 ZZ*= 	B>>''D ( B B B	B 	B!	3 	3	3 	3	3 	3	3 	3	3 	3	B 	BsG    F	 F F# F/ F;!G	FF #F,/F8;GGc                 X    | j                  | j                  j                  dd      sJ y )Nr   TrS   )r6   r   r}   rl   s    r   test_yesno_noninteractivez/FileOutputDisplayTest.test_yesno_noninteractive   s0    ))NN  )T * ; 	; ;r   r+   c                     d|_         | j                  j                  dt        d      \  }}|t	        |      ft
        j                  ddhfk(  sJ y )Nz2 1rR   Tr/   r   r   r.   r   	checklistTAGSsetrJ   rK   r   
mock_inputr[   tag_lists       r   test_checklist_validz*FileOutputDisplayTest.test_checklist_valid   sS    "'
1144 2 1hc(m$66:J(KKKKr   c                     d|_         | j                  j                  dt        d      \  }}|t	        |      ft
        j                  h dfk(  sJ y )N rR   Tr/   >   r   r   r   r   r   s       r   test_checklist_emptyz*FileOutputDisplayTest.test_checklist_empty   sK    "$
11%QU1Vhc(m$:R(SSSSr   c                     g d|_         | j                  j                  dt        d      }|t        j
                  dgfk(  sJ y )N)10ztag1 please1rR   Tr/   r   )rv   r   r   r   rJ   rK   r   r   rO   s      r   test_checklist_miss_validz/FileOutputDisplayTest.test_checklist_miss_valid   s>    !;
nn&&udd&K|1111r   c                     ddg|_         | j                  j                  dt        d      }|t        j
                  g fk(  sJ y )Nr   rX   rR   Tr/   )rv   r   r   r   rJ   rZ   r   s      r   test_checklist_miss_quitz.FileOutputDisplayTest.test_checklist_miss_quit   sB    "&
nn&&udd&K|**B////r   c                     t         }| j                  | j                  j                  dt         |      \  }}|t        j
                  k(  sJ ||k(  sJ y )NrR   rS   )r   r6   r   r   rJ   rK   rc   s       r   test_checklist_noninteractivez3FileOutputDisplayTest.test_checklist_noninteractive   sV    11NN$$eT7 2 Df |&&&   r   c                     dgg dddgg}dhddhddhg}t        |      D ]8  \  }}t        | j                  j                  |t                    }|||   k(  r8J  y )Nr   )r   2r   r   3r   r   r   )	enumerater   r   _scrub_checklist_inputr   )r   indicesexpilist_set_tagss         r    test_scrub_checklist_input_validz6FileOutputDisplayTest.test_scrub_checklist_input_valid   s     E#J
 HVV

 "'* 	&HAu55eTBDHs1v%%%	&r   c                 ~    g d}d}||_          | j                  j                  | }|t        j                  |fk(  sJ y )N)rR   /var/www/htmlrf   Tr   )r.   r   directory_selectrJ   rK   )r   r   args
user_inputreturneds        r   test_directory_selectz+FileOutputDisplayTest.test_directory_select   sA    7$
",
24>>22D9LOOZ8888r   c                     d}| j                  | j                  j                  d|      \  }}|t        j                  k(  sJ ||k(  sJ y )Nr   rR   rS   )r6   r   r   rJ   rK   rc   s       r   $test_directory_select_noninteractivez:FileOutputDisplayTest.test_directory_select_noninteractive   sT    !11NN++UG 2 Ef |&&&   r   c                 ~   | j                   j                  }t        j                  d      5 }d|j                  _        t        j                  d      5 } ||i |}d d d        d d d        |rj                  j                  du sJ S j                  j                  dk(  sJ S # 1 sw Y   LxY w# 1 sw Y   PxY w)Nz'certbot._internal.display.obj.sys.stdinFr   rI   )	r   r;   r   r1   isattyr.   warningcalled
call_count)r   funcr   kwargsr;   
mock_stdinr'   rU   s           r   r6   z+FileOutputDisplayTest._force_noninteractive   s    "nn@@ZZAB 	/j-2J*BC /{t.v./	/
 &&--666  &&11Q666/ /	/ 	/s#   'B3	B'B3'B0	,B33B<c                 x    dgdgdgddgddgg}|D ]'  }| j                   j                  |t              g k(  r'J  y )N04r   r   r   o)r   r   r   )r   r   r   s      r   "test_scrub_checklist_input_invalidz8FileOutputDisplayTest.test_scrub_checklist_input_invalid   sZ     EEH&M#J
  	LE>>88EKKK	Lr   c                     | j                   j                  dt               | j                   j                  dt               y )NrR   )r   _print_menurM   r   rl   s    r   test_print_menuz%FileOutputDisplayTest.test_print_menu   s,     	""5'2""5$/r   c                    d}t        j                  |d      5  | j                  j                  d      t        j
                  dfk(  sJ 	 d d d        d}t        j                  ||      5  | j                  j                  d      t        j
                  t        |      fk(  sJ 	 d d d        y # 1 sw Y   fxY w# 1 sw Y   y xY w)Nr+   r   r-   rI   r      )r   r1   r   _get_valid_int_ansrJ   rK   int)r   r3   anss      r   test_get_valid_int_ans_validz2FileOutputDisplayTest.test_get_valid_int_ans_valid   s    PZZ*= 	P>>44Q7LOOQ;OOOO	PZZ*= 	,>>44Q7#c(+, , ,	, 	,	P 	P	, 	,s   1B1-:B=1B:=Cc                     ddgg ddgg}d}|D ]S  }t        j                  ||      5  | j                  j                  d      t        j
                  dfk(  sJ 	 d d d        U y # 1 sw Y   `xY w)Nr   rX   )r   oneCr+   ru   r   )r   r1   r   r   rJ   rZ   )r   answersr3   r   s       r   test_get_valid_int_ans_invalidz4FileOutputDisplayTest.test_get_valid_int_ans_invalid  s     #JE

 Q 	.C.C@ .~~88;!(("-. . .. .	.. .s   1A&&A/	)#__name__
__module____qualname____doc__r   r   r1   r)   r4   r8   r<   rF   rP   rV   r]   r`   rd   rm   rp   r~   r   r   r   r   r   r   r   r   r   r6   r   r   r   r   __classcell__)r   s   @r   r   r      sf   J
 TZZ67N 8NC#
$	1 TZZ 1 2+2+
4)&![[
B,; TZZCDL EL TZZCDT ET
 TZZCD2 E2 TZZCD0 E0!&" TZZCD9 E9!
L0,.r   r   c                   j    e Zd ZdZd Z ej                  d      d        Zd Zd Z	d Z
d Zd	 Zd
 Zy)NoninteractiveDisplayTestz:Test non-interactive display. These tests are pretty easy!c                 ~    t        j                         | _        t        j                  | j                        | _        y )N)r   r   r   r   NoninteractiveDisplayr   rl   s    r   r   zNoninteractiveDisplayTest.setUp  s)    >>+$::4;K;KLr   r   c                     | j                   j                  dd       | j                  j                  j                  d   d   }d|v sJ |j
                  j                  dd       y )Nr   
   r   r   r    r&   s      r   r)   z4NoninteractiveDisplayTest.test_notification_no_pause  s\    ##Ir2!!''11!4Q7F""",,-A9Mr   c                 x   ddl m} | j                  j                  ddd       | j                  j
                  j                  d   d   }|d|j                  z   k(  sJ | j                  j                  dd       | j                  j
                  j                  d   d   }d|v sJ d|j                  z   |v sJ y r>   rB   rE   s      r   rF   z6NoninteractiveDisplayTest.test_notification_decoration  rG   r   c                    d}| j                   j                  d|      }|t        j                  |fk(  sJ t	        j
                  t        j                        5  | j                   j                  d       d d d        y # 1 sw Y   y xY w)Nzan incomputable valuer   rS   )r   rY   rJ   rK   ri   rj   r   MissingCommandlineFlagr   drO   s      r   
test_inputz$NoninteractiveDisplayTest.test_input(  sp    #nn""9a"8|****]]6889 	,NN  +	, 	, 	,s   A??Bc                 &   | j                   j                  dt        d      }|t        j                  dfk(  sJ t        j                  t        j                        5  | j                   j                  dt               d d d        y # 1 sw Y   y xY w)Nr   rI   rS   )	r   rL   rM   rJ   rK   ri   rj   r   r   )r   rO   s     r   rP   z#NoninteractiveDisplayTest.test_menu/  so    nn!!)Wa!@|****]]6889 	4NN	73	4 	4 	4s   !BBc                     d}| j                   j                  d|      }||k(  sJ t        j                  t        j
                        5  | j                   j                  d       d d d        y # 1 sw Y   y xY w)NFr   rS   )r   r}   ri   rj   r   r   r   s      r   r~   z$NoninteractiveDisplayTest.test_yesno5  sd    nn""9a"8axx]]6889 	,NN  +	, 	, 	,s   
A//A8c                 .   ddg}| j                   j                  dt        |      }|t        j                  |fk(  sJ t        j                  t        j                        5  | j                   j                  dt               d d d        y # 1 sw Y   y xY w)NrI   r   r   rS   )	r   r   r   rJ   rK   ri   rj   r   r   r   s      r   test_checklistz(NoninteractiveDisplayTest.test_checklist<  sx    Fnn&&y$&B|****]]6889 	6NN$$Y5	6 	6 	6s   !!BBc                    d}t         j                  |f}| j                  j                  d|      }||k(  sJ t	        j
                  t        j                        5  | j                  j                  d       d d d        y # 1 sw Y   y xY w)Nr   rR   )rJ   rK   r   r   ri   rj   r   r   )r   rT   expectedactuals       r   r   z/NoninteractiveDisplayTest.test_directory_selectC  sr    ! OOW-00@6!!!]]6889 	3NN++E2	3 	3 	3s   B  B	N)r   r   r   r   r   r   r1   r)   rF   r   rP   r~   r   r    r   r   r   r     sK    DM TZZ67N 8N	1,4,63r   r   __main__rI   )r   sysunittestr   ri   certbotr   certbot._internal.displayr   r   certbot.displayr   rJ   rM   r   TestCaser   r   r   exitmainargv__file__r   r   r   <module>r      s    0 
     8 0$&@
A|.H-- |.~;3 1 1 ;3| zCHH[V[[!"
234 r   