Best Python code snippet using localstack_python
test_iam.py
Source:test_iam.py
...3import moto4import boto35import freezegun6@pytest.fixture7def generate_policy():8 def generate(name, description=None):9 description = description or "Nothing"10 document = {11 "Version": "2012-10-17",12 "Statement": [{"Effect": "Allow", "Action": "*", "Resource": "*"}],13 }14 response = boto3.client("iam").create_policy(15 PolicyName=name,16 PolicyDocument=json.dumps(document),17 Description=description,18 )19 return response["Policy"]["Arn"]20 return generate21@pytest.fixture22def generate_role():23 def generate(name, description=None):24 description = description or "Nothing"25 document = {26 "Version": "2012-10-17",27 "Statement": [{"Effect": "Allow", "Action": "*", "Resource": "*"}],28 }29 response = boto3.client("iam").create_role(30 RoleName=name,31 AssumeRolePolicyDocument=json.dumps(document),32 Description=description,33 )34 return response["Role"]["Arn"]35 return generate36@moto.mock_iam37def test_iam_user_has_name_assertion():38 user = boto3.resource("iam").User("foo")39 user.has.name("foo")40 user.does_not_have.name("bar")41@freezegun.freeze_time("01/02/2021 10:00:00+00:00")42@moto.mock_iam43def test_iam_user_was_created_at_assertion():44 user = boto3.resource("iam").User("foo").create()45 user.was.created_at("01/02/2021 10:00:00+00:00")46 user.was_not.created_at("02/02/2021 10:00:00+00:00")47 user.was_not.created_at("01/02/2021 10:00:01+00:00")48@moto.mock_iam49def test_iam_user_belongs_to_group_assertion():50 user = boto3.resource("iam").User("foo").create()51 group = boto3.resource("iam").Group("bar").create()52 user.does_not_belong_to.group(group)53 user.is_not_part_of.group(group)54 group.add_user(UserName=user.name)55 user.belongs_to.group(group)56 user.is_part_of.group(group)57@moto.mock_iam58def test_iam_policy_has_name_assertion(generate_policy):59 policy_arn = generate_policy("foo")60 policy = boto3.resource("iam").Policy(policy_arn)61 policy.has.name("foo")62 policy.does_not_have.name("bar")63@moto.mock_iam64def test_iam_policy_has_description_assertion(generate_policy):65 policy_arn = generate_policy("foo", description="description")66 policy = boto3.resource("iam").Policy(policy_arn)67 policy.has.description("description")68 policy.does_not_have.description("not the description")69@freezegun.freeze_time("01/02/2021 10:00:00+00:00")70@moto.mock_iam71def test_iam_policy_was_created_at_assertion(generate_policy):72 policy_arn = generate_policy("foo")73 policy = boto3.resource("iam").Policy(policy_arn)74 policy.was.created_at("01/02/2021 10:00:00+00:00")75 policy.was_not.created_at("02/02/2021 10:00:00+00:00")76 policy.was_not.created_at("01/02/2021 10:00:01+00:00")77@freezegun.freeze_time("01/02/2021 10:00:00+00:00")78@moto.mock_iam79def test_iam_policy_was_last_updated_at_assertion(generate_policy):80 policy_arn = generate_policy("foo")81 policy = boto3.resource("iam").Policy(policy_arn)82 policy.was.last_updated_at("01/02/2021 10:00:00+00:00")83 policy.was_not.last_updated_at("02/02/2021 10:00:00+00:00")84@moto.mock_iam85def test_iam_policy_should_be_attached_to_assertion_with_user(generate_policy):86 policy_arn = generate_policy("foo")87 policy = boto3.resource("iam").Policy(policy_arn)88 good_user = boto3.resource("iam").User("good").create()89 policy.attach_user(UserName=good_user.name)90 policy.should_be.attached_to(good_user)91 bad_user = boto3.resource("iam").User("bad").create()92 policy.should_not_be.attached_to(bad_user)93@moto.mock_iam94def test_iam_policy_should_be_attached_to_assertion_with_role(95 generate_policy, generate_role96):97 policy_arn = generate_policy("foo")98 policy = boto3.resource("iam").Policy(policy_arn)99 generate_role("good")100 good_role = boto3.resource("iam").Role("good")101 policy.attach_role(RoleName=good_role.name)102 policy.should_be.attached_to(good_role)103 generate_role("bad")104 bad_role = boto3.resource("iam").Role("bad")105 policy.should_not_be.attached_to(bad_role)106@moto.mock_iam107def test_iam_policy_should_be_attached_to_assertion_with_group(generate_policy):108 policy_arn = generate_policy("foo")109 policy = boto3.resource("iam").Policy(policy_arn)110 good_group = boto3.resource("iam").Group("good").create()111 policy.attach_group(GroupName=good_group.name)112 policy.should_be.attached_to(good_group)113 bad_group = boto3.resource("iam").Group("bad").create()114 policy.should_not_be.attached_to(bad_group)115@moto.mock_iam116def test_iam_policy_should_be_attached_to_assertion_with_multiple(117 generate_policy, generate_role118):119 policy_arn = generate_policy("foo")120 policy = boto3.resource("iam").Policy(policy_arn)121 good_user = boto3.resource("iam").User("good").create()122 good_group = boto3.resource("iam").Group("good").create()123 generate_role("good")124 good_role = boto3.resource("iam").Role("good")125 policy.attach_user(UserName=good_user.name)126 policy.attach_role(RoleName=good_role.name)127 policy.attach_group(GroupName=good_group.name)128 policy.should_be.attached_to(good_group, good_user, good_role)129 bad_user = boto3.resource("iam").User("bad").create()130 policy.should_not_be.attached_to(good_group, bad_user, good_role)131@moto.mock_iam132def test_iam_group_has_name_assertion():133 group = boto3.resource("iam").Group("foo")134 group.has.name("foo")135 group.does_not_have.name("bar")136@freezegun.freeze_time("01/02/2021 10:00:00+00:00")137@moto.mock_iam138def test_iam_group_was_created_at_assertion():139 group = boto3.resource("iam").Group("foo").create()140 group.was.created_at("01/02/2021 10:00:00+00:00")141 group.was_not.created_at("02/02/2021 10:00:00+00:00")142 group.was_not.created_at("01/02/2021 10:00:01+00:00")143@moto.mock_iam144def test_iam_group_should_contain_assertion():145 group = boto3.resource("iam").Group("foo").create()146 user = boto3.resource("iam").User("bar").create()147 group.should_not.contain(user)148 group.add_user(UserName=user.name)149 group.should.contain(user)150@moto.mock_iam151def test_iam_group_has_policy_assertion(generate_policy):152 group = boto3.resource("iam").Group("foo").create()153 policy_arn = generate_policy("bar")154 policy = boto3.resource("iam").Policy(policy_arn)155 group.does_not_have.policy(policy)156 policy.attach_group(GroupName=group.name)157 group.has.policy(policy)158@moto.mock_iam159def test_iam_role_has_name_assertion(generate_role):160 generate_role("foo")161 role = boto3.resource("iam").Role("foo")162 role.has.name("foo")163 role.does_not_have.name("bar")164@moto.mock_iam165def test_iam_role_has_description_assertion(generate_role):166 generate_role("foo", description="description")167 role = boto3.resource("iam").Role("foo")168 role.has.description("description")169 role.does_not_have.description("not the description")170@freezegun.freeze_time("01/02/2021 10:00:00+00:00")171@moto.mock_iam172def test_iam_role_was_created_at_assertion(generate_role):173 generate_role("foo")174 role = boto3.resource("iam").Role("foo")175 role.was.created_at("01/02/2021 10:00:00+00:00")176 role.was_not.created_at("02/02/2021 10:00:00+00:00")177 role.was_not.created_at("01/02/2021 10:00:01+00:00")178@moto.mock_iam179def test_iam_role_has_policy_assertion(generate_policy, generate_role):180 policy_arn = generate_policy("good")181 good_policy = boto3.resource("iam").Policy(policy_arn)182 generate_role("foo")183 role = boto3.resource("iam").Role("foo")184 role.attach_policy(PolicyArn=good_policy.arn)185 role.has.policy(good_policy)186 role.uses.policy(good_policy)187 bad_policy = boto3.resource("iam").Policy("bad")188 role.does_not_have.policy(bad_policy)...
test_sev_policy.py
Source:test_sev_policy.py
1from cli.toolkit.sev.policy import generate_policy2def test_basic_policy():3 no_debug_policy = generate_policy(4 enable_debug=False,5 enable_key_sharing=True,6 require_sev_es=False,7 enable_send=True,8 limit_to_domain=False,9 limit_to_sev=False,10 minimum_firmware_version=None,11 )12 assert no_debug_policy == 0x113 debug_policy = generate_policy(14 enable_debug=True,15 enable_key_sharing=True,16 require_sev_es=False,17 enable_send=True,18 limit_to_domain=False,19 limit_to_sev=False,20 minimum_firmware_version=None,21 )22 assert debug_policy == 0x023 restrictive_policy = generate_policy(24 enable_debug=False,25 enable_key_sharing=False,26 require_sev_es=True,27 enable_send=False,28 limit_to_domain=True,29 limit_to_sev=True,30 minimum_firmware_version=None,31 )32 assert restrictive_policy == 0x3F33def test_policy_with_firmware_version():34 debug_policy_with_firmware_version = generate_policy(35 enable_debug=True,36 enable_key_sharing=True,37 require_sev_es=False,38 enable_send=True,39 limit_to_domain=False,40 limit_to_sev=False,41 minimum_firmware_version=(1, 51),42 )43 assert debug_policy_with_firmware_version == 0x33010000, hex(44 debug_policy_with_firmware_version45 )46 restrictive_policy_with_firmware_version = generate_policy(47 enable_debug=False,48 enable_key_sharing=False,49 require_sev_es=True,50 enable_send=False,51 limit_to_domain=True,52 limit_to_sev=True,53 minimum_firmware_version=(1, 51),54 )55 assert restrictive_policy_with_firmware_version == 0x3301003F, hex(56 restrictive_policy_with_firmware_version...
auth.py
Source:auth.py
...7 id_token = verify_token(token)8 print(id_token)9 if id_token and id_token.get('permissions'):10 scopes = '|'.join(id_token['permissions'])11 policy = generate_policy(12 id_token['sub'], 13 'Allow', 14 event['methodArn'],15 scopes=scopes16 )17 return policy18 else:19 policy = generate_policy(20 id_token['sub'],21 "Deny",22 event['methodArn']23 )24 return policy25def generate_policy(principal_id, effect, resource, scopes=None):26 policy = {27 'principalId': principal_id,28 'policyDocument': {29 'Version': '2012-10-17',30 'Statement': [31 {32 "Action": "execute-api:Invoke",33 "Effect": effect,34 "Resource": resource35 }36 ]37 }38 }39 if scopes:...
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!!