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"
|
Covered test cases where CertificateService should/shouldn't raise InvalidSubjectAttribute error