a
    ]7                     @   s   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 e
dZedZG dd dejZG dd	 d	ejZG d
d dejZG dd dejZG dd dejZedkre  dS )zTests for josepy.json_util.    N)errors
interfaces	test_utilutilzcert.pemzcsr.pemc                   @   sH   e Zd 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 )	FieldTestz!Tests for josepy.json_util.Field.c                 C   sP   ddl m} tddgddgddgD ]$\}}}| |d||d| q&d S )Nr   FieldTFfoo)default	omitempty)josepy.json_utilr   	itertoolsproductZassertFalseZomit)selfr   r
   r   value r   7/usr/lib/python3/dist-packages/josepy/json_util_test.pytest_no_omit_boolean   s    zFieldTest.test_no_omit_booleanc                 C   sz   t  }dd }dd }ddlm} |d}||}| d|| ||}| d|| | d	|| d S )
Nc                 S   s   dS )Ndr   Zunused_valuer   r   r   decoder   s    z+FieldTest.test_descriptors.<locals>.decoderc                 S   s   dS )Ner   r   r   r   r   encoder    s    z+FieldTest.test_descriptors.<locals>.encoderr   r   r	   r   r   )	mock	MagicMockr   r   r   assertEqualencoder   decode)r   
mock_valuer   r   r   fieldr   r   r   test_descriptors   s    

zFieldTest.test_descriptorsc                 C   sN   G dd dt j}| }ddlm} | |||u  | d|| d S )Nc                   @   s    e Zd Zdd Zedd ZdS )z<FieldTest.test_default_encoder_is_partial.<locals>.MockFieldc                 S   s   dS Nr	   r   r   r   r   r   to_partial_json0   s    zLFieldTest.test_default_encoder_is_partial.<locals>.MockField.to_partial_jsonc                 S   s   d S Nr   clsZjobjr   r   r   	from_json3   s    zFFieldTest.test_default_encoder_is_partial.<locals>.MockField.from_jsonN)__name__
__module____qualname__r#   classmethodr'   r   r   r   r   	MockField.   s   r,   r   r   r	   )r   ZJSONDeSerializabler   r   
assertTruedefault_encoderZassertNotEqual)r   r,   Z
mock_fieldr   r   r   r   test_default_encoder_is_partial-   s
    z)FieldTest.test_default_encoder_is_partialc                 C   s,   t  }ddlm} | |||u  d S Nr   r   )r   r   r   r   r-   r.   r   r   r   r   r   r    test_default_encoder_passthrough=   s    z*FieldTest.test_default_encoder_passthroughc                 C   s&   ddl m} | d|g d d S )Nr   r   )         )r   r   r   default_decoder)r   r   r   r   r   "test_default_decoder_list_to_tupleB   s    z,FieldTest.test_default_decoder_list_to_tuplec                 C   sD   ddl m} |ddi}| t|tj | |tjdd d S )Nr   r   xr4   r8   )r   r   r6   r-   
isinstancer   Z
frozendictr   )r   r   objr   r   r   'test_default_decoder_dict_to_frozendictF   s    z1FieldTest.test_default_decoder_dict_to_frozendictc                 C   s,   t  }ddlm} | |||u  d S r0   )r   r   r   r   r-   r6   r1   r   r   r    test_default_decoder_passthroughL   s    z*FieldTest.test_default_decoder_passthroughN)r(   r)   r*   __doc__r   r    r/   r2   r7   r<   r=   r   r   r   r   r      s   r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )JSONObjectWithFieldsMetaTestz4Tests for josepy.json_util.JSONObjectWithFieldsMeta.c                    s   ddl m} ddl m} |d _|d _t|G  fdddt}G dd	 d	|}G  fd
dd|}| _| _	| _
d S )Nr   r   )JSONObjectWithFieldsMetaZBazZBaz2c                       s   e Zd ZdZ jZdS )z-JSONObjectWithFieldsMetaTest.setUp.<locals>.AbarN)r(   r)   r*   	__slots__r   bazr   r"   r   r   A]   s   rE   c                   @   s   e Zd ZdS )z-JSONObjectWithFieldsMetaTest.setUp.<locals>.BN)r(   r)   r*   r   r   r   r   Bb   s   rF   c                       s   e Zd Z jZdS )z-JSONObjectWithFieldsMetaTest.setUp.<locals>.CN)r(   r)   r*   field2rD   r   r"   r   r   Ce   s   rH   )r   r   r@   r   rG   sixZadd_metaclassobjecta_clsb_clsc_cls)r   r   r@   rE   rF   rH   r   r"   r   setUpU   s    

z"JSONObjectWithFieldsMetaTest.setUpc                 C   s0   |  d| ji| jj |  d| ji| jj d S NrD   )r   r   rK   _fieldsrL   r"   r   r   r   test_fieldsl   s    z(JSONObjectWithFieldsMetaTest.test_fieldsc                 C   s   |  d| ji| jj d S rO   )r   rG   rM   rP   r"   r   r   r   test_fields_inheritanceq   s    z4JSONObjectWithFieldsMetaTest.test_fields_inheritancec                 C   s$   |  d| jj |  d| jj d S )N)rB   rD   )rD   )r   rK   rC   rL   r"   r   r   r   
test_slotsu   s    z'JSONObjectWithFieldsMetaTest.test_slotsc                 C   s$   |  d| jj |  d| jj d S )NrA   r   )r   rK   Z_orig_slotsrL   r"   r   r   r   test_orig_slotsy   s    z,JSONObjectWithFieldsMetaTest.test_orig_slotsN)	r(   r)   r*   r>   rN   rQ   rR   rS   rT   r   r   r   r   r?   R   s   r?   c                   @   sp   e Zd 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d Zdd Zdd Zdd Zdd ZdS )JSONObjectWithFieldsTestz0Tests for josepy.json_util.JSONObjectWithFields.c                    sF   ddl m} ddl m  G  fddd|}|| _|d ddd| _d S )	Nr   )JSONObjectWithFieldsr   c                       sP   e Zd Z dddd dd dZ dZ dZejd	d
 Zejdd
 ZdS )z@JSONObjectWithFieldsTest.setUp.<locals>.MockJSONObjectWithFieldsr8   Tc                 S   s   | d S Nr4   r   r9   r   r   r   <lambda>       zIJSONObjectWithFieldsTest.setUp.<locals>.MockJSONObjectWithFields.<lambda>c                 S   s   | d S rW   r   r9   r   r   r   rX      rY   )r   r   r   yZc                 S   s   | dkrt  | S N  )r   SerializationErrorr   r   r   r   rZ      s    zBJSONObjectWithFieldsTest.setUp.<locals>.MockJSONObjectWithFields.yc                 S   s   | dkrt  | S r\   )r   DeserializationErrorr_   r   r   r   rZ      s    N)r(   r)   r*   r8   rZ   zr   r   r   r   r   r   MockJSONObjectWithFields   s   
rb   r4   r5   r8   rZ   ra   )r   rV   r   rb   r   )r   rV   rb   r   r   r   rN      s
    zJSONObjectWithFieldsTest.setUpc                 C   s   |  | j| jddd d S )Nr4   r5   rZ   ra   )r   r   rb   r"   r   r   r   test_init_defaults   s    z+JSONObjectWithFieldsTest.test_init_defaultsc                 C   s"   |  d| jddddd d S )N
      r   rc   r8   )r   rb   r   r"   r   r   r   test_encode   s
    
z$JSONObjectWithFieldsTest.test_encodec                 C   s   |  tj| jjd d S r!   )assertRaisesr   Errorr   r   r"   r   r   r   test_encode_wrong_field   s    z0JSONObjectWithFieldsTest.test_encode_wrong_fieldc                 C   s    |  tj| jdd djd d S )Nr]   rd   rZ   )ri   r   r^   rb   r   r"   r   r   r   +test_encode_serialization_error_passthrough   s    zDJSONObjectWithFieldsTest.test_encode_serialization_error_passthroughc                 C   s   |  | j ddd d S )Nr4   r5   rZ   r[   )r   r   fields_to_partial_jsonr"   r   r   r   'test_fields_to_partial_json_omits_empty   s    z@JSONObjectWithFieldsTest.test_fields_to_partial_json_omits_emptyc                 C   s&   |  d ddd| jddd d S )Nr4   r5   rc   rm   r   rb   fields_from_jsonr"   r   r   r   -test_fields_from_json_fills_default_for_empty   s    
zFJSONObjectWithFieldsTest.test_fields_from_json_fills_default_for_emptyc                 C   sh   |  tj| jjddi |  tj| jjddi |  tj| jjddd |  tj| jjddd d S )NrZ   r   r[   )r8   rZ   )r8   r[   )ri   r   r`   rb   rq   r"   r   r   r   &test_fields_from_json_fails_on_missing   s     z?JSONObjectWithFieldsTest.test_fields_from_json_fails_on_missingc                 C   s(   |  | jdddd dddd d S )Nr3   r4   r5   rc   r8   rZ   r[   )r   rb   r#   r"   r   r   r   #test_fields_to_partial_json_encoder   s    
z<JSONObjectWithFieldsTest.test_fields_to_partial_json_encoderc              	   C   s(   |  dddd| jdddd d S )Nr4   r5   rc      rt   rp   r"   r   r   r   test_fields_from_json_decoder   s    

z6JSONObjectWithFieldsTest.test_fields_from_json_decoderc                 C   s    |  tj| jddddj d S )Nr3   r]   r5   rc   )ri   r   r^   rb   r#   r"   r   r   r   -test_fields_to_partial_json_error_passthrough   s
    zFJSONObjectWithFieldsTest.test_fields_to_partial_json_error_passthroughc                 C   s    |  tj| jjdddd d S )Nrv   r]   r5   rt   )ri   r   r`   rb   r'   r"   r   r   r   'test_fields_from_json_error_passthrough   s
    
z@JSONObjectWithFieldsTest.test_fields_from_json_error_passthroughN)r(   r)   r*   r>   rN   re   rh   rk   rl   ro   rr   rs   ru   rw   rx   ry   r   r   r   r   rU      s   rU   c                   @   s|   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d Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )DeEncodersTestc                 C   s   d| _ d| _d S )Na  MIIB3jCCAYigAwIBAgICBTkwDQYJKoZIhvcNAQELBQAwdzELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE1pY2hpZ2FuMRIwEAYDVQQHDAlBbm4gQXJib3IxKzApBgNVBAoMIlVuaXZlcnNpdHkgb2YgTWljaGlnYW4gYW5kIHRoZSBFRkYxFDASBgNVBAMMC2V4YW1wbGUuY29tMB4XDTE0MTIxMTIyMzQ0NVoXDTE0MTIxODIyMzQ0NVowdzELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE1pY2hpZ2FuMRIwEAYDVQQHDAlBbm4gQXJib3IxKzApBgNVBAoMIlVuaXZlcnNpdHkgb2YgTWljaGlnYW4gYW5kIHRoZSBFRkYxFDASBgNVBAMMC2V4YW1wbGUuY29tMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKx1c7RR7R_drnBSQ_zfx1vQLHUbFLh1AQQQ5R8DZUXd36efNK79vukFhN9HFoHZiUvOjm0c-pVE6K-EdE_twuUCAwEAATANBgkqhkiG9w0BAQsFAANBAC24z0IdwIVKSlntksllvr6zJepBH5fMndfk3XJp10jT6VE-14KNtjh02a56GoraAvJAT5_H67E8GvJ_ocNnB_oa  MIIBXTCCAQcCAQAweTELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE1pY2hpZ2FuMRIwEAYDVQQHDAlBbm4gQXJib3IxDDAKBgNVBAoMA0VGRjEfMB0GA1UECwwWVW5pdmVyc2l0eSBvZiBNaWNoaWdhbjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wXDANBgkqhkiG9w0BAQEFAANLADBIAkEArHVztFHtH92ucFJD_N_HW9AsdRsUuHUBBBDlHwNlRd3fp580rv2-6QWE30cWgdmJS86ObRz6lUTor4R0T-3C5QIDAQABoCkwJwYJKoZIhvcNAQkOMRowGDAWBgNVHREEDzANggtleGFtcGxlLmNvbTANBgkqhkiG9w0BAQsFAANBAHJH_O6BtC9aGzEVCMGOZ7z9iIRHWSzr9x_bOzn7hLwsbXPAgO1QxEwL-X-4g20Gn9XBE1N9W6HCIEut2d8wACg)b64_certb64_csrr"   r   r   r   rN      s    zDeEncodersTest.setUpc                 C   s6   ddl m} |d}| t|tj | d| d S )Nr   )encode_b64jose   xeA)r   r}   r-   r:   rI   string_typesr   )r   r}   encodedr   r   r   test_encode_b64jose   s    z"DeEncodersTest.test_encode_b64josec                 C   s6   ddl m} |d}| t|tj | d| d S )Nr   decode_b64joser   r~   )r   r   r-   r:   rI   binary_typer   )r   r   decodedr   r   r   test_decode_b64jose   s    z"DeEncodersTest.test_decode_b64josec                 C   s    ddl m} | tj|d d S )Nr   r   r8   )r   r   ri   r   r`   r   r   r   r   r   !test_decode_b64jose_padding_error  s    z0DeEncodersTest.test_decode_b64jose_padding_errorc                 C   sL   ddl m} | d|ddd | jtj|ddd | jtj|ddd d S )	Nr   r      fooZm9vr5   )sizer4   rv   r   r   r   ri   r   r`   r   r   r   r   test_decode_b64jose_size  s    

z'DeEncodersTest.test_decode_b64jose_sizec                 C   sR   ddl m} | d|dddd | d|dddd | jtj|dd	dd d S )
Nr   r   r   r   r5   Tr   Zminimumr4   rv   r   r   r   r   r    test_decode_b64jose_minimum_size  s    
z/DeEncodersTest.test_decode_b64jose_minimum_sizec                 C   s6   ddl m} |d}| d| | t|tj d S )Nr   )encode_hex16r   666f6f)r   r   r   r-   r:   rI   r   )r   r   r   r   r   r   test_encode_hex16  s    z DeEncodersTest.test_encode_hex16c                 C   s6   ddl m} |d}| d| | t|tj d S )Nr   decode_hex16r   r   )r   r   r   r-   r:   rI   r   )r   r   r   r   r   r   test_decode_hex16  s    z DeEncodersTest.test_decode_hex16c                 C   sR   ddl m} | d|dddd | d|dddd | jtj|dd	dd d S )
Nr   r   r   r   r5   Tr   r4   rv   )r   r   r   ri   r   r`   r   r   r   r   r   test_decode_hex16_minimum_size!  s    
z-DeEncodersTest.test_decode_hex16_minimum_sizec                 C   s    ddl m} | tj|d d S )Nr   r   r8   )r   r   ri   r   r`   r   r   r   r   test_decode_hex16_odd_length(  s    z+DeEncodersTest.test_decode_hex16_odd_lengthc                 C   s"   ddl m} | | j|t d S )Nr   )encode_cert)r   r   r   r{   CERT)r   r   r   r   r   test_encode_cert,  s    zDeEncodersTest.test_encode_certc                 C   sH   ddl m} || j}| t|tj | |t | 	t
j|d d S )Nr   )decode_cert )r   r   r{   r-   r:   r   ComparableX509r   r   ri   r   r`   )r   r   Zcertr   r   r   test_decode_cert0  s
    
zDeEncodersTest.test_decode_certc                 C   s"   ddl m} | | j|t d S )Nr   )
encode_csr)r   r   r   r|   CSR)r   r   r   r   r   test_encode_csr7  s    zDeEncodersTest.test_encode_csrc                 C   sH   ddl m} || j}| t|tj | |t | 	t
j|d d S )Nr   )
decode_csrr   )r   r   r|   r-   r:   r   r   r   r   ri   r   r`   )r   r   Zcsrr   r   r   test_decode_csr;  s
    
zDeEncodersTest.test_decode_csrN)r(   r)   r*   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz      s   rz   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TypedJSONObjectWithFieldsTestc                 C   sH   ddl m} G dd d|}|jG dd d|}|| _|dd| _d S )	Nr   )TypedJSONObjectWithFieldsc                   @   s   e Zd Zi ZdZdS )zPTypedJSONObjectWithFieldsTest.setUp.<locals>.MockParentTypedJSONObjectWithFieldstypeN)r(   r)   r*   ZTYPESZtype_field_namer   r   r   r   #MockParentTypedJSONObjectWithFieldsK  s   r   c                   @   s(   e Zd ZdZdZedd Zdd ZdS )zJTypedJSONObjectWithFieldsTest.setUp.<locals>.MockTypedJSONObjectWithFieldstestr	   c                 S   s   d|d iS r!   r   r%   r   r   r   rq   U  s    z[TypedJSONObjectWithFieldsTest.setUp.<locals>.MockTypedJSONObjectWithFields.fields_from_jsonc                 S   s
   d| j iS r!   r   r"   r   r   r   rn   Y  s    zaTypedJSONObjectWithFieldsTest.setUp.<locals>.MockTypedJSONObjectWithFields.fields_to_partial_jsonN)r(   r)   r*   typrC   r+   rq   rn   r   r   r   r   MockTypedJSONObjectWithFieldsO  s
   
r   rB   r   )r   r   register
parent_clsmsg)r   r   r   r   r   r   r   rN   E  s    
z#TypedJSONObjectWithFieldsTest.setUpc                 C   s   |  | j ddd d S )Nr   rB   r   r	   )r   r   r#   r"   r   r   r   test_to_partial_json_  s    z2TypedJSONObjectWithFieldsTest.test_to_partial_jsonc                 C   s*   g dddfD ]}|  tj| jj| qd S )Nr   rg   Zasdri   r   r`   r   r'   )r   r   r   r   r   test_from_json_non_dict_failse  s    z;TypedJSONObjectWithFieldsTest.test_from_json_non_dict_failsc                 C   s   |  tj| jji  d S r$   r   r"   r   r   r   !test_from_json_dict_no_type_failsj  s    z?TypedJSONObjectWithFieldsTest.test_from_json_dict_no_type_failsc                 C   s   |  tj| jjddi d S )Nr   rB   )ri   r   ZUnrecognizedTypeErrorr   r'   r"   r   r   r   !test_from_json_unknown_type_failsn  s    z?TypedJSONObjectWithFieldsTest.test_from_json_unknown_type_failsc                 C   s"   |  ddi| jddd d S )Nr	   rB   r   r   )r   r   r'   r"   r   r   r   test_from_json_returns_objr  s    z8TypedJSONObjectWithFieldsTest.test_from_json_returns_objN)	r(   r)   r*   rN   r   r   r   r   r   r   r   r   r   r   C  s   r   __main__)r>   r   Zunittestr   rI   Zjosepyr   r   r   r   Zload_comparable_certr   Zload_comparable_csrr   ZTestCaser   r?   rU   rz   r   r(   mainr   r   r   r   <module>   s   

D-]g4