-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_get_certificate.py
More file actions
89 lines (76 loc) · 2.75 KB
/
Copy pathtest_get_certificate.py
File metadata and controls
89 lines (76 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pytest
from app import _get_certificate_for_host
from conftest import SAMPLE_CERT, SAMPLE_KEY
def test_get_certificate_standard_domain():
"""Test that standard domains return default cert configuration"""
host = "api.gov.bc.ca"
namespace = "test-ns"
cert_snis = []
certs = []
cert, serial_number = _get_certificate_for_host(host, namespace, cert_snis, certs)
assert cert is None
assert serial_number is None
def test_get_certificate_custom_domain_found():
"""Test successful custom domain certificate lookup"""
host = "test.custom.gov.bc.ca"
namespace = "test-ns"
cert_id = "41d14845-669f-4dcd-aff2-926fb32a4b25"
cert_serial_number = "1"
certs = [{
"id": cert_id,
"cert": SAMPLE_CERT,
"created_at": 1731713874,
"tags": ["ns.test-ns"],
"key": SAMPLE_KEY
}]
cert_snis = [{
"name": "test.custom.gov.bc.ca",
"id": "79009c9e-0f4d-40b5-9707-bf2fe9f50502",
"created_at": 1731713874,
"certificate": {
"id": cert_id
},
"tags": ["ns.test-ns"]
}]
cert, returned_serial_number = _get_certificate_for_host(host, namespace, cert_snis, certs)
assert cert is not None
assert returned_serial_number == cert_serial_number
assert cert["snis"] == [host]
def test_get_certificate_custom_domain_sni_not_found():
"""Test custom domain with no matching certificate"""
host = "missing.custom.gov.bc.ca"
namespace = "test-ns"
cert_snis = [{
"name": "other.custom.gov.bc.ca",
"certificate": {
"id": "some-cert-id"
}
}]
certs = []
with pytest.raises(Exception) as exc_info:
_get_certificate_for_host(host, namespace, cert_snis, certs)
assert str(exc_info.value) == f"Certificate not found for host {host}"
def test_get_certificate_missing_cert():
"""Test when SNI references a non-existent certificate"""
host = "test.custom.gov.bc.ca"
namespace = "test-ns"
cert_id = "non-existent-cert"
cert_snis = [{
"name": host,
"certificate": {
"id": cert_id
}
}]
certs = [] # Empty certs list, so cert won't be found
with pytest.raises(Exception) as exc_info:
_get_certificate_for_host(host, namespace, cert_snis, certs)
assert str(exc_info.value) == f"Certificate not found for id {cert_id}"
def test_get_certificate_empty_cert_snis():
"""Test custom domain with empty cert_snis"""
host = "test.custom.gov.bc.ca"
namespace = "test-ns"
cert_snis = []
certs = []
with pytest.raises(Exception) as exc_info:
_get_certificate_for_host(host, namespace, cert_snis, certs)
assert str(exc_info.value) == f"Certificate SNI not found for host {host}"