o
    &zhM                     @   sB   d dl Z d dlmZ d dlmZ d dlmZ G dd de jZdS )    N)ImageKit)Default)Urlc                   @   s  e Zd ZdCddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Z d?d@ Z!dAdB Z"dS )DTestGenerateURLreturnNc                 C   s   t dddd| _d S )Nprivate_key_testpublic_key_test%https://test-domain.com/test-endpoint)private_key
public_keyurl_endpoint)r   client)self r   R/var/www/html/kangema/venv/lib/python3.10/site-packages/tests/test_generate_url.pysetUp   s
   zTestGenerateURL.setUpc                 C   s.   ddddgd}| j |}| |d d S )N/default-image.jpg300400heightwidthpathtransformationFhttps://test-domain.com/test-endpoint/tr:h-300,w-400/default-image.jpgr   urlassertEqualr   optionsr   r   r   r   test_generate_url_with_path   s   
z+TestGenerateURL.test_generate_url_with_pathc                 C   s4   ddddgddid}| j |}| |d d S )	Nr   r   r   r   zik-attachmentT)r   r   query_parameterszYhttps://test-domain.com/test-endpoint/tr:h-300,w-400/default-image.jpg?ik-attachment=truer   r   r   r   r   .test_generate_url_with_path_with_ik_attachment   s   
z>TestGenerateURL.test_generate_url_with_path_with_ik_attachmentc                 C   s0   dddddgd}| j |}| |d d S )Nr   zf-autor   r   )rawr   r   r   zMhttps://test-domain.com/test-endpoint/tr:f-auto,h-300,w-400/default-image.jpgr   r   r   r   r   3test_generate_url_With_path_with_transformation_raw&   s   zCTestGenerateURL.test_generate_url_With_path_with_transformation_rawc                 C   0   dddddgd}| j |}| |d dS )	z
        Overriding urlEndpoint parameter. Passing a urlEndpoint value which is
        different from what I've used during SDK initialization and see if the url
        returned is using this new parameter
        r   z$https://ik.imagekit.io/new/endpoint/r   r   r   )r   r   r   zDhttps://ik.imagekit.io/new/endpoint/tr:h-300,w-400/default-image.jpgNr   r   r   r   r   8test_overriding_url_endpoint_generation_consists_new_url2   s   
zHTestGenerateURL.test_overriding_url_endpoint_generation_consists_new_urlc                 C   s6   dddddddgd}| j |}| |d	 d S )
Nr   value1value2)param1param2r   r   r   )r   r"   r   zbhttps://test-domain.com/test-endpoint/tr:h-300,w-400/default-image.jpg?param1=value1&param2=value2r   r   r   r   r   "test_generate_url_query_parametersC   s   
z2TestGenerateURL.test_generate_url_query_parametersc                 C   s<   ddddddddd	d
igd}| j |}| |d d S )N5https://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpgr   r   jpgtrue1zar-4-3,q-40)r   r   formatprogressiveeffect_contrastr$   rotationZ   srcr   zhttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?tr=h-300%2Cw-400%2Cf-jpg%2Cpr-true%2Ce-contrast-1%2Car-4-3%2Cq-40%3Art-90r   r   r   r   r   test_generate_url_with_srcO   s    z*TestGenerateURL.test_generate_url_with_srcc                 C   s@   dddidddddd	d
digd}| j |}| |d d S )NzHhttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?queryparam1=value1r*   r(   r   r   r.   r/   r0   r   r   r1   r2   r3   r4   r5   )r7   r"   r   zhttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?queryparam1=value1&param1=value1&tr=h-300%2Cw-400%2Cf-jpg%2Cpr-true%2Ce-contrast-1%3Art-90r   r   r   r   r   3test_generate_url_with_src_with_query_params_doublee   s    zCTestGenerateURL.test_generate_url_with_src_with_query_params_doublec                 C   s   dddddgdd}| j |}| |d | |d	 | | d| | d| | d
| | d| | j |}| |dd d d d | |dd dd  d d S )Nr   1https://ik.imagekit.io/your_imagekit_id/endpoint/r   r   r   T)r   r   r   signed r   z&&z??default-image.jpg   ?r   //)r   r   assertIsNotassertInassertNotInr   splitassertNotEqualr   r   r   r   &test_generate_url_with_path_and_signed|   s   
"z6TestGenerateURL.test_generate_url_with_path_and_signedc                 C   2   ddddigd}| j |}| tjj| dS )zN
        Check path param url generation doesn't contain double slash
        /test-signed-url.jpgTr   d   r   r<   r   N)r   r   rD   r   SIGNATURE_PARAMETERvaluer   r   r   r   5test_generate_url_with_path_and_signed_in_proper_form      zETestGenerateURL.test_generate_url_with_path_and_signed_in_proper_formc                 C   rI   )zl
        Check query params does not contain timestamp parameter if expire_seconds isn't specified.
        rJ   Tr   rK   rL   N)r   r   rE   r   TIMESTAMP_PARAMETERrN   r   r   r   r   Itest_generate_url_signed_without_expiry_does_not_have_timestamp_parameter   rP   zYTestGenerateURL.test_generate_url_signed_without_expiry_does_not_have_timestamp_parameterc                 C   s>   dddddgdd}| j |}| d| | |d	 d S )
Nr   r;   r   r   )r   	fake_xxxxqueryr   r   r   transformation_positionrS   z[https://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=h-300%2Cfake_xxxx-400)r   r   rD   r   r   r   r   r   1test_url_with_new_transformation_returns_as_it_is   s   
zATestGenerateURL.test_url_with_new_transformation_returns_as_it_isc                 C   s0   ddddigdd}| j |}| |d d S )Nr   r;   r   r   rT   rU   zKhttps://ik.imagekit.io/your_imagekit_id/endpoint/default-image.jpg?tr=h-300r   r   r   r   r   Ltest_query_url_generation_transformation_as_query_and_transformations_in_url   s   z\TestGenerateURL.test_query_url_generation_transformation_as_query_and_transformations_in_urlc                 C   :   ddddddddd	igd
}| j |}| |d d S )Nr-   r   r   r.   r/   r0   r9   r4   r5   r6   ohttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?tr=h-300%2Cw-400%2Cf-jpg%2Cpr-true%2Ce-contrast-1%3Art-90r   r   r   r   r   .test_generate_url_with_chained_transformations      z>TestGenerateURL.test_generate_url_with_chained_transformationsc                 C   s0   ddddgdd}| j |}| |d d S )Nz$/default-image.jpg?client=123&user=5r   r   r   rT   r   r   rV   zZhttps://test-domain.com/test-endpoint/default-image.jpg?client=123&user=5&tr=h-300%2Cw-400r   r   r   r   r   .test_url_check_query_param_are_added_correctly   s   
z>TestGenerateURL.test_url_check_query_param_are_added_correctlyc                 C   rY   )NzEhttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?client=123&ab=cr   r   r.   r/   r0   r9   r4   r5   r6   zhttps://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?client=123&ab=c&tr=h-300%2Cw-400%2Cf-jpg%2Cpr-true%2Ce-contrast-1%3Art-90r   r   r   r   r   ;test_generate_url_with_src_query_parameters_merge_correctly   r\   zKTestGenerateURL.test_generate_url_with_src_query_parameters_merge_correctlyc                 C   s<   ddddddddd	igd
d}| j |}| |d d S )Nr-   r   r   r.   r/   r0   r9   r4   r5   r   )r7   r   rV   rZ   r   r   r   r   r   ;test_generate_url_with_src_and_transformation_position_path   s    
zKTestGenerateURL.test_generate_url_with_src_and_transformation_position_pathc                 C   s2   dddddgddd}|  ttf| jj| d S )	Nr   r;   r   r   r   Tfake)r   r   r   r<   rV   )assertRaisesKeyError
ValueErrorr   r   r   r    r   r   r   test_url_with_invalid_trans_pos  s   
z/TestGenerateURL.test_url_with_invalid_trans_posc                 C   s,   ddddgdd}|  | j|d d S )Nr;   r   r   r   T)r   r   r<   r=   )r   r   r   re   r   r   r   test_url_without_path_and_src  s
   
z-TestGenerateURL.test_url_without_path_and_srcc                 C   sj   ddddddddd	d
igdd}| j |}| |dd d d | |dd dd  d d S )Nr   r   r   r.   r/   -r0   )r   r   r1   r2   effect_sharpenr3   r4   r5   r   r]   ztr:h-300r   /r>   rA   rB   )r   r   r   rF   rG   r   r   r   r   :test_url_contains_slash_if_transformation_position_is_path'  s   "zJTestGenerateURL.test_url_contains_slash_if_transformation_position_is_pathc                 C   s0   dddigddd}| j |}| d| d S )Nr   r   r   TrK   )r   r   r<   expire_secondszik-t)r   r   rD   r   r   r   r   &test_url_signed_with_expire_in_seconds;  s   
z6TestGenerateURL.test_url_signed_with_expire_in_secondsc                 C   T   d}t |}| |d t d|dd}ddd}| j|}| |d	|  d S )
Nu:   https://test-domain.com/test-endpoint/test_é_path_alt.jpgz>https://test-domain.com/test-endpoint/test_%C3%A9_path_alt.jpgr   r	      c(	 u   /test_é_path_alt.jpgTr   r<   u@   https://test-domain.com/test-endpoint/test_é_path_alt.jpg?ik-s=r   encode_string_if_requiredr   get_signaturer   r   r   r   
encodedUrl	signaturer    r   r   r   *test_url_signed_with_diacritic_in_filenameI     
z:TestGenerateURL.test_url_signed_with_diacritic_in_filenamec                 C   ro   )
Nu?   https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpgzGhttps://test-domain.com/test-endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpgr   r	   rp      /aéb/test_é_path_alt.jpgTrq   uE   https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?ik-s=rr   ru   r   r   r   3test_url_signed_with_diacritic_in_filename_and_path[  ry   zCTestGenerateURL.test_url_signed_with_diacritic_in_filename_and_pathc                 C   ^   d}t |}| |d t d|dd}dddigd	d
d}| j|}| |d|  d S )Nub   https://test-domain.com/test-endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpgznhttps://test-domain.com/test-endpoint/tr:l-text,i-Imagekit%C3%A9,fs-50,l-end/a%C3%A9b/test_%C3%A9_path_alt.jpgr   r	   rp   rz   r$      l-text,i-Imagekité,fs-50,l-endTr   r   r   r<   rV   uh   https://test-domain.com/test-endpoint/tr:l-text,i-Imagekité,fs-50,l-end/aéb/test_é_path_alt.jpg?ik-s=rr   ru   r   r   r   Ftest_url_signed_with_diacritic_in_filename_path_transforamtion_in_pathm  &   

zVTestGenerateURL.test_url_signed_with_diacritic_in_filename_path_transforamtion_in_pathc                 C   r|   )Nul   https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-endzthttps://test-domain.com/test-endpoint/a%C3%A9b/test_%C3%A9_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-endr   r	   rp   rz   r$   r}   TrT   r~   ur   https://test-domain.com/test-endpoint/aéb/test_é_path_alt.jpg?tr=l-text%2Ci-Imagekit%C3%A9%2Cfs-50%2Cl-end&ik-s=rr   ru   r   r   r   Gtest_url_signed_with_diacritic_in_filename_path_transforamtion_in_query  r   zWTestGenerateURL.test_url_signed_with_diacritic_in_filename_path_transforamtion_in_queryc                 C   r&   )	zd
        In case when both path and src fields are provided, the `path` should be preferred
        r   r-   r   r   r   r   r7   r   r   Nr   r   r   r   r   -test_generate_url_with_path_and_src_uses_path  s   
z=TestGenerateURL.test_generate_url_with_path_and_src_uses_pathc                 C   s   ddi ddddddd	d
dddddddddddddddddddddd d!d d"d#d d d$d%dd&d d d'd(d)d*d+gd,}| j |}| |d- d.S )/zH
        In case where all transformation parameters are passed
        z/test_path.jpgr-   r   i,  r   i  aspect_ratioz4-3quality(   cropforce	crop_modeextractfocusleftr1   jpegradius2   bgA94D34borderz5-A94D34r4   r5   blur
   named	some_namer2   r/   losslesstrim   zfolder/file.jpg/   z2-2-0.8-0.024zbl-15_st-40_x-10_y-N5zfrom-red_to-whiteTzw-200,h-200)metadatacolor_profiledefault_imagedprri   
effect_usmr3   effect_grayeffect_shadoweffect_gradientoriginalr$   r   at  https://test-domain.com/test-endpoint/tr:h-300,w-400,ar-4-3,q-40,c-force,cm-extract,fo-left,f-jpeg,r-50,bg-A94D34,b-5-A94D34,rt-90,bl-10,n-some_name,pr-true,lo-true,t-5,md-true,cp-true,di-folder@@file.jpg,dpr-3,e-sharpen-10,e-usm-2-2-0.8-0.024,e-contrast-true,e-grayscale-true,e-shadow-bl-15_st-40_x-10_y-N5,e-gradient-from-red_to-white,orig-true,w-200,h-200/test_path.jpgNr   r   r   r   r   !test_generate_url_with_all_params  sr   	
%z1TestGenerateURL.test_generate_url_with_all_paramsc                 C   (   d}| j jd|dd}| |d d S )NBhttps://test-domain.com/test-endpoint/tr:w-100/test-signed-url.pngr   &https://test-domain.com/test-endpoint/rK   (5e5037a31a7121cbe2964e220b4338cc6e1ba66dr   url_objrt   r   r   r   rw   r   r   r   *test_get_signature_with_100_expire_seconds  
   z:TestGenerateURL.test_get_signature_with_100_expire_secondsc                 C   r   )Nr   r   r   r   (41b3075c40bc84147eb71b8b49ae7fbf349d0f00r   r   r   r   r   )test_get_signature_without_expire_seconds  r   z9TestGenerateURL.test_get_signature_without_expire_secondsc                 C   r   )Nr   r   r	   r   r   r   r   r   r   r   7test_get_signature_without_expire_seconds_without_slash  r   zGTestGenerateURL.test_get_signature_without_expire_seconds_without_slashc                 C   s(   dddd}| j |}| |d d S )Nz/coffee.jpgFr   )r   r<   rm   0https://test-domain.com/test-endpoint/coffee.jpgr   r   r   r   r   $test_generate_url_without_transforms  s   z4TestGenerateURL.test_generate_url_without_transformsc                 C   s(   dddd}| j |}| |d d S )Nr   Fr   )r7   r<   rm   r   r   r   r   r   (test_generate_url_without_transforms_src  s   z8TestGenerateURL.test_generate_url_without_transforms_src)r   N)#__name__
__module____qualname__r   r!   r#   r%   r'   r,   r8   r:   rH   rO   rR   rW   rX   r[   r^   r_   r`   rf   rg   rl   rn   rx   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sB    


1r   )	unittestimagekitio.clientr   imagekitio.constants.defaultsr   imagekitio.urlr   TestCaser   r   r   r   r   <module>   s
    