Best Python code snippet using localstack_python
test_kms.py
Source:test_kms.py
1import botocore.exceptions2import pytest3from localstack import config4from localstack.constants import TEST_AWS_ACCOUNT_ID5class TestKMS:6 def test_create_key(self, kms_client):7 response = kms_client.list_keys()8 assert response["ResponseMetadata"]["HTTPStatusCode"] == 2009 keys_before = response["Keys"]10 response = kms_client.create_key(11 Policy="policy1", Description="test key 123", KeyUsage="ENCRYPT_DECRYPT"12 )13 assert response["ResponseMetadata"]["HTTPStatusCode"] == 20014 key_id = response["KeyMetadata"]["KeyId"]15 response = kms_client.list_keys()16 assert len(response["Keys"]) == len(keys_before) + 117 response = kms_client.describe_key(KeyId=key_id)["KeyMetadata"]18 assert response["KeyId"] == key_id19 assert ":%s:" % config.DEFAULT_REGION in response["Arn"]20 assert ":%s:" % TEST_AWS_ACCOUNT_ID in response["Arn"]21 def test_create_grant_with_invalid_key(self, kms_client):22 with pytest.raises(botocore.exceptions.ClientError):23 kms_client.create_grant(24 KeyId="invalid",25 GranteePrincipal="arn:aws:iam::000000000000:role/test",26 Operations=["Decrypt", "Encrypt"],27 )28 def test_list_grants_with_invalid_key(self, kms_client):29 with pytest.raises(botocore.exceptions.ClientError):30 kms_client.list_grants(31 KeyId="invalid",32 )33 def test_create_grant_with_valid_key(self, kms_client, kms_key):34 key_id = kms_key["KeyMetadata"]["KeyId"]35 grants_before = kms_client.list_grants(KeyId=key_id)["Grants"]36 grant = kms_client.create_grant(37 KeyId=key_id,38 GranteePrincipal="arn:aws:iam::000000000000:role/test",39 Operations=["Decrypt", "Encrypt"],40 )41 assert "GrantId" in grant42 assert "GrantToken" in grant43 grants_after = kms_client.list_grants(KeyId=key_id)["Grants"]44 assert len(grants_after) == len(grants_before) + 145 def test_revoke_grant(self, kms_client, kms_grant_and_key):46 grant = kms_grant_and_key[0]47 key_id = kms_grant_and_key[1]["KeyMetadata"]["KeyId"]48 grants_before = kms_client.list_grants(KeyId=key_id)["Grants"]49 kms_client.revoke_grant(KeyId=key_id, GrantId=grant["GrantId"])50 grants_after = kms_client.list_grants(KeyId=key_id)["Grants"]51 assert len(grants_after) == len(grants_before) - 152 def test_retire_grant(self, kms_client, kms_grant_and_key):53 grant = kms_grant_and_key[0]54 key_id = kms_grant_and_key[1]["KeyMetadata"]["KeyId"]55 grants_before = kms_client.list_grants(KeyId=key_id)["Grants"]56 kms_client.retire_grant(GrantToken=grant["GrantToken"])57 grants_after = kms_client.list_grants(KeyId=key_id)["Grants"]58 assert len(grants_after) == len(grants_before) - 159 def test_asymmetric_keys(self, kms_client, kms_key):60 key_id = kms_key["KeyMetadata"]["KeyId"]61 # generate key pair without plaintext62 result = kms_client.generate_data_key_pair_without_plaintext(63 KeyId=key_id, KeyPairSpec="RSA_2048"64 )65 assert result.get("PrivateKeyCiphertextBlob")66 assert not result.get("PrivateKeyPlaintext")67 assert result.get("PublicKey")68 # generate key pair69 result = kms_client.generate_data_key_pair(KeyId=key_id, KeyPairSpec="RSA_2048")70 assert result.get("PrivateKeyCiphertextBlob")71 assert result.get("PrivateKeyPlaintext")72 assert result.get("PublicKey")73 # get public key74 result1 = kms_client.get_public_key(KeyId=key_id)75 assert result.get("KeyId") == result1.get("KeyId")76 assert result.get("KeySpec") == result1.get("KeySpec")77 assert result.get("PublicKey") == result1.get("PublicKey")78 # assert correct value of encrypted key79 decrypted = kms_client.decrypt(80 CiphertextBlob=result["PrivateKeyCiphertextBlob"], KeyId=key_id81 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!