moto/tests/test_iam/test_iam_server_certificates.py

97 lines
3.0 KiB
Python
Raw Normal View History

from datetime import datetime
2021-09-22 19:42:42 +00:00
import boto3
import pytest
from botocore.exceptions import ClientError
2024-01-07 12:03:33 +00:00
from moto import mock_aws
2022-08-13 09:49:43 +00:00
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2021-09-22 19:42:42 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-22 19:42:42 +00:00
def test_get_all_server_certs():
conn = boto3.client("iam", region_name="us-east-1")
conn.upload_server_certificate(
ServerCertificateName="certname",
CertificateBody="certbody",
PrivateKey="privatekey",
)
certs = conn.list_server_certificates()["ServerCertificateMetadataList"]
assert len(certs) == 1
2021-09-22 19:42:42 +00:00
cert1 = certs[0]
assert cert1["ServerCertificateName"] == "certname"
assert cert1["Arn"] == f"arn:aws:iam::{ACCOUNT_ID}:server-certificate/certname"
2021-09-22 19:42:42 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-22 19:42:42 +00:00
def test_get_server_cert_doesnt_exist():
conn = boto3.client("iam", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
conn.get_server_certificate(ServerCertificateName="NonExistant")
err = ex.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert (
err["Message"]
== "The Server Certificate with name NonExistant cannot be found."
2021-09-22 19:42:42 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-22 19:42:42 +00:00
def test_get_server_cert():
conn = boto3.client("iam", region_name="us-east-1")
conn.upload_server_certificate(
ServerCertificateName="certname",
CertificateBody="certbody",
PrivateKey="privatekey",
)
cert = conn.get_server_certificate(ServerCertificateName="certname")[
"ServerCertificate"
]
assert cert["CertificateBody"] == "certbody"
assert "CertificateChain" not in cert
assert "Tags" not in cert
2021-09-22 19:42:42 +00:00
metadata = cert["ServerCertificateMetadata"]
assert metadata["Path"] == "/"
assert metadata["ServerCertificateName"] == "certname"
assert metadata["Arn"] == f"arn:aws:iam::{ACCOUNT_ID}:server-certificate/certname"
assert "ServerCertificateId" in metadata
assert isinstance(metadata["UploadDate"], datetime)
assert isinstance(metadata["Expiration"], datetime)
2021-09-22 19:42:42 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-22 19:42:42 +00:00
def test_delete_server_cert():
conn = boto3.client("iam", region_name="us-east-1")
conn.upload_server_certificate(
ServerCertificateName="certname",
CertificateBody="certbody",
PrivateKey="privatekey",
)
conn.get_server_certificate(ServerCertificateName="certname")
conn.delete_server_certificate(ServerCertificateName="certname")
with pytest.raises(ClientError) as ex:
conn.get_server_certificate(ServerCertificateName="certname")
err = ex.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert (
err["Message"] == "The Server Certificate with name certname cannot be found."
2021-09-22 19:42:42 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2021-09-22 19:42:42 +00:00
def test_delete_unknown_server_cert():
conn = boto3.client("iam", region_name="us-east-1")
with pytest.raises(ClientError) as ex:
conn.delete_server_certificate(ServerCertificateName="certname")
err = ex.value.response["Error"]
assert err["Code"] == "NoSuchEntity"
assert (
err["Message"] == "The Server Certificate with name certname cannot be found."
2021-09-22 19:42:42 +00:00
)