
    rkim%              
          d Z ddlZddlmc mZ ddlZddlZej                  j                  dej                  j                  ej                  j                  ej                  j                  e                         ddlZddlZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZ dZdZd Z  G d	 d
      Z! G d d      Z" G d d      Z# G d d      Z$ejJ                  jL                   G d d             Z'y)uu  
Tests pour le module app.core.email.

- Tests unitaires  : mock SMTP, aucun envoi réel.
- Test d'intégration : envoi réel vers komarf28@gmail.com (marqué `integration`).

Lancer uniquement les tests unitaires (rapides, sans réseau) :
    pytest tests/test_email.py -v -m "not integration"

Lancer tous les tests dont l'envoi réel :
    pytest tests/test_email.py -v
    N)MIMEMultipart)patch	MagicMock)	SMTP_HOST	SMTP_PORTDEFAULT_EMAIL
send_emailsend_verification_emailsend_reset_password_email_verification_html_reset_password_htmlzkomarf28@gmail.comz3eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.signaturec                  ^    t               } t        |       | _        t        d      | _        | S )z;Retourne un mock SMTP_SSL utilisable comme context manager.return_valueF)r   	__enter____exit__)smtp_instances    O/home/runner/work/therecruiter-backend/therecruiter-backend/tests/test_email.py_make_smtp_mockr   ,   s)    KM']CM&E:M    c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)TestEmailTemplatesc                    d}t        |      }||v }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      nddt        j                         v st        j
                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d }y )	Nz-https://example.com/verify-email?token=abc123inz%(py0)s in %(py2)slinkhtmlpy0py2assert %(py4)spy4)	r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationselfr   r   @py_assert1@py_format3@py_format5s         r   $test_verification_html_contains_linkz7TestEmailTemplates.test_verification_html_contains_link9   sx    >!$'t|ttttttr   c                    t        d      }d}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}d	}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}d
}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}y )Nhttps://example.com/verifyz<!DOCTYPE html>r   z%(py1)s in %(py3)sr   py1py3assert %(py5)spy5z<htmlz</html>	r   r$   r%   r)   r&   r'   r(   r*   r+   r-   r   @py_assert0@py_assert2@py_format4@py_format6s         r   $test_verification_html_is_valid_htmlz7TestEmailTemplates.test_verification_html_is_valid_html>   s   !">? ( D(((( D((( ((((((D(((D(((((((w$w$w$$ yD    yD   y      D   D       r   c                 d   t        d      }d}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}y )	Nr3   KARABAr   r4   r   r5   r8   r9   r:   r;   s         r   %test_verification_html_contains_brandz8TestEmailTemplates.test_verification_html_contains_brandD   sf    !">?x4x4x44r   c                    d}t        |      }||v }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      nddt        j                         v st        j
                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d }y )	Nz,https://example.com/reset-password?token=xyzr   r   r   r   r   r"   r#   )	r   r$   r%   r&   r'   r(   r)   r*   r+   r,   s         r   &test_reset_password_html_contains_linkz9TestEmailTemplates.test_reset_password_html_contains_linkH   sx    =#D)t|ttttttr   c                 d   t        d      }d}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}y )	Nzhttps://example.com/reset15r   r4   r   r5   r8   r9   )	r   r$   r%   r)   r&   r'   r(   r*   r+   r;   s         r   (test_reset_password_html_mentions_expiryz;TestEmailTemplates.test_reset_password_html_mentions_expiryM   se    #$?@tt|tttttr   c                 d   t        d      }d}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d x}}y )	Nr3   24r   r4   r   r5   r8   r9   r:   r;   s         r   &test_verification_html_mentions_expiryz9TestEmailTemplates.test_verification_html_mentions_expiryQ   se    !">?tt|tttttr   N)	__name__
__module____qualname__r1   r@   rC   rE   rH   rK    r   r   r   r   8   s     
! 
r   r   c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)TestSendEmailc                     t               }t        d|      5 }t        t        ddd       |j	                  t
        t               d d d        y # 1 sw Y   y xY w)Napp.core.email.smtplib.SMTP_SSLr   
Test sujetCorps texte<p>Corps HTML</p>)r   r   r	   TEST_RECIPIENTassert_called_once_withr   r   )r-   	smtp_mocksmtp_clss      r   test_send_email_calls_smtp_sslz,TestSendEmail.test_send_email_calls_smtp_sslW   sP    #%	49M 	CQY~|]DWX,,Y	B	C 	C 	Cs   -AAc                 V   t               }t        d|      5  t        t        dd       |j                  j                          |j                  j                  d   }|d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndd	z  }d
d|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY w)NrS   r   rT   rU   r   ==z%(py1)s == %(py3)sr   r5   r8   r9   )r   r   r	   rW   loginassert_called_once	call_argsr   r$   r%   r)   r&   r'   r(   r*   r+   )r-   rY   argsr<   r=   r>   r?   s          r   test_send_email_authenticatesz+TestSendEmail.test_send_email_authenticates]   s    #%	49M 	,~|]COO..0??,,Q/D7+7m++++7m+++7++++++m+++m+++++++		, 	, 	,s   C>DD(c                     t               }t        d|      5  t        t        dd       |j                  j                          d d d        y # 1 sw Y   y xY w)NrS   r   rT   rU   )r   r   r	   rW   send_messagera   r-   rY   s     r   test_send_email_sends_messagez+TestSendEmail.test_send_email_sends_messagee   sH    #%	49M 	8~|]C""557	8 	8 	8s   ,AAc                 (   t               }t        d|      5  t        t        dd       |j                  j
                  d   d   }|d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      d	t        j                         v st        j                  t              rt        j                  t              nd	d
z  }dd|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY w)NrS   r   rT   rU   r   Tor]   r_   rW   r5   r8   r9   )r   r   r	   rW   rf   rb   r$   r%   r)   r&   r'   r(   r*   r+   )r-   rY   sent_msgr<   r=   r>   r?   s          r   !test_send_email_correct_recipientz/TestSendEmail.test_send_email_correct_recipientk   s    #%	49M 	4~|]C --77:1=HD>3>^3333>^333>333333^333^3333333	4 	4 	4s   C'DDc                    t               }t        d|      5  t        t        dd       |j                  j
                  d   d   }|d   }d}||k(  }|slt        j                  d|fd||f      t        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            d x}x}}d d d        y # 1 sw Y   y xY w)NrS   r   zMon sujet uniquerU   r   Subjectr]   )z%(py1)s == %(py4)s)r6   r#   zassert %(py6)spy6)r   r   r	   rW   rf   rb   r$   r%   r)   r*   r+   )r-   rY   rk   r<   @py_assert3r=   r0   @py_format7s           r   test_send_email_correct_subjectz-TestSendEmail.test_send_email_correct_subjectr   s    #%	49M 	=~'9=I --77:1=HI&<*<<&*<<<<<&*<<<<&<<<*<<<<<<<<	= 	= 	=s   B.CCc                 2   t               }t        d|      5  t        t        dd       |j                  j
                  d   d   }d}|d   }|j                  } |       }||v }|st        j                  d|fd	||f      t        j                  |      t        j                  |      t        j                  |      t        j                  |      d
z  }dd|iz  }	t        t        j                  |	            d x}x}x}x}}d d d        y # 1 sw Y   y xY w)NrS   r   SujetCorpsr   karabaFromr   )zD%(py1)s in %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s.lower
}()
})r6   r#   ro   py8zassert %(py10)spy10)r   r   r	   rW   rf   rb   lowerr$   r%   r)   r*   r+   )
r-   rY   rk   r<   rp   @py_assert5@py_assert7r=   @py_format9@py_format11s
             r   test_send_email_from_headerz)TestSendEmail.test_send_email_from_headery   s    #%	49M 	8~w8 --77:1=H7x/7/557577877777877778777/777577777777777	8 	8 	8s   C,DDc           	         t               }t        d|      5  t        j                  t        j                  ddd      5  t        dd      5  t        dd      5  t        t        dd       |j                  j                          d d d        d d d        d d d        d d d        y # 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)	NrS   r    )EMAIL_ADDRESSEMAIL_PASSWORDzapp.core.email.DEFAULT_EMAILzapp.core.email.DEFAULT_PASSWORDrt   ru   )	r   r   dictosenvironr	   rW   rf   assert_not_calledrg   s     r   )test_send_email_skips_when_no_credentialsz7TestSendEmail.test_send_email_skips_when_no_credentials   s    #%	49M 	CBJJ"PR(ST C92> C@"E C">7GD!..@@BCCC	C 	CC CC CC C	C 	CsS   (C B?B3,B'	B3B?C'B0,B33B<8B??C	CCc                     t               }t        j                  d      |j                  _        t        d|      5  t        t        dd       d d d        y # 1 sw Y   y xY w)NzErreur SMTPrS   r   rt   ru   )r   smtplibSMTPExceptionrf   side_effectr   r	   rW   rg   s     r   -test_send_email_handles_smtp_error_gracefullyz;TestSendEmail.test_send_email_handles_smtp_error_gracefully   sO    #%	-4-B-B=-Q	*49M 	9~w8	9 	9 	9s   AA c                    t               }t        d|      5  t        t        ddd       |j                  j
                  d   d   }|j                         }t        |      }d}||k(  }|st        j                  d|fd	||f      d
t        j                         v st        j                  t              rt        j                  t              nd
dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}}|D 	cg c]  }	|	j!                          }
}	d}||
v }|st        j                  d|fd||
f      t        j                  |      dt        j                         v st        j                  |
      rt        j                  |
      nddz  }dd|iz  }t        t        j                  |            d x}}d}||
v }|st        j                  d|fd||
f      t        j                  |      dt        j                         v st        j                  |
      rt        j                  |
      nddz  }dd|iz  }t        t        j                  |            d x}}d d d        y c c}	w # 1 sw Y   y xY w)NrS   r   rt   rU   rV   r      r]   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)slenpayloads)r    r6   r7   ro   zassert %(py8)srx   z
text/plainr   r4   content_typesr5   r8   r9   z	text/html)r   r   r	   rW   rf   rb   get_payloadr   r$   r%   r&   r'   r(   r)   r*   r+   get_content_type)r-   rY   rk   r   r=   r{   @py_assert4rq   r}   pr   r<   r>   r?   s                 r   2test_send_email_with_html_creates_alternative_mimez@TestSendEmail.test_send_email_with_html_creates_alternative_mime   s   #%	49M 	0~w?RS --77:1=H++-Hx=%A%=A%%%%=A%%%%%%3%%%3%%%%%%x%%%x%%%=%%%A%%%%%%%;CDaQ//1DMD0<=0000<=000<000000=000=0000000/;-////;-///;//////-///-///////	0 	0 E	0 	0s   EK+KEKKK%N)rL   rM   rN   r[   rd   rh   rl   rr   r   r   r   r   rO   r   r   rQ   rQ   V   s1    C,84=8C9
0r   rQ   c                   $    e Zd Zd Zd Zd Zd Zy)TestSendVerificationEmailc                     t        d      5 }t        t        t               |j	                          d d d        y # 1 sw Y   y xY wNapp.core.email.send_email)r   r
   rW   
FAKE_TOKENra   r-   	mock_sends     r   (test_verification_email_calls_send_emailzBTestSendVerificationEmail.test_verification_email_calls_send_email   s8    ./ 	+9#NJ?((*	+ 	+ 	+	   %:Ac                    t        d      5 }t        t        t               |j                  d   |j                  }}|j                  d   d   }g }d}|j
                  } |       }||v }	|	}
|	sd}|j
                  } |       }||v }|}
|
st        j                  d|	fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      d	z  }d
d|iz  }|j                  |       |	st        j                  dfdf      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                        t        j                  |      dz  }dd|iz  }|j                  |       t        j                  |d      i z  }dd|iz  }t        t        j                  |            d x}
x}x}x}	x}x}x}x}x}}d d d        y # 1 sw Y   y xY w)Nr   r      u   vérifemailr   zD%(py3)s in %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s.lower
}()
}subjectr7   r9   py7py9%(py11)spy11zJ%(py14)s in %(py20)s
{%(py20)s = %(py18)s
{%(py18)s = %(py16)s.lower
}()
}py14py16py18py20%(py22)spy22assert %(py25)spy25)r   r
   rW   r   rb   rz   r$   r%   r)   r&   r'   r(   append_format_boolopr*   r+   )r-   r   _kwargsr   r.   r=   @py_assert6@py_assert8r   r<   @py_assert13@py_assert17@py_assert19@py_assert15@py_format10@py_format12@py_format21@py_format23@py_format24@py_format26s                        r   test_verification_email_subjectz9TestSendVerificationEmail.test_verification_email_subject   ss   ./ 	M9#NJ?!++A.	0C0CvA))!,Q/GL8Lw}}L}L8.L'LW]]L]_L'_2LLLLL8LLL8LLLLLLwLLLwLLL}LLLLLLLLLL'_LLL'LLLLLLWLLLWLLL]LLL_LLLLLLLLLLLLLLL		M 	M 	Ms   H;IIc                 L   t        d      5 }t        t        t               |j                  d   d   }t        |v }|st        j                  d|fdt        |f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      nddz  }d	d
|iz  }t        t        j                  |            d }d d d        y # 1 sw Y   y xY wNr   r   r   r   r   r   
plain_bodyr   r"   r#   )r   r
   rW   r   rb   r$   r%   r&   r'   r(   r)   r*   r+   r-   r   r   r.   r/   r0   s         r   %test_verification_email_token_in_linkz?TestSendVerificationEmail.test_verification_email_token_in_link   s    ./ 	,9#NJ?",,Q/2J++++:++++++:+++:++++++++++++++++	, 	, 	,   DDD#c                    t        d      5 }t        t        t               |j                  d   d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              nddz  }dd|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY w	Nr   r   r]   r_   rW   r5   r8   r9   )r   r
   rW   r   rb   r$   r%   r)   r&   r'   r(   r*   r+   r-   r   r<   r=   r>   r?   s         r   !test_verification_email_recipientz;TestSendVerificationEmail.test_verification_email_recipient   s    ./ 	?9#NJ?&&q)!,>,>>>>,>>>,>>>>>>>>>>>>>>>>	? 	? 	?   CC00C9N)rL   rM   rN   r   r   r   r   rO   r   r   r   r      s    +
M,?r   r   c                   $    e Zd Zd Zd Zd Zd Zy)TestSendResetPasswordEmailc                     t        d      5 }t        t        t               |j	                          d d d        y # 1 sw Y   y xY wr   )r   r   rW   r   ra   r   s     r   !test_reset_email_calls_send_emailz<TestSendResetPasswordEmail.test_reset_email_calls_send_email   s8    ./ 	+9%njA((*	+ 	+ 	+r   c                    t        d      5 }t        t        t               |j                  d   d   }g }d}|j
                  } |       }||v }|}|sd}	|j
                  }
 |
       }|	|v }|}|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      d	z  }d
d|iz  }|j                  |       |st        j                  dfd	f      t        j                  |	      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  
      t        j                  |      dz  }dd|iz  }|j                  |       t        j                  |d      i z  }dd|iz  }t        t        j                  |            d x}x}x}x}x}x}x}	x}x}
}d d d        y # 1 sw Y   y xY w)Nr   r   r   zmot de passepasswordr   r   r   r   r   r   r   r   r   r   r   r   )r   r   rW   r   rb   rz   r$   r%   r)   r&   r'   r(   r   r   r*   r+   )r-   r   r   r.   r=   r   r   r   r<   r   r   r   r   r   r   r   r   r   r   s                      r   test_reset_email_subjectz3TestSendResetPasswordEmail.test_reset_email_subject   sY   ./ 	V9%njA))!,Q/GU>UW]]U]_U>_4U
UgmmUmoU
o8UUUUU>_UUU>UUUUUUWUUUWUUU]UUU_UUUUUUU
oUUU
UUUUUUgUUUgUUUmUUUoUUUUUUUUUUUUUUU	V 	V 	Vs   H H55H>c                 L   t        d      5 }t        t        t               |j                  d   d   }t        |v }|st        j                  d|fdt        |f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      nddz  }d	d
|iz  }t        t        j                  |            d }d d d        y # 1 sw Y   y xY wr   )r   r   rW   r   rb   r$   r%   r&   r'   r(   r)   r*   r+   r   s         r   test_reset_email_token_in_linkz9TestSendResetPasswordEmail.test_reset_email_token_in_link   s    ./ 	,9%njA",,Q/2J++++:++++++:+++:++++++++++++++++	, 	, 	,r   c                    t        d      5 }t        t        t               |j                  d   d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              nddz  }dd|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY wr   )r   r   rW   r   rb   r$   r%   r)   r&   r'   r(   r*   r+   r   s         r   test_reset_email_recipientz5TestSendResetPasswordEmail.test_reset_email_recipient   s    ./ 	?9%njA&&q)!,>,>>>>,>>>,>>>>>>>>>>>>>>>>	? 	? 	?r   N)rL   rM   rN   r   r   r   r   rO   r   r   r   r      s    +
V,?r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestRealEmailDeliveryu   
    Ces tests envoient de vrais emails vers komarf28@gmail.com.
    Ils nécessitent que le serveur mail soit accessible.
    c                 (    t        t        dd       y)zEnvoi direct via send_email.u   [TEST] Email brut – Karabaz7Ceci est un test d'envoi brut depuis le serveur Karaba.)to_emailr   	body_textN)r	   rW   r-   s    r   test_send_raw_emailz)TestRealEmailDelivery.test_send_raw_email   s    #2O	
r   c                 ,    t        t        t               y)u,   Envoi d'un email de vérification avec HTML.N)r
   rW   r   r   s    r   !test_send_verification_email_realz7TestRealEmailDelivery.test_send_verification_email_real   s    
;r   c                 ,    t        t        t               y)z-Envoi d'un email de reset password avec HTML.N)r   rW   r   r   s    r   #test_send_reset_password_email_realz9TestRealEmailDelivery.test_send_reset_password_email_real   s    !.*=r   N)rL   rM   rN   __doc__r   r   r   rO   r   r   r   r      s    

<>r   r   )(r   builtinsr&   _pytest.assertion.rewrite	assertionrewriter$   sysr   pathinsertdirnameabspath__file__r   pytestemail.mime.multipartr   unittest.mockr   r   app.core.emailr   r   r   r	   r
   r   r   r   rW   r   r   r   rQ   r   r   markintegrationr   rO   r   r   <module>r      s     
 	 277??277??277??83L#MN O   . *	 	 	 &B
 <D0 D0N? ?2? ?8 > > >r   