How to use create_secret method in localstack

Best Python code snippet using localstack_python

test_secretsmanager.py

Source:test_secretsmanager.py Github

copy

Full Screen

...12DEFAULT_SECRET_NAME = "test-secret"13@mock_secretsmanager14def test_get_secret_value():15 conn = boto3.client("secretsmanager", region_name="us-west-2")16 create_secret = conn.create_secret(17 Name="java-util-test-password", SecretString="foosecret"18 )19 result = conn.get_secret_value(SecretId="java-util-test-password")20 assert result["SecretString"] == "foosecret"21@mock_secretsmanager22def test_get_secret_value_by_arn():23 conn = boto3.client("secretsmanager", region_name="us-west-2")24 secret_value = "test_get_secret_value_by_arn"25 result = conn.create_secret(26 Name="java-util-test-password", SecretString=secret_value27 )28 result = conn.get_secret_value(SecretId=result["ARN"])29 assert result["SecretString"] == secret_value30@mock_secretsmanager31def test_get_secret_value_binary():32 conn = boto3.client("secretsmanager", region_name="us-west-2")33 create_secret = conn.create_secret(34 Name="java-util-test-password", SecretBinary=b("foosecret")35 )36 result = conn.get_secret_value(SecretId="java-util-test-password")37 assert result["SecretBinary"] == b("foosecret")38@mock_secretsmanager39def test_get_secret_that_does_not_exist():40 conn = boto3.client("secretsmanager", region_name="us-west-2")41 with assert_raises(ClientError) as cm:42 result = conn.get_secret_value(SecretId="i-dont-exist")43 assert_equal(44 "Secrets Manager can't find the specified secret.",45 cm.exception.response["Error"]["Message"],46 )47@mock_secretsmanager48def test_get_secret_that_does_not_match():49 conn = boto3.client("secretsmanager", region_name="us-west-2")50 create_secret = conn.create_secret(51 Name="java-util-test-password", SecretString="foosecret"52 )53 with assert_raises(ClientError) as cm:54 result = conn.get_secret_value(SecretId="i-dont-match")55 assert_equal(56 "Secrets Manager can't find the specified secret.",57 cm.exception.response["Error"]["Message"],58 )59@mock_secretsmanager60def test_get_secret_value_that_is_marked_deleted():61 conn = boto3.client("secretsmanager", region_name="us-west-2")62 conn.create_secret(Name="test-secret", SecretString="foosecret")63 conn.delete_secret(SecretId="test-secret")64 with assert_raises(ClientError):65 result = conn.get_secret_value(SecretId="test-secret")66@mock_secretsmanager67def test_get_secret_that_has_no_value():68 conn = boto3.client("secretsmanager", region_name="us-west-2")69 create_secret = conn.create_secret(Name="java-util-test-password")70 with assert_raises(ClientError) as cm:71 result = conn.get_secret_value(SecretId="java-util-test-password")72 assert_equal(73 "Secrets Manager can't find the specified secret value for staging label: AWSCURRENT",74 cm.exception.response["Error"]["Message"],75 )76@mock_secretsmanager77def test_get_secret_version_that_does_not_exist():78 conn = boto3.client("secretsmanager", region_name="us-west-2")79 result = conn.create_secret(Name="java-util-test-password")80 secret_arn = result["ARN"]81 missing_version_id = "00000000-0000-0000-0000-000000000000"82 with assert_raises(ClientError) as cm:83 conn.get_secret_value(SecretId=secret_arn, VersionId=missing_version_id)84 assert_equal(85 (86 "An error occurred (ResourceNotFoundException) when calling the GetSecretValue operation: Secrets "87 "Manager can't find the specified secret value for VersionId: 00000000-0000-0000-0000-000000000000"88 ),89 cm.exception.response["Error"]["Message"],90 )91@mock_secretsmanager92def test_create_secret():93 conn = boto3.client("secretsmanager", region_name="us-east-1")94 result = conn.create_secret(Name="test-secret", SecretString="foosecret")95 assert result["ARN"]96 assert result["Name"] == "test-secret"97 secret = conn.get_secret_value(SecretId="test-secret")98 assert secret["SecretString"] == "foosecret"99@mock_secretsmanager100def test_create_secret_with_tags():101 conn = boto3.client("secretsmanager", region_name="us-east-1")102 secret_name = "test-secret-with-tags"103 result = conn.create_secret(104 Name=secret_name,105 SecretString="foosecret",106 Tags=[{"Key": "Foo", "Value": "Bar"}, {"Key": "Mykey", "Value": "Myvalue"}],107 )108 assert result["ARN"]109 assert result["Name"] == secret_name110 secret_value = conn.get_secret_value(SecretId=secret_name)111 assert secret_value["SecretString"] == "foosecret"112 secret_details = conn.describe_secret(SecretId=secret_name)113 assert secret_details["Tags"] == [114 {"Key": "Foo", "Value": "Bar"},115 {"Key": "Mykey", "Value": "Myvalue"},116 ]117@mock_secretsmanager118def test_create_secret_with_description():119 conn = boto3.client("secretsmanager", region_name="us-east-1")120 secret_name = "test-secret-with-tags"121 result = conn.create_secret(122 Name=secret_name, SecretString="foosecret", Description="desc"123 )124 assert result["ARN"]125 assert result["Name"] == secret_name126 secret_value = conn.get_secret_value(SecretId=secret_name)127 assert secret_value["SecretString"] == "foosecret"128 secret_details = conn.describe_secret(SecretId=secret_name)129 assert secret_details["Description"] == "desc"130@mock_secretsmanager131def test_create_secret_with_tags_and_description():132 conn = boto3.client("secretsmanager", region_name="us-east-1")133 secret_name = "test-secret-with-tags"134 result = conn.create_secret(135 Name=secret_name,136 SecretString="foosecret",137 Description="desc",138 Tags=[{"Key": "Foo", "Value": "Bar"}, {"Key": "Mykey", "Value": "Myvalue"}],139 )140 assert result["ARN"]141 assert result["Name"] == secret_name142 secret_value = conn.get_secret_value(SecretId=secret_name)143 assert secret_value["SecretString"] == "foosecret"144 secret_details = conn.describe_secret(SecretId=secret_name)145 assert secret_details["Tags"] == [146 {"Key": "Foo", "Value": "Bar"},147 {"Key": "Mykey", "Value": "Myvalue"},148 ]149 assert secret_details["Description"] == "desc"150@mock_secretsmanager151def test_delete_secret():152 conn = boto3.client("secretsmanager", region_name="us-west-2")153 conn.create_secret(Name="test-secret", SecretString="foosecret")154 deleted_secret = conn.delete_secret(SecretId="test-secret")155 assert deleted_secret["ARN"]156 assert deleted_secret["Name"] == "test-secret"157 assert deleted_secret["DeletionDate"] > datetime.fromtimestamp(1, pytz.utc)158 secret_details = conn.describe_secret(SecretId="test-secret")159 assert secret_details["ARN"]160 assert secret_details["Name"] == "test-secret"161 assert secret_details["DeletedDate"] > datetime.fromtimestamp(1, pytz.utc)162@mock_secretsmanager163def test_delete_secret_force():164 conn = boto3.client("secretsmanager", region_name="us-west-2")165 conn.create_secret(Name="test-secret", SecretString="foosecret")166 result = conn.delete_secret(SecretId="test-secret", ForceDeleteWithoutRecovery=True)167 assert result["ARN"]168 assert result["DeletionDate"] > datetime.fromtimestamp(1, pytz.utc)169 assert result["Name"] == "test-secret"170 with assert_raises(ClientError):171 result = conn.get_secret_value(SecretId="test-secret")172@mock_secretsmanager173def test_delete_secret_force_with_arn():174 conn = boto3.client("secretsmanager", region_name="us-west-2")175 create_secret = conn.create_secret(Name="test-secret", SecretString="foosecret")176 result = conn.delete_secret(177 SecretId=create_secret["ARN"], ForceDeleteWithoutRecovery=True178 )179 assert result["ARN"]180 assert result["DeletionDate"] > datetime.fromtimestamp(1, pytz.utc)181 assert result["Name"] == "test-secret"182 with assert_raises(ClientError):183 result = conn.get_secret_value(SecretId="test-secret")184@mock_secretsmanager185def test_delete_secret_that_does_not_exist():186 conn = boto3.client("secretsmanager", region_name="us-west-2")187 with assert_raises(ClientError):188 result = conn.delete_secret(189 SecretId="i-dont-exist", ForceDeleteWithoutRecovery=True190 )191@mock_secretsmanager192def test_delete_secret_fails_with_both_force_delete_flag_and_recovery_window_flag():193 conn = boto3.client("secretsmanager", region_name="us-west-2")194 conn.create_secret(Name="test-secret", SecretString="foosecret")195 with assert_raises(ClientError):196 result = conn.delete_secret(197 SecretId="test-secret",198 RecoveryWindowInDays=1,199 ForceDeleteWithoutRecovery=True,200 )201@mock_secretsmanager202def test_delete_secret_recovery_window_too_short():203 conn = boto3.client("secretsmanager", region_name="us-west-2")204 conn.create_secret(Name="test-secret", SecretString="foosecret")205 with assert_raises(ClientError):206 result = conn.delete_secret(SecretId="test-secret", RecoveryWindowInDays=6)207@mock_secretsmanager208def test_delete_secret_recovery_window_too_long():209 conn = boto3.client("secretsmanager", region_name="us-west-2")210 conn.create_secret(Name="test-secret", SecretString="foosecret")211 with assert_raises(ClientError):212 result = conn.delete_secret(SecretId="test-secret", RecoveryWindowInDays=31)213@mock_secretsmanager214def test_delete_secret_that_is_marked_deleted():215 conn = boto3.client("secretsmanager", region_name="us-west-2")216 conn.create_secret(Name="test-secret", SecretString="foosecret")217 deleted_secret = conn.delete_secret(SecretId="test-secret")218 with assert_raises(ClientError):219 result = conn.delete_secret(SecretId="test-secret")220@mock_secretsmanager221def test_get_random_password_default_length():222 conn = boto3.client("secretsmanager", region_name="us-west-2")223 random_password = conn.get_random_password()224 assert len(random_password["RandomPassword"]) == 32225@mock_secretsmanager226def test_get_random_password_default_requirements():227 # When require_each_included_type, default true228 conn = boto3.client("secretsmanager", region_name="us-west-2")229 random_password = conn.get_random_password()230 # Should contain lowercase, upppercase, digit, special character231 assert any(c.islower() for c in random_password["RandomPassword"])232 assert any(c.isupper() for c in random_password["RandomPassword"])233 assert any(c.isdigit() for c in random_password["RandomPassword"])234 assert any(c in string.punctuation for c in random_password["RandomPassword"])235@mock_secretsmanager236def test_get_random_password_custom_length():237 conn = boto3.client("secretsmanager", region_name="us-west-2")238 random_password = conn.get_random_password(PasswordLength=50)239 assert len(random_password["RandomPassword"]) == 50240@mock_secretsmanager241def test_get_random_exclude_lowercase():242 conn = boto3.client("secretsmanager", region_name="us-west-2")243 random_password = conn.get_random_password(PasswordLength=55, ExcludeLowercase=True)244 assert any(c.islower() for c in random_password["RandomPassword"]) == False245@mock_secretsmanager246def test_get_random_exclude_uppercase():247 conn = boto3.client("secretsmanager", region_name="us-west-2")248 random_password = conn.get_random_password(PasswordLength=55, ExcludeUppercase=True)249 assert any(c.isupper() for c in random_password["RandomPassword"]) == False250@mock_secretsmanager251def test_get_random_exclude_characters_and_symbols():252 conn = boto3.client("secretsmanager", region_name="us-west-2")253 random_password = conn.get_random_password(254 PasswordLength=20, ExcludeCharacters="xyzDje@?!."255 )256 assert any(c in "xyzDje@?!." for c in random_password["RandomPassword"]) == False257 assert len(random_password["RandomPassword"]) == 20258@mock_secretsmanager259def test_get_random_exclude_numbers():260 conn = boto3.client("secretsmanager", region_name="us-west-2")261 random_password = conn.get_random_password(PasswordLength=100, ExcludeNumbers=True)262 assert any(c.isdigit() for c in random_password["RandomPassword"]) == False263@mock_secretsmanager264def test_get_random_exclude_punctuation():265 conn = boto3.client("secretsmanager", region_name="us-west-2")266 random_password = conn.get_random_password(267 PasswordLength=100, ExcludePunctuation=True268 )269 assert (270 any(c in string.punctuation for c in random_password["RandomPassword"]) == False271 )272@mock_secretsmanager273def test_get_random_include_space_false():274 conn = boto3.client("secretsmanager", region_name="us-west-2")275 random_password = conn.get_random_password(PasswordLength=300)276 assert any(c.isspace() for c in random_password["RandomPassword"]) == False277@mock_secretsmanager278def test_get_random_include_space_true():279 conn = boto3.client("secretsmanager", region_name="us-west-2")280 random_password = conn.get_random_password(PasswordLength=4, IncludeSpace=True)281 assert any(c.isspace() for c in random_password["RandomPassword"]) == True282@mock_secretsmanager283def test_get_random_require_each_included_type():284 conn = boto3.client("secretsmanager", region_name="us-west-2")285 random_password = conn.get_random_password(286 PasswordLength=4, RequireEachIncludedType=True287 )288 assert (289 any(c in string.punctuation for c in random_password["RandomPassword"]) == True290 )291 assert (292 any(c in string.ascii_lowercase for c in random_password["RandomPassword"])293 == True294 )295 assert (296 any(c in string.ascii_uppercase for c in random_password["RandomPassword"])297 == True298 )299 assert any(c in string.digits for c in random_password["RandomPassword"]) == True300@mock_secretsmanager301def test_get_random_too_short_password():302 conn = boto3.client("secretsmanager", region_name="us-west-2")303 with assert_raises(ClientError):304 random_password = conn.get_random_password(PasswordLength=3)305@mock_secretsmanager306def test_get_random_too_long_password():307 conn = boto3.client("secretsmanager", region_name="us-west-2")308 with assert_raises(Exception):309 random_password = conn.get_random_password(PasswordLength=5555)310@mock_secretsmanager311def test_describe_secret():312 conn = boto3.client("secretsmanager", region_name="us-west-2")313 conn.create_secret(Name="test-secret", SecretString="foosecret")314 conn.create_secret(Name="test-secret-2", SecretString="barsecret")315 secret_description = conn.describe_secret(SecretId="test-secret")316 secret_description_2 = conn.describe_secret(SecretId="test-secret-2")317 assert secret_description # Returned dict is not empty318 assert secret_description["Name"] == ("test-secret")319 assert secret_description["ARN"] != "" # Test arn not empty320 assert secret_description_2["Name"] == ("test-secret-2")321 assert secret_description_2["ARN"] != "" # Test arn not empty322@mock_secretsmanager323def test_describe_secret_with_arn():324 conn = boto3.client("secretsmanager", region_name="us-west-2")325 results = conn.create_secret(Name="test-secret", SecretString="foosecret")326 secret_description = conn.describe_secret(SecretId=results["ARN"])327 assert secret_description # Returned dict is not empty328 secret_description["Name"].should.equal("test-secret")329 secret_description["ARN"].should.equal(results["ARN"])330 conn.list_secrets()["SecretList"][0]["ARN"].should.equal(results["ARN"])331@mock_secretsmanager332def test_describe_secret_that_does_not_exist():333 conn = boto3.client("secretsmanager", region_name="us-west-2")334 with assert_raises(ClientError):335 result = conn.get_secret_value(SecretId="i-dont-exist")336@mock_secretsmanager337def test_describe_secret_that_does_not_match():338 conn = boto3.client("secretsmanager", region_name="us-west-2")339 conn.create_secret(Name="test-secret", SecretString="foosecret")340 with assert_raises(ClientError):341 result = conn.get_secret_value(SecretId="i-dont-match")342@mock_secretsmanager343def test_restore_secret():344 conn = boto3.client("secretsmanager", region_name="us-west-2")345 conn.create_secret(Name="test-secret", SecretString="foosecret")346 conn.delete_secret(SecretId="test-secret")347 described_secret_before = conn.describe_secret(SecretId="test-secret")348 assert described_secret_before["DeletedDate"] > datetime.fromtimestamp(1, pytz.utc)349 restored_secret = conn.restore_secret(SecretId="test-secret")350 assert restored_secret["ARN"]351 assert restored_secret["Name"] == "test-secret"352 described_secret_after = conn.describe_secret(SecretId="test-secret")353 assert "DeletedDate" not in described_secret_after354@mock_secretsmanager355def test_restore_secret_that_is_not_deleted():356 conn = boto3.client("secretsmanager", region_name="us-west-2")357 conn.create_secret(Name="test-secret", SecretString="foosecret")358 restored_secret = conn.restore_secret(SecretId="test-secret")359 assert restored_secret["ARN"]360 assert restored_secret["Name"] == "test-secret"361@mock_secretsmanager362def test_restore_secret_that_does_not_exist():363 conn = boto3.client("secretsmanager", region_name="us-west-2")364 with assert_raises(ClientError):365 result = conn.restore_secret(SecretId="i-dont-exist")366@mock_secretsmanager367def test_rotate_secret():368 conn = boto3.client("secretsmanager", region_name="us-west-2")369 conn.create_secret(370 Name=DEFAULT_SECRET_NAME, SecretString="foosecret", Description="foodescription"371 )372 rotated_secret = conn.rotate_secret(SecretId=DEFAULT_SECRET_NAME)373 assert rotated_secret374 assert rotated_secret["ARN"] != "" # Test arn not empty375 assert rotated_secret["Name"] == DEFAULT_SECRET_NAME376 assert rotated_secret["VersionId"] != ""377 describe_secret = conn.describe_secret(SecretId=DEFAULT_SECRET_NAME)378 assert describe_secret["Description"] == "foodescription"379@mock_secretsmanager380def test_rotate_secret_enable_rotation():381 conn = boto3.client("secretsmanager", region_name="us-west-2")382 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretString="foosecret")383 initial_description = conn.describe_secret(SecretId=DEFAULT_SECRET_NAME)384 assert initial_description385 assert initial_description["RotationEnabled"] is False386 assert initial_description["RotationRules"]["AutomaticallyAfterDays"] == 0387 conn.rotate_secret(388 SecretId=DEFAULT_SECRET_NAME, RotationRules={"AutomaticallyAfterDays": 42}389 )390 rotated_description = conn.describe_secret(SecretId=DEFAULT_SECRET_NAME)391 assert rotated_description392 assert rotated_description["RotationEnabled"] is True393 assert rotated_description["RotationRules"]["AutomaticallyAfterDays"] == 42394@mock_secretsmanager395def test_rotate_secret_that_is_marked_deleted():396 conn = boto3.client("secretsmanager", region_name="us-west-2")397 conn.create_secret(Name="test-secret", SecretString="foosecret")398 conn.delete_secret(SecretId="test-secret")399 with assert_raises(ClientError):400 result = conn.rotate_secret(SecretId="test-secret")401@mock_secretsmanager402def test_rotate_secret_that_does_not_exist():403 conn = boto3.client("secretsmanager", "us-west-2")404 with assert_raises(ClientError):405 result = conn.rotate_secret(SecretId="i-dont-exist")406@mock_secretsmanager407def test_rotate_secret_that_does_not_match():408 conn = boto3.client("secretsmanager", region_name="us-west-2")409 conn.create_secret(Name="test-secret", SecretString="foosecret")410 with assert_raises(ClientError):411 result = conn.rotate_secret(SecretId="i-dont-match")412@mock_secretsmanager413def test_rotate_secret_client_request_token_too_short():414 # Test is intentionally empty. Boto3 catches too short ClientRequestToken415 # and raises ParamValidationError before Moto can see it.416 # test_server actually handles this error.417 assert True418@mock_secretsmanager419def test_rotate_secret_client_request_token_too_long():420 conn = boto3.client("secretsmanager", region_name="us-west-2")421 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretString="foosecret")422 client_request_token = (423 "ED9F8B6C-85B7-446A-B7E4-38F2A3BEB13C-" "ED9F8B6C-85B7-446A-B7E4-38F2A3BEB13C"424 )425 with assert_raises(ClientError):426 result = conn.rotate_secret(427 SecretId=DEFAULT_SECRET_NAME, ClientRequestToken=client_request_token428 )429@mock_secretsmanager430def test_rotate_secret_rotation_lambda_arn_too_long():431 conn = boto3.client("secretsmanager", region_name="us-west-2")432 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretString="foosecret")433 rotation_lambda_arn = "85B7-446A-B7E4" * 147 # == 2058 characters434 with assert_raises(ClientError):435 result = conn.rotate_secret(436 SecretId=DEFAULT_SECRET_NAME, RotationLambdaARN=rotation_lambda_arn437 )438@mock_secretsmanager439def test_rotate_secret_rotation_period_zero():440 # Test is intentionally empty. Boto3 catches zero day rotation period441 # and raises ParamValidationError before Moto can see it.442 # test_server actually handles this error.443 assert True444@mock_secretsmanager445def test_rotate_secret_rotation_period_too_long():446 conn = boto3.client("secretsmanager", region_name="us-west-2")447 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretString="foosecret")448 rotation_rules = {"AutomaticallyAfterDays": 1001}449 with assert_raises(ClientError):450 result = conn.rotate_secret(451 SecretId=DEFAULT_SECRET_NAME, RotationRules=rotation_rules452 )453@mock_secretsmanager454def test_put_secret_value_on_non_existing_secret():455 conn = boto3.client("secretsmanager", region_name="us-west-2")456 with assert_raises(ClientError) as cm:457 conn.put_secret_value(458 SecretId=DEFAULT_SECRET_NAME,459 SecretString="foosecret",460 VersionStages=["AWSCURRENT"],461 )462 assert_equal(463 "Secrets Manager can't find the specified secret.",464 cm.exception.response["Error"]["Message"],465 )466@mock_secretsmanager467def test_put_secret_value_puts_new_secret():468 conn = boto3.client("secretsmanager", region_name="us-west-2")469 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary=b("foosecret"))470 put_secret_value_dict = conn.put_secret_value(471 SecretId=DEFAULT_SECRET_NAME,472 SecretString="foosecret",473 VersionStages=["AWSCURRENT"],474 )475 version_id = put_secret_value_dict["VersionId"]476 get_secret_value_dict = conn.get_secret_value(477 SecretId=DEFAULT_SECRET_NAME, VersionId=version_id, VersionStage="AWSCURRENT"478 )479 assert get_secret_value_dict480 assert get_secret_value_dict["SecretString"] == "foosecret"481@mock_secretsmanager482def test_put_secret_binary_value_puts_new_secret():483 conn = boto3.client("secretsmanager", region_name="us-west-2")484 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary=b("foosecret"))485 put_secret_value_dict = conn.put_secret_value(486 SecretId=DEFAULT_SECRET_NAME,487 SecretBinary=b("foosecret"),488 VersionStages=["AWSCURRENT"],489 )490 version_id = put_secret_value_dict["VersionId"]491 get_secret_value_dict = conn.get_secret_value(492 SecretId=DEFAULT_SECRET_NAME, VersionId=version_id, VersionStage="AWSCURRENT"493 )494 assert get_secret_value_dict495 assert get_secret_value_dict["SecretBinary"] == b("foosecret")496@mock_secretsmanager497def test_create_and_put_secret_binary_value_puts_new_secret():498 conn = boto3.client("secretsmanager", region_name="us-west-2")499 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary=b("foosecret"))500 conn.put_secret_value(501 SecretId=DEFAULT_SECRET_NAME, SecretBinary=b("foosecret_update")502 )503 latest_secret = conn.get_secret_value(SecretId=DEFAULT_SECRET_NAME)504 assert latest_secret505 assert latest_secret["SecretBinary"] == b("foosecret_update")506@mock_secretsmanager507def test_put_secret_binary_requires_either_string_or_binary():508 conn = boto3.client("secretsmanager", region_name="us-west-2")509 with assert_raises(ClientError) as ire:510 conn.put_secret_value(SecretId=DEFAULT_SECRET_NAME)511 ire.exception.response["Error"]["Code"].should.equal("InvalidRequestException")512 ire.exception.response["Error"]["Message"].should.equal(513 "You must provide either SecretString or SecretBinary."514 )515@mock_secretsmanager516def test_put_secret_value_can_get_first_version_if_put_twice():517 conn = boto3.client("secretsmanager", region_name="us-west-2")518 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary=b("foosecret"))519 put_secret_value_dict = conn.put_secret_value(520 SecretId=DEFAULT_SECRET_NAME,521 SecretString="first_secret",522 VersionStages=["AWSCURRENT"],523 )524 first_version_id = put_secret_value_dict["VersionId"]525 conn.put_secret_value(526 SecretId=DEFAULT_SECRET_NAME,527 SecretString="second_secret",528 VersionStages=["AWSCURRENT"],529 )530 first_secret_value_dict = conn.get_secret_value(531 SecretId=DEFAULT_SECRET_NAME, VersionId=first_version_id532 )533 first_secret_value = first_secret_value_dict["SecretString"]534 assert first_secret_value == "first_secret"535@mock_secretsmanager536def test_put_secret_value_versions_differ_if_same_secret_put_twice():537 conn = boto3.client("secretsmanager", region_name="us-west-2")538 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary="foosecret")539 put_secret_value_dict = conn.put_secret_value(540 SecretId=DEFAULT_SECRET_NAME,541 SecretString="dupe_secret",542 VersionStages=["AWSCURRENT"],543 )544 first_version_id = put_secret_value_dict["VersionId"]545 put_secret_value_dict = conn.put_secret_value(546 SecretId=DEFAULT_SECRET_NAME,547 SecretString="dupe_secret",548 VersionStages=["AWSCURRENT"],549 )550 second_version_id = put_secret_value_dict["VersionId"]551 assert first_version_id != second_version_id552@mock_secretsmanager553def test_put_secret_value_maintains_description_and_tags():554 conn = boto3.client("secretsmanager", region_name="us-west-2")555 previous_response = conn.create_secret(556 Name=DEFAULT_SECRET_NAME,557 SecretString="foosecret",558 Description="desc",559 Tags=[{"Key": "Foo", "Value": "Bar"}, {"Key": "Mykey", "Value": "Myvalue"}],560 )561 previous_version_id = previous_response["VersionId"]562 conn = boto3.client("secretsmanager", region_name="us-west-2")563 current_response = conn.put_secret_value(564 SecretId=DEFAULT_SECRET_NAME,565 SecretString="dupe_secret",566 VersionStages=["AWSCURRENT"],567 )568 current_version_id = current_response["VersionId"]569 secret_details = conn.describe_secret(SecretId=DEFAULT_SECRET_NAME)570 assert secret_details["Tags"] == [571 {"Key": "Foo", "Value": "Bar"},572 {"Key": "Mykey", "Value": "Myvalue"},573 ]574 assert secret_details["Description"] == "desc"575 assert secret_details["VersionIdsToStages"] is not None576 assert previous_version_id in secret_details["VersionIdsToStages"]577 assert current_version_id in secret_details["VersionIdsToStages"]578 assert secret_details["VersionIdsToStages"][previous_version_id] == ["AWSPREVIOUS"]579 assert secret_details["VersionIdsToStages"][current_version_id] == ["AWSCURRENT"]580@mock_secretsmanager581def test_can_list_secret_version_ids():582 conn = boto3.client("secretsmanager", region_name="us-west-2")583 conn.create_secret(Name=DEFAULT_SECRET_NAME, SecretBinary="foosecret")584 put_secret_value_dict = conn.put_secret_value(585 SecretId=DEFAULT_SECRET_NAME,586 SecretString="dupe_secret",587 VersionStages=["AWSCURRENT"],588 )589 first_version_id = put_secret_value_dict["VersionId"]590 put_secret_value_dict = conn.put_secret_value(591 SecretId=DEFAULT_SECRET_NAME,592 SecretString="dupe_secret",593 VersionStages=["AWSCURRENT"],594 )595 second_version_id = put_secret_value_dict["VersionId"]596 versions_list = conn.list_secret_version_ids(SecretId=DEFAULT_SECRET_NAME)597 returned_version_ids = [v["VersionId"] for v in versions_list["Versions"]]598 assert [first_version_id, second_version_id].sort() == returned_version_ids.sort()599@mock_secretsmanager600def test_update_secret():601 conn = boto3.client("secretsmanager", region_name="us-west-2")602 created_secret = conn.create_secret(Name="test-secret", SecretString="foosecret")603 assert created_secret["ARN"]604 assert created_secret["Name"] == "test-secret"605 assert created_secret["VersionId"] != ""606 secret = conn.get_secret_value(SecretId="test-secret")607 assert secret["SecretString"] == "foosecret"608 updated_secret = conn.update_secret(609 SecretId="test-secret", SecretString="barsecret"610 )611 assert updated_secret["ARN"]612 assert updated_secret["Name"] == "test-secret"613 assert updated_secret["VersionId"] != ""614 secret = conn.get_secret_value(SecretId="test-secret")615 assert secret["SecretString"] == "barsecret"616 assert created_secret["VersionId"] != updated_secret["VersionId"]617@mock_secretsmanager618def test_update_secret_with_tags_and_description():619 conn = boto3.client("secretsmanager", region_name="us-west-2")620 created_secret = conn.create_secret(621 Name="test-secret",622 SecretString="foosecret",623 Description="desc",624 Tags=[{"Key": "Foo", "Value": "Bar"}, {"Key": "Mykey", "Value": "Myvalue"}],625 )626 assert created_secret["ARN"]627 assert created_secret["Name"] == "test-secret"628 assert created_secret["VersionId"] != ""629 secret = conn.get_secret_value(SecretId="test-secret")630 assert secret["SecretString"] == "foosecret"631 updated_secret = conn.update_secret(632 SecretId="test-secret", SecretString="barsecret"633 )634 assert updated_secret["ARN"]635 assert updated_secret["Name"] == "test-secret"636 assert updated_secret["VersionId"] != ""637 secret = conn.get_secret_value(SecretId="test-secret")638 assert secret["SecretString"] == "barsecret"639 assert created_secret["VersionId"] != updated_secret["VersionId"]640 secret_details = conn.describe_secret(SecretId="test-secret")641 assert secret_details["Tags"] == [642 {"Key": "Foo", "Value": "Bar"},643 {"Key": "Mykey", "Value": "Myvalue"},644 ]645 assert secret_details["Description"] == "desc"646@mock_secretsmanager647def test_update_secret_which_does_not_exit():648 conn = boto3.client("secretsmanager", region_name="us-west-2")649 with assert_raises(ClientError) as cm:650 updated_secret = conn.update_secret(651 SecretId="test-secret", SecretString="barsecret"652 )653 assert_equal(654 "Secrets Manager can't find the specified secret.",655 cm.exception.response["Error"]["Message"],656 )657@mock_secretsmanager658def test_update_secret_marked_as_deleted():659 conn = boto3.client("secretsmanager", region_name="us-west-2")660 created_secret = conn.create_secret(Name="test-secret", SecretString="foosecret")661 deleted_secret = conn.delete_secret(SecretId="test-secret")662 with assert_raises(ClientError) as cm:663 updated_secret = conn.update_secret(664 SecretId="test-secret", SecretString="barsecret"665 )666 assert (667 "because it was marked for deletion."668 in cm.exception.response["Error"]["Message"]669 )670@mock_secretsmanager671def test_update_secret_marked_as_deleted_after_restoring():672 conn = boto3.client("secretsmanager", region_name="us-west-2")673 created_secret = conn.create_secret(Name="test-secret", SecretString="foosecret")674 deleted_secret = conn.delete_secret(SecretId="test-secret")675 restored_secret = conn.restore_secret(SecretId="test-secret")676 updated_secret = conn.update_secret(677 SecretId="test-secret", SecretString="barsecret"678 )679 assert updated_secret["ARN"]680 assert updated_secret["Name"] == "test-secret"681 assert updated_secret["VersionId"] != ""682@mock_secretsmanager683def test_tag_resource():684 conn = boto3.client("secretsmanager", region_name="us-west-2")685 conn.create_secret(Name="test-secret", SecretString="foosecret")686 conn.tag_resource(687 SecretId="test-secret", Tags=[{"Key": "FirstTag", "Value": "SomeValue"},],688 )689 conn.tag_resource(690 SecretId="test-secret", Tags=[{"Key": "SecondTag", "Value": "AnotherValue"},],691 )692 secrets = conn.list_secrets()693 assert secrets["SecretList"][0].get("Tags") == [694 {"Key": "FirstTag", "Value": "SomeValue"},695 {"Key": "SecondTag", "Value": "AnotherValue"},696 ]697 with assert_raises(ClientError) as cm:698 conn.tag_resource(699 SecretId="dummy-test-secret",700 Tags=[{"Key": "FirstTag", "Value": "SomeValue"},],701 )702 assert_equal(703 "Secrets Manager can't find the specified secret.",704 cm.exception.response["Error"]["Message"],705 )706@mock_secretsmanager707def test_secret_versions_to_stages_attribute_discrepancy():708 client = boto3.client("secretsmanager", region_name="us-west-2")709 resp = client.create_secret(Name=DEFAULT_SECRET_NAME, SecretString="foosecret")710 previous_version_id = resp["VersionId"]711 resp = client.put_secret_value(712 SecretId=DEFAULT_SECRET_NAME,713 SecretString="dupe_secret",714 VersionStages=["AWSCURRENT"],715 )716 current_version_id = resp["VersionId"]717 secret = client.describe_secret(SecretId=DEFAULT_SECRET_NAME)718 describe_vtos = secret["VersionIdsToStages"]719 assert describe_vtos[current_version_id] == ["AWSCURRENT"]720 assert describe_vtos[previous_version_id] == ["AWSPREVIOUS"]721 secret = client.list_secrets(722 Filters=[{"Key": "name", "Values": [DEFAULT_SECRET_NAME]}]723 ).get("SecretList")[0]...

Full Screen

Full Screen

test_list_secrets.py

Source:test_list_secrets.py Github

copy

Full Screen

...18 assert_items_equal(secrets["SecretList"], [])19@mock_secretsmanager20def test_list_secrets():21 conn = boto_client()22 conn.create_secret(Name="test-secret", SecretString="foosecret")23 conn.create_secret(24 Name="test-secret-2",25 SecretString="barsecret",26 Tags=[{"Key": "a", "Value": "1"}],27 )28 secrets = conn.list_secrets()29 assert secrets["SecretList"][0]["ARN"] is not None30 assert secrets["SecretList"][0]["Name"] == "test-secret"31 assert secrets["SecretList"][0]["SecretVersionsToStages"] is not None32 assert secrets["SecretList"][1]["ARN"] is not None33 assert secrets["SecretList"][1]["Name"] == "test-secret-2"34 assert secrets["SecretList"][1]["Tags"] == [{"Key": "a", "Value": "1"}]35 assert secrets["SecretList"][1]["SecretVersionsToStages"] is not None36@mock_secretsmanager37def test_with_name_filter():38 conn = boto_client()39 conn.create_secret(Name="foo", SecretString="secret")40 conn.create_secret(Name="bar", SecretString="secret")41 secrets = conn.list_secrets(Filters=[{"Key": "name", "Values": ["foo"]}])42 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))43 assert_items_equal(secret_names, ["foo"])44@mock_secretsmanager45def test_with_tag_key_filter():46 conn = boto_client()47 conn.create_secret(48 Name="foo", SecretString="secret", Tags=[{"Key": "baz", "Value": "1"}]49 )50 conn.create_secret(Name="bar", SecretString="secret")51 secrets = conn.list_secrets(Filters=[{"Key": "tag-key", "Values": ["baz"]}])52 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))53 assert_items_equal(secret_names, ["foo"])54@mock_secretsmanager55def test_with_tag_value_filter():56 conn = boto_client()57 conn.create_secret(58 Name="foo", SecretString="secret", Tags=[{"Key": "1", "Value": "baz"}]59 )60 conn.create_secret(Name="bar", SecretString="secret")61 secrets = conn.list_secrets(Filters=[{"Key": "tag-value", "Values": ["baz"]}])62 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))63 assert_items_equal(secret_names, ["foo"])64@mock_secretsmanager65def test_with_description_filter():66 conn = boto_client()67 conn.create_secret(Name="foo", SecretString="secret", Description="baz qux")68 conn.create_secret(Name="bar", SecretString="secret")69 secrets = conn.list_secrets(Filters=[{"Key": "description", "Values": ["baz"]}])70 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))71 assert_items_equal(secret_names, ["foo"])72@mock_secretsmanager73def test_with_all_filter():74 # The 'all' filter will match a secret that contains ANY field with the criteria. In other words an implicit OR.75 conn = boto_client()76 conn.create_secret(Name="foo", SecretString="secret")77 conn.create_secret(Name="bar", SecretString="secret", Description="foo")78 conn.create_secret(79 Name="baz", SecretString="secret", Tags=[{"Key": "foo", "Value": "1"}]80 )81 conn.create_secret(82 Name="qux", SecretString="secret", Tags=[{"Key": "1", "Value": "foo"}]83 )84 conn.create_secret(85 Name="multi", SecretString="secret", Tags=[{"Key": "foo", "Value": "foo"}]86 )87 conn.create_secret(Name="none", SecretString="secret")88 secrets = conn.list_secrets(Filters=[{"Key": "all", "Values": ["foo"]}])89 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))90 assert_items_equal(secret_names, ["foo", "bar", "baz", "qux", "multi"])91@mock_secretsmanager92def test_with_no_filter_key():93 conn = boto_client()94 with assert_raises(ClientError) as ire:95 conn.list_secrets(Filters=[{"Values": ["foo"]}])96 ire.exception.response["Error"]["Code"].should.equal("InvalidParameterException")97 ire.exception.response["Error"]["Message"].should.equal("Invalid filter key")98@mock_secretsmanager99def test_with_no_filter_values():100 conn = boto_client()101 conn.create_secret(Name="foo", SecretString="secret", Description="hello")102 with assert_raises(ClientError) as ire:103 conn.list_secrets(Filters=[{"Key": "description"}])104 ire.exception.response["Error"]["Code"].should.equal("InvalidParameterException")105 ire.exception.response["Error"]["Message"].should.equal(106 "Invalid filter values for key: description"107 )108@mock_secretsmanager109def test_with_invalid_filter_key():110 conn = boto_client()111 with assert_raises(ClientError) as ire:112 conn.list_secrets(Filters=[{"Key": "invalid", "Values": ["foo"]}])113 ire.exception.response["Error"]["Code"].should.equal("ValidationException")114 ire.exception.response["Error"]["Message"].should.equal(115 "1 validation error detected: Value 'invalid' at 'filters.1.member.key' failed to satisfy constraint: Member "116 "must satisfy enum value set: [all, name, tag-key, description, tag-value]"117 )118@mock_secretsmanager119def test_with_duplicate_filter_keys():120 # Multiple filters with the same key combine with an implicit AND operator121 conn = boto_client()122 conn.create_secret(Name="foo", SecretString="secret", Description="one two")123 conn.create_secret(Name="bar", SecretString="secret", Description="one")124 conn.create_secret(Name="baz", SecretString="secret", Description="two")125 conn.create_secret(Name="qux", SecretString="secret", Description="unrelated")126 secrets = conn.list_secrets(127 Filters=[128 {"Key": "description", "Values": ["one"]},129 {"Key": "description", "Values": ["two"]},130 ]131 )132 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))133 assert_items_equal(secret_names, ["foo"])134@mock_secretsmanager135def test_with_multiple_filters():136 # Multiple filters combine with an implicit AND operator137 conn = boto_client()138 conn.create_secret(139 Name="foo", SecretString="secret", Tags=[{"Key": "right", "Value": "right"}]140 )141 conn.create_secret(142 Name="bar", SecretString="secret", Tags=[{"Key": "right", "Value": "wrong"}]143 )144 conn.create_secret(145 Name="baz", SecretString="secret", Tags=[{"Key": "wrong", "Value": "right"}]146 )147 conn.create_secret(148 Name="qux", SecretString="secret", Tags=[{"Key": "wrong", "Value": "wrong"}]149 )150 secrets = conn.list_secrets(151 Filters=[152 {"Key": "tag-key", "Values": ["right"]},153 {"Key": "tag-value", "Values": ["right"]},154 ]155 )156 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))157 assert_items_equal(secret_names, ["foo"])158@mock_secretsmanager159def test_with_filter_with_multiple_values():160 conn = boto_client()161 conn.create_secret(Name="foo", SecretString="secret")162 conn.create_secret(Name="bar", SecretString="secret")163 conn.create_secret(Name="baz", SecretString="secret")164 secrets = conn.list_secrets(Filters=[{"Key": "name", "Values": ["foo", "bar"]}])165 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))166 assert_items_equal(secret_names, ["foo", "bar"])167@mock_secretsmanager168def test_with_filter_with_value_with_multiple_words():169 conn = boto_client()170 conn.create_secret(Name="foo", SecretString="secret", Description="one two")171 conn.create_secret(Name="bar", SecretString="secret", Description="one and two")172 conn.create_secret(Name="baz", SecretString="secret", Description="one")173 conn.create_secret(Name="qux", SecretString="secret", Description="two")174 conn.create_secret(Name="none", SecretString="secret", Description="unrelated")175 secrets = conn.list_secrets(Filters=[{"Key": "description", "Values": ["one two"]}])176 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful