Ë
    ¹2Úim  ã                   óŒ   — d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ d„ Z G d„ d	e«      Z G d
„ de«      Zd„ Zy)é    N)Úsettings)Úoverride_settings)Úforce_bytes)Úis_download_response)Úencode_basename_asciiÚencode_basename_utf8c                 ó0   — || _         || _        || _        | S )a¼  Mimic ``as_view()``, but returns view instance.

    Use this function to get view instances on which you can run unit tests,
    by testing specific methods.

    This is an early implementation of
    https://code.djangoproject.com/ticket/20456

    ``view``
        A view instance, such as ``TemplateView(template_name='dummy.html')``.
        Initialization arguments are the same you would pass to ``as_view()``.

    ``request``
        A request object, typically built with
        :class:`~django.test.client.RequestFactory`.

    ``args`` and ``kwargs``
        "URLconf" positional and keyword arguments, the same you would pass to
        :func:`~django.core.urlresolvers.reverse`.

    )ÚrequestÚargsÚkwargs)Úviewr
   r   r   s       úO/var/www/html/acx/venv/lib/python3.12/site-packages/django_downloadview/test.pyÚ
setup_viewr      s   € ð, €D„LØ€D„IØ€D„KØ€Kó    c                   ó,   ‡ — e Zd ZdZˆ fd„Zˆ fd„Zˆ xZS )Útemporary_media_roota*  Temporarily override settings.MEDIA_ROOT with a temporary directory.

    The temporary directory is automatically created and destroyed.

    Use this function as a context manager:

    >>> from django_downloadview.test import temporary_media_root
    >>> from django.conf import settings  # NoQA
    >>> global_media_root = settings.MEDIA_ROOT
    >>> with temporary_media_root():
    ...     global_media_root == settings.MEDIA_ROOT
    False
    >>> global_media_root == settings.MEDIA_ROOT
    True

    Or as a decorator:

    >>> @temporary_media_root()
    ... def use_temporary_media_root():
    ...     return settings.MEDIA_ROOT
    >>> tmp_media_root = use_temporary_media_root()
    >>> global_media_root == tmp_media_root
    False
    >>> global_media_root == settings.MEDIA_ROOT
    True

    c                 óp   •— t        j                  «       }|| j                  d<   t        t        | �  «        y)zPCreate a temporary directory and use it to override
        settings.MEDIA_ROOT.Ú
MEDIA_ROOTN)ÚtempfileÚmkdtempÚoptionsÚsuperr   Úenable)ÚselfÚtmp_dirÚ	__class__s     €r   r   ztemporary_media_root.enableE   s/   ø€ ô ×"Ñ"Ó$ˆØ%,ˆ�‰�\Ñ"ÜÔ" DÑ0Õ2r   c                 óp   •— t        j                  t        j                  «       t        t
        | �  «        y)zKRemove directory settings.MEDIA_ROOT then restore original
        setting.N)ÚshutilÚrmtreer   r   r   r   Údisable)r   r   s    €r   r    ztemporary_media_root.disableL   s%   ø€ ô 	�‰”h×)Ñ)Ô*ÜÔ" DÑ1Õ3r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    Ú__classcell__)r   s   @r   r   r   (   s   ø„ ñô83÷4ð 4r   r   c                   ó:   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
y	)
ÚDownloadResponseValidatorz5Utility class to validate DownloadResponse instances.c                 óŠ   — | j                  ||«       |j                  «       D ]  \  }}t        | d|z  «      } ||||«       Œ  y)a
  Assert that ``response`` is a valid DownloadResponse instance.

        Optional ``assertions`` dictionary can be used to check additional
        items:

        * ``basename``: the basename of the file in the response.

        * ``content_type``: the value of "Content-Type" header.

        * ``mime_type``: the MIME type part of "Content-Type" header (without
          charset).

        * ``content``: the contents of the file.

        * ``attachment``: whether the file is returned as attachment or not.

        z	assert_%sN)Úassert_download_responseÚitemsÚgetattr)r   Ú	test_caseÚresponseÚ
assertionsÚkeyÚvalueÚassert_funcs          r   Ú__call__z"DownloadResponseValidator.__call__V   sM   € ð$ 	×%Ñ% i°Ô:Ø$×*Ñ*Ó,ò 	4‰JˆC�Ü! $¨°cÑ(9Ó:ˆKÙ˜	 8¨UÕ3ñ	4r   c                 ó8   — |j                  t        |«      «       y )N)Ú
assertTruer   )r   r,   r-   s      r   r)   z2DownloadResponseValidator.assert_download_responsem   s   € Ø×ÑÔ1°(Ó;Õ<r   c                 óÜ   — t        |«      }t        |«      }d}d}||k(  rd}d|d   v rd}nd}d|d   v rd}|r|j                  d|› d�|d   «       |r|j                  d|› �|d   «       y	y	)
z Implies ``attachement is True``.FTz
filename*=úContent-Dispositionz	filename=z
filename="ú"zfilename*=UTF-8''N)r   r   ÚassertIn)r   r,   r-   r0   Ú
ascii_nameÚ	utf8_nameÚ
check_utf8Úcheck_asciis           r   Úassert_basenamez)DownloadResponseValidator.assert_basenamep   s©   € ä*¨5Ó1ˆ
Ü(¨Ó/ˆ	Øˆ
ØˆØ˜Ò"ØˆKØ˜xÐ(=Ñ>Ñ>Ø!‘
àˆJØ˜hÐ'<Ñ=Ñ=Ø"�ÙØ×ÑØ˜Z˜L¨Ð*ØÐ.Ñ/ôñ Ø×ÑØ# I ;Ð/ØÐ.Ñ/õð r   c                 ó.   — |j                  |d   |«       y ©NzContent-Type)ÚassertEqual©r   r,   r-   r0   s       r   Úassert_content_typez-DownloadResponseValidator.assert_content_type‰   s   € Ø×Ñ˜h ~Ñ6¸Õ>r   c                 óJ   — |j                  |d   j                  |«      «       y r?   )r4   Ú
startswithrA   s       r   Úassert_mime_typez*DownloadResponseValidator.assert_mime_typeŒ   s    € Ø×Ñ˜X nÑ5×@Ñ@ÀÓGÕHr   c                 ó¦   — |j                   D �cg c]  }t        |«      ‘Œ }}|j                  dj                  |«      t        |«      «       yc c}w )z9Assert value equals response's content (byte comparison).r   N)Ústreaming_contentr   r@   Újoin)r   r,   r-   r0   ÚsÚpartss         r   Úassert_contentz(DownloadResponseValidator.assert_content�   sB   € à)1×)CÑ)CÖD A”˜Q•ÐDˆÐDØ×Ñ˜cŸh™h u›o¬{¸5Ó/AÕBùò Es   �Ac                 ón   — |r|j                  d|d   v «       y |j                  d|vxs d|d   v«       y )Nzattachment;r6   )r4   rA   s       r   Úassert_attachmentz+DownloadResponseValidator.assert_attachment”   sH   € ÙØ× Ñ  °(Ð;PÑ2QÐ!QÕRà× Ñ Ø%¨XÐ5ò HØ ¨Ð1FÑ(GÐGõr   N)r!   r"   r#   r$   r2   r)   r=   rB   rE   rK   rM   © r   r   r'   r'   S   s*   „ Ù?ò4ò.=òò2?òIòCó
r   r'   c                 ó*   — t        «       } || |fi |¤ŽS )aå  Make ``test_case`` assert that ``response`` meets ``assertions``.

    Optional ``assertions`` dictionary can be used to check additional items:

    * ``basename``: the basename of the file in the response.

    * ``content_type``: the value of "Content-Type" header.

    * ``mime_type``: the MIME type part of "Content-Type" header (without
      charset).

    * ``content``: the contents of the file.

    * ``attachment``: whether the file is returned as attachment or not.

    )r'   )r,   r-   r.   Ú	validators       r   r)   r)   ž   s   € ô" *Ó+€IÙ�Y Ñ7¨JÑ7Ð7r   )r   r   Údjango.confr   Údjango.test.utilsr   Údjango.utils.encodingr   Údjango_downloadview.middlewaresr   Údjango_downloadview.responser   r   r   r   Úobjectr'   r)   rN   r   r   ú<module>rW      sA   ðÛ Û å  Ý /Ý -å @ß Tòô8(4Ð,ô (4ôVH ô HóV8r   