o
    B_j%                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ee	
 jd Zejdee ejdZedurAeedsAejd= dd	lmZmZmZmZ dd
lmZmZ ddlmZ G dd dejZe dkrpej!dd dS dS )z+Standalone Platform App Standard v1 checks.    )annotationsN)Path)patch   platform__path__)ActionContractAppManifestCredentialFieldWordPressPackageContract)error_envelopesuccess_envelope)resolve_capabilityc                   @  sh   e Zd Ze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S )AppContractChecksc                   C  s   t ddddgddS )Nplatform.healthzReport local health.objecthealthytyperequired
capabilitydescriptionoutput_schema)r    r   r   @/var/www/html/flask_server/tests/platform/app_contract_checks.py_health_action   s
   
z AppContractChecks._health_actionc                 C  s^   t ddddtddftddd	d
gddtdddd	ddgdddd}| g |  d S )N
caller_appCaller1.0.0zTest caller application.	api_tokenz	API tokenr   Healthr   r   r   r   zplatform.credentials.testzConnection test)r    	connectedproviderr   r   required_credentialsr   )	health_v1test_connection_v1)app_iddisplay_nameapp_versionr   credentialsactions)r	   r
   r   assertEqualvalidateselfmanifestr   r   r   ,test_valid_manifest_and_credential_reference#   s    

z>AppContractChecks.test_valid_manifest_and_credential_referencec                 C  s^   t ddddtddddd	gd
dtddddgd
ddd}| tdd | D  d S )Nbad_appBadr   z"Invalid credential reference test.records.lookupLookup)missingr   recordr   r$   r   r!   r   r   )	lookup_v1r&   r(   r)   r*   r   r,   c                 s      | ]}d |v V  qdS )zunknown credentialsNr   .0errorr   r   r   	<genexpr>@       zHAppContractChecks.test_unknown_credential_is_rejected.<locals>.<genexpr>)r	   r   
assertTrueanyr.   r/   r   r   r   #test_unknown_credential_is_rejected3   s   

 z5AppContractChecks.test_unknown_credential_is_rejectedc                 C  sN  t ddddddtddd	d
gddid}t ddddtddd	d
gddtddd	dgdddd}td||gd | dtddd W d    n1 sPw   Y  t dddddtddd	d
gddid}td||gd, | t tddd W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   r   r   zCapability caller test.)ztarget_app:records.lookupr&   r   r!   r   r   r   r   )r(   r)   r*   r   outbound_capabilitiesr,   
target_appTargetzCapability target test.r5   r6   r8   r&   r9   r:   z$platform.apps.registry.load_manifest)side_effectr9   zDenied capability caller test.)r	   r   r   r-   r   assertRaisesPermissionError)r0   callertargetdeniedr   r   r   5test_capability_requires_explicit_outbound_permissionB   sV   
	



"zGAppContractChecks.test_capability_requires_explicit_outbound_permissionc                 C  sV   t dddddid}tdddddd	}| |d
  | |d  | d|d  d S )Napp_alookupz	request-1id   )r(   action
request_iddatarecord_not_foundzRecord not found)r(   rS   rT   codemessager>   rU   z1.0contract_version)r   r   assertIsNoner-   )r0   successfailurer   r   r   'test_envelopes_never_mix_data_and_errork   s   z9AppContractChecks.test_envelopes_never_mix_data_and_errorc                 C  sZ   t dddddtdddd	gd
ditdddddddd}| tdd | D  d S )Npack_appPackr   z Invalid package capability test.r&   r   r!   r   r   r   r   pack_app_wpwordpress-pack/packpack.phppack/pack.php)r5   
package_idversionsource_directory
entrypointplugin_filecapabilitiesr(   r)   r*   r   r,   wordpress_packagec                 s  r;   )zunknown capabilitiesNr   r<   r   r   r   r?      r@   zYAppContractChecks.test_wordpress_pack_cannot_use_undeclared_capability.<locals>.<genexpr>r	   r   r   rA   rB   r.   r/   r   r   r   4test_wordpress_pack_cannot_use_undeclared_capabilityw   s   
 zFAppContractChecks.test_wordpress_pack_cannot_use_undeclared_capabilityc                 C  s\   t dddddtdddd	gd
ditddddddddd}| tdd | D  d S )Nr^   r_   r   zPartial package metadata test.r&   r   r!   r   r   r   r   r`   ra   rb   rc   r   zpack.zip)re   rf   rg   rh   ri   rj   artifact_namerk   c                 s  r;   )zrelease metadataNr   r<   r   r   r   r?      r@   zNAppContractChecks.test_partial_release_metadata_is_rejected.<locals>.<genexpr>rm   r/   r   r   r   )test_partial_release_metadata_is_rejected   s   
 z;AppContractChecks.test_partial_release_metadata_is_rejectedc                 C  sV   t ddddd|  id}| }| tdd |D  | td	d |D  d S )
Nr3   zBad versiononezInvalid versioning test.healthr:   c                 s  r;   )zsemantic versioningNr   r<   r   r   r   r?      r@   zSAppContractChecks.test_semver_and_versioned_actions_are_required.<locals>.<genexpr>c                 s  r;   )zcontract suffixNr   r<   r   r   r   r?      r@   )r	   r   r.   rA   rB   r0   r1   errorsr   r   r   .test_semver_and_versioned_actions_are_required   s   
z@AppContractChecks.test_semver_and_versioned_actions_are_requiredc                 C  sh   t dddd|  tddddid	d
dd}| }| tdd |D  | tdd |D  d S )Nr3   z
Bad schemar   zInvalid schema and error test.r5   r6   r   array)zBad-Code)r   r   r   ru   rG   r:   c                 s  r;   )r   Nr   r<   r   r   r   r?      r@   zUAppContractChecks.test_output_schema_and_error_codes_are_validated.<locals>.<genexpr>c                 s  r;   )zinvalid error codeNr   r<   r   r   r   r?      r@   )r	   r   r   r.   rA   rB   rt   r   r   r   0test_output_schema_and_error_codes_are_validated   s"   zBAppContractChecks.test_output_schema_and_error_codes_are_validatedc                 C  sR   t dddd|  tddddgd	d
dddd}| tdd | D  d S )Nr3   zBad side effectr   zInvalid idempotency test.zrecords.createCreater   	record_idr   TF)r   r   r   side_effects
idempotent)r&   	create_v1r:   c                 s  r;   )zidempotency keyNr   r<   r   r   r   r?      r@   zQAppContractChecks.test_non_idempotent_side_effect_requires_key.<locals>.<genexpr>)r	   r   r   rA   rB   r.   r/   r   r   r   ,test_non_idempotent_side_effect_requires_key   s$   
z>AppContractChecks.test_non_idempotent_side_effect_requires_keyc                 C  sL   t ddddd|  itddddd	d
dd}| tdd | D  d S )Nr^   r_   r   z'Invalid WordPress plugin identity test.r&   r`   ra   rb   zanother/pack.phpro   rd   rk   c                 s  r;   )ri   Nr   r<   r   r   r   r?      r@   zWAppContractChecks.test_wordpress_plugin_file_must_match_package_root.<locals>.<genexpr>)r	   r   r   rA   rB   r.   r/   r   r   r   2test_wordpress_plugin_file_must_match_package_root   s$   
zDAppContractChecks.test_wordpress_plugin_file_must_match_package_rootN)__name__
__module____qualname__staticmethodr   r2   rC   rN   r]   rn   rq   rv   rx   r~   r   r   r   r   r   r      s    
)r   __main__)	verbosity)"__doc__
__future__r   sysunittestpathlibr   unittest.mockr   __file__resolveparentsPROJECT_ROOTpathinsertstrmodulesgetloaded_platformhasattrplatform.apps.contractsr   r	   r
   r   platform.apps.envelopesr   r   platform.apps.registryr   TestCaser   r   mainr   r   r   r   <module>   s&     G