How to use create_usage_plan_key method in localstack

Best Python code snippet using localstack_python

test_apigw_usage_plan_key.py

Source:test_apigw_usage_plan_key.py Github

copy

Full Screen

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__':...

Full Screen

Full Screen

usage_plan.py

Source:usage_plan.py Github

copy

Full Screen

...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: ...

Full Screen

Full Screen

APIKeyCreator.py

Source:APIKeyCreator.py Github

copy

Full Screen

...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...

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