o
    h)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%   c/var/www/html/ndineBlogger/venv/lib/python3.10/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>(   s    z5TestDocumentQuerySet.test_operators.<locals>.<lambda>)key)r   r   r   r   r   r    r!   sorted)r"   aaa_documentzzz_documentr$   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)order_by_relevancez-title)r   r   r   r   order_byr   r    r!   )r"   r2   r3   r$   r%   r%   r&   test_custom_ordering+   s   z)TestDocumentQuerySet.test_custom_orderingN)__name__
__module____qualname__fixturesr'   r5   r8   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.comEditors)nameadministratorzadministrator@email.comzDocument addersadd_document)codename)group
collection
permissionr   )r   uploaded_by_user)create_userr>   rB   rC   groupsaddr   r   getcreate_superuserrF   r   adders_groupr   r   get_first_root_noder   r   r   r#   r"   r%   r%   r&   setUp:   s2   zTestDocumentPermissions.setUpc                 C      |  | j| j d S r-   )
assertTruer#   is_editable_by_userrF   rT   r%   r%   r&   test_administrator_can_editZ      z3TestDocumentPermissions.test_administrator_can_editc                 C   rV   r-   )rW   r#   rX   rC   rT   r%   r%   r&   test_editor_can_edit]   rZ   z,TestDocumentPermissions.test_editor_can_editc                 C   rV   r-   )rW   r#   rX   rB   rT   r%   r%   r&   test_owner_can_edit`   rZ   z+TestDocumentPermissions.test_owner_can_editc                 C   rV   r-   )assertFalser#   rX   r>   rT   r%   r%   r&   test_user_cant_editc   rZ   z+TestDocumentPermissions.test_user_cant_editN)r9   r:   r;   rU   rY   r[   r\   r^   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_documentrT   r%   r%   r&   rU   h   s   z$TestDocumentFilenameProperties.setUpc                 C   4   |  d| jj |  d| jj |  d| jj d S )Nr`   ra   rb   )r    r#   filenamere   rf   rT   r%   r%   r&   test_filename{      z,TestDocumentFilenameProperties.test_filenamec                 C   rg   )Nr.   pdf )r    r#   file_extensionre   rf   rT   r%   r%   r&   test_file_extension   rj   z2TestDocumentFilenameProperties.test_file_extensionc                 C   rg   )Nzapplication/mswordzapplication/pdfzapplication/octet-stream)r    r#   content_typere   rf   rT   r%   r%   r&   test_content_type   s
   z0TestDocumentFilenameProperties.test_content_typec                 C   s:   |  | j d |  | j d |  | j d d S )N(7d8c4778b182e4f3bd442408c64a6e22a4b0ed85)r    r#   get_file_hashre   rf   rT   r%   r%   r&   test_file_hash   s   
z-TestDocumentFilenameProperties.test_file_hashc                 C   rg   )NzFattachment; filename=sample_name.doc; filename*=UTF-8''sample_name.docinlinez>attachment; filename=sample_name; filename*=UTF-8''sample_name)r    r#   content_dispositionre   rf   rT   r%   r%   r&   test_content_disposition   s   z7TestDocumentFilenameProperties.test_content_dispositionc                 C   s(   | j j  | jj  | jj  d S r-   )r#   rc   deletere   rf   rT   r%   r%   r&   tearDown   s   z'TestDocumentFilenameProperties.tearDownN)
r9   r:   r;   rU   ri   rn   rp   rs   rv   rx   r%   r%   r%   r&   r_   g   s    r_   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 s5w   Y  | |jj	
| d S )Nz
Test Imager   rc   )r   atomicr   r   r   r   rc   rE   rW   storageexistsrw   r]   )r"   r#   rh   r%   r%   r&   #test_document_file_deleted_oncommit   s   
	zDTestFilesDeletedForDefaultModels.test_document_file_deleted_oncommitN)r9   r:   r;   __doc__r<   r~   r%   r%   r%   r&   ry      s    ry   rk   )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.docrz   ztest.pdf)r   r   r   r   document_invaliddocument_validrT   r%   r%   r&   rU      s   z$TestDocumentValidateExtensions.setUpc                 C   sT   |  t}| j  W d   n1 sw   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.rc   r   )assertRaisesr   r   
full_cleanr    	exceptionmessage_dict)r"   eexpected_messager%   r%   r&   !test_create_doc_invalid_extension   s   	z@TestDocumentValidateExtensions.test_create_doc_invalid_extensionc                 C   s0   z| j   W dS  ty   | d Y dS w )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)r   r   r   failrT   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-   )r   rc   rw   r   rT   r%   r%   r&   rx      s   z'TestDocumentValidateExtensions.tearDownN)r9   r:   r;   rU   r   r   rx   r%   r%   r%   r&   r      s
    r   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 )NRoot0001   r   )rE   pathdepthnumchild)r   r   get_or_creater   register_signal_handlersrT   r%   r%   r&   rU      s   
z%TestFilesDeletedForCustomModels.setUpc                 C   s&   t  }| |jj d|j d d S )N.r   )r   r    _meta	app_labelr9   )r"   clsr%   r%   r&   test_document_model	  s   z3TestFilesDeletedForCustomModels.test_document_modelN)r9   r:   r;   rU   r   r%   r%   r%   r&   r      s    r   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 )TestGetDocumentModelr   r   c                 C   s   |  t t dS )z4Test get_document_model with a custom document modelN)assertIsr   r   rT   r%   r%   r&   test_custom_get_document_model     z3TestGetDocumentModel.test_custom_get_document_modelc                 C   s   |  ttj d S r-   )r    r   r   r   rT   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 modelr   N)r    r   rT   r%   r%   r&   %test_custom_get_document_model_string  r   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   r   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   r   r    r   rT   r%   r%   r&   'test_standard_get_document_model_string&  s   z<TestGetDocumentModel.test_standard_get_document_model_stringztests.UnknownModelc                 C   6   |  t t  W d   dS 1 sw   Y  dS )z-Test get_document_model with an unknown modelNr   r   r   rT   r%   r%   r&   test_unknown_get_document_model,     "z4TestGetDocumentModel.test_unknown_get_document_modelzinvalid-stringc                 C   r   )z4Test get_document_model with an invalid model stringNr   rT   r%   r%   r&   test_invalid_get_document_model2  r   z4TestGetDocumentModel.test_invalid_get_document_modelN)r9   r:   r;   r   r   r   r   r   r   r   r   r%   r%   r%   r&   r     s    




r   N)'django.confr   django.contrib.auth.modelsr   r   django.core.exceptionsr   r   django.core.files.baser   	django.dbr   django.testr	   r
   django.test.utilsr   wagtail.documentsr   r   r   r   wagtail.images.tests.utilsr   wagtail.modelsr   r   wagtail.test.testapp.modelsr   r   wagtail.test.utilsr   r   r=   r_   ry   r   r   r   r%   r%   r%   r&   <module>   s*    $.E
,