Best Python code snippet using localstack_python
test_deregister.py
Source:test_deregister.py
1# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13from argparse import Namespace14from mock import MagicMock, call15from awscli.customizations.codedeploy.deregister import Deregister16from awscli.testutils import unittest17class TestDeregister(unittest.TestCase):18 def setUp(self):19 self.instance_name = 'instance-name'20 self.tags = [{'Key': 'k1', 'Value': 'v1'}]21 self.iam_user_arn = 'arn:aws:iam::012345678912:user/{0}'.format(22 self.instance_name23 )24 self.access_key_id = 'ACCESSKEYID'25 self.region = 'us-east-1'26 self.policy_name = 'codedeploy-agent'27 self.endpoint_url = 'https://codedeploy.aws.amazon.com'28 self.args = Namespace()29 self.args.instance_name = self.instance_name30 self.args.no_delete_iam_user = False31 self.globals = Namespace()32 self.globals.region = self.region33 self.globals.endpoint_url = self.endpoint_url34 self.globals.verify_ssl = False35 self.codedeploy = MagicMock()36 self.codedeploy.get_on_premises_instance.return_value = {37 'instanceInfo': {38 'iamUserArn': self.iam_user_arn,39 'tags': None40 }41 }42 self.iam = MagicMock()43 self.list_user_policies = MagicMock()44 self.list_user_policies.paginate.return_value = [45 {'PolicyNames': [self.policy_name]}46 ]47 self.list_access_keys = MagicMock()48 self.list_access_keys.paginate.return_value = [49 {'AccessKeyMetadata': [{'AccessKeyId': self.access_key_id}]}50 ]51 self.iam.get_paginator.side_effect = [52 self.list_user_policies, self.list_access_keys53 ]54 self.session = MagicMock()55 self.session.create_client.side_effect = [self.codedeploy, self.iam]56 self.deregister = Deregister(self.session)57 def test_deregister_throws_on_invalid_region(self):58 self.globals.region = None59 self.session.get_config_variable.return_value = None60 with self.assertRaisesRegexp(RuntimeError, 'Region not specified.'):61 self.deregister._run_main(self.args, self.globals)62 def test_deregister_throws_on_invalid_instance_name(self):63 self.args.instance_name = 'invalid%@^&%#&'64 with self.assertRaisesRegexp(65 ValueError, 'Instance name contains invalid characters.'):66 self.deregister._run_main(self.args, self.globals)67 def test_deregister_creates_clients(self):68 self.deregister._run_main(self.args, self.globals)69 self.session.create_client.assert_has_calls([70 call(71 'codedeploy',72 region_name=self.region,73 endpoint_url=self.endpoint_url,74 verify=self.globals.verify_ssl75 ),76 call('iam', region_name=self.region)77 ])78 def test_deregister_with_tags(self):79 self.codedeploy.get_on_premises_instance.return_value = {80 'instanceInfo': {81 'iamUserArn': self.iam_user_arn,82 'tags': self.tags83 }84 }85 self.deregister._run_main(self.args, self.globals)86 self.codedeploy.get_on_premises_instance.assert_called_with(87 instanceName=self.instance_name88 )89 self.assertIn('iam_user_arn', self.args)90 self.assertEquals(self.iam_user_arn, self.args.iam_user_arn)91 self.assertIn('user_name', self.args)92 self.assertEquals(self.instance_name, self.args.user_name)93 self.assertIn('tags', self.args)94 self.assertEquals(self.tags, self.args.tags)95 self.codedeploy.remove_tags_from_on_premises_instances.\96 assert_called_with(97 tags=self.tags,98 instanceNames=[self.instance_name]99 )100 self.codedeploy.deregister_on_premises_instance.\101 assert_called_with(102 instanceName=self.instance_name103 )104 def test_deregister_with_no_tags(self):105 self.codedeploy.get_on_premises_instance.return_value = {106 'instanceInfo': {107 'iamUserArn': self.iam_user_arn,108 'tags': None109 }110 }111 self.deregister._run_main(self.args, self.globals)112 self.codedeploy.get_on_premises_instance.assert_called_with(113 instanceName=self.instance_name114 )115 self.assertIn('iam_user_arn', self.args)116 self.assertEquals(self.iam_user_arn, self.args.iam_user_arn)117 self.assertIn('user_name', self.args)118 self.assertEquals(self.instance_name, self.args.user_name)119 self.assertIn('tags', self.args)120 self.assertEquals(None, self.args.tags)121 self.assertFalse(122 self.codedeploy.remove_tags_from_on_premises_instances.called123 )124 self.codedeploy.deregister_on_premises_instance.\125 assert_called_with(126 instanceName=self.instance_name127 )128 def test_deregister_with_delete_iam_user(self):129 self.args.no_delete_iam_user = False130 self.deregister._run_main(self.args, self.globals)131 self.codedeploy.get_on_premises_instance.assert_called_with(132 instanceName=self.instance_name133 )134 self.codedeploy.deregister_on_premises_instance.\135 assert_called_with(136 instanceName=self.instance_name137 )138 self.iam.get_paginator.assert_has_calls([139 call('list_user_policies'),140 call('list_access_keys')141 ])142 self.list_user_policies.paginate.assert_called_with(143 UserName=self.instance_name144 )145 self.iam.delete_user_policy.assert_called_with(146 UserName=self.instance_name,147 PolicyName=self.policy_name148 )149 self.list_access_keys.paginate.assert_called_with(150 UserName=self.instance_name151 )152 self.iam.delete_access_key.assert_called_with(153 UserName=self.instance_name,154 AccessKeyId=self.access_key_id155 )156 self.iam.delete_user.assert_called_with(157 UserName=self.instance_name158 )159 def test_deregister_with_no_delete_iam_user(self):160 self.args.no_delete_iam_user = True161 self.deregister._run_main(self.args, self.globals)162 self.codedeploy.get_on_premises_instance.assert_called_with(163 instanceName=self.instance_name164 )165 self.codedeploy.deregister_on_premises_instance.\166 assert_called_with(167 instanceName=self.instance_name168 )169 self.assertFalse(self.iam.get_paginator.called)170 self.assertFalse(self.list_user_policies.paginate.called)171 self.assertFalse(self.iam.delete_user_policy.called)172 self.assertFalse(self.list_access_keys.paginate.called)173 self.assertFalse(self.iam.delete_access_key.called)174 self.assertFalse(self.iam.delete_user.called)175if __name__ == "__main__":...
list_iam_users_details.py
Source:list_iam_users_details.py
...10"""11for key in users['Users']:12 list_user_policies=iam.list_groups_for_user(UserName=key['UserName']) #Will fetch the policies which are from AWS IAM Groups13 policies = [ gp['GroupName'] for gp in list_user_policies['Groups']] # Will fetch embedded user policies14 policies.append(iam.list_user_policies(UserName=key['UserName'])['PolicyNames']) # Combining both together for a user15 users_list[key['UserName']]=policies16 17for user,policies in users_list.items():18 print user, policies...
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!!