Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b484b79b

Přidáno uživatelem Stanislav Král před téměř 4 roky(ů)

Covered test cases where CertificateService should/shouldn't raise InvalidSubjectAttribute error

Zobrazit rozdíly:

tests/integration_tests/services/certificate_service_test.py
6 6
from src.constants import SSL_ID, CA_ID, AUTHENTICATION_ID, INTERMEDIATE_CA_ID, ROOT_CA_ID, CERTIFICATE_ID, SIGNATURE_ID
7 7
from src.model.subject import Subject
8 8
from src.services.certificate_service import RevocationReasonInvalidException, CertificateStatusInvalidException, \
9
    CertificateNotFoundException, CertificateAlreadyRevokedException, CertificateCannotBeSetToValid
9
    CertificateNotFoundException, CertificateAlreadyRevokedException, CertificateCannotBeSetToValid, \
10
    InvalidSubjectAttribute
10 11

  
11 12

  
12 13
def export_crt(crt):
......
510 511
    assert root_ca_cert.pem_data in pkcs_info
511 512
    assert len(re.findall("BEGIN CERTIFICATE", pkcs_info)) == 1
512 513
    assert len(re.findall("END CERTIFICATE", pkcs_info)) == 1
514

  
515

  
516
@pytest.mark.parametrize(
517
    "cc",
518
    [
519
        pytest.param(None),
520
        pytest.param("CZ")
521
    ],
522
)
523
def test_create_root_ca_valid_cc(certificate_service, sample_private_key, cc):
524
    # test whether create_root_ca does not throw any exception when valid country code is passed
525
    certificate_service.create_root_ca(sample_private_key, Subject(common_name="Foo", country=cc))
526

  
527

  
528
@pytest.mark.parametrize(
529
    "cc",
530
    [
531
        pytest.param(None),
532
        pytest.param("CZ"),
533
    ],
534
)
535
@pytest.mark.parametrize(
536
    "func",
537
    [
538
        "create_ca",
539
        "create_end_cert"
540
    ],
541
)
542
def test_create_certificate_valid_cc(certificate_service, sample_certificate, sample_private_key, cc, func):
543
    # test whether create_ca and create_end_cert do not throw any exception when valid country code is passed
544
    getattr(certificate_service, func)(sample_private_key, Subject(common_name="Foo", country=cc), sample_certificate,
545
                                       sample_private_key)
546

  
547

  
548
@pytest.mark.parametrize(
549
    "cc",
550
    [
551
        pytest.param(""),
552
        pytest.param("C"),
553
        pytest.param("CCZ"),
554
    ],
555
)
556
def test_create_root_ca_invalid_cc_length(certificate_service, private_key_service, cc):
557
    # assert that create_root_ca raises an exception when an invalid subject country code is passed
558
    with pytest.raises(InvalidSubjectAttribute) as e:
559
        certificate_service.create_root_ca(private_key_service.create_new_key(), Subject(common_name="Foo", country=cc))
560
    assert e.value.attribute_name == "Country code"
561
    assert e.value.reason == "Must consist of exactly 2 letters"
562

  
563

  
564
@pytest.mark.parametrize(
565
    "cc",
566
    [
567
        pytest.param(""),
568
        pytest.param("C"),
569
        pytest.param("CCZ"),
570
    ],
571
)
572
@pytest.mark.parametrize(
573
    "func",
574
    [
575
        "create_ca",
576
        "create_end_cert"
577
    ],
578
)
579
def test_create_certificate_invalid_cc_length(certificate_service, sample_certificate, sample_private_key, cc, func):
580
    # assert that create_ca and create_end_cert raise an exception when an invalid subject country code is passed
581
    with pytest.raises(InvalidSubjectAttribute) as e:
582
        getattr(certificate_service, func)(sample_private_key, Subject(common_name="Foo", country=cc),
583
                                           sample_certificate, sample_private_key)
584
    assert e.value.attribute_name == "Country code"
585
    assert e.value.reason == "Must consist of exactly 2 letters"
tests/integration_tests/services/conftest.py
9 9
from src.dao.certificate_repository import CertificateRepository
10 10
from src.dao.private_key_repository import PrivateKeyRepository
11 11
from src.db.init_queries import SCHEMA_SQL, DEFAULT_VALUES_SQL
12
from src.model.subject import Subject
12 13
from src.services.certificate_service import CertificateService
13 14
from src.services.crl_ocsp.crl_ocsp_service import CrlOcspService
14 15
from src.services.cryptography import CryptographyService
......
57 58
    return ConnectionProvider().connect(test_configuration())
58 59

  
59 60

  
61
@pytest.fixture
62
def sample_private_key(private_key_service):
63
    return private_key_service.create_new_key()
64

  
65

  
66
@pytest.fixture
67
def sample_certificate(certificate_service, sample_private_key):
68
    return certificate_service.create_root_ca(sample_private_key, Subject("Foo"))
69

  
70

  
60 71
# scope defaults to "function" which means that the fixture is run once per test (function)
61 72
@pytest.fixture
62 73
def certificate_repository_unique(connection_unique):

Také k dispozici: Unified diff