test_boto_apigateway.py 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import datetime
  5. import logging
  6. import random
  7. import string
  8. # Import Salt Testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import skipIf, TestCase
  11. from tests.support.mock import MagicMock, patch
  12. # Import Salt libs
  13. import salt.loader
  14. import salt.modules.boto_apigateway as boto_apigateway
  15. from salt.utils.versions import LooseVersion
  16. # Import 3rd-party libs
  17. # pylint: disable=import-error,no-name-in-module
  18. try:
  19. import boto3
  20. import botocore
  21. from botocore.exceptions import ClientError
  22. HAS_BOTO = True
  23. except ImportError:
  24. HAS_BOTO = False
  25. from salt.ext.six.moves import range, zip
  26. # pylint: enable=import-error,no-name-in-module
  27. # the boto_lambda module relies on the connect_to_region() method
  28. # which was added in boto 2.8.0
  29. # https://github.com/boto/boto/commit/33ac26b416fbb48a60602542b4ce15dcc7029f12
  30. required_boto3_version = '1.2.1'
  31. required_botocore_version = '1.4.49'
  32. region = 'us-east-1'
  33. access_key = 'GKTADJGHEIQSXMKKRBJ08H'
  34. secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
  35. conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key, 'profile': {}}
  36. error_message = 'An error occurred (101) when calling the {0} operation: Test-defined error'
  37. error_content = {
  38. 'Error': {
  39. 'Code': 101,
  40. 'Message': "Test-defined error"
  41. }
  42. }
  43. api_key_ret = {
  44. u'description': u'test-lambda-api-key', u'enabled': True,
  45. u'stageKeys': [u'123yd1l123/test'],
  46. u'lastUpdatedDate': datetime.datetime(2015, 11, 4, 19, 22, 18),
  47. u'createdDate': datetime.datetime(2015, 11, 4, 19, 21, 7),
  48. u'id': u'88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  49. u'name': u'test-salt-key',
  50. 'ResponseMetadata': {'HTTPStatusCode': 200,
  51. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  52. api_model_error_schema = u'{"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"},"fields":{"type":"string"}},"definitions":{}}'
  53. api_model_ret = {
  54. u'contentType': u'application/json',
  55. u'name': u'Error',
  56. u'description': u'Error Model',
  57. u'id': u'iltqcb',
  58. u'schema': api_model_error_schema,
  59. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  60. api_resources_ret = {
  61. u'items': [{u'id': u'hhg2t8f4h9',
  62. u'path': u'/'},
  63. {u'id': u'isr8q2',
  64. u'parentId': u'hhg2t8f4h9',
  65. u'path': u'/api',
  66. u'pathPart': u'api'},
  67. {u'id': u'5pvx7w',
  68. u'parentId': 'isr8q2',
  69. u'path': u'/api/users',
  70. u'pathPart': u'users',
  71. u'resourceMethods': {u'OPTIONS': {},
  72. u'POST': {}}}],
  73. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  74. api_create_resource_ret = {
  75. u'id': u'123abc',
  76. u'parentId': u'hhg2t8f4h9',
  77. u'path': u'/api3',
  78. u'pathPart': u'api3',
  79. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  80. usage_plan1 = dict(
  81. id='plan1_id',
  82. name='plan1_name',
  83. description='plan1_desc',
  84. apiStages=[],
  85. throttle=dict(
  86. burstLimit=123,
  87. rateLimit=123.0
  88. ),
  89. quota=dict(
  90. limit=123,
  91. offset=123,
  92. period='DAY'
  93. )
  94. )
  95. usage_plan2 = dict(
  96. id='plan2_id',
  97. name='plan2_name',
  98. description='plan2_desc',
  99. apiStages=[],
  100. throttle=dict(
  101. burstLimit=123,
  102. rateLimit=123.0
  103. ),
  104. quota=dict(
  105. limit=123,
  106. offset=123,
  107. period='DAY'
  108. )
  109. )
  110. usage_plan1b = dict(
  111. id='another_plan1_id',
  112. name='plan1_name',
  113. description='another_plan1_desc',
  114. apiStages=[],
  115. throttle=dict(
  116. burstLimit=123,
  117. rateLimit=123.0
  118. ),
  119. quota=dict(
  120. limit=123,
  121. offset=123,
  122. period='DAY'
  123. )
  124. )
  125. usage_plans_ret = dict(
  126. items=[
  127. usage_plan1, usage_plan2, usage_plan1b
  128. ]
  129. )
  130. log = logging.getLogger(__name__)
  131. def _has_required_boto():
  132. '''
  133. Returns True/False boolean depending on if Boto is installed and correct
  134. version.
  135. '''
  136. if not HAS_BOTO:
  137. return False
  138. elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version):
  139. return False
  140. else:
  141. return True
  142. def _has_required_botocore():
  143. '''
  144. Returns True/False boolean depending on if botocore supports usage plan
  145. '''
  146. if not HAS_BOTO:
  147. return False
  148. elif LooseVersion(botocore.__version__) < LooseVersion(required_botocore_version):
  149. return False
  150. else:
  151. return True
  152. class BotoApiGatewayTestCaseBase(TestCase, LoaderModuleMockMixin):
  153. conn = None
  154. def setup_loader_modules(self):
  155. self.opts = opts = salt.config.DEFAULT_MINION_OPTS
  156. utils = salt.loader.utils(
  157. opts,
  158. whitelist=['boto3', 'args', 'systemd', 'path', 'platform'])
  159. return {
  160. boto_apigateway: {
  161. '__opts__': opts,
  162. '__utils__': utils,
  163. }
  164. }
  165. def setUp(self):
  166. TestCase.setUp(self)
  167. # __virtual__ must be caller in order for _get_conn to be injected
  168. boto_apigateway.__init__(self.opts)
  169. delattr(self, 'opts')
  170. # Set up MagicMock to replace the boto3 session
  171. # connections keep getting cached from prior tests, can't find the
  172. # correct context object to clear it. So randomize the cache key, to prevent any
  173. # cache hits
  174. conn_parameters['key'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(50))
  175. self.patcher = patch('boto3.session.Session')
  176. self.addCleanup(self.patcher.stop)
  177. self.addCleanup(delattr, self, 'patcher')
  178. mock_session = self.patcher.start()
  179. session_instance = mock_session.return_value
  180. self.conn = MagicMock()
  181. session_instance.client.return_value = self.conn
  182. self.addCleanup(delattr, self, 'conn')
  183. class BotoApiGatewayTestCaseMixin(object):
  184. def _diff_list_dicts(self, listdict1, listdict2, sortkey):
  185. '''
  186. Compares the two list of dictionaries to ensure they have same content. Returns True
  187. if there is difference, else False
  188. '''
  189. if len(listdict1) != len(listdict2):
  190. return True
  191. listdict1_sorted = sorted(listdict1, key=lambda x: x[sortkey])
  192. listdict2_sorted = sorted(listdict2, key=lambda x: x[sortkey])
  193. for item1, item2 in zip(listdict1_sorted, listdict2_sorted):
  194. if len(set(item1) & set(item2)) != len(set(item2)):
  195. return True
  196. return False
  197. #@skipIf(True, 'Skip these tests while investigating failures')
  198. @skipIf(HAS_BOTO is False, 'The boto3 module must be installed.')
  199. @skipIf(_has_required_boto() is False,
  200. 'The boto3 module must be greater than'
  201. ' or equal to version {0}'.format(required_boto3_version))
  202. @skipIf(_has_required_botocore() is False,
  203. 'The botocore module must be greater than'
  204. ' or equal to version {0}'.format(required_botocore_version))
  205. class BotoApiGatewayTestCase(BotoApiGatewayTestCaseBase, BotoApiGatewayTestCaseMixin):
  206. '''
  207. TestCase for salt.modules.boto_apigateway module
  208. '''
  209. def test_that_when_checking_if_a_rest_api_exists_and_a_rest_api_exists_the_api_exists_method_returns_true(self):
  210. '''
  211. Tests checking an apigateway rest api existence when api's name exists
  212. '''
  213. self.conn.get_rest_apis.return_value = {'items': [{'name': 'myapi', 'id': '1234def'}]}
  214. api_exists_result = boto_apigateway.api_exists(name='myapi', **conn_parameters)
  215. self.assertTrue(api_exists_result['exists'])
  216. def test_that_when_checking_if_a_rest_api_exists_and_multiple_rest_api_exist_the_api_exists_method_returns_true(self):
  217. '''
  218. Tests checking an apigateway rest api existence when multiple api's with same name exists
  219. '''
  220. self.conn.get_rest_apis.return_value = {'items': [{'name': 'myapi', 'id': '1234abc'},
  221. {'name': 'myapi', 'id': '1234def'}]}
  222. api_exists_result = boto_apigateway.api_exists(name='myapi', **conn_parameters)
  223. self.assertTrue(api_exists_result['exists'])
  224. def test_that_when_checking_if_a_rest_api_exists_and_no_rest_api_exists_the_api_exists_method_returns_false(self):
  225. '''
  226. Tests checking an apigateway rest api existence when no matching rest api name exists
  227. '''
  228. self.conn.get_rest_apis.return_value = {'items': [{'name': 'myapi', 'id': '1234abc'},
  229. {'name': 'myapi', 'id': '1234def'}]}
  230. api_exists_result = boto_apigateway.api_exists(name='myapi123', **conn_parameters)
  231. self.assertFalse(api_exists_result['exists'])
  232. def test_that_when_describing_rest_apis_and_no_name_given_the_describe_apis_method_returns_list_of_all_rest_apis(self):
  233. '''
  234. Tests that all rest apis defined for a region is returned
  235. '''
  236. self.conn.get_rest_apis.return_value = {u'items': [{u'description': u'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification',
  237. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  238. u'id': u'2ut6i4vyle',
  239. u'name': u'Swagger Petstore'},
  240. {u'description': u'testingabcd',
  241. u'createdDate': datetime.datetime(2015, 12, 3, 21, 57, 58),
  242. u'id': u'g41ls77hz0',
  243. u'name': u'testingabc'},
  244. {u'description': u'a simple food delivery service test',
  245. u'createdDate': datetime.datetime(2015, 11, 4, 23, 57, 28),
  246. u'id': u'h7pbwydho9',
  247. u'name': u'Food Delivery Service'},
  248. {u'description': u'Created by AWS Lambda',
  249. u'createdDate': datetime.datetime(2015, 11, 4, 17, 55, 41),
  250. u'id': u'i2yyd1ldvj',
  251. u'name': u'LambdaMicroservice'},
  252. {u'description': u'cloud tap service with combination of API GW and Lambda',
  253. u'createdDate': datetime.datetime(2015, 11, 17, 22, 3, 18),
  254. u'id': u'rm06h9oac4',
  255. u'name': u'API Gateway Cloudtap Service'},
  256. {u'description': u'testing1234',
  257. u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  258. u'id': u'vtir6ssxvd',
  259. u'name': u'testing123'}],
  260. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  261. items = self.conn.get_rest_apis.return_value['items']
  262. get_apis_result = boto_apigateway.describe_apis(**conn_parameters)
  263. items_dt = [boto_apigateway._convert_datetime_str(item) for item in items]
  264. apis = get_apis_result.get('restapi')
  265. diff = self._diff_list_dicts(apis, items_dt, 'id')
  266. self.assertTrue(apis)
  267. self.assertEqual(len(apis), len(items))
  268. self.assertFalse(diff)
  269. def test_that_when_describing_rest_apis_and_name_is_testing123_the_describe_apis_method_returns_list_of_two_rest_apis(self):
  270. '''
  271. Tests that exactly 2 apis are returned matching 'testing123'
  272. '''
  273. self.conn.get_rest_apis.return_value = {u'items': [{u'description': u'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification',
  274. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  275. u'id': u'2ut6i4vyle',
  276. u'name': u'Swagger Petstore'},
  277. {u'description': u'testingabcd',
  278. u'createdDate': datetime.datetime(2015, 12, 3, 21, 57, 58),
  279. u'id': u'g41ls77hz0',
  280. u'name': u'testing123'},
  281. {u'description': u'a simple food delivery service test',
  282. u'createdDate': datetime.datetime(2015, 11, 4, 23, 57, 28),
  283. u'id': u'h7pbwydho9',
  284. u'name': u'Food Delivery Service'},
  285. {u'description': u'Created by AWS Lambda',
  286. u'createdDate': datetime.datetime(2015, 11, 4, 17, 55, 41),
  287. u'id': u'i2yyd1ldvj',
  288. u'name': u'LambdaMicroservice'},
  289. {u'description': u'cloud tap service with combination of API GW and Lambda',
  290. u'createdDate': datetime.datetime(2015, 11, 17, 22, 3, 18),
  291. u'id': u'rm06h9oac4',
  292. u'name': u'API Gateway Cloudtap Service'},
  293. {u'description': u'testing1234',
  294. u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  295. u'id': u'vtir6ssxvd',
  296. u'name': u'testing123'}],
  297. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  298. expected_items = [{u'description': u'testingabcd', u'createdDate': datetime.datetime(2015, 12, 3, 21, 57, 58),
  299. u'id': u'g41ls77hz0', u'name': u'testing123'},
  300. {u'description': u'testing1234', u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  301. u'id': u'vtir6ssxvd', u'name': u'testing123'}]
  302. get_apis_result = boto_apigateway.describe_apis(name='testing123', **conn_parameters)
  303. expected_items_dt = [boto_apigateway._convert_datetime_str(item) for item in expected_items]
  304. apis = get_apis_result.get('restapi')
  305. diff = self._diff_list_dicts(apis, expected_items_dt, 'id')
  306. self.assertTrue(apis)
  307. self.assertIs(diff, False)
  308. def test_that_when_describing_rest_apis_and_name_is_testing123_the_describe_apis_method_returns_no_matching_items(self):
  309. '''
  310. Tests that no apis are returned matching 'testing123'
  311. '''
  312. self.conn.get_rest_apis.return_value = {u'items': [{u'description': u'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification',
  313. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  314. u'id': u'2ut6i4vyle',
  315. u'name': u'Swagger Petstore'},
  316. {u'description': u'a simple food delivery service test',
  317. u'createdDate': datetime.datetime(2015, 11, 4, 23, 57, 28),
  318. u'id': u'h7pbwydho9',
  319. u'name': u'Food Delivery Service'},
  320. {u'description': u'Created by AWS Lambda',
  321. u'createdDate': datetime.datetime(2015, 11, 4, 17, 55, 41),
  322. u'id': u'i2yyd1ldvj',
  323. u'name': u'LambdaMicroservice'},
  324. {u'description': u'cloud tap service with combination of API GW and Lambda',
  325. u'createdDate': datetime.datetime(2015, 11, 17, 22, 3, 18),
  326. u'id': u'rm06h9oac4',
  327. u'name': u'API Gateway Cloudtap Service'}],
  328. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  329. get_apis_result = boto_apigateway.describe_apis(name='testing123', **conn_parameters)
  330. apis = get_apis_result.get('restapi')
  331. self.assertFalse(apis)
  332. def test_that_when_creating_a_rest_api_succeeds_the_create_api_method_returns_true(self):
  333. '''
  334. test True if rest api is created
  335. '''
  336. created_date = datetime.datetime.now()
  337. assigned_api_id = 'created_api_id'
  338. self.conn.create_rest_api.return_value = {u'description': u'unit-testing1234',
  339. u'createdDate': created_date,
  340. u'id': assigned_api_id,
  341. u'name': u'unit-testing123',
  342. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  343. create_api_result = boto_apigateway.create_api(name='unit-testing123', description='unit-testing1234', **conn_parameters)
  344. api = create_api_result.get('restapi')
  345. self.assertTrue(create_api_result.get('created'))
  346. self.assertTrue(api)
  347. self.assertEqual(api['id'], assigned_api_id)
  348. self.assertEqual(api['createdDate'], '{0}'.format(created_date))
  349. self.assertEqual(api['name'], 'unit-testing123')
  350. self.assertEqual(api['description'], 'unit-testing1234')
  351. def test_that_when_creating_a_rest_api_fails_the_create_api_method_returns_error(self):
  352. '''
  353. test True for rest api creation error.
  354. '''
  355. self.conn.create_rest_api.side_effect = ClientError(error_content, 'create_rest_api')
  356. create_api_result = boto_apigateway.create_api(name='unit-testing123', description='unit-testing1234', **conn_parameters)
  357. api = create_api_result.get('restapi')
  358. self.assertEqual(create_api_result.get('error').get('message'), error_message.format('create_rest_api'))
  359. def test_that_when_deleting_rest_apis_and_name_is_testing123_matching_two_apis_the_delete_api_method_returns_delete_count_of_two(self):
  360. '''
  361. test True if the deleted count for "testing123" api is 2.
  362. '''
  363. self.conn.get_rest_apis.return_value = {u'items': [{u'description': u'A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification',
  364. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  365. u'id': u'2ut6i4vyle',
  366. u'name': u'Swagger Petstore'},
  367. {u'description': u'testingabcd',
  368. u'createdDate': datetime.datetime(2015, 12, 3, 21, 57, 58),
  369. u'id': u'g41ls77hz0',
  370. u'name': u'testing123'},
  371. {u'description': u'a simple food delivery service test',
  372. u'createdDate': datetime.datetime(2015, 11, 4, 23, 57, 28),
  373. u'id': u'h7pbwydho9',
  374. u'name': u'Food Delivery Service'},
  375. {u'description': u'Created by AWS Lambda',
  376. u'createdDate': datetime.datetime(2015, 11, 4, 17, 55, 41),
  377. u'id': u'i2yyd1ldvj',
  378. u'name': u'LambdaMicroservice'},
  379. {u'description': u'cloud tap service with combination of API GW and Lambda',
  380. u'createdDate': datetime.datetime(2015, 11, 17, 22, 3, 18),
  381. u'id': u'rm06h9oac4',
  382. u'name': u'API Gateway Cloudtap Service'},
  383. {u'description': u'testing1234',
  384. u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  385. u'id': u'vtir6ssxvd',
  386. u'name': u'testing123'}],
  387. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  388. self.conn.delete_rest_api.return_value = None
  389. delete_api_result = boto_apigateway.delete_api(name='testing123', **conn_parameters)
  390. self.assertTrue(delete_api_result.get('deleted'))
  391. self.assertEqual(delete_api_result.get('count'), 2)
  392. def test_that_when_deleting_rest_apis_and_name_given_provides_no_match_the_delete_api_method_returns_false(self):
  393. '''
  394. Test that the given api name doesn't exists, and delete_api should return deleted status of False
  395. '''
  396. self.conn.get_rest_apis.return_value = {u'items': [{u'description': u'testing1234',
  397. u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  398. u'id': u'vtir6ssxvd',
  399. u'name': u'testing1234'}],
  400. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  401. self.conn.delete_rest_api.return_value = None
  402. delete_api_result = boto_apigateway.delete_api(name='testing123', **conn_parameters)
  403. self.assertFalse(delete_api_result.get('deleted'))
  404. def test_that_describing_api_keys_the_describe_api_keys_method_returns_all_api_keys(self):
  405. '''
  406. tests True if all api_keys are returned.
  407. '''
  408. self.conn.get_api_keys.return_value = {
  409. u'items': [{u'description': u'test-lambda-api-key', u'enabled': True,
  410. u'stageKeys': [u'123yd1l123/test'],
  411. u'lastUpdatedDate': datetime.datetime(2015, 11, 4, 19, 22, 18),
  412. u'createdDate': datetime.datetime(2015, 11, 4, 19, 21, 7),
  413. u'id': u'88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  414. u'name': u'test-salt-key'},
  415. {u'description': u'testing_salt_123', u'enabled': True,
  416. u'stageKeys': [],
  417. u'lastUpdatedDate': datetime.datetime(2015, 12, 5, 0, 14, 49),
  418. u'createdDate': datetime.datetime(2015, 12, 4, 22, 29, 33),
  419. u'id': u'999999989b8cNSp4505pL6OgDe3oW7oY29Z3eIZ4',
  420. u'name': u'testing_salt'}],
  421. 'ResponseMetadata': {'HTTPStatusCode': 200,
  422. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  423. items = self.conn.get_api_keys.return_value['items']
  424. get_api_keys_result = boto_apigateway.describe_api_keys(**conn_parameters)
  425. items_dt = [boto_apigateway._convert_datetime_str(item) for item in items]
  426. api_keys = get_api_keys_result.get('apiKeys')
  427. diff = False
  428. if len(api_keys) != len(items):
  429. diff = True
  430. else:
  431. # compare individual items.
  432. diff = self._diff_list_dicts(api_keys, items_dt, 'id')
  433. self.assertTrue(api_keys)
  434. self.assertIs(diff, False)
  435. def test_that_describing_api_keys_fails_the_desribe_api_keys_method_returns_error(self):
  436. '''
  437. test True for describe api keys error.
  438. '''
  439. self.conn.get_api_keys.side_effect = ClientError(error_content, 'get_api_keys')
  440. result = boto_apigateway.describe_api_keys(**conn_parameters)
  441. self.assertEqual(result.get('error', {}).get('message'), error_message.format('get_api_keys'))
  442. def test_that_describing_an_api_key_the_describe_api_key_method_returns_matching_api_key(self):
  443. '''
  444. tests True if the key is found.
  445. '''
  446. self.conn.get_api_key.return_value = api_key_ret
  447. result = boto_apigateway.describe_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  448. **conn_parameters)
  449. self.assertEqual(result.get('apiKey', {}).get('id'), self.conn.get_api_key.return_value.get('id'))
  450. def test_that_describing_an_api_key_that_does_not_exists_the_desribe_api_key_method_returns_error(self):
  451. '''
  452. test True for error being thrown.
  453. '''
  454. self.conn.get_api_key.side_effect = ClientError(error_content, 'get_api_keys')
  455. result = boto_apigateway.describe_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  456. **conn_parameters)
  457. self.assertEqual(result.get('error', {}).get('message'),
  458. error_message.format('get_api_keys'))
  459. def test_that_when_creating_an_api_key_succeeds_the_create_api_key_method_returns_true(self):
  460. '''
  461. tests that we can successfully create an api key and the createDat and lastUpdateDate are
  462. converted to string
  463. '''
  464. now = datetime.datetime.now()
  465. self.conn.create_api_key.return_value = {
  466. u'description': u'test-lambda-api-key', u'enabled': True,
  467. u'stageKeys': [u'123yd1l123/test'],
  468. u'lastUpdatedDate': now,
  469. u'createdDate': now,
  470. u'id': u'88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  471. u'name': u'test-salt-key',
  472. 'ResponseMetadata': {'HTTPStatusCode': 200,
  473. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  474. create_api_key_result = boto_apigateway.create_api_key('test-salt-key', 'test-lambda-api-key', **conn_parameters)
  475. api_key = create_api_key_result.get('apiKey')
  476. now_str = '{0}'.format(now)
  477. self.assertTrue(create_api_key_result.get('created'))
  478. self.assertEqual(api_key.get('lastUpdatedDate'), now_str)
  479. self.assertEqual(api_key.get('createdDate'), now_str)
  480. def test_that_when_creating_an_api_key_fails_the_create_api_key_method_returns_error(self):
  481. '''
  482. tests that we properly handle errors when create an api key fails.
  483. '''
  484. self.conn.create_api_key.side_effect = ClientError(error_content, 'create_api_key')
  485. create_api_key_result = boto_apigateway.create_api_key('test-salt-key', 'unit-testing1234')
  486. api_key = create_api_key_result.get('apiKey')
  487. self.assertFalse(api_key)
  488. self.assertIs(create_api_key_result.get('created'), False)
  489. self.assertEqual(create_api_key_result.get('error').get('message'), error_message.format('create_api_key'))
  490. def test_that_when_deleting_an_api_key_that_exists_the_delete_api_key_method_returns_true(self):
  491. '''
  492. test True if the api key is successfully deleted.
  493. '''
  494. self.conn.delete_api_key.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  495. result = boto_apigateway.delete_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2', **conn_parameters)
  496. self.assertTrue(result.get('deleted'))
  497. def test_that_when_deleting_an_api_key_that_does_not_exist_the_delete_api_key_method_returns_false(self):
  498. '''
  499. Test that the given api key doesn't exists, and delete_api_key should return deleted status of False
  500. '''
  501. self.conn.delete_api_key.side_effect = ClientError(error_content, 'delete_api_key')
  502. result = boto_apigateway.delete_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2', **conn_parameters)
  503. self.assertFalse(result.get('deleted'))
  504. def test_that_when_updating_an_api_key_description_successfully_the_update_api_key_description_method_returns_true(self):
  505. '''
  506. Test True if api key descriptipn update is successful
  507. '''
  508. self.conn.update_api_key.return_value = api_key_ret
  509. result = boto_apigateway.update_api_key_description(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  510. description='test-lambda-api-key', **conn_parameters)
  511. self.assertTrue(result.get('updated'))
  512. def test_that_when_updating_an_api_key_description_for_a_key_that_does_not_exist_the_update_api_key_description_method_returns_false(self):
  513. '''
  514. Test False if api key doesn't exists for the update request
  515. '''
  516. self.conn.update_api_key.side_effect = ClientError(error_content, 'update_api_key')
  517. result = boto_apigateway.update_api_key_description(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  518. description='test-lambda-api-key', **conn_parameters)
  519. self.assertFalse(result.get('updated'))
  520. def test_that_when_enabling_an_api_key_that_exists_the_enable_api_key_method_returns_api_key(self):
  521. '''
  522. Test True for the status of the enabled flag of the returned api key
  523. '''
  524. self.conn.update_api_key.return_value = api_key_ret
  525. result = boto_apigateway.enable_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  526. **conn_parameters)
  527. self.assertTrue(result.get('apiKey', {}).get('enabled'))
  528. def test_that_when_enabling_an_api_key_that_does_not_exist_the_enable_api_key_method_returns_error(self):
  529. '''
  530. Test Equality of the returned value of 'erorr'
  531. '''
  532. self.conn.update_api_key.side_effect = ClientError(error_content, 'update_api_key')
  533. result = boto_apigateway.enable_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  534. **conn_parameters)
  535. self.assertEqual(result.get('error').get('message'), error_message.format('update_api_key'))
  536. def test_that_when_disabling_an_api_key_that_exists_the_disable_api_key_method_returns_api_key(self):
  537. '''
  538. Test False for the status of the enabled flag of the returned api key
  539. '''
  540. self.conn.update_api_key.return_value = api_key_ret.copy()
  541. self.conn.update_api_key.return_value['enabled'] = False
  542. result = boto_apigateway.disable_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  543. **conn_parameters)
  544. self.assertFalse(result.get('apiKey', {}).get('enabled'))
  545. def test_that_when_disabling_an_api_key_that_does_not_exist_the_disable_api_key_method_returns_error(self):
  546. '''
  547. Test Equality of the returned value of 'erorr'
  548. '''
  549. self.conn.update_api_key.side_effect = ClientError(error_content, 'update_api_key')
  550. result = boto_apigateway.disable_api_key(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  551. **conn_parameters)
  552. self.assertEqual(result.get('error').get('message'), error_message.format('update_api_key'))
  553. def test_that_when_associating_stages_to_an_api_key_that_exists_the_associate_api_key_stagekeys_method_returns_true(self):
  554. '''
  555. Test True for returned value of 'associated'
  556. '''
  557. self.conn.update_api_key.retuen_value = api_key_ret
  558. result = boto_apigateway.associate_api_key_stagekeys(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  559. stagekeyslist=[u'123yd1l123/test'],
  560. **conn_parameters)
  561. self.assertTrue(result.get('associated'))
  562. def test_that_when_associating_stages_to_an_api_key_that_does_not_exist_the_associate_api_key_stagekeys_method_returns_false(self):
  563. '''
  564. Test False returned value of 'associated'
  565. '''
  566. self.conn.update_api_key.side_effect = ClientError(error_content, 'update_api_key')
  567. result = boto_apigateway.associate_api_key_stagekeys(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  568. stagekeyslist=[u'123yd1l123/test'],
  569. **conn_parameters)
  570. self.assertFalse(result.get('associated'))
  571. def test_that_when_disassociating_stages_to_an_api_key_that_exists_the_disassociate_api_key_stagekeys_method_returns_true(self):
  572. '''
  573. Test True for returned value of 'associated'
  574. '''
  575. self.conn.update_api_key.retuen_value = None
  576. result = boto_apigateway.disassociate_api_key_stagekeys(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  577. stagekeyslist=[u'123yd1l123/test'],
  578. **conn_parameters)
  579. self.assertTrue(result.get('disassociated'))
  580. def test_that_when_disassociating_stages_to_an_api_key_that_does_not_exist_the_disassociate_api_key_stagekeys_method_returns_false(self):
  581. '''
  582. Test False returned value of 'associated'
  583. '''
  584. self.conn.update_api_key.side_effect = ClientError(error_content, 'update_api_key')
  585. result = boto_apigateway.disassociate_api_key_stagekeys(apiKey='88883333amaa1ZMVGCoLeaTrQk8kzOC36vCgRcT2',
  586. stagekeyslist=[u'123yd1l123/test'],
  587. **conn_parameters)
  588. self.assertFalse(result.get('disassociated'))
  589. def test_that_when_describing_api_deployments_the_describe_api_deployments_method_returns_list_of_deployments(self):
  590. '''
  591. Test Equality for number of deployments is 2
  592. '''
  593. self.conn.get_deployments.return_value = {u'items': [{u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  594. u'id': u'n05smo'},
  595. {u'createdDate': datetime.datetime(2015, 12, 2, 19, 51, 44),
  596. u'id': u'n05sm1'}],
  597. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  598. result = boto_apigateway.describe_api_deployments(restApiId='rm06h9oac4', **conn_parameters)
  599. self.assertEqual(len(result.get('deployments', {})), 2)
  600. def test_that_when_describing_api_deployments_and_an_error_occurred_the_describe_api_deployments_method_returns_error(self):
  601. '''
  602. Test Equality of error returned
  603. '''
  604. self.conn.get_deployments.side_effect = ClientError(error_content, 'get_deployments')
  605. result = boto_apigateway.describe_api_deployments(restApiId='rm06h9oac4', **conn_parameters)
  606. self.assertEqual(result.get('error').get('message'), error_message.format('get_deployments'))
  607. def test_that_when_describing_an_api_deployment_the_describe_api_deployment_method_returns_the_deployment(self):
  608. '''
  609. Test True for the returned deployment
  610. '''
  611. self.conn.get_deployment.return_value = {u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  612. u'id': u'n05smo',
  613. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  614. result = boto_apigateway.describe_api_deployment(restApiId='rm06h9oac4', deploymentId='n05smo', **conn_parameters)
  615. self.assertTrue(result.get('deployment'))
  616. def test_that_when_describing_api_deployment_that_does_not_exist_the_describe_api_deployment_method_returns_error(self):
  617. '''
  618. Test Equality of error returned
  619. '''
  620. self.conn.get_deployment.side_effect = ClientError(error_content, 'get_deployment')
  621. result = boto_apigateway.describe_api_deployment(restApiId='rm06h9oac4', deploymentId='n05smo', **conn_parameters)
  622. self.assertEqual(result.get('error').get('message'), error_message.format('get_deployment'))
  623. def test_that_when_activating_api_deployment_for_stage_and_deployment_that_exist_the_activate_api_deployment_method_returns_true(self):
  624. '''
  625. Test True for value of 'set'
  626. '''
  627. self.conn.update_stage.return_value = {u'cacheClusterEnabled': False,
  628. u'cacheClusterStatus': 'NOT_AVAAILABLE',
  629. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  630. u'deploymentId': 'n05smo',
  631. u'description': 'test',
  632. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  633. u'stageName': 'test',
  634. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  635. result = boto_apigateway.activate_api_deployment(restApiId='rm06h9oac4', stageName='test', deploymentId='n05smo',
  636. **conn_parameters)
  637. self.assertTrue(result.get('set'))
  638. def test_that_when_activating_api_deployment_for_stage_that_does_not_exist_the_activate_api_deployment_method_returns_false(self):
  639. '''
  640. Test False for value of 'set'
  641. '''
  642. self.conn.update_stage.side_effect = ClientError(error_content, 'update_stage')
  643. result = boto_apigateway.activate_api_deployment(restApiId='rm06h9oac4', stageName='test', deploymentId='n05smo',
  644. **conn_parameters)
  645. self.assertFalse(result.get('set'))
  646. def test_that_when_creating_an_api_deployment_succeeds_the_create_api_deployment_method_returns_true(self):
  647. '''
  648. tests that we can successfully create an api deployment and the createDate is
  649. converted to string
  650. '''
  651. now = datetime.datetime.now()
  652. self.conn.create_deployment.return_value = {
  653. u'description': u'test-lambda-api-key',
  654. u'id': 'n05smo',
  655. u'createdDate': now,
  656. 'ResponseMetadata': {'HTTPStatusCode': 200,
  657. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  658. result = boto_apigateway.create_api_deployment(restApiId='rm06h9oac4', stageName='test', **conn_parameters)
  659. deployment = result.get('deployment')
  660. now_str = '{0}'.format(now)
  661. self.assertTrue(result.get('created'))
  662. self.assertEqual(deployment.get('createdDate'), now_str)
  663. def test_that_when_creating_an_deployment_fails_the_create_api_deployment_method_returns_error(self):
  664. '''
  665. tests that we properly handle errors when create an api deployment fails.
  666. '''
  667. self.conn.create_deployment.side_effect = ClientError(error_content, 'create_deployment')
  668. result = boto_apigateway.create_api_deployment(restApiId='rm06h9oac4', stageName='test', **conn_parameters)
  669. self.assertIs(result.get('created'), False)
  670. self.assertEqual(result.get('error').get('message'), error_message.format('create_deployment'))
  671. def test_that_when_deleting_an_api_deployment_that_exists_the_delete_api_deployment_method_returns_true(self):
  672. '''
  673. test True if the api deployment is successfully deleted.
  674. '''
  675. self.conn.delete_deployment.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  676. result = boto_apigateway.delete_api_deployment(restApiId='rm06h9oac4', deploymentId='n05smo', **conn_parameters)
  677. self.assertTrue(result.get('deleted'))
  678. def test_that_when_deleting_an_api_deployment_that_does_not_exist_the_delete_api_deployment_method_returns_false(self):
  679. '''
  680. Test that the given api deployment doesn't exists, and delete_api_deployment should return deleted status of False
  681. '''
  682. self.conn.delete_deployment.side_effect = ClientError(error_content, 'delete_deployment')
  683. result = boto_apigateway.delete_api_deployment(restApiId='rm06h9oac4', deploymentId='n05smo1', **conn_parameters)
  684. self.assertFalse(result.get('deleted'))
  685. def test_that_when_describing_api_stages_the_describe_api_stages_method_returns_list_of_stages(self):
  686. '''
  687. Test Equality for number of stages for the given deployment is 2
  688. '''
  689. self.conn.get_stages.return_value = {u'item': [{u'cacheClusterEnabled': False,
  690. u'cacheClusterStatus': 'NOT_AVAILABLE',
  691. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  692. u'deploymentId': u'n05smo',
  693. u'description': u'test',
  694. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  695. u'stageName': u'test'},
  696. {u'cacheClusterEnabled': False,
  697. u'cacheClusterStatus': 'NOT_AVAILABLE',
  698. u'createdDate': datetime.datetime(2015, 12, 17, 16, 33, 50),
  699. u'deploymentId': u'n05smo',
  700. u'description': u'dev',
  701. u'lastUpdatedDate': datetime.datetime(2015, 12, 17, 16, 33, 50),
  702. u'stageName': u'dev'}],
  703. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  704. result = boto_apigateway.describe_api_stages(restApiId='rm06h9oac4', deploymentId='n05smo', **conn_parameters)
  705. self.assertEqual(len(result.get('stages', {})), 2)
  706. def test_that_when_describing_api_stages_and_that_the_deployment_does_not_exist_the_describe_api_stages_method_returns_error(self):
  707. '''
  708. Test Equality of error returned
  709. '''
  710. self.conn.get_stages.side_effect = ClientError(error_content, 'get_stages')
  711. result = boto_apigateway.describe_api_stages(restApiId='rm06h9oac4', deploymentId='n05smo', **conn_parameters)
  712. self.assertEqual(result.get('error').get('message'), error_message.format('get_stages'))
  713. def test_that_when_describing_an_api_stage_the_describe_api_stage_method_returns_the_stage(self):
  714. '''
  715. Test True for the returned stage
  716. '''
  717. self.conn.get_stage.return_value = {u'cacheClusterEnabled': False,
  718. u'cacheClusterStatus': 'NOT_AVAILABLE',
  719. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  720. u'deploymentId': u'n05smo',
  721. u'description': u'test',
  722. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  723. u'stageName': u'test',
  724. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  725. result = boto_apigateway.describe_api_stage(restApiId='rm06h9oac4', stageName='test', **conn_parameters)
  726. self.assertTrue(result.get('stage'))
  727. def test_that_when_describing_api_stage_that_does_not_exist_the_describe_api_stage_method_returns_error(self):
  728. '''
  729. Test Equality of error returned
  730. '''
  731. self.conn.get_stage.side_effect = ClientError(error_content, 'get_stage')
  732. result = boto_apigateway.describe_api_stage(restApiId='rm06h9oac4', stageName='no_such_stage', **conn_parameters)
  733. self.assertEqual(result.get('error').get('message'), error_message.format('get_stage'))
  734. def test_that_when_overwriting_stage_variables_to_an_existing_stage_the_overwrite_api_stage_variables_method_returns_the_updated_stage(self):
  735. '''
  736. Test True for the returned stage
  737. '''
  738. self.conn.get_stage.return_value = {u'cacheClusterEnabled': False,
  739. u'cacheClusterStatus': 'NOT_AVAILABLE',
  740. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  741. u'deploymentId': u'n05smo',
  742. u'description': u'test',
  743. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  744. u'stageName': u'test',
  745. u'variables': {'key1': 'val1'},
  746. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  747. self.conn.update_stage.return_value = {u'cacheClusterEnabled': False,
  748. u'cacheClusterStatus': 'NOT_AVAILABLE',
  749. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  750. u'deploymentId': u'n05smo',
  751. u'description': u'test',
  752. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  753. u'stageName': u'test',
  754. u'variables': {'key1': 'val2'},
  755. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  756. result = boto_apigateway.overwrite_api_stage_variables(restApiId='rm06h9oac4', stageName='test',
  757. variables=dict(key1='val2'), **conn_parameters)
  758. self.assertEqual(result.get('stage').get('variables').get('key1'), 'val2')
  759. def test_that_when_overwriting_stage_variables_to_a_nonexisting_stage_the_overwrite_api_stage_variables_method_returns_error(self):
  760. '''
  761. Test Equality of error returned
  762. '''
  763. self.conn.get_stage.side_effect = ClientError(error_content, 'get_stage')
  764. result = boto_apigateway.overwrite_api_stage_variables(restApiId='rm06h9oac4', stageName='no_such_stage',
  765. variables=dict(key1="val1", key2="val2"), **conn_parameters)
  766. self.assertEqual(result.get('error').get('message'), error_message.format('get_stage'))
  767. def test_that_when_overwriting_stage_variables_to_an_existing_stage_the_overwrite_api_stage_variables_method_returns_error(self):
  768. '''
  769. Test Equality of error returned due to update_stage
  770. '''
  771. self.conn.get_stage.return_value = {u'cacheClusterEnabled': False,
  772. u'cacheClusterStatus': 'NOT_AVAILABLE',
  773. u'createdDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  774. u'deploymentId': u'n05smo',
  775. u'description': u'test',
  776. u'lastUpdatedDate': datetime.datetime(2015, 11, 17, 16, 33, 50),
  777. u'stageName': u'test',
  778. u'variables': {'key1': 'val1'},
  779. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  780. self.conn.update_stage.side_effect = ClientError(error_content, 'update_stage')
  781. result = boto_apigateway.overwrite_api_stage_variables(restApiId='rm06h9oac4', stageName='test',
  782. variables=dict(key1='val2'), **conn_parameters)
  783. self.assertEqual(result.get('error').get('message'), error_message.format('update_stage'))
  784. def test_that_when_creating_an_api_stage_succeeds_the_create_api_stage_method_returns_true(self):
  785. '''
  786. tests that we can successfully create an api stage and the createDate is
  787. converted to string
  788. '''
  789. now = datetime.datetime.now()
  790. self.conn.create_stage.return_value = {u'cacheClusterEnabled': False,
  791. u'cacheClusterStatus': 'NOT_AVAILABLE',
  792. u'createdDate': now,
  793. u'deploymentId': u'n05smo',
  794. u'description': u'test',
  795. u'lastUpdatedDate': now,
  796. u'stageName': u'test',
  797. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  798. result = boto_apigateway.create_api_stage(restApiId='rm06h9oac4', stageName='test', deploymentId='n05smo',
  799. **conn_parameters)
  800. stage = result.get('stage')
  801. now_str = '{0}'.format(now)
  802. self.assertIs(result.get('created'), True)
  803. self.assertEqual(stage.get('createdDate'), now_str)
  804. self.assertEqual(stage.get('lastUpdatedDate'), now_str)
  805. def test_that_when_creating_an_api_stage_fails_the_create_api_stage_method_returns_error(self):
  806. '''
  807. tests that we properly handle errors when create an api stage fails.
  808. '''
  809. self.conn.create_stage.side_effect = ClientError(error_content, 'create_stage')
  810. result = boto_apigateway.create_api_stage(restApiId='rm06h9oac4', stageName='test', deploymentId='n05smo',
  811. **conn_parameters)
  812. self.assertIs(result.get('created'), False)
  813. self.assertEqual(result.get('error').get('message'), error_message.format('create_stage'))
  814. def test_that_when_deleting_an_api_stage_that_exists_the_delete_api_stage_method_returns_true(self):
  815. '''
  816. test True if the api stage is successfully deleted.
  817. '''
  818. self.conn.delete_stage.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  819. result = boto_apigateway.delete_api_stage(restApiId='rm06h9oac4', stageName='test', **conn_parameters)
  820. self.assertTrue(result.get('deleted'))
  821. def test_that_when_deleting_an_api_stage_that_does_not_exist_the_delete_api_stage_method_returns_false(self):
  822. '''
  823. Test that the given api stage doesn't exists, and delete_api_stage should return deleted status of False
  824. '''
  825. self.conn.delete_stage.side_effect = ClientError(error_content, 'delete_stage')
  826. result = boto_apigateway.delete_api_stage(restApiId='rm06h9oac4', stageName='no_such_stage', **conn_parameters)
  827. self.assertFalse(result.get('deleted'))
  828. def test_that_when_flushing_api_stage_cache_for_an_existing_stage_the_flush_api_stage_cache_method_returns_true(self):
  829. '''
  830. Test True for 'flushed'
  831. '''
  832. self.conn.flush_stage_cache.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  833. result = boto_apigateway.flush_api_stage_cache(restApiId='rm06h9oac4', stageName='no_such_stage', **conn_parameters)
  834. self.assertTrue(result.get('flushed'))
  835. def test_that_when_flushing_api_stage_cache_and_the_stage_does_not_exist_the_flush_api_stage_cache_method_returns_false(self):
  836. '''
  837. Test False for 'flushed'
  838. '''
  839. self.conn.flush_stage_cache.side_effect = ClientError(error_content, 'flush_stage_cache')
  840. result = boto_apigateway.flush_api_stage_cache(restApiId='rm06h9oac4', stageName='no_such_stage', **conn_parameters)
  841. self.assertFalse(result.get('flushed'))
  842. def test_that_when_describing_api_models_the_describe_api_models_method_returns_list_of_models(self):
  843. '''
  844. Test Equality for number of models for the given api is 2
  845. '''
  846. self.conn.get_models.return_value = {u'items': [{u'contentType': u'application/json',
  847. u'name': u'Error',
  848. u'description': u'Error Model',
  849. u'id': u'iltqcb',
  850. u'schema': u'{"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"},"fields":{"type":"string"}},"definitions":{}}'},
  851. {u'contentType': u'application/json',
  852. u'name': u'User',
  853. u'description': u'User Model',
  854. u'id': u'iltqcc',
  855. u'schema': u'{"properties":{"username":{"type":"string","description":"A unique username for the user"},"password":{"type":"string","description":"A password for the new user"}},"definitions":{}}'}],
  856. 'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  857. result = boto_apigateway.describe_api_models(restApiId='rm06h9oac4', **conn_parameters)
  858. self.assertEqual(len(result.get('models', {})), 2)
  859. def test_that_when_describing_api_models_and_that_the_api_does_not_exist_the_describe_api_models_method_returns_error(self):
  860. '''
  861. Test Equality of error returned
  862. '''
  863. self.conn.get_models.side_effect = ClientError(error_content, 'get_models')
  864. result = boto_apigateway.describe_api_models(restApiId='rm06h9oac4', **conn_parameters)
  865. self.assertEqual(result.get('error').get('message'), error_message.format('get_models'))
  866. def test_that_when_describing_api_model_the_describe_api_model_method_returns_the_model(self):
  867. '''
  868. Test True for the returned stage
  869. '''
  870. self.conn.get_model.return_value = api_model_ret
  871. result = boto_apigateway.describe_api_model(restApiId='rm06h9oac4', modelName='Error', **conn_parameters)
  872. self.assertTrue(result.get('model'))
  873. def test_that_when_describing_api_model_and_that_the_model_does_not_exist_the_describe_api_model_method_returns_error(self):
  874. '''
  875. Test Equality of error returned
  876. '''
  877. self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
  878. result = boto_apigateway.describe_api_model(restApiId='rm06h9oac4', modelName='Error', **conn_parameters)
  879. self.assertEqual(result.get('error').get('message'), error_message.format('get_model'))
  880. def test_that_model_exists_the_api_model_exists_method_returns_true(self):
  881. '''
  882. Tests True when model exists
  883. '''
  884. self.conn.get_model.return_value = api_model_ret
  885. result = boto_apigateway.api_model_exists(restApiId='rm06h9oac4', modelName='Error', **conn_parameters)
  886. self.assertTrue(result.get('exists'))
  887. def test_that_model_does_not_exists_the_api_model_exists_method_returns_false(self):
  888. '''
  889. Tests False when model does not exist
  890. '''
  891. self.conn.get_model.side_effect = ClientError(error_content, 'get_model')
  892. result = boto_apigateway.api_model_exists(restApiId='rm06h9oac4', modelName='Error', **conn_parameters)
  893. self.assertFalse(result.get('exists'))
  894. def test_that_updating_model_schema_the_update_api_model_schema_method_returns_true(self):
  895. '''
  896. Tests True when model schema is updated.
  897. '''
  898. self.conn.update_model.return_value = api_model_ret
  899. result = boto_apigateway.update_api_model_schema(restApiId='rm06h9oac4', modelName='Error',
  900. schema=api_model_error_schema, **conn_parameters)
  901. self.assertTrue(result.get('updated'))
  902. def test_that_updating_model_schema_when_model_does_not_exist_the_update_api_model_schema_emthod_returns_false(self):
  903. '''
  904. Tests False when model schema is not upated.
  905. '''
  906. self.conn.update_model.side_effect = ClientError(error_content, 'update_model')
  907. result = boto_apigateway.update_api_model_schema(restApiId='rm06h9oac4', modelName='no_such_model',
  908. schema=api_model_error_schema, **conn_parameters)
  909. self.assertFalse(result.get('updated'))
  910. def test_that_when_creating_an_api_model_succeeds_the_create_api_model_method_returns_true(self):
  911. '''
  912. tests that we can successfully create an api model
  913. '''
  914. self.conn.create_model.return_value = api_model_ret
  915. result = boto_apigateway.create_api_model(restApiId='rm06h9oac4', modelName='Error',
  916. modelDescription='Error Model', schema=api_model_error_schema,
  917. **conn_parameters)
  918. self.assertTrue(result.get('created'))
  919. def test_that_when_creating_an_api_model_fails_the_create_api_model_method_returns_error(self):
  920. '''
  921. tests that we properly handle errors when create an api model fails.
  922. '''
  923. self.conn.create_model.side_effect = ClientError(error_content, 'create_model')
  924. result = boto_apigateway.create_api_model(restApiId='rm06h9oac4', modelName='Error',
  925. modelDescription='Error Model', schema=api_model_error_schema,
  926. **conn_parameters)
  927. self.assertFalse(result.get('created'))
  928. def test_that_when_deleting_an_api_model_that_exists_the_delete_api_model_method_returns_true(self):
  929. '''
  930. test True if the api model is successfully deleted.
  931. '''
  932. self.conn.delete_model.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  933. result = boto_apigateway.delete_api_model(restApiId='rm06h9oac4', modelName='Error', **conn_parameters)
  934. self.assertTrue(result.get('deleted'))
  935. def test_that_when_deleting_an_api_model_that_does_not_exist_the_delete_api_model_method_returns_false(self):
  936. '''
  937. Test that the given api model doesn't exists, and delete_api_model should return deleted status of False
  938. '''
  939. self.conn.delete_model.side_effect = ClientError(error_content, 'delete_model')
  940. result = boto_apigateway.delete_api_model(restApiId='rm06h9oac4', modelName='no_such_model', **conn_parameters)
  941. self.assertFalse(result.get('deleted'))
  942. def test_that_when_describing_api_resources_the_describe_api_resources_method_returns_list_of_3_resources(self):
  943. '''
  944. Test Equality for number of resources for the given api is 3
  945. '''
  946. self.conn.get_resources.return_value = api_resources_ret
  947. result = boto_apigateway.describe_api_resources(restApiId='rm06h9oac4', **conn_parameters)
  948. self.assertEqual(len(result.get('resources')), len(api_resources_ret.get('items')))
  949. def test_that_when_describing_api_resources_and_that_the_api_does_not_exist_the_describe_api_resources_method_returns_error(self):
  950. '''
  951. Test Equality of error returned
  952. '''
  953. self.conn.get_resources.side_effect = ClientError(error_content, 'get_resources')
  954. result = boto_apigateway.describe_api_resources(restApiId='rm06h9oac4', **conn_parameters)
  955. self.assertEqual(result.get('error').get('message'), error_message.format('get_resources'))
  956. def test_that_when_describing_an_api_resource_that_exists_the_describe_api_resource_method_returns_the_resource(self):
  957. '''
  958. Test Equality of the resource path returned is /api
  959. '''
  960. self.conn.get_resources.return_value = api_resources_ret
  961. result = boto_apigateway.describe_api_resource(restApiId='rm06h9oac4', path="/api", **conn_parameters)
  962. self.assertEqual(result.get('resource', {}).get('path'), '/api')
  963. def test_that_when_describing_an_api_resource_that_does_not_exist_the_describe_api_resource_method_returns_the_resource_as_none(self):
  964. '''
  965. Test Equality of the 'resource' is None
  966. '''
  967. self.conn.get_resources.return_value = api_resources_ret
  968. result = boto_apigateway.describe_api_resource(restApiId='rm06h9oac4', path='/path/does/not/exist',
  969. **conn_parameters)
  970. self.assertEqual(result.get('resource'), None)
  971. def test_that_when_describing_an_api_resource_and_that_the_api_does_not_exist_the_describe_api_resource_method_returns_error(self):
  972. '''
  973. Test Equality of error returned
  974. '''
  975. self.conn.get_resources.side_effect = ClientError(error_content, 'get_resources')
  976. result = boto_apigateway.describe_api_resource(restApiId='bad_id', path="/api", **conn_parameters)
  977. self.assertEqual(result.get('error').get('message'), error_message.format('get_resources'))
  978. def test_that_when_creating_api_resources_for_a_path_that_creates_one_new_resource_the_create_resources_api_method_returns_all_resources(self):
  979. '''
  980. Tests that a path of '/api3' returns 2 resources, named '/' and '/api'.
  981. '''
  982. self.conn.get_resources.return_value = api_resources_ret
  983. self.conn.create_resource.return_value = api_create_resource_ret
  984. result = boto_apigateway.create_api_resources(restApiId='rm06h9oac4', path='/api3', **conn_parameters)
  985. resources = result.get('resources')
  986. self.assertIs(result.get('created'), True)
  987. self.assertEqual(len(resources), 2)
  988. self.assertEqual(resources[0].get('path'), '/')
  989. self.assertEqual(resources[1].get('path'), '/api3')
  990. def test_that_when_creating_api_resources_for_a_path_whose_resources_exist_the_create_resources_api_method_returns_all_resources(self):
  991. '''
  992. Tests that a path of '/api/users' as defined in api_resources_ret return resources named '/', '/api',
  993. and '/api/users'
  994. '''
  995. self.conn.get_resources.return_value = api_resources_ret
  996. result = boto_apigateway.create_api_resources(restApiId='rm06h9oac4', path='/api/users', **conn_parameters)
  997. resources = result.get('resources')
  998. self.assertIs(result.get('created'), True)
  999. self.assertEqual(len(resources), len(api_resources_ret.get('items')))
  1000. self.assertEqual(resources[0].get('path'), '/')
  1001. self.assertEqual(resources[1].get('path'), '/api')
  1002. self.assertEqual(resources[2].get('path'), '/api/users')
  1003. def test_that_when_creating_api_resource_fails_the_create_resources_api_method_returns_false(self):
  1004. '''
  1005. Tests False if we failed to create a resource
  1006. '''
  1007. self.conn.get_resources.return_value = api_resources_ret
  1008. self.conn.create_resource.side_effect = ClientError(error_content, 'create_resource')
  1009. result = boto_apigateway.create_api_resources(restApiId='rm06h9oac4', path='/api4', **conn_parameters)
  1010. self.assertFalse(result.get('created'))
  1011. def test_that_when_deleting_api_resources_for_a_resource_that_exists_the_delete_api_resources_method_returns_true(self):
  1012. '''
  1013. Tests True for '/api'
  1014. '''
  1015. self.conn.get_resources.return_value = api_resources_ret
  1016. result = boto_apigateway.delete_api_resources(restApiId='rm06h9oac4', path='/api', **conn_parameters)
  1017. self.assertTrue(result.get('deleted'))
  1018. def test_that_when_deleting_api_resources_for_a_resource_that_does_not_exist_the_delete_api_resources_method_returns_false(self):
  1019. '''
  1020. Tests False for '/api5'
  1021. '''
  1022. self.conn.get_resources.return_value = api_resources_ret
  1023. result = boto_apigateway.delete_api_resources(restApiId='rm06h9oac4', path='/api5', **conn_parameters)
  1024. self.assertFalse(result.get('deleted'))
  1025. def test_that_when_deleting_the_root_api_resource_the_delete_api_resources_method_returns_false(self):
  1026. '''
  1027. Tests False for '/'
  1028. '''
  1029. self.conn.get_resources.return_value = api_resources_ret
  1030. result = boto_apigateway.delete_api_resources(restApiId='rm06h9oac4', path='/', **conn_parameters)
  1031. self.assertFalse(result.get('deleted'))
  1032. def test_that_when_deleting_api_resources_and_delete_resource_throws_error_the_delete_api_resources_method_returns_false(self):
  1033. '''
  1034. Tests False delete_resource side side_effect
  1035. '''
  1036. self.conn.get_resources.return_value = api_resources_ret
  1037. self.conn.delete_resource.side_effect = ClientError(error_content, 'delete_resource')
  1038. result = boto_apigateway.delete_api_resources(restApiId='rm06h9oac4', path='/api', **conn_parameters)
  1039. self.assertFalse(result.get('deleted'))
  1040. def test_that_when_describing_an_api_resource_method_that_exists_the_describe_api_resource_method_returns_the_method(self):
  1041. '''
  1042. Tests True for '/api/users' and POST
  1043. '''
  1044. self.conn.get_resources.return_value = api_resources_ret
  1045. self.conn.get_method.return_value = {u'httpMethod': 'POST',
  1046. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1047. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1048. result = boto_apigateway.describe_api_resource_method(restApiId='rm06h9oac4',
  1049. resourcePath='/api/users',
  1050. httpMethod='POST', **conn_parameters)
  1051. self.assertTrue(result.get('method'))
  1052. def test_that_when_describing_an_api_resource_method_whose_method_does_not_exist_the_describe_api_resource_method_returns_error(self):
  1053. '''
  1054. Tests Equality of returned error for '/api/users' and PUT
  1055. '''
  1056. self.conn.get_resources.return_value = api_resources_ret
  1057. self.conn.get_method.side_effect = ClientError(error_content, 'get_method')
  1058. result = boto_apigateway.describe_api_resource_method(restApiId='rm06h9oac4',
  1059. resourcePath='/api/users',
  1060. httpMethod='PUT', **conn_parameters)
  1061. self.assertEqual(result.get('error').get('message'), error_message.format('get_method'))
  1062. def test_that_when_describing_an_api_resource_method_whose_resource_does_not_exist_the_describe_api_resrouce_method_returns_error(self):
  1063. '''
  1064. Tests True for resource not found error for '/does/not/exist' and POST
  1065. '''
  1066. self.conn.get_resources.return_value = api_resources_ret
  1067. result = boto_apigateway.describe_api_resource_method(restApiId='rm06h9oac4',
  1068. resourcePath='/does/not/exist',
  1069. httpMethod='POST', **conn_parameters)
  1070. self.assertTrue(result.get('error'))
  1071. def test_that_when_creating_an_api_method_the_create_api_method_method_returns_true(self):
  1072. '''
  1073. Tests True on 'created' for '/api/users' and 'GET'
  1074. '''
  1075. self.conn.get_resources.return_value = api_resources_ret
  1076. self.conn.put_method.return_value = {u'httpMethod': 'GET',
  1077. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1078. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1079. result = boto_apigateway.create_api_method(restApiId='rm06h9oac4',
  1080. resourcePath='/api/users',
  1081. httpMethod='GET',
  1082. authorizationType='NONE', **conn_parameters)
  1083. self.assertTrue(result.get('created'))
  1084. def test_that_when_creating_an_api_method_and_resource_does_not_exist_the_create_api_method_method_returns_false(self):
  1085. '''
  1086. Tests False on 'created' for '/api5', and 'GET'
  1087. '''
  1088. self.conn.get_resources.return_value = api_resources_ret
  1089. result = boto_apigateway.create_api_method(restApiId='rm06h9oac4',
  1090. resourcePath='/api5',
  1091. httpMethod='GET',
  1092. authorizationType='NONE', **conn_parameters)
  1093. self.assertFalse(result.get('created'))
  1094. def test_that_when_creating_an_api_method_and_error_thrown_on_put_method_the_create_api_method_method_returns_false(self):
  1095. '''
  1096. Tests False on 'created' for '/api/users' and 'GET'
  1097. '''
  1098. self.conn.get_resources.return_value = api_resources_ret
  1099. self.conn.put_method.side_effect = ClientError(error_content, 'put_method')
  1100. result = boto_apigateway.create_api_method(restApiId='rm06h9oac4',
  1101. resourcePath='/api/users',
  1102. httpMethod='GET',
  1103. authorizationType='NONE', **conn_parameters)
  1104. self.assertFalse(result.get('created'))
  1105. def test_that_when_deleting_an_api_method_for_a_method_that_exist_the_delete_api_method_method_returns_true(self):
  1106. '''
  1107. Tests True for '/api/users' and 'POST'
  1108. '''
  1109. self.conn.get_resources.return_value = api_resources_ret
  1110. self.conn.delete_method.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  1111. result = boto_apigateway.delete_api_method(restApiId='rm06h9oac4', resourcePath='/api/users',
  1112. httpMethod='POST', **conn_parameters)
  1113. self.assertTrue(result.get('deleted'))
  1114. def test_that_when_deleting_an_api_method_for_a_method_that_does_not_exist_the_delete_api_method_method_returns_false(self):
  1115. '''
  1116. Tests False for '/api/users' and 'GET'
  1117. '''
  1118. self.conn.get_resources.return_value = api_resources_ret
  1119. self.conn.delete_method.side_effect = ClientError(error_content, 'delete_method')
  1120. result = boto_apigateway.delete_api_method(restApiId='rm06h9oac4', resourcePath='/api/users',
  1121. httpMethod='GET', **conn_parameters)
  1122. self.assertFalse(result.get('deleted'))
  1123. def test_that_when_deleting_an_api_method_for_a_resource_that_does_not_exist_the_delete_api_method_method_returns_false(self):
  1124. '''
  1125. Tests False for '/api/users5' and 'POST'
  1126. '''
  1127. self.conn.get_resources.return_value = api_resources_ret
  1128. result = boto_apigateway.delete_api_method(restApiId='rm06h9oac4', resourcePath='/api/users5',
  1129. httpMethod='POST', **conn_parameters)
  1130. self.assertFalse(result.get('deleted'))
  1131. def test_that_when_describing_an_api_method_response_that_exists_the_describe_api_method_respond_method_returns_the_response(self):
  1132. '''
  1133. Tests True for 'response' for '/api/users', 'POST', and 200
  1134. '''
  1135. self.conn.get_resources.return_value = api_resources_ret
  1136. self.conn.get_method_response.return_value = {u'statusCode': 200,
  1137. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1138. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1139. result = boto_apigateway.describe_api_method_response(restApiId='rm06h9oac4',
  1140. resourcePath='/api/users',
  1141. httpMethod='POST',
  1142. statusCode=200, **conn_parameters)
  1143. self.assertTrue(result.get('response'))
  1144. def test_that_when_describing_an_api_method_response_and_response_code_does_not_exist_the_describe_api_method_response_method_returns_error(self):
  1145. '''
  1146. Tests Equality of error msg thrown from get_method_response for '/api/users', 'POST', and 250
  1147. '''
  1148. self.conn.get_resources.return_value = api_resources_ret
  1149. self.conn.get_method_response.side_effect = ClientError(error_content, 'get_method_response')
  1150. result = boto_apigateway.describe_api_method_response(restApiId='rm06h9oac4',
  1151. resourcePath='/api/users',
  1152. httpMethod='POST',
  1153. statusCode=250, **conn_parameters)
  1154. self.assertEqual(result.get('error').get('message'), error_message.format('get_method_response'))
  1155. def test_that_when_describing_an_api_method_response_and_resource_does_not_exist_the_describe_api_method_response_method_returns_error(self):
  1156. '''
  1157. Tests True for existence of 'error' for '/api5/users', 'POST', and 200
  1158. '''
  1159. self.conn.get_resources.return_value = api_resources_ret
  1160. result = boto_apigateway.describe_api_method_response(restApiId='rm06h9oac4',
  1161. resourcePath='/api5/users',
  1162. httpMethod='POST',
  1163. statusCode=200, **conn_parameters)
  1164. self.assertTrue(result.get('error'))
  1165. def test_that_when_creating_an_api_method_response_the_create_api_method_response_method_returns_true(self):
  1166. '''
  1167. Tests True on 'created' for '/api/users', 'POST', 201
  1168. '''
  1169. self.conn.get_resources.return_value = api_resources_ret
  1170. self.conn.put_method_response.return_value = {u'statusCode': '201',
  1171. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1172. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1173. result = boto_apigateway.create_api_method_response(restApiId='rm06h9oac4',
  1174. resourcePath='/api/users',
  1175. httpMethod='POST',
  1176. statusCode='201', **conn_parameters)
  1177. self.assertTrue(result.get('created'))
  1178. def test_that_when_creating_an_api_method_response_and_resource_does_not_exist_the_create_api_method_response_method_returns_false(self):
  1179. '''
  1180. Tests False on 'created' for '/api5', 'POST', 200
  1181. '''
  1182. self.conn.get_resources.return_value = api_resources_ret
  1183. result = boto_apigateway.create_api_method_response(restApiId='rm06h9oac4',
  1184. resourcePath='/api5',
  1185. httpMethod='POST',
  1186. statusCode='200', **conn_parameters)
  1187. self.assertFalse(result.get('created'))
  1188. def test_that_when_creating_an_api_method_response_and_error_thrown_on_put_method_response_the_create_api_method_response_method_returns_false(self):
  1189. '''
  1190. Tests False on 'created' for '/api/users', 'POST', 200
  1191. '''
  1192. self.conn.get_resources.return_value = api_resources_ret
  1193. self.conn.put_method_response.side_effect = ClientError(error_content, 'put_method_response')
  1194. result = boto_apigateway.create_api_method_response(restApiId='rm06h9oac4',
  1195. resourcePath='/api/users',
  1196. httpMethod='POST',
  1197. statusCode='200', **conn_parameters)
  1198. self.assertFalse(result.get('created'))
  1199. def test_that_when_deleting_an_api_method_response_for_a_response_that_exist_the_delete_api_method_response_method_returns_true(self):
  1200. '''
  1201. Tests True for '/api/users', 'POST', 200
  1202. '''
  1203. self.conn.get_resources.return_value = api_resources_ret
  1204. self.conn.delete_method_response.return_value = {'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '2d31072c-9d15-11e5-9977-6d9fcfda9c0a'}}
  1205. result = boto_apigateway.delete_api_method_response(restApiId='rm06h9oac4', resourcePath='/api/users',
  1206. httpMethod='POST', statusCode='200', **conn_parameters)
  1207. self.assertTrue(result.get('deleted'))
  1208. def test_that_when_deleting_an_api_method_response_for_a_response_that_does_not_exist_the_delete_api_method_response_method_returns_false(self):
  1209. '''
  1210. Tests False for '/api/users', 'POST', 201
  1211. '''
  1212. self.conn.get_resources.return_value = api_resources_ret
  1213. self.conn.delete_method_response.side_effect = ClientError(error_content, 'delete_method_response')
  1214. result = boto_apigateway.delete_api_method_response(restApiId='rm06h9oac4', resourcePath='/api/users',
  1215. httpMethod='GET', statusCode='201', **conn_parameters)
  1216. self.assertFalse(result.get('deleted'))
  1217. def test_that_when_deleting_an_api_method_response_for_a_resource_that_does_not_exist_the_delete_api_method_response_method_returns_false(self):
  1218. '''
  1219. Tests False for '/api/users5', 'POST', 200
  1220. '''
  1221. self.conn.get_resources.return_value = api_resources_ret
  1222. result = boto_apigateway.delete_api_method_response(restApiId='rm06h9oac4', resourcePath='/api/users5',
  1223. httpMethod='POST', statusCode='200', **conn_parameters)
  1224. self.assertFalse(result.get('deleted'))
  1225. def test_that_when_describing_an_api_integration_that_exists_the_describe_api_integration_method_returns_the_intgration(self):
  1226. '''
  1227. Tests True for 'integration' for '/api/users', 'POST'
  1228. '''
  1229. self.conn.get_resources.return_value = api_resources_ret
  1230. self.conn.get_integration.return_value = {u'type': 'AWS',
  1231. u'uri': 'arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:1234568992820:function:echo_event/invocations',
  1232. u'credentials': 'testing',
  1233. u'httpMethod': 'POST',
  1234. u'intgrationResponses': {'200': {}},
  1235. u'requestTemplates': {'application/json': {}},
  1236. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1237. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1238. result = boto_apigateway.describe_api_integration(restApiId='rm06h9oac4',
  1239. resourcePath='/api/users',
  1240. httpMethod='POST',
  1241. **conn_parameters)
  1242. self.assertTrue(result.get('integration'))
  1243. def test_that_when_describing_an_api_integration_and_method_does_not_have_integration_defined_the_describe_api_integration_method_returns_error(self):
  1244. '''
  1245. Tests Equality of error msg thrown from get_method_response for '/api/users', 'GET'
  1246. '''
  1247. self.conn.get_resources.return_value = api_resources_ret
  1248. self.conn.get_integration.side_effect = ClientError(error_content, 'get_integration')
  1249. result = boto_apigateway.describe_api_integration(restApiId='rm06h9oac4',
  1250. resourcePath='/api/users',
  1251. httpMethod='GET',
  1252. **conn_parameters)
  1253. self.assertEqual(result.get('error').get('message'), error_message.format('get_integration'))
  1254. def test_that_when_describing_an_api_integration_and_resource_does_not_exist_the_describe_api_integration_method_returns_error(self):
  1255. '''
  1256. Tests True for existence of 'error' for '/api5/users', 'POST'
  1257. '''
  1258. self.conn.get_resources.return_value = api_resources_ret
  1259. result = boto_apigateway.describe_api_integration(restApiId='rm06h9oac4',
  1260. resourcePath='/api5/users',
  1261. httpMethod='POST',
  1262. **conn_parameters)
  1263. self.assertTrue(result.get('error'))
  1264. def test_that_when_describing_an_api_integration_response_that_exists_the_describe_api_integration_response_method_returns_the_intgration(self):
  1265. '''
  1266. Tests True for 'response' for '/api/users', 'POST', 200
  1267. '''
  1268. self.conn.get_resources.return_value = api_resources_ret
  1269. self.conn.get_integration_response.return_value = {u'responseParameters': {},
  1270. u'statusCode': 200,
  1271. 'ResponseMetadata': {'HTTPStatusCode': 200,
  1272. 'RequestId': '7cc233dd-9dc8-11e5-ba47-1b7350cc2757'}}
  1273. result = boto_apigateway.describe_api_integration_response(restApiId='rm06h9oac4',
  1274. resourcePath='/api/users',
  1275. httpMethod='POST',
  1276. statusCode='200',
  1277. **conn_parameters)
  1278. self.assertTrue(result.get('response'))
  1279. def test_that_when_describing_an_api_integration_response_and_status_code_does_not_exist_the_describe_api_integration_response_method_returns_error(self):
  1280. '''
  1281. Tests Equality of error msg thrown from get_method_response for '/api/users', 'POST', 201
  1282. '''
  1283. self.conn.get_resources.return_value = api_resources_ret
  1284. self.conn.get_integration_response.side_effect = ClientError(error_content, 'get_integration_response')
  1285. result = boto_apigateway.describe_api_integration_response(restApiId='rm06h9oac4',
  1286. resourcePath='/api/users',
  1287. httpMethod='POST',
  1288. statusCode='201',
  1289. **conn_parameters)
  1290. self.assertEqual(result.get('error').get('message'), error_message.format('get_integration_response'))
  1291. def test_that_when_describing_an_api_integration_response_and_resource_does_not_exist_the_describe_api_integration_response_method_returns_error(self):
  1292. '''
  1293. Tests True for existence of 'error' for '/api5/users', 'POST', 200
  1294. '''
  1295. self.conn.get_resources.return_value = api_resources_ret
  1296. result = boto_apigateway.describe_api_integration_response(restApiId='rm06h9oac4',
  1297. resourcePath='/api5/users',
  1298. httpMethod='POST',
  1299. statusCode='200',
  1300. **conn_parameters)
  1301. self.assertTrue(result.get('error'))
  1302. def test_that_when_describing_usage_plans_and_an_exception_is_thrown_in_get_usage_plans(self):
  1303. '''
  1304. Tests True for existence of 'error'
  1305. '''
  1306. self.conn.get_usage_plans.side_effect = ClientError(error_content, 'get_usage_plans_exception')
  1307. result = boto_apigateway.describe_usage_plans(name='some plan', **conn_parameters)
  1308. self.assertEqual(result.get('error').get('message'), error_message.format('get_usage_plans_exception'))
  1309. def test_that_when_describing_usage_plans_and_plan_name_or_id_does_not_exist_that_results_have_empty_plans_list(self):
  1310. '''
  1311. Tests for plans equaling empty list
  1312. '''
  1313. self.conn.get_usage_plans.return_value = usage_plans_ret
  1314. result = boto_apigateway.describe_usage_plans(name='does not exist', **conn_parameters)
  1315. self.assertEqual(result.get('plans'), [])
  1316. result = boto_apigateway.describe_usage_plans(plan_id='does not exist', **conn_parameters)
  1317. self.assertEqual(result.get('plans'), [])
  1318. result = boto_apigateway.describe_usage_plans(name='does not exist', plan_id='does not exist', **conn_parameters)
  1319. self.assertEqual(result.get('plans'), [])
  1320. result = boto_apigateway.describe_usage_plans(name='plan1_name', plan_id='does not exist', **conn_parameters)
  1321. self.assertEqual(result.get('plans'), [])
  1322. result = boto_apigateway.describe_usage_plans(name='does not exist', plan_id='plan1_id', **conn_parameters)
  1323. self.assertEqual(result.get('plans'), [])
  1324. def test_that_when_describing_usage_plans_for_plans_that_exist_that_the_function_returns_all_matching_plans(self):
  1325. '''
  1326. Tests for plans filtering properly if they exist
  1327. '''
  1328. self.conn.get_usage_plans.return_value = usage_plans_ret
  1329. result = boto_apigateway.describe_usage_plans(name=usage_plan1['name'], **conn_parameters)
  1330. self.assertEqual(len(result.get('plans')), 2)
  1331. for plan in result['plans']:
  1332. self.assertTrue(plan in [usage_plan1, usage_plan1b])
  1333. def test_that_when_creating_or_updating_a_usage_plan_and_throttle_or_quota_failed_to_validate_that_an_error_is_returned(self):
  1334. '''
  1335. Tests for TypeError and ValueError in throttle and quota
  1336. '''
  1337. for throttle, quota in (([], None), (None, []), ('abc', None), (None, 'def')):
  1338. res = boto_apigateway.create_usage_plan('plan1_name', description=None, throttle=throttle, quota=quota, **conn_parameters)
  1339. self.assertNotEqual(None, res.get('error'))
  1340. res = boto_apigateway.update_usage_plan('plan1_id', throttle=throttle, quota=quota, **conn_parameters)
  1341. self.assertNotEqual(None, res.get('error'))
  1342. for quota in ({'limit': 123}, {'period': 123}, {'period': 'DAY'}):
  1343. res = boto_apigateway.create_usage_plan('plan1_name', description=None, throttle=None, quota=quota, **conn_parameters)
  1344. self.assertNotEqual(None, res.get('error'))
  1345. res = boto_apigateway.update_usage_plan('plan1_id', quota=quota, **conn_parameters)
  1346. self.assertNotEqual(None, res.get('error'))
  1347. self.assertTrue(self.conn.get_usage_plans.call_count == 0)
  1348. self.assertTrue(self.conn.create_usage_plan.call_count == 0)
  1349. self.assertTrue(self.conn.update_usage_plan.call_count == 0)
  1350. def test_that_when_creating_a_usage_plan_and_create_usage_plan_throws_an_exception_that_an_error_is_returned(self):
  1351. '''
  1352. tests for ClientError
  1353. '''
  1354. self.conn.create_usage_plan.side_effect = ClientError(error_content, 'create_usage_plan_exception')
  1355. result = boto_apigateway.create_usage_plan(name='some plan', **conn_parameters)
  1356. self.assertEqual(result.get('error').get('message'), error_message.format('create_usage_plan_exception'))
  1357. def test_that_create_usage_plan_succeeds(self):
  1358. '''
  1359. tests for success user plan creation
  1360. '''
  1361. res = 'unit test create_usage_plan succeeded'
  1362. self.conn.create_usage_plan.return_value = res
  1363. result = boto_apigateway.create_usage_plan(name='some plan', **conn_parameters)
  1364. self.assertEqual(result.get('created'), True)
  1365. self.assertEqual(result.get('result'), res)
  1366. def test_that_when_udpating_a_usage_plan_and_update_usage_plan_throws_an_exception_that_an_error_is_returned(self):
  1367. '''
  1368. tests for ClientError
  1369. '''
  1370. self.conn.update_usage_plan.side_effect = ClientError(error_content, 'update_usage_plan_exception')
  1371. result = boto_apigateway.update_usage_plan(plan_id='plan1_id', **conn_parameters)
  1372. self.assertEqual(result.get('error').get('message'), error_message.format('update_usage_plan_exception'))
  1373. def test_that_when_updating_a_usage_plan_and_if_throttle_and_quota_parameters_are_none_update_usage_plan_removes_throttle_and_quota(self):
  1374. '''
  1375. tests for throttle and quota removal
  1376. '''
  1377. ret = 'some success status'
  1378. self.conn.update_usage_plan.return_value = ret
  1379. result = boto_apigateway.update_usage_plan(plan_id='plan1_id', throttle=None, quota=None, **conn_parameters)
  1380. self.assertEqual(result.get('updated'), True)
  1381. self.assertEqual(result.get('result'), ret)
  1382. self.assertTrue(self.conn.update_usage_plan.call_count >= 1)
  1383. def test_that_when_deleting_usage_plan_and_describe_usage_plans_had_error_that_the_same_error_is_returned(self):
  1384. '''
  1385. tests for error in describe_usage_plans returns error
  1386. '''
  1387. ret = 'get_usage_plans_exception'
  1388. self.conn.get_usage_plans.side_effect = ClientError(error_content, ret)
  1389. result = boto_apigateway.delete_usage_plan(plan_id='some plan id', **conn_parameters)
  1390. self.assertEqual(result.get('error').get('message'), error_message.format(ret))
  1391. self.assertTrue(self.conn.delete_usage_plan.call_count == 0)
  1392. def test_that_when_deleting_usage_plan_and_plan_exists_that_the_functions_returns_deleted_true(self):
  1393. self.conn.get_usage_plans.return_value = usage_plans_ret
  1394. ret = 'delete_usage_plan_retval'
  1395. self.conn.delete_usage_plan.return_value = ret
  1396. result = boto_apigateway.delete_usage_plan(plan_id='plan1_id', **conn_parameters)
  1397. self.assertEqual(result.get('deleted'), True)
  1398. self.assertEqual(result.get('usagePlanId'), 'plan1_id')
  1399. self.assertTrue(self.conn.delete_usage_plan.call_count >= 1)
  1400. def test_that_when_deleting_usage_plan_and_plan_does_not_exist_that_the_functions_returns_deleted_true(self):
  1401. '''
  1402. tests for ClientError
  1403. '''
  1404. self.conn.get_usage_plans.return_value = dict(
  1405. items=[]
  1406. )
  1407. ret = 'delete_usage_plan_retval'
  1408. self.conn.delete_usage_plan.return_value = ret
  1409. result = boto_apigateway.delete_usage_plan(plan_id='plan1_id', **conn_parameters)
  1410. self.assertEqual(result.get('deleted'), True)
  1411. self.assertEqual(result.get('usagePlanId'), 'plan1_id')
  1412. self.assertTrue(self.conn.delete_usage_plan.call_count == 0)
  1413. def test_that_when_deleting_usage_plan_and_delete_usage_plan_throws_exception_that_an_error_is_returned(self):
  1414. '''
  1415. tests for ClientError
  1416. '''
  1417. self.conn.get_usage_plans.return_value = usage_plans_ret
  1418. error_msg = 'delete_usage_plan_exception'
  1419. self.conn.delete_usage_plan.side_effect = ClientError(error_content, error_msg)
  1420. result = boto_apigateway.delete_usage_plan(plan_id='plan1_id', **conn_parameters)
  1421. self.assertEqual(result.get('error').get('message'), error_message.format(error_msg))
  1422. self.assertTrue(self.conn.delete_usage_plan.call_count >= 1)
  1423. def test_that_attach_or_detach_usage_plan_when_apis_is_empty_that_success_is_returned(self):
  1424. '''
  1425. tests for border cases when apis is empty list
  1426. '''
  1427. result = boto_apigateway.attach_usage_plan_to_apis(plan_id='plan1_id', apis=[], **conn_parameters)
  1428. self.assertEqual(result.get('success'), True)
  1429. self.assertEqual(result.get('result', 'no result?'), None)
  1430. self.assertTrue(self.conn.update_usage_plan.call_count == 0)
  1431. result = boto_apigateway.detach_usage_plan_from_apis(plan_id='plan1_id', apis=[], **conn_parameters)
  1432. self.assertEqual(result.get('success'), True)
  1433. self.assertEqual(result.get('result', 'no result?'), None)
  1434. self.assertTrue(self.conn.update_usage_plan.call_count == 0)
  1435. def test_that_attach_or_detach_usage_plan_when_api_does_not_contain_apiId_or_stage_that_an_error_is_returned(self):
  1436. '''
  1437. tests for invalid key in api object
  1438. '''
  1439. for api in ({'apiId': 'some Id'}, {'stage': 'some stage'}, {}):
  1440. result = boto_apigateway.attach_usage_plan_to_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1441. self.assertNotEqual(result.get('error'), None)
  1442. result = boto_apigateway.detach_usage_plan_from_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1443. self.assertNotEqual(result.get('error'), None)
  1444. self.assertTrue(self.conn.update_usage_plan.call_count == 0)
  1445. def test_that_attach_or_detach_usage_plan_and_update_usage_plan_throws_exception_that_an_error_is_returned(self):
  1446. '''
  1447. tests for ClientError
  1448. '''
  1449. api = {'apiId': 'some_id', 'stage': 'some_stage'}
  1450. error_msg = 'update_usage_plan_exception'
  1451. self.conn.update_usage_plan.side_effect = ClientError(error_content, error_msg)
  1452. result = boto_apigateway.attach_usage_plan_to_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1453. self.assertEqual(result.get('error').get('message'), error_message.format(error_msg))
  1454. result = boto_apigateway.detach_usage_plan_from_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1455. self.assertEqual(result.get('error').get('message'), error_message.format(error_msg))
  1456. def test_that_attach_or_detach_usage_plan_updated_successfully(self):
  1457. '''
  1458. tests for update_usage_plan called
  1459. '''
  1460. api = {'apiId': 'some_id', 'stage': 'some_stage'}
  1461. attach_ret = 'update_usage_plan_add_op_succeeded'
  1462. detach_ret = 'update_usage_plan_remove_op_succeeded'
  1463. self.conn.update_usage_plan.side_effect = [attach_ret, detach_ret]
  1464. result = boto_apigateway.attach_usage_plan_to_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1465. self.assertEqual(result.get('success'), True)
  1466. self.assertEqual(result.get('result'), attach_ret)
  1467. result = boto_apigateway.detach_usage_plan_from_apis(plan_id='plan1_id', apis=[api], **conn_parameters)
  1468. self.assertEqual(result.get('success'), True)
  1469. self.assertEqual(result.get('result'), detach_ret)