Best Python code snippet using localstack_python
test_apigw_base_path_mapping.py
Source:test_apigw_base_path_mapping.py
1#!/usr/bin/python2# TODO: License goes here3import library.apigw_base_path_mapping as apigw_base_path_mapping4from library.apigw_base_path_mapping import ApiGwBasePathMapping5import mock6from mock import patch7from mock import create_autospec8from mock import ANY9import unittest10import boto11from botocore.exceptions import BotoCoreError12class TestApiGwBasePathMapping(unittest.TestCase):13 def setUp(self):14 self.module = mock.MagicMock()15 self.module.check_mode = False16 self.module.exit_json = mock.MagicMock()17 self.module.fail_json = mock.MagicMock()18 self.bpm = ApiGwBasePathMapping(self.module)19 self.bpm.client = mock.MagicMock()20 self.bpm.module.params = {21 'name': 'testify',22 'rest_api_id': 'rest_id',23 'base_path': 'test_base_path',24 'stage': 'test_stage',25 'state': 'present',26 }27 reload(apigw_base_path_mapping)28 def test_boto_module_not_found(self):29 # Setup Mock Import Function30 import __builtin__ as builtins31 real_import = builtins.__import__32 def mock_import(name, *args):33 if name == 'boto': raise ImportError34 return real_import(name, *args)35 with mock.patch('__builtin__.__import__', side_effect=mock_import):36 reload(apigw_base_path_mapping)37 ApiGwBasePathMapping(self.module)38 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')39 def test_boto3_module_not_found(self):40 # Setup Mock Import Function41 import __builtin__ as builtins42 real_import = builtins.__import__43 def mock_import(name, *args):44 if name == 'boto3': raise ImportError45 return real_import(name, *args)46 with mock.patch('__builtin__.__import__', side_effect=mock_import):47 reload(apigw_base_path_mapping)48 ApiGwBasePathMapping(self.module)49 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')50 @patch.object(apigw_base_path_mapping, 'boto3')51 def test_boto3_client_properly_instantiated(self, mock_boto):52 ApiGwBasePathMapping(self.module)53 mock_boto.client.assert_called_once_with('apigateway')54 @patch.object(ApiGwBasePathMapping, '_update_base_path_mapping', return_value=(None, None))55 def test_process_request_calls_get_base_path_mappings_and_stores_result_when_invoked(self, m):56 resp = {57 'items': [58 {'basePath': 'not a match', 'restApiId': 'rest_api_id', 'stage': 'whatever'},59 {'basePath': 'test_base_path', 'restApiId': 'rest_api_id', 'stage': 'yes'},60 ],61 }62 self.bpm.client.get_base_path_mappings = mock.MagicMock(return_value=resp)63 self.bpm.process_request()64 self.assertEqual(resp['items'][1], self.bpm.me)65 self.bpm.client.get_base_path_mappings.assert_called_once_with(domainName='testify')66 def test_process_request_stores_None_result_when_not_found_in_get_base_path_mappings_result(self):67 resp = {68 'items': [69 {'basePath': 'not a match', 'restApiId': 'rest_api_id', 'stage': 'whatever'},70 {'basePath': 'not so much', 'restApiId': 'rest_api_id', 'stage': 'yes'},71 ],72 }73 self.bpm.client.get_base_path_mappings = mock.MagicMock(return_value=resp)74 self.bpm.process_request()75 self.assertEqual(None, self.bpm.me)76 self.bpm.client.get_base_path_mappings.assert_called_once_with(domainName='testify')77 def test_process_request_calls_fail_json_when_get_base_path_mappings_raises_exception(self):78 self.bpm.client.get_base_path_mappings = mock.MagicMock(side_effect=BotoCoreError())79 self.bpm.process_request()80 self.bpm.client.get_base_path_mappings.assert_called_once_with(domainName='testify')81 self.bpm.module.fail_json.assert_called_once_with(82 msg='Error when getting base_path_mappings from boto3: An unspecified error occurred'83 )84 @patch.object(ApiGwBasePathMapping, '_delete_base_path_mapping', return_value='Mitchell!')85 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value='found')86 def test_process_request_calls_exit_json_with_expected_value_after_successful_delete(self, mr, md):87 self.bpm.module.params = {88 'base_path': 'test_base_path',89 'name': 'testify',90 'state': 'absent',91 }92 self.bpm.process_request()93 self.bpm.module.exit_json.assert_called_once_with(changed='Mitchell!', base_path_mapping=None)94 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value='found!')95 def test_process_request_calls_delete_base_path_mapping_when_state_absent_and_base_path_mapping_found(self, m):96 self.bpm.module.params = {97 'base_path': 'test_base_path',98 'name': 'testify',99 'state': 'absent',100 }101 self.bpm.process_request()102 self.bpm.client.delete_base_path_mapping.assert_called_once_with(domainName='testify', basePath='test_base_path')103 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value={'id': 'found'})104 def test_process_request_skips_delete_and_calls_exit_json_with_true_when_check_mode_set_and_auth_found(self, m):105 self.bpm.module.params = {106 'base_path': 'test_base_path',107 'name': 'testify',108 'state': 'absent',109 }110 self.bpm.module.check_mode = True111 self.bpm.process_request()112 self.assertEqual(0, self.bpm.client.delete_base_path_mapping.call_count)113 self.bpm.module.exit_json.assert_called_once_with(changed=True, base_path_mapping=None)114 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value={'id': 'found'})115 def test_process_request_calls_fail_json_when_delete_base_path_mapping_raises_error(self, m):116 self.bpm.module.params = {117 'base_path': 'test_base_path',118 'name': 'testify',119 'state': 'absent',120 }121 self.bpm.client.delete_base_path_mapping = mock.MagicMock(side_effect=BotoCoreError)122 self.bpm.process_request()123 self.bpm.client.delete_base_path_mapping.assert_called_once_with(domainName='testify', basePath='test_base_path')124 self.bpm.module.fail_json.assert_called_once_with(125 msg='Error when deleting base_path_mapping via boto3: An unspecified error occurred'126 )127 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)128 def test_process_request_skips_delete_when_base_path_mapping_not_found(self, m):129 self.bpm.module.params = {130 'base_path': 'test_base_path',131 'name': 'testify',132 'state': 'absent',133 }134 self.bpm.process_request()135 self.assertEqual(0, self.bpm.client.delete_base_path_mapping.call_count)136 @patch.object(ApiGwBasePathMapping, '_create_base_path_mapping', return_value=('heart', 'pumping'))137 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)138 def test_process_request_calls_exit_json_with_expected_value_after_successful_create(self, mra, mca):139 self.bpm.process_request()140 self.bpm.module.exit_json.assert_called_once_with(changed='heart', base_path_mapping='pumping')141 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)142 def test_process_request_returns_create_base_path_mapping_result_when_create_succeeds(self, m):143 self.bpm.client.create_base_path_mapping = mock.MagicMock(return_value='woot')144 self.bpm.process_request()145 self.bpm.module.exit_json.assert_called_once_with(changed=True, base_path_mapping='woot')146 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)147 def test_process_request_calls_create_base_path_mapping_when_state_present_and_base_path_mapping_not_found(self, m):148 self.bpm.process_request()149 self.bpm.client.create_base_path_mapping.assert_called_once_with(150 domainName='testify',151 restApiId='rest_id',152 basePath='test_base_path',153 stage='test_stage'154 )155 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)156 def test_process_request_calls_fail_json_when_create_base_path_mapping_raises_exception(self, m):157 self.bpm.client.create_base_path_mapping = mock.MagicMock(side_effect=BotoCoreError())158 self.bpm.process_request()159 self.bpm.client.create_base_path_mapping.assert_called_once_with(160 domainName='testify',161 restApiId='rest_id',162 basePath='test_base_path',163 stage='test_stage'164 )165 self.bpm.module.fail_json.assert_called_once_with(166 msg='Error when creating base_path_mapping via boto3: An unspecified error occurred'167 )168 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)169 def test_process_request_skips_create_call_and_returns_changed_True_when_check_mode(self, m):170 self.bpm.module.check_mode = True171 self.bpm.process_request()172 self.assertEqual(0, self.bpm.client.create_base_path_mapping.call_count)173 self.bpm.module.exit_json.assert_called_once_with(changed=True, base_path_mapping=None)174 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value=None)175 def test_process_request_calls_fail_json_when_state_present_and_required_field_missing(self, mra):176 self.bpm.module.params.pop('rest_api_id', None)177 self.bpm.process_request()178 self.bpm.module.fail_json.assert_called_with(179 msg="Field 'rest_api_id' is required when attempting to create a Base Path Mapping resource"180 )181 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping')182 def test_process_request_calls_update_base_path_mapping_when_state_present_and_base_path_mapping_changed(self, m):183 m.return_value = {184 'basePath': 'test_base_path',185 'stage': 'original stage',186 'restApiId': 'ab12345',187 }188 expected_patches = [189 {'op': 'replace', 'path': '/stage', 'value': 'test_stage'},190 ]191 self.bpm.process_request()192 self.bpm.client.update_base_path_mapping.assert_called_once_with(193 domainName='testify',194 basePath='test_base_path',195 patchOperations=expected_patches196 )197 @patch('library.apigw_base_path_mapping.ApiGwBasePathMapping._create_patches', return_value=[])198 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value={'something': 'here'})199 def test_process_request_skips_update_base_path_mapping_and_replies_false_when_no_changes(self, m, mcp):200 self.bpm.process_request()201 self.assertEqual(0, self.bpm.client.update_base_path_mapping.call_count)202 self.bpm.module.exit_json.assert_called_once_with(changed=False, base_path_mapping={'something': 'here'})203 @patch('library.apigw_base_path_mapping.ApiGwBasePathMapping._create_patches', return_value=['patches!'])204 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value={'id': 'hi'})205 def test_process_request_calls_fail_json_when_update_base_path_mapping_raises_exception(self, m, mcp):206 self.bpm.client.update_base_path_mapping = mock.MagicMock(side_effect=BotoCoreError())207 self.bpm.process_request()208 self.bpm.client.update_base_path_mapping.assert_called_once_with(209 domainName='testify',210 basePath='test_base_path',211 patchOperations=['patches!']212 )213 self.bpm.module.fail_json.assert_called_once_with(214 msg='Error when updating base_path_mapping via boto3: An unspecified error occurred'215 )216 @patch('library.apigw_base_path_mapping.ApiGwBasePathMapping._create_patches', return_value=['patches!'])217 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', side_effect=[{'id': 'hi'}, 'second call'])218 def test_process_request_returns_result_of_find_when_update_is_successful(self, m, mcp):219 self.bpm.process_request()220 self.bpm.client.update_base_path_mapping.assert_called_once_with(221 domainName='testify',222 basePath='test_base_path',223 patchOperations=['patches!']224 )225 self.bpm.module.exit_json.assert_called_once_with(changed=True, base_path_mapping='second call')226 @patch('library.apigw_base_path_mapping.ApiGwBasePathMapping._create_patches', return_value=['patches!'])227 @patch.object(ApiGwBasePathMapping, '_retrieve_base_path_mapping', return_value={'something': 'here'})228 def test_process_request_skips_update_base_path_mapping_and_replies_true_when_check_mode(self, m, mcp):229 self.bpm.module.check_mode = True230 self.bpm.process_request()231 self.assertEqual(0, self.bpm.client.update_base_path_mapping.call_count)232 self.bpm.module.exit_json.assert_called_once_with(changed=True, base_path_mapping={'something': 'here'})233 def test_define_argument_spec(self):234 result = ApiGwBasePathMapping._define_module_argument_spec()235 self.assertIsInstance(result, dict)236 self.assertEqual(result, dict(237 name=dict(required=True, aliases=['domain_name']),238 rest_api_id=dict(required=False),239 stage=dict(required=False),240 base_path=dict(required=False, default='(none)'),241 state=dict(default='present', choices=['present', 'absent']),242 ))243 @patch.object(apigw_base_path_mapping, 'AnsibleModule')244 @patch.object(apigw_base_path_mapping, 'ApiGwBasePathMapping')245 def test_main(self, mock_ApiGwBasePathMapping, mock_AnsibleModule):246 mock_ApiGwBasePathMapping_instance = mock.MagicMock()247 mock_AnsibleModule_instance = mock.MagicMock()248 mock_ApiGwBasePathMapping.return_value = mock_ApiGwBasePathMapping_instance249 mock_AnsibleModule.return_value = mock_AnsibleModule_instance250 apigw_base_path_mapping.main()251 mock_ApiGwBasePathMapping.assert_called_once_with(mock_AnsibleModule_instance)252 assert mock_ApiGwBasePathMapping_instance.process_request.call_count == 1253if __name__ == '__main__':...
api_gateway.py
Source:api_gateway.py
...47 "base_path": item.get("basePath")48 })49 return mappings50@helpers.attempt_api_call_multiple_times51def delete_base_path_mapping(cl: boto3.client, domain_name: str, base_path: str, dry: bool):52 """53 TODO: create custom waiter for BasePathMappingDeleted, could call54 cl.get_base_path_mappings until base_path is not in returned object55 list56 """57 if dry:58 return helpers.dry_run_success_resp()59 return helpers.make_call_catch_err(60 cl.delete_base_path_mapping, domainName=domain_name, basePath=base_path61 )62def get_usage_plan(cl: boto3.client, usage_plan_id: str):63 """64 Describe a usage plan, if it exists65 """...
api_gateway_domain.py
Source:api_gateway_domain.py
...25 response = api_client.get_base_path_mappings(domainName=domain_name)26 if response:27 for item in response['items']:28 print(item)29 api_client.delete_base_path_mapping(domainName=domain_name, basePath=base_path)30 api_client.delete_domain_name(domainName=domain_name)31def configure_base_path_mapping(api_client, api_id, domain_name, deploy_stage, base_path):32 response = None33 try:34 response = api_client.get_base_path_mapping(domainName=domain_name, basePath=base_path)35 except api_client.exceptions.NotFoundException:36 info(f'Base path mapping for {domain_name}:{base_path} does not exist.')37 if response:38 info(f'Base path mapping {domain_name}:{base_path} already exists.')39 api_client.delete_base_path_mapping(domainName=domain_name, basePath=base_path)40 response = api_client.create_base_path_mapping(41 domainName=domain_name, basePath=base_path, restApiId=api_id, stage=deploy_stage42 )43 if response['ResponseMetadata']['HTTPStatusCode'] == 201:44 info(f'Base path mapping created for {domain_name}:{base_path}')45 else:46 warn(f'Unable to create base path mapping for {domain_name}:{base_path}')47def create_custom_domain_name(api_client, domain_name, certificate_arn):48 response = None49 try:50 response = api_client.get_domain_name(domainName=domain_name)51 except api_client.exceptions.NotFoundException:52 info(f'Custom domain name {domain_name} does not exist.')53 if response:...
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!!