a
    Dg)1                     @   s>  d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZmZmZ d d	lmZ d d
lmZmZ d dlmZmZ d dlmZ G dd deZ G dd deeZ!G dd deZ"G dd deZ#edgdG dd deZ$eddG dd de#Z%G dd deeZ&dS )     )settings)Group
Permission)ImproperlyConfiguredValidationError)ContentFile)transaction)TestCaseTransactionTestCase)override_settings)get_document_modelget_document_model_stringmodelssignal_handlers)get_test_image_file)
CollectionGroupCollectionPermission)CustomDocumentReimportedDocumentModel)WagtailTestUtilsc                   @   s*   e Zd ZdgZdd Zdd Zdd ZdS )	TestDocumentQuerySettest_empty.jsonc                 C   s4   t jjjdd}t jjd}| t||g d S )NTest documenttitleTest)r   DocumentobjectscreatesearchassertEquallist)selfdocumentresults r%   b/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/documents/tests/test_models.pytest_search_method   s    z'TestDocumentQuerySet.test_search_methodc                 C   sz   t jjjdd}t jjjdd}t jjjddd}| t||g t jjjddd}t|dd	 d
}| |||g d S )NAAA Test documentr   ZZZ Test documentzaaa testand)operatororc                 S   s   | j S Nr   )docr%   r%   r&   <lambda>(       z5TestDocumentQuerySet.test_operators.<locals>.<lambda>)key)r   r   r   r   r   r    r!   sorted)r"   aaa_documentzzz_documentr$   Zsorted_resultsr%   r%   r&   test_operators    s    z#TestDocumentQuerySet.test_operatorsc                 C   s|   t jjjdd}t jjjdd}t jjdjddd}| t|||g t jjdjddd}| t|||g d S )	Nr(   r   r)   r   r   F)Zorder_by_relevancez-title)r   r   r   r   Zorder_byr   r    r!   )r"   r3   r4   r$   r%   r%   r&   test_custom_ordering+   s    z)TestDocumentQuerySet.test_custom_orderingN)__name__
__module____qualname__fixturesr'   r5   r6   r%   r%   r%   r&   r      s   r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestDocumentPermissionsc                 C   s   | j dddd| _| j dddd| _| j dddd| _| jjtjjd	d
 | j	dddd| _
tjjdd
| _tjj| jt tjjddd | jj| j tjjjd| jd| _d S )Nuserzuser@email.compassword)usernameemailr=   ownerzowner@email.comeditorzeditor@email.comZEditors)nameadministratorzadministrator@email.comzDocument addersZadd_document)codename)groupZ
collectionZ
permissionr   )r   Zuploaded_by_user)Zcreate_userr<   r@   rA   groupsaddr   r   getZcreate_superuserrC   r   Zadders_groupr   r   Zget_first_root_noder   r   r   r#   r"   r%   r%   r&   setUp:   s2    zTestDocumentPermissions.setUpc                 C   s   |  | j| j d S r-   )
assertTruer#   is_editable_by_userrC   rI   r%   r%   r&   test_administrator_can_editZ   s    z3TestDocumentPermissions.test_administrator_can_editc                 C   s   |  | j| j d S r-   )rK   r#   rL   rA   rI   r%   r%   r&   test_editor_can_edit]   s    z,TestDocumentPermissions.test_editor_can_editc                 C   s   |  | j| j d S r-   )rK   r#   rL   r@   rI   r%   r%   r&   test_owner_can_edit`   s    z+TestDocumentPermissions.test_owner_can_editc                 C   s   |  | j| j d S r-   )assertFalser#   rL   r<   rI   r%   r%   r&   test_user_cant_editc   s    z+TestDocumentPermissions.test_user_cant_editN)r7   r8   r9   rJ   rM   rN   rO   rQ   r%   r%   r%   r&   r;   9   s
    r;   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )TestDocumentFilenamePropertiesc                 C   sj   t jdd| _| jjdtd t jdd| _| jjdtd t jdd| _| jjdtd d S )Nr   r   sample_name.doczA boring example documentsample_name.pdfsample_name)r   r   r#   filesaver   pdf_documentextensionless_documentrI   r%   r%   r&   rJ   h   s    z$TestDocumentFilenameProperties.setUpc                 C   s4   |  d| jj |  d| jj |  d| jj d S )NrS   rT   rU   )r    r#   filenamerX   rY   rI   r%   r%   r&   test_filename{   s    z,TestDocumentFilenameProperties.test_filenamec                 C   s4   |  d| jj |  d| jj |  d| jj d S )Nr.   pdf )r    r#   Zfile_extensionrX   rY   rI   r%   r%   r&   test_file_extension   s    z2TestDocumentFilenameProperties.test_file_extensionc                 C   s4   |  d| jj |  d| jj |  d| jj d S )Nzapplication/mswordzapplication/pdfzapplication/octet-stream)r    r#   content_typerX   rY   rI   r%   r%   r&   test_content_type   s
    z0TestDocumentFilenameProperties.test_content_typec                 C   s:   |  | j d |  | j d |  | j d d S )NZ(7d8c4778b182e4f3bd442408c64a6e22a4b0ed85)r    r#   Zget_file_hashrX   rY   rI   r%   r%   r&   test_file_hash   s    
z-TestDocumentFilenameProperties.test_file_hashc                 C   s4   |  d| jj |  d| jj |  d| jj d S )NzFattachment; filename=sample_name.doc; filename*=UTF-8''sample_name.docinlinez>attachment; filename=sample_name; filename*=UTF-8''sample_name)r    r#   content_dispositionrX   rY   rI   r%   r%   r&   test_content_disposition   s    z7TestDocumentFilenameProperties.test_content_dispositionc                 C   s(   | j j  | jj  | jj  d S r-   )r#   rV   deleterX   rY   rI   r%   r%   r&   tearDown   s    z'TestDocumentFilenameProperties.tearDownN)
r7   r8   r9   rJ   r[   r^   r`   ra   rd   rf   r%   r%   r%   r&   rR   g   s   rR   c                   @   s   e Zd ZdZdgZdd ZdS ) TestFilesDeletedForDefaultModelsa  
    Because we expect file deletion to only happen once a transaction is
    successfully committed, we must run these tests using TransactionTestCase
    per the following documentation:

        Django's TestCase class wraps each test in a transaction and rolls back that
        transaction after each test, in order to provide test isolation. This means
        that no transaction is ever actually committed, thus your on_commit()
        callbacks will never be run. If you need to test the results of an
        on_commit() callback, use a TransactionTestCase instead.
        https://docs.djangoproject.com/en/1.10/topics/db/transactions/#use-in-tests
    r   c                 C   s   t  \ t jjdt d}|jj}| |jj	
| |  | |jj	
| W d    n1 sj0    Y  | |jj	
| d S )Nz
Test Imager   rV   )r   Zatomicr   r   r   r   rV   rB   rK   Zstorageexistsre   rP   )r"   r#   rZ   r%   r%   r&   #test_document_file_deleted_oncommit   s    
2zDTestFilesDeletedForDefaultModels.test_document_file_deleted_oncommitN)r7   r8   r9   __doc__r:   rj   r%   r%   r%   r&   rg      s   rg   r\   )ZWAGTAILDOCS_EXTENSIONSc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestDocumentValidateExtensionsc                 C   s,   t jjjddd| _t jjjddd| _d S )Nr   ztest.docrh   ztest.pdf)r   r   r   r   document_invaliddocument_validrI   r%   r%   r&   rJ      s    z$TestDocumentValidateExtensions.setUpc                 C   sT   |  t}| j  W d   n1 s*0    Y  d}| |jjd d | dS )a3  
        Checks if the uploaded document has the expected extensions
        mentioned in settings.WAGTAILDOCS_EXTENSIONS

        This is caught in form.error and should be raised during model
        creation when called full_clean. This specific testcase invalid
        file extension is passed
        NuE   File extension “doc” is not allowed. Allowed extensions are: pdf.rV   r   )assertRaisesr   rm   
full_cleanr    	exceptionZmessage_dict)r"   eZexpected_messager%   r%   r&   !test_create_doc_invalid_extension   s
    	(z@TestDocumentValidateExtensions.test_create_doc_invalid_extensionc                 C   s0   z| j   W n ty*   | d Y n0 dS )a7  
        Checks if the uploaded document has the expected extensions
        mentioned in settings.WAGTAILDOCS_EXTENSIONS

        This is caught in form.error and should be raised during
        model creation when called full_clean. In this specific
        testcase invalid file extension is passed.
        z>Validation error is raised even when valid file name is passedN)rn   rp   r   ZfailrI   r%   r%   r&   test_create_doc_valid_extension   s    	z>TestDocumentValidateExtensions.test_create_doc_valid_extensionc                 C   s   | j j  | jj  d S r-   )rm   rV   re   rn   rI   r%   r%   r&   rf      s    z'TestDocumentValidateExtensions.tearDownN)r7   r8   r9   rJ   rs   rt   rf   r%   r%   r%   r&   rl      s   rl   tests.CustomDocumentWAGTAILDOCS_DOCUMENT_MODELc                   @   s   e Zd Zdd Zdd ZdS )TestFilesDeletedForCustomModelsc                 C   s    t jjddddd t  d S )NZRootZ0001   r   )rB   pathdepthZnumchild)r   r   Zget_or_creater   Zregister_signal_handlersrI   r%   r%   r&   rJ      s    
z%TestFilesDeletedForCustomModels.setUpc                 C   s&   t  }| |jj d|j d d S )N.ru   )r   r    Z_metaZ	app_labelr7   )r"   clsr%   r%   r&   test_document_model	  s    z3TestFilesDeletedForCustomModels.test_document_modelN)r7   r8   r9   rJ   r~   r%   r%   r%   r&   rx      s   rx   c                   @   sx   e Zd Zedddd Zdd Zedddd Ze d	d
 Ze dd Zedddd Z	edddd Z
dS )TestGetDocumentModelru   rv   c                 C   s   |  t t dS )z4Test get_document_model with a custom document modelN)assertIsr   r   rI   r%   r%   r&   test_custom_get_document_model  s    z3TestGetDocumentModel.test_custom_get_document_modelc                 C   s   |  ttj d S r-   )r    r   r   r   rI   r%   r%   r&   &test_get_document_model_at_import_time  s    z;TestGetDocumentModel.test_get_document_model_at_import_timec                 C   s   |  t d dS )z;Test get_document_model_string with a custom document modelru   N)r    r   rI   r%   r%   r&   %test_custom_get_document_model_string  s    z:TestGetDocumentModel.test_custom_get_document_model_stringc                 C   s"   t `ddlm} | t | dS )z:Test get_document_model with no WAGTAILDOCS_DOCUMENT_MODELr   )r   N)r   rw   Zwagtail.documents.modelsr   r   r   )r"   r   r%   r%   r&    test_standard_get_document_model  s    z5TestGetDocumentModel.test_standard_get_document_modelc                 C   s   t `| t d dS )zATest get_document_model_string with no WAGTAILDOCS_DOCUMENT_MODELzwagtaildocs.DocumentN)r   rw   r    r   rI   r%   r%   r&   'test_standard_get_document_model_string&  s    z<TestGetDocumentModel.test_standard_get_document_model_stringztests.UnknownModelc                 C   s4   |  t t  W d   n1 s&0    Y  dS )z-Test get_document_model with an unknown modelNro   r   r   rI   r%   r%   r&   test_unknown_get_document_model,  s    z4TestGetDocumentModel.test_unknown_get_document_modelzinvalid-stringc                 C   s4   |  t t  W d   n1 s&0    Y  dS )z4Test get_document_model with an invalid model stringNr   rI   r%   r%   r&   test_invalid_get_document_model2  s    z4TestGetDocumentModel.test_invalid_get_document_modelN)r7   r8   r9   r   r   r   r   r   r   r   r   r%   r%   r%   r&   r     s   




r   N)'Zdjango.confr   Zdjango.contrib.auth.modelsr   r   Zdjango.core.exceptionsr   r   Zdjango.core.files.baser   Z	django.dbr   Zdjango.testr	   r
   Zdjango.test.utilsr   Zwagtail.documentsr   r   r   r   Zwagtail.images.tests.utilsr   Zwagtail.modelsr   r   Zwagtail.test.testapp.modelsr   r   Zwagtail.test.utilsr   r   r;   rR   rg   rl   rx   r   r%   r%   r%   r&   <module>   s(   $.E
,