Best Python code snippet using localstack_python
test_apigw_usage_plan_key.py
Source: test_apigw_usage_plan_key.py
1#!/usr/bin/python2# TODO: License goes here3import library.apigw_usage_plan_key as apigw_usage_plan_key4from library.apigw_usage_plan_key import ApiGwUsagePlanKey5import mock6from mock import patch7from mock import create_autospec8from mock import ANY9import unittest10import boto11from botocore.exceptions import BotoCoreError12class TestApiGwUsagePlanKey(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.usage_plan_key = ApiGwUsagePlanKey(self.module)19 self.usage_plan_key.client = mock.MagicMock()20 self.usage_plan_key.module.params = {21 'usage_plan_id': 'upid' ,22 'api_key_id': 'akid',23 'key_type': 'API_KEY',24 'state': 'present',25 }26 reload(apigw_usage_plan_key)27 def test_boto_module_not_found(self):28 # Setup Mock Import Function29 import __builtin__ as builtins30 real_import = builtins.__import__31 def mock_import(name, *args):32 if name == 'boto': raise ImportError33 return real_import(name, *args)34 with mock.patch('__builtin__.__import__', side_effect=mock_import):35 reload(apigw_usage_plan_key)36 ApiGwUsagePlanKey(self.module)37 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')38 def test_boto3_module_not_found(self):39 # Setup Mock Import Function40 import __builtin__ as builtins41 real_import = builtins.__import__42 def mock_import(name, *args):43 if name == 'boto3': raise ImportError44 return real_import(name, *args)45 with mock.patch('__builtin__.__import__', side_effect=mock_import):46 reload(apigw_usage_plan_key)47 ApiGwUsagePlanKey(self.module)48 self.module.fail_json.assert_called_with(msg='boto and boto3 are required for this module')49 @patch.object(apigw_usage_plan_key, 'boto3')50 def test_boto3_client_properly_instantiated(self, mock_boto):51 ApiGwUsagePlanKey(self.module)52 mock_boto.client.assert_called_once_with('apigateway')53 def test_process_request_calls_get_usage_plan_keys_and_stores_result_when_invoked(self):54 resp = {55 'items': [56 {'id': 'wrong_id'},57 {'id': 'akid'},58 ],59 }60 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(return_value=resp)61 self.usage_plan_key.process_request()62 self.assertEqual(resp['items'][1], self.usage_plan_key.me)63 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')64 def test_process_request_stores_None_result_when_not_found_in_get_usage_plan_keys_result(self):65 resp = {66 'items': [67 {'id': 'wrong id'},68 {'id': 'wronger id'},69 ],70 }71 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(return_value=resp)72 self.usage_plan_key.process_request()73 self.assertEqual(None, self.usage_plan_key.me)74 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')75 def test_process_request_calls_fail_json_when_get_usage_plan_keys_raises_exception(self):76 self.usage_plan_key.client.get_usage_plan_keys = mock.MagicMock(side_effect=BotoCoreError())77 self.usage_plan_key.process_request()78 self.usage_plan_key.client.get_usage_plan_keys.assert_called_once_with(usagePlanId='upid')79 self.usage_plan_key.module.fail_json.assert_called_once_with(80 msg='Error when getting usage_plan_keys from boto3: An unspecified error occurred'81 )82 @patch.object(ApiGwUsagePlanKey, '_delete_usage_plan_key', return_value='Mitchell!')83 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})84 def test_process_request_calls_exit_json_with_expected_value_after_successful_delete(self, mr, md):85 self.usage_plan_key.module.params = {86 'usage_plan_id': 'upid',87 'api_key_id': 'akid',88 'state': 'absent',89 }90 self.usage_plan_key.process_request()91 self.usage_plan_key.module.exit_json.assert_called_once_with(changed='Mitchell!', usage_plan_key=None)92 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})93 def test_process_request_calls_delete_usage_plan_key_when_state_absent_and_usage_plan_key_found(self, m):94 self.usage_plan_key.module.params = {95 'usage_plan_id': 'upid',96 'api_key_id': 'akid',97 'state': 'absent',98 }99 self.usage_plan_key.process_request()100 self.usage_plan_key.client.delete_usage_plan_key.assert_called_once_with(usagePlanId='upid', keyId='akid')101 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})102 def test_process_request_skips_delete_and_calls_exit_json_with_true_when_check_mode_set_and_auth_found(self, m):103 self.usage_plan_key.module.params = {104 'usage_plan_id': 'upid',105 'api_key_id': 'akid',106 'state': 'absent',107 }108 self.usage_plan_key.module.check_mode = True109 self.usage_plan_key.process_request()110 self.assertEqual(0, self.usage_plan_key.client.delete_usage_plan_key.call_count)111 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key=None)112 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value={'id': 'found'})113 def test_process_request_calls_fail_json_when_delete_usage_plan_key_raises_error(self, m):114 self.usage_plan_key.module.params = {115 'usage_plan_id': 'upid',116 'api_key_id': 'akid',117 'state': 'absent',118 }119 self.usage_plan_key.client.delete_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError)120 self.usage_plan_key.process_request()121 self.usage_plan_key.client.delete_usage_plan_key.assert_called_once_with(usagePlanId='upid', keyId='akid')122 self.usage_plan_key.module.fail_json.assert_called_once_with(123 msg='Error when deleting usage_plan_key via boto3: An unspecified error occurred'124 )125 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)126 def test_process_request_skips_delete_when_usage_plan_key_not_found(self, m):127 self.usage_plan_key.module.params = {128 'usage_plan_id': 'upid',129 'api_key_id': 'akid',130 'state': 'absent',131 }132 self.usage_plan_key.process_request()133 self.assertEqual(0, self.usage_plan_key.client.delete_usage_plan_key.call_count)134 @patch.object(ApiGwUsagePlanKey, '_create_usage_plan_key', return_value=('veins', 'clogging'))135 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)136 def test_process_request_calls_exit_json_with_expected_value_after_successful_create(self, mra, mca):137 self.usage_plan_key.process_request()138 self.usage_plan_key.module.exit_json.assert_called_once_with(changed='veins', usage_plan_key='clogging')139 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)140 def test_process_request_returns_create_usage_plan_key_result_when_create_succeeds(self, m):141 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(return_value='woot')142 self.usage_plan_key.process_request()143 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key='woot')144 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)145 def test_process_request_calls_create_usage_plan_key_when_state_present_and_usage_plan_key_not_found(self, m):146 self.usage_plan_key.process_request()147 self.usage_plan_key.client.create_usage_plan_key.assert_called_once_with(148 usagePlanId='upid',149 keyId='akid',150 keyType='API_KEY'151 )152 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)153 def test_process_request_calls_fail_json_when_create_usage_plan_key_raises_exception(self, m):154 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError())155 self.usage_plan_key.process_request()156 self.usage_plan_key.client.create_usage_plan_key.assert_called_once_with(157 usagePlanId='upid',158 keyId='akid',159 keyType='API_KEY'160 )161 self.usage_plan_key.module.fail_json.assert_called_once_with(162 msg='Error when creating usage_plan_key via boto3: An unspecified error occurred'163 )164 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value=None)165 def test_process_request_skips_create_call_and_returns_changed_True_when_check_mode(self, m):166 self.usage_plan_key.module.check_mode = True167 self.usage_plan_key.process_request()168 self.assertEqual(0, self.usage_plan_key.client.create_usage_plan_key.call_count)169 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=True, usage_plan_key=None)170 @patch.object(ApiGwUsagePlanKey, '_retrieve_usage_plan_key', return_value='something')171 def test_process_request_calls_exit_json_properly_when_state_present_and_key_exists(self, m):172 self.usage_plan_key.client.create_usage_plan_key = mock.MagicMock(side_effect=BotoCoreError())173 self.usage_plan_key.process_request()174 self.usage_plan_key.module.exit_json.assert_called_once_with(changed=False, usage_plan_key='something')175 def test_define_argument_spec(self):176 result = ApiGwUsagePlanKey._define_module_argument_spec()177 self.assertIsInstance(result, dict)178 self.assertEqual(result, dict(179 usage_plan_id=dict(required=True),180 api_key_id=dict(required=True),181 key_type=dict(required=False, default='API_KEY', choices=['API_KEY']),182 state=dict(default='present', choices=['present', 'absent']),183 ))184 @patch.object(apigw_usage_plan_key, 'AnsibleModule')185 @patch.object(apigw_usage_plan_key, 'ApiGwUsagePlanKey')186 def test_main(self, mock_ApiGwUsagePlanKey, mock_AnsibleModule):187 mock_ApiGwUsagePlanKey_instance = mock.MagicMock()188 mock_AnsibleModule_instance = mock.MagicMock()189 mock_ApiGwUsagePlanKey.return_value = mock_ApiGwUsagePlanKey_instance190 mock_AnsibleModule.return_value = mock_AnsibleModule_instance191 apigw_usage_plan_key.main()192 mock_ApiGwUsagePlanKey.assert_called_once_with(mock_AnsibleModule_instance)193 assert mock_ApiGwUsagePlanKey_instance.process_request.call_count == 1194if __name__ == '__main__':...
usage_plan.py
Source: usage_plan.py
...79 usage_plan_id = up['id'] 80 break 81 # Map the API key that was created to the Usage Plan using a 82 # Usage Plan Key83 response = client.create_usage_plan_key( 84 usagePlanId=usage_plan_id, 85 keyId=key_id, 86 keyType="API_KEY" 87 )88 except Exception as e:89 print(e) 90 return response 91def create_api_keys_for_all_users():92 for u in User.objects.all():93 p = Profile.objects.get(user=u)94 # if they don't have local api keys, create those first95 if not p.read_api_key:96 p.read_api_key = generate_random_string(128)97 if not p.cud_api_key: ...
APIKeyCreator.py
Source: APIKeyCreator.py
...10 id = response['id']11 print('API key created ...... id ' + id)12 print(response)13 return id14def create_usage_plan_key(api_key_id,usage_plan_id):15 print('\n Adding key to usage plan .... ID ' + usage_plan_id)16 response = client.create_usage_plan_key(17 usagePlanId=usage_plan_id,18 keyId=api_key_id,19 keyType='API_KEY'20 )21 print('Usage plan created ...')22 print(response)23def create_API_key_and_add_to_usage_plan(key_name, key_description, usage_plan_id):24 api_key_id = get_created_API_key_id(key_name, key_description)25 create_usage_plan_key(api_key_id, usage_plan_id)26 return api_key_id...
Check out the latest blogs from LambdaTest on this topic:
The fact is not alien to us anymore that cross browser testing is imperative to enhance your application’s user experience. Enhanced knowledge of popular and highly acclaimed testing frameworks goes a long way in developing a new app. It holds more significance if you are a full-stack developer or expert programmer.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Having a good web design can empower business and make your brand stand out. According to a survey by Top Design Firms, 50% of users believe that website design is crucial to an organization’s overall brand. Therefore, businesses should prioritize website design to meet customer expectations and build their brand identity. Your website is the face of your business, so it’s important that it’s updated regularly as per the current web design trends.
Enterprise resource planning (ERP) is a form of business process management software—typically a suite of integrated applications—that assists a company in managing its operations, interpreting data, and automating various back-office processes. The introduction of a new ERP system is analogous to the introduction of a new product into the market. If the product is not handled appropriately, it will fail, resulting in significant losses for the business. Most significantly, the employees’ time, effort, and morale would suffer as a result of the procedure.
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!!