Best Python code snippet using localstack_python
test_secretsmanager_basics.py
Source:test_secretsmanager_basics.py
1# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.2# SPDX-License-Identifier: Apache-2.03"""4Unit tests for secretsmanager_basics.py5"""6import uuid7import boto38from botocore.exceptions import ClientError9import pytest10from secretsmanager_basics import SecretsManagerSecret11@pytest.mark.parametrize('secret_value,error_code', [12 ('test-secret', None),13 (b'test-secret', None),14 ('test-secret', 'TestException')])15def test_create(make_stubber, secret_value, error_code):16 secretsmanager_client = boto3.client('secretsmanager')17 secretsmanager_stubber = make_stubber(secretsmanager_client)18 secret = SecretsManagerSecret(secretsmanager_client)19 name = 'test-name'20 secretsmanager_stubber.stub_create_secret(21 name, secret_value, error_code=error_code)22 if error_code is None:23 secret.create(name, secret_value)24 assert secret.name == name25 else:26 with pytest.raises(ClientError) as exc_info:27 secret.create(name, secret_value)28 assert exc_info.value.response['Error']['Code'] == error_code29@pytest.mark.parametrize('name,error_code', [30 ('test-name', None),31 (None, None),32 ('test-name', 'TestException')])33def test_describe(make_stubber, name, error_code):34 secretsmanager_client = boto3.client('secretsmanager')35 secretsmanager_stubber = make_stubber(secretsmanager_client)36 secret = SecretsManagerSecret(secretsmanager_client)37 existing_name = 'secret-name'38 if name is None:39 secret.name = existing_name40 secretsmanager_stubber.stub_describe_secret(41 name if name is not None else existing_name, error_code=error_code)42 if error_code is None:43 secret.describe(name)44 if name is None:45 assert secret.name == existing_name46 else:47 assert secret.name == name48 else:49 with pytest.raises(ClientError) as exc_info:50 secret.describe(name)51 assert exc_info.value.response['Error']['Code'] == error_code52@pytest.mark.parametrize('stage,secret_value,error_code', [53 ('test-stage', 'test-value', None),54 ('test-stage', b'test-value', None),55 (None, 'test-value', None),56 ('test-stage', 'test-value', 'TestException')])57def test_get_value(make_stubber, stage, secret_value, error_code):58 secretsmanager_client = boto3.client('secretsmanager')59 secretsmanager_stubber = make_stubber(secretsmanager_client)60 secret = SecretsManagerSecret(secretsmanager_client)61 secret.name = 'test-name'62 secretsmanager_stubber.stub_get_secret_value(63 secret.name, stage, secret_value, error_code=error_code)64 if error_code is None:65 got_response = secret.get_value(stage)66 if isinstance(secret_value, str):67 assert got_response['SecretString'] == secret_value68 elif isinstance(secret_value, bytes):69 assert got_response['SecretBinary'] == secret_value70 else:71 with pytest.raises(ClientError) as exc_info:72 secret.get_value(stage)73 assert exc_info.value.response['Error']['Code'] == error_code74@pytest.mark.parametrize('error_code', [None, 'TestException'])75def test_get_random_password(make_stubber, error_code):76 secretsmanager_client = boto3.client('secretsmanager')77 secretsmanager_stubber = make_stubber(secretsmanager_client)78 secret = SecretsManagerSecret(secretsmanager_client)79 pw_length = 2080 password = 'test-password'81 secretsmanager_stubber.stub_get_random_password(82 pw_length, password, error_code=error_code)83 if error_code is None:84 got_password = secret.get_random_password(pw_length)85 assert got_password == password86 else:87 with pytest.raises(ClientError) as exc_info:88 secret.get_random_password(pw_length)89 assert exc_info.value.response['Error']['Code'] == error_code90@pytest.mark.parametrize('secret_value,stages,error_code', [91 ('test-value', ['test-stage'], None),92 (b'test-value', None, None),93 ('test-value', ['test-stage'], 'TestException')])94def test_put_value(make_stubber, secret_value, stages, error_code):95 secretsmanager_client = boto3.client('secretsmanager')96 secretsmanager_stubber = make_stubber(secretsmanager_client)97 secret = SecretsManagerSecret(secretsmanager_client)98 secret.name = 'test-name'99 secretsmanager_stubber.stub_put_secret_value(100 secret.name, secret_value, stages, error_code=error_code)101 if error_code is None:102 secret.put_value(secret_value, stages)103 else:104 with pytest.raises(ClientError) as exc_info:105 secret.put_value(secret_value, stages)106 assert exc_info.value.response['Error']['Code'] == error_code107@pytest.mark.parametrize('error_code', [None, 'TestException'])108def test_update_version_stage(make_stubber, error_code):109 secretsmanager_client = boto3.client('secretsmanager')110 secretsmanager_stubber = make_stubber(secretsmanager_client)111 secret = SecretsManagerSecret(secretsmanager_client)112 secret.name = 'test-name'113 stage = 'test-stage'114 remove_from = str(uuid.uuid4())115 move_to = str(uuid.uuid4())116 secretsmanager_stubber.stub_update_secret_version_stage(117 secret.name, stage, remove_from, move_to, error_code=error_code)118 if error_code is None:119 secret.update_version_stage(stage, remove_from, move_to)120 else:121 with pytest.raises(ClientError) as exc_info:122 secret.update_version_stage(stage, remove_from, move_to)123 assert exc_info.value.response['Error']['Code'] == error_code124@pytest.mark.parametrize('error_code', [None, 'TestException'])125def test_delete(make_stubber, error_code):126 secretsmanager_client = boto3.client('secretsmanager')127 secretsmanager_stubber = make_stubber(secretsmanager_client)128 secret = SecretsManagerSecret(secretsmanager_client)129 secret.name = 'test-name'130 secretsmanager_stubber.stub_delete_secret(secret.name, error_code=error_code)131 if error_code is None:132 secret.delete(True)133 else:134 with pytest.raises(ClientError) as exc_info:135 secret.delete(True)136 assert exc_info.value.response['Error']['Code'] == error_code137@pytest.mark.parametrize('error_code', [None, 'TestException'])138def test_list(make_stubber, error_code):139 secretsmanager_client = boto3.client('secretsmanager')140 secretsmanager_stubber = make_stubber(secretsmanager_client)141 secret = SecretsManagerSecret(secretsmanager_client)142 secrets = [{'Name': f'test-name-{secid}'} for secid in range(5)]143 secretsmanager_stubber.stub_list_secrets(secrets, error_code=error_code)144 if error_code is None:145 assert secrets == list(secret.list(10))146 else:147 with pytest.raises(ClientError) as exc_info:148 list(secret.list(10))...
test_secretsmanager.py
Source:test_secretsmanager.py
1import unittest2import json3from datetime import datetime4from localstack.utils.aws import aws_stack5TEST_SECRET_NAME_1 = 'test_secret_put'6TEST_SECRET_NAME_2 = 'test_secret_2nd'7TEST_SECRET_NAME_3 = 'test_secret_3rd'8RESOURCE_POLICY = {9 'Version': '2012-10-17',10 'Statement': [{11 'Effect': 'Allow',12 'Principal': {13 'AWS': 'arn:aws:iam::123456789012:root'14 },15 'Action': 'secretsmanager:GetSecretValue',16 'Resource': '*'17 }]18}19class SecretsManagerTest(unittest.TestCase):20 def setUp(self):21 self.secretsmanager_client = aws_stack.connect_to_service('secretsmanager')22 def test_create_and_update_secret(self):23 rs = self.secretsmanager_client.create_secret(24 Name=TEST_SECRET_NAME_1,25 SecretString='my_secret',26 Description='testing creation of secrets'27 )28 secret_arn = rs['ARN']29 rs = self.secretsmanager_client.get_secret_value(30 SecretId=TEST_SECRET_NAME_1,31 )32 self.assertEqual(rs['Name'], TEST_SECRET_NAME_1)33 self.assertEqual(rs['SecretString'], 'my_secret')34 self.assertEqual(rs['ARN'], secret_arn)35 self.assertTrue(isinstance(rs['CreatedDate'], datetime))36 self.secretsmanager_client.put_secret_value(37 SecretId=TEST_SECRET_NAME_1,38 SecretString='new_secret'39 )40 rs = self.secretsmanager_client.get_secret_value(41 SecretId=TEST_SECRET_NAME_1,42 )43 self.assertEqual(rs['Name'], TEST_SECRET_NAME_1)44 self.assertEqual(rs['SecretString'], 'new_secret')45 # clean up46 self.secretsmanager_client.delete_secret(47 SecretId=TEST_SECRET_NAME_1,48 ForceDeleteWithoutRecovery=True49 )50 def test_call_lists_secrets_multiple_time(self):51 self.secretsmanager_client.create_secret(52 Name=TEST_SECRET_NAME_2,53 SecretString='my_secret',54 Description='testing creation of secrets'55 )56 # call list_secrets 1st57 rs = self.secretsmanager_client.list_secrets()58 secrets = [59 secret for secret in rs['SecretList'] if secret['Name'] == TEST_SECRET_NAME_260 ]61 self.assertEqual(len(secrets), 1)62 secret_arn = secrets[0]['ARN']63 # call list_secrets 2nd64 rs = self.secretsmanager_client.list_secrets()65 secrets = [66 secret for secret in rs['SecretList'] if secret['Name'] == TEST_SECRET_NAME_267 ]68 self.assertEqual(len(secrets), 1)69 self.assertEqual(secrets[0]['ARN'], secret_arn)70 # clean up71 self.secretsmanager_client.delete_secret(72 SecretId=TEST_SECRET_NAME_2,73 ForceDeleteWithoutRecovery=True74 )75 def test_create_multi_secrets(self):76 secret_names = [TEST_SECRET_NAME_1, TEST_SECRET_NAME_2, TEST_SECRET_NAME_3]77 arns = []78 for secret_name in secret_names:79 rs = self.secretsmanager_client.create_secret(80 Name=secret_name,81 SecretString='my_secret_{}'.format(secret_name),82 Description='testing creation of secrets'83 )84 arns.append(rs['ARN'])85 rs = self.secretsmanager_client.list_secrets()86 secrets = {87 secret['Name']: secret['ARN']88 for secret in rs['SecretList'] if secret['Name'] in secret_names89 }90 self.assertEqual(len(secrets.keys()), len(secret_names))91 for arn in arns:92 self.assertIn(arn, secrets.values())93 # clean up94 for secret_name in secret_names:95 self.secretsmanager_client.delete_secret(96 SecretId=secret_name,97 ForceDeleteWithoutRecovery=True98 )99 def test_get_random_exclude_characters_and_symbols(self):100 random_password = self.secretsmanager_client.get_random_password(101 PasswordLength=120, ExcludeCharacters='xyzDje@?!.'102 )103 self.assertEqual(120, len(random_password['RandomPassword']))104 self.assertTrue(all([c not in 'xyzDje@?!.' for c in random_password['RandomPassword']]))105 def test_resource_policy(self):106 self.secretsmanager_client.create_secret(107 Name=TEST_SECRET_NAME_1,108 SecretString='my_secret',109 Description='testing creation of secrets'110 )111 self.secretsmanager_client.put_resource_policy(112 SecretId=TEST_SECRET_NAME_1,113 ResourcePolicy=json.dumps(RESOURCE_POLICY)114 )115 rs = self.secretsmanager_client.get_resource_policy(116 SecretId=TEST_SECRET_NAME_1117 )118 policy = json.loads(rs['ResourcePolicy'])119 self.assertEqual(policy['Version'], RESOURCE_POLICY['Version'])120 self.assertEqual(policy['Statement'], RESOURCE_POLICY['Statement'])121 rs = self.secretsmanager_client.delete_resource_policy(122 SecretId=TEST_SECRET_NAME_1123 )124 self.assertEqual(rs['ResponseMetadata']['HTTPStatusCode'], 200)125 # clean up126 self.secretsmanager_client.delete_secret(127 SecretId=TEST_SECRET_NAME_1,128 ForceDeleteWithoutRecovery=True...
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!!