<reponame>cyralinc/sidecar-certificate-manager
import json

import pytest

from _internal.certificate import Certificate
from _internal.utils import encode_b64_str

class TestCertificate:
    def test_to_json_b64_success(self, valid_cert):
        json_str = valid_cert.to_json_b64()
        json_obj = json.loads(json_str, strict=False)
        assert json_obj[valid_cert._cert_key] == encode_b64_str(valid_cert.full_chain)
        assert json_obj[valid_cert._pkey_key] == encode_b64_str(valid_cert.private_key)

    def test_toAndFrom_json_b64_revertible(self, valid_cert):
        json_str = valid_cert.to_json_b64()
        cert = Certificate.from_json_b64(json_str)
        assert cert.full_chain == valid_cert.full_chain
        assert cert.private_key == valid_cert.private_key
