
    2i5                     V    S SK r S SKrS SKJr  S SKJr   " S S\ R                  5      rg)    N)Client)FAKE_TEST_API_KEYc                   d    \ rS rSr\S 5       r\S 5       rS rS rS r	S r
S rS	 rS
 rS rSrg)
TestClient	   c                     [         R                  " S5      U l        [         R                  " S5      U l        U R                  R	                  5         U R                  R	                  5         g )Nzposthog.client.batch_postzposthog.consumer.batch_post)mockpatchclient_post_patcherconsumer_post_patcherstartclss    W/home/james-whalen/.local/lib/python3.13/site-packages/posthog/test/test_before_send.py
setUpClassTestClient.setUpClass
   sL     #'**-H"I$(JJ/L$M!%%'!!'')    c                 l    U R                   R                  5         U R                  R                  5         g )N)r   stopr   r   s    r   tearDownClassTestClient.tearDownClass   s&    $$&!!&&(r   c                 *    [        SX5        SU l        g)zMark the failure handlerFAILTN)printfailed)selfebatchs      r   set_failTestClient.set_fail   s    far   c                 L    SU l         [        [        U R                  S9U l        g )NF)on_error)r   r   r   r   client)r   s    r   setUpTestClient.setUp   s    .Gr   c                 ^  ^ / mU4S jn[        [        U R                  US9nUR                  SSSS05      u  p4U R	                  U5        U R                  US   S   S	5        U R                  US   S   S5        U R                  [        T5      S
5        U R                  TS   S   S5        g)z1Test that before_send callback can modify events.c                 l   > TR                  U R                  5       5        SU ;  a  0 U S'   SU S   S'   U $ )N
propertiesTprocessed_by_before_send)appendcopy)eventprocessed_eventss    r   my_before_sendKTestClient.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_event3TestClient.test_before_send_callback_modifies_event    s    	 >
 ~~g|j'=RS \*+EFM\*:6@-.2)!,W5|Dr   c                 >   S n[        [        U R                  US9nUR                  SS5      u  p4U R	                  U5        U R                  U5        UR                  SS5      u  p4U R	                  U5        U R                  U5        U R                  US   S5        g)zATest that before_send callback can drop events by returning None.c                 2    U R                  S5      S:X  a  g U $ )Nr,   test_drop_me)getr,   s    r   drop_test_eventsJTestClient.test_before_send_callback_drops_event.<locals>.drop_test_events9   s    yy!^3Lr   r0   r2   rA   keep_mer,   N)r   r   r   r7   r8   assertIsNoneassertIsNotNoner9   )r   rD   r#   r;   r<   s        r   %test_before_send_callback_drops_event0TestClient.test_before_send_callback_drops_event6   s    	
 CS

 ~~g~> # ~~gy9 S!Wy1r   c                     S n[        [        U R                  US9nUR                  SS5      u  p4U R	                  U5        U R                  U5        U R                  US   S5        g)z;Test that exceptions in before_send don't crash the client.c                     [        S5      e)NzOops!)
ValueErrorrC   s    r   buggy_before_sendRTestClient.test_before_send_callback_handles_exceptions.<locals>.buggy_before_sendP   s    W%%r   r0   r2   robust_eventr,   Nr   r   r   r7   r8   rH   r9   )r   rN   r#   r;   r<   s        r   ,test_before_send_callback_handles_exceptions7TestClient.test_before_send_callback_handles_exceptionsM   sc    	& CT
 ~~g~> 	 S!W~6r   c                 $   S n[        [        U R                  US9nUR                  SS5      u  p4U R	                  U5        U R	                  US   S   5        UR                  SSS05      u  p4U R	                  U5        U R	                  US   S   5        UR                  SS	S05      u  p4U R	                  U5        U R	                  US   S   5        UR                  SS
5      u  p4U R	                  U5        U R	                  US   S   5        g)z=Test that before_send works with capture, identify, set, etc.c                 ,    SU ;  a  0 U S'   SU S   S'   U $ )Nr(   Tmarked rC   s    r   
add_markerSTestClient.test_before_send_callback_works_with_all_event_types.<locals>.add_marker`   s(    5(&(l#,0E,)Lr   r0   r2   r,   r(   rV   traitr5   propzhttps://example.comN)r   r   r   r7   r8   identifysetpage)r   rX   r#   r;   r<   s        r   4test_before_send_callback_works_with_all_event_types?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                     [        [        U R                  SS9nUR                  SS5      u  p#U R	                  U5        U R                  U5        U R                  US   S5        g)z9Test that client works normally when before_send is None.Nr0   r2   normal_eventr,   rQ   )r   r#   r;   r<   s       r   ,test_before_send_callback_disabled_when_none7TestClient.test_before_send_callback_disabled_when_none~   sU    )DMMtT~~g~> S!W~6r   c                    S n[        [        U R                  US9nUR                  SSSSSS.5      u  p4U R	                  U5        U R                  US	   S
   S5        U R                  SUS	   5        U R                  US	   S   S5        g)z(Test a realistic PII scrubbing use case.c                     U R                  S0 5      nSU;   a-  US   nSU;   a  UR                  S5      S   nSU 3US'   OSUS'   UR                  SS 5        U $ )Nr(   email@r6   z***@z***credit_card)rB   splitpop)r,   r(   rg   domains       r   	scrub_piiMTestClient.test_before_send_callback_pii_scrubbing_example.<locals>.scrub_pii   so    <4J *$"7+%<"[[-a0F,0/Jw'*/Jw' NN=$/Lr   r0   r2   form_submitzuser@example.comz1234-5678-9012-3456contact)rg   ri   	form_namer(   rg   z***@example.comri   rq   N)r   r   r   r7   r8   r9   assertNotIn)r   rm   r#   r;   r<   s        r   /test_before_send_callback_pii_scrubbing_example:TestClient.test_before_send_callback_pii_scrubbing_example   s    	" 9
 ~~+4&
 	 \*735FGL(9:\*;7Cr   )r#   r   N)__name__
__module____qualname____firstlineno__classmethodr   r   r   r$   r=   rI   rR   r_   rc   rs   __static_attributes__rW   r   r   r   r   	   sT    * * ) )
HE,2.7 5B7$Dr   r   )unittestr	   posthog.clientr   posthog.test.test_utilsr   TestCaser   rW   r   r   <module>r      s'      ! 5bD"" bDr   