
    Bvh5                     V    d dl Z d dlZd dlmZ d dlmZ  G d de j                        Zy)    N)Client)FAKE_TEST_API_KEYc                   \    e Zd Zed        Zed        Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zy)
TestClientc                     t        j                  d      | _        t        j                  d      | _        | j                  j	                          | j                  j	                          y )Nzposthog.client.batch_postzposthog.consumer.batch_post)mockpatchclient_post_patcherconsumer_post_patcherstartclss    G/RAG/venv/lib/python3.12/site-packages/posthog/test/test_before_send.py
setUpClasszTestClient.setUpClass
   sL     #'**-H"I$(JJ/L$M!%%'!!'')    c                 l    | j                   j                          | j                  j                          y )N)r
   stopr   r   s    r   tearDownClasszTestClient.tearDownClass   s&    $$&!!&&(r   c                 ,    t        d||       d| _        y)zMark the failure handlerFAILTN)printfailed)selfebatchs      r   set_failzTestClient.set_fail   s    far   c                 R    d| _         t        t        | j                        | _        y )NF)on_error)r   r   r   r   client)r   s    r   setUpzTestClient.setUp   s    .Gr   c                 d   g fd}t        t        | j                  |      }|j                  ddddi      \  }}| j	                  |       | j                  |d   d   d	       | j                  |d   d   d       | j                  t              d
       | j                  d   d   d       y)z1Test that before_send callback can modify events.c                 h    j                  | j                                d| vri | d<   d| d   d<   | S )N
propertiesTprocessed_by_before_send)appendcopy)eventprocessed_eventss    r   my_before_sendzKTestClient.test_before_send_callback_modifies_event.<locals>.my_before_send$   s>    ##EJJL15(&(l#>BE, :;Lr   r   before_senduser1
test_eventoriginalvaluer#   r$   T   r   r'   N)r   r   r   capture
assertTrueassertEquallen)r   r)   r   successmsgr(   s        @r   (test_before_send_callback_modifies_eventz3TestClient.test_before_send_callback_modifies_event    s    	 >
 ~~g|j'=RS \*+EFM\*:6@-.2)!,W5|Dr   c                 H   d }t        t        | j                  |      }|j                  dd      \  }}| j	                  |       | j                  |       |j                  dd      \  }}| j	                  |       | j                  |       | j                  |d   d       y)zATest that before_send callback can drop events by returning None.c                 0    | j                  d      dk(  ry | S )Nr'   test_drop_me)getr'   s    r   drop_test_eventszJTestClient.test_before_send_callback_drops_event.<locals>.drop_test_events9   s    yy!^3Lr   r*   r,   r:   keep_mer'   N)r   r   r   r1   r2   assertIsNoneassertIsNotNoner3   )r   r=   r   r5   r6   s        r   %test_before_send_callback_drops_eventz0TestClient.test_before_send_callback_drops_event6   s    	
 CS

 ~~g~> # ~~gy9 S!Wy1r   c                     d }t        t        | j                  |      }|j                  dd      \  }}| j	                  |       | j                  |       | j                  |d   d       y)z;Test that exceptions in before_send don't crash the client.c                     t        d      )NzOops!)
ValueErrorr<   s    r   buggy_before_sendzRTestClient.test_before_send_callback_handles_exceptions.<locals>.buggy_before_sendP   s    W%%r   r*   r,   robust_eventr'   Nr   r   r   r1   r2   r@   r3   )r   rE   r   r5   r6   s        r   ,test_before_send_callback_handles_exceptionsz7TestClient.test_before_send_callback_handles_exceptionsM   se    	& CT
 ~~g~> 	 S!W~6r   c                 2   d }t        t        | j                  |      }|j                  dd      \  }}| j	                  |       | j	                  |d   d          |j                  dddi      \  }}| j	                  |       | j	                  |d   d          |j                  dd	di      \  }}| j	                  |       | j	                  |d   d          |j                  dd
      \  }}| j	                  |       | j	                  |d   d          y)z=Test that before_send works with capture, identify, set, etc.c                 (    d| vri | d<   d| d   d<   | S )Nr#   Tmarked r<   s    r   
add_markerzSTestClient.test_before_send_callback_works_with_all_event_types.<locals>.add_marker`   s(    5(&(l#,0E,)Lr   r*   r,   r'   r#   rK   traitr/   propzhttps://example.comN)r   r   r   r1   r2   identifysetpage)r   rM   r   r5   r6   s        r   4test_before_send_callback_works_with_all_event_typesz?TestClient.test_before_send_callback_works_with_all_event_types]   s	   	 :

 ~~gw7 L)(34 w'0BC L)(34 zz'FG+<= L)(34 {{7,AB L)(34r   c                     t        t        | j                  d      }|j                  dd      \  }}| j	                  |       | j                  |       | j                  |d   d       y)z9Test that client works normally when before_send is None.Nr*   r,   normal_eventr'   rG   )r   r   r5   r6   s       r   ,test_before_send_callback_disabled_when_nonez7TestClient.test_before_send_callback_disabled_when_none~   sW    )DMMtT~~g~> S!W~6r   c                 "   d }t        t        | j                  |      }|j                  dddddd      \  }}| j	                  |       | j                  |d	   d
   d       | j                  d|d	          | j                  |d	   d   d       y)z(Test a realistic PII scrubbing use case.c                     | j                  di       }d|v r+|d   }d|v r|j                  d      d   }d| |d<   nd|d<   |j                  dd        | S )Nr#   email@r0   z***@z***credit_card)r;   splitpop)r'   r#   rY   domains       r   	scrub_piizMTestClient.test_before_send_callback_pii_scrubbing_example.<locals>.scrub_pii   so    <4J *$"7+%<"[[-a0F,0/Jw'*/Jw' NN=$/Lr   r*   r,   form_submitzuser@example.comz1234-5678-9012-3456contact)rY   r[   	form_namer#   rY   z***@example.comr[   rb   N)r   r   r   r1   r2   r3   assertNotIn)r   r_   r   r5   r6   s        r   /test_before_send_callback_pii_scrubbing_examplez:TestClient.test_before_send_callback_pii_scrubbing_example   s    	" 9
 ~~+4&
 	 \*735FGL(9:\*;7Cr   N)__name__
__module____qualname__classmethodr   r   r   r    r7   rA   rH   rS   rV   rd   rL   r   r   r   r   	   sT    * * ) )
HE,2.7 5B7$Dr   r   )unittestr   posthog.clientr   posthog.test.test_utilsr   TestCaser   rL   r   r   <module>rm      s'      ! 5bD"" bDr   