test_boto_cognitoidentity.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import logging
  5. import random
  6. import string
  7. # Import Salt Testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import skipIf, TestCase
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt libs
  12. import salt.config
  13. import salt.loader
  14. from salt.utils.versions import LooseVersion
  15. import salt.modules.boto_cognitoidentity as boto_cognitoidentity
  16. # Import 3rd-party libs
  17. # pylint: disable=import-error,no-name-in-module
  18. try:
  19. import boto3
  20. from botocore.exceptions import ClientError
  21. HAS_BOTO = True
  22. except ImportError:
  23. HAS_BOTO = False
  24. from salt.ext.six.moves import range # pylint: disable=import-error
  25. # pylint: enable=import-error,no-name-in-module
  26. # the boto_lambda module relies on the connect_to_region() method
  27. # which was added in boto 2.8.0
  28. # https://github.com/boto/boto/commit/33ac26b416fbb48a60602542b4ce15dcc7029f12
  29. required_boto3_version = '1.2.1'
  30. region = 'us-east-1'
  31. access_key = 'GKTADJGHEIQSXMKKRBJ08H'
  32. secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
  33. conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key, 'profile': {}}
  34. error_message = 'An error occurred (101) when calling the {0} operation: Test-defined error'
  35. error_content = {
  36. 'Error': {
  37. 'Code': 101,
  38. 'Message': "Test-defined error"
  39. }
  40. }
  41. first_pool_id = 'first_pool_id'
  42. first_pool_name = 'first_pool'
  43. second_pool_id = 'second_pool_id'
  44. second_pool_name = 'second_pool'
  45. second_pool_name_updated = 'second_pool_updated'
  46. third_pool_id = 'third_pool_id'
  47. third_pool_name = first_pool_name
  48. identity_pools_ret = dict(IdentityPools=[dict(IdentityPoolId=first_pool_id,
  49. IdentityPoolName=first_pool_name),
  50. dict(IdentityPoolId=second_pool_id,
  51. IdentityPoolName=second_pool_name),
  52. dict(IdentityPoolId=third_pool_id,
  53. IdentityPoolName=third_pool_name)])
  54. first_pool_ret = dict(IdentityPoolId=first_pool_id,
  55. IdentityPoolName=first_pool_name,
  56. AllowUnauthenticatedIdentities=False,
  57. SupportedLoginProviders={'accounts.google.com': 'testing123',
  58. 'api.twitter.com': 'testing123',
  59. 'graph.facebook.com': 'testing123',
  60. 'www.amazon.com': 'testing123'},
  61. DeveloperProviderName='test_provider',
  62. OpenIdConnectProviderARNs=['some_provider_arn',
  63. 'another_provider_arn'])
  64. first_pool_role_ret = dict(IdentityPoolId=first_pool_id,
  65. Roles=dict(authenticated='first_pool_auth_role',
  66. unauthenticated='first_pool_unauth_role'))
  67. second_pool_ret = dict(IdentityPoolId=second_pool_id,
  68. IdentityPoolName=second_pool_name,
  69. AllowUnauthenticatedIdentities=False)
  70. third_pool_ret = dict(IdentityPoolId=third_pool_id,
  71. IdentityPoolName=third_pool_name,
  72. AllowUnauthenticatedIdentities=False,
  73. DeveloperProviderName='test_provider2')
  74. third_pool_role_ret = dict(IdentityPoolId=third_pool_id)
  75. default_pool_ret = dict(IdentityPoolId='default_pool_id',
  76. IdentityPoolName='default_pool_name',
  77. AllowUnauthenticatedIdentities=False,
  78. DeveloperProviderName='test_provider_default')
  79. default_pool_role_ret = dict(IdentityPoolId='default_pool_id')
  80. log = logging.getLogger(__name__)
  81. def _has_required_boto():
  82. '''
  83. Returns True/False boolean depending on if Boto is installed and correct
  84. version.
  85. '''
  86. if not HAS_BOTO:
  87. return False
  88. elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version):
  89. return False
  90. else:
  91. return True
  92. class BotoCognitoIdentityTestCaseBase(TestCase, LoaderModuleMockMixin):
  93. conn = None
  94. def setup_loader_modules(self):
  95. self.opts = opts = salt.config.DEFAULT_MINION_OPTS
  96. utils = salt.loader.utils(
  97. opts,
  98. whitelist=['boto3', 'args', 'systemd', 'path', 'platform'],
  99. context={})
  100. return {boto_cognitoidentity: {'__utils__': utils}}
  101. def setUp(self):
  102. super(BotoCognitoIdentityTestCaseBase, self).setUp()
  103. boto_cognitoidentity.__init__(self.opts)
  104. del self.opts
  105. # Set up MagicMock to replace the boto3 session
  106. # connections keep getting cached from prior tests, can't find the
  107. # correct context object to clear it. So randomize the cache key, to prevent any
  108. # cache hits
  109. conn_parameters['key'] = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(50))
  110. self.patcher = patch('boto3.session.Session')
  111. self.addCleanup(self.patcher.stop)
  112. self.addCleanup(delattr, self, 'patcher')
  113. mock_session = self.patcher.start()
  114. session_instance = mock_session.return_value
  115. self.conn = MagicMock()
  116. self.addCleanup(delattr, self, 'conn')
  117. session_instance.client.return_value = self.conn
  118. class BotoCognitoIdentityTestCaseMixin(object):
  119. pass
  120. @skipIf(True, 'Skip these tests while investigating failures')
  121. @skipIf(HAS_BOTO is False, 'The boto module must be installed.')
  122. @skipIf(_has_required_boto() is False, 'The boto3 module must be greater than'
  123. ' or equal to version {0}'
  124. .format(required_boto3_version))
  125. class BotoCognitoIdentityTestCase(BotoCognitoIdentityTestCaseBase, BotoCognitoIdentityTestCaseMixin):
  126. '''
  127. TestCase for salt.modules.boto_cognitoidentity module
  128. '''
  129. def _describe_identity_pool_side_effect(self, *args, **kwargs):
  130. if kwargs.get('IdentityPoolId') == first_pool_id:
  131. return first_pool_ret
  132. elif kwargs.get('IdentityPoolId') == third_pool_id:
  133. return third_pool_ret
  134. else:
  135. return default_pool_ret
  136. def test_that_when_describing_a_named_identity_pool_and_pool_exists_the_describe_identity_pool_method_returns_pools_properties(self):
  137. '''
  138. Tests describing identity pool when the pool's name exists
  139. '''
  140. self.conn.list_identity_pools.return_value = identity_pools_ret
  141. self.conn.describe_identity_pool.side_effect = self._describe_identity_pool_side_effect
  142. result = boto_cognitoidentity.describe_identity_pools(IdentityPoolName=first_pool_name, **conn_parameters)
  143. self.assertEqual(result.get('identity_pools'), [first_pool_ret, third_pool_ret])
  144. def test_that_when_describing_a_identity_pool_by_its_id_and_pool_exists_the_desribe_identity_pool_method_returns_pools_properties(self):
  145. '''
  146. Tests describing identity pool when the given pool's id exists
  147. '''
  148. self.conn.describe_identity_pool.return_value = third_pool_ret
  149. result = boto_cognitoidentity.describe_identity_pools(IdentityPoolName='', IdentityPoolId=third_pool_id, **conn_parameters)
  150. self.assertEqual(result.get('identity_pools'), [third_pool_ret])
  151. self.assertTrue(self.conn.list_identity_pools.call_count == 0)
  152. def test_that_when_describing_a_named_identity_pool_and_pool_does_not_exist_the_describe_identity_pool_method_returns_none(self):
  153. '''
  154. Tests describing identity pool when the pool's name doesn't exist
  155. '''
  156. self.conn.list_identity_pools.return_value = identity_pools_ret
  157. self.conn.describe_identity_pool.return_value = first_pool_ret
  158. result = boto_cognitoidentity.describe_identity_pools(IdentityPoolName='no_such_pool', **conn_parameters)
  159. self.assertEqual(result.get('identity_pools', 'no such key'), None)
  160. def test_that_when_describing_a_named_identity_pool_and_error_thrown_the_describe_identity_pool_method_returns_error(self):
  161. '''
  162. Tests describing identity pool returns error when there is an exception to boto3 calls
  163. '''
  164. self.conn.list_identity_pools.return_value = identity_pools_ret
  165. self.conn.describe_identity_pool.side_effect = ClientError(error_content, 'error on describe identity pool')
  166. result = boto_cognitoidentity.describe_identity_pools(IdentityPoolName=first_pool_name, **conn_parameters)
  167. self.assertEqual(result.get('error', {}).get('message'), error_message.format('error on describe identity pool'))
  168. def test_that_when_create_identity_pool_the_create_identity_pool_method_returns_created_identity_pool(self):
  169. '''
  170. Tests the positive case where create identity pool succeeds
  171. '''
  172. return_val = default_pool_ret.copy()
  173. return_val.pop('DeveloperProviderName', None)
  174. self.conn.create_identity_pool.return_value = return_val
  175. result = boto_cognitoidentity.create_identity_pool(IdentityPoolName='default_pool_name', **conn_parameters)
  176. mock_calls = self.conn.mock_calls
  177. #name, args, kwargs = mock_calls[0]
  178. self.assertTrue(result.get('created'))
  179. self.assertEqual(len(mock_calls), 1)
  180. self.assertEqual(mock_calls[0][0], 'create_identity_pool')
  181. self.assertNotIn('DeveloperProviderName', mock_calls[0][2])
  182. def test_that_when_create_identity_pool_and_error_thrown_the_create_identity_pool_method_returns_error(self):
  183. '''
  184. Tests the negative case where create identity pool has a boto3 client error
  185. '''
  186. self.conn.create_identity_pool.side_effect = ClientError(error_content, 'create_identity_pool')
  187. result = boto_cognitoidentity.create_identity_pool(IdentityPoolName='default_pool_name', **conn_parameters)
  188. self.assertIs(result.get('created'), False)
  189. self.assertEqual(result.get('error', {}).get('message'), error_message.format('create_identity_pool'))
  190. def test_that_when_delete_identity_pools_with_multiple_matching_pool_names_the_delete_identity_pools_methos_returns_true_and_deleted_count(self):
  191. '''
  192. Tests that given 2 matching pool ids, the operation returns deleted status of true and
  193. count 2
  194. '''
  195. self.conn.list_identity_pools.return_value = identity_pools_ret
  196. self.conn.delete_identity_pool.return_value = None
  197. result = boto_cognitoidentity.delete_identity_pools(IdentityPoolName=first_pool_name, **conn_parameters)
  198. mock_calls = self.conn.mock_calls
  199. self.assertTrue(result.get('deleted'))
  200. self.assertEqual(result.get('count'), 2)
  201. self.assertEqual(len(mock_calls), 3)
  202. self.assertEqual(mock_calls[1][0], 'delete_identity_pool')
  203. self.assertEqual(mock_calls[2][0], 'delete_identity_pool')
  204. self.assertEqual(mock_calls[1][2].get('IdentityPoolId'), first_pool_id)
  205. self.assertEqual(mock_calls[2][2].get('IdentityPoolId'), third_pool_id)
  206. def test_that_when_delete_identity_pools_with_no_matching_pool_names_the_delete_identity_pools_method_returns_false(self):
  207. '''
  208. Tests that the given pool name does not exist, the operation returns deleted status of false
  209. and count 0
  210. '''
  211. self.conn.list_identity_pools.return_value = identity_pools_ret
  212. result = boto_cognitoidentity.delete_identity_pools(IdentityPoolName='no_such_pool_name', **conn_parameters)
  213. mock_calls = self.conn.mock_calls
  214. self.assertIs(result.get('deleted'), False)
  215. self.assertEqual(result.get('count'), 0)
  216. self.assertEqual(len(mock_calls), 1)
  217. def test_that_when_delete_identity_pools_and_error_thrown_the_delete_identity_pools_method_returns_false_and_the_error(self):
  218. '''
  219. Tests that the delete_identity_pool method throws an exception
  220. '''
  221. self.conn.delete_identity_pool.side_effect = ClientError(error_content, 'delete_identity_pool')
  222. # try to delete an unexistent pool id "no_such_pool_id"
  223. result = boto_cognitoidentity.delete_identity_pools(IdentityPoolName=first_pool_name, IdentityPoolId='no_such_pool_id', **conn_parameters)
  224. mock_calls = self.conn.mock_calls
  225. self.assertIs(result.get('deleted'), False)
  226. self.assertIs(result.get('count'), None)
  227. self.assertEqual(result.get('error', {}).get('message'), error_message.format('delete_identity_pool'))
  228. self.assertEqual(len(mock_calls), 1)
  229. def _get_identity_pool_roles_side_effect(self, *args, **kwargs):
  230. if kwargs.get('IdentityPoolId') == first_pool_id:
  231. return first_pool_role_ret
  232. elif kwargs.get('IdentityPoolId') == third_pool_id:
  233. return third_pool_role_ret
  234. else:
  235. return default_pool_role_ret
  236. def test_that_when_get_identity_pool_roles_with_matching_pool_names_the_get_identity_pool_roles_method_returns_pools_role_properties(self):
  237. '''
  238. Tests that the given 2 pool id's matching the given pool name, the results are
  239. passed through as a list of identity_pool_roles
  240. '''
  241. self.conn.list_identity_pools.return_value = identity_pools_ret
  242. self.conn.get_identity_pool_roles.side_effect = self._get_identity_pool_roles_side_effect
  243. result = boto_cognitoidentity.get_identity_pool_roles(IdentityPoolName=first_pool_name, **conn_parameters)
  244. id_pool_roles = result.get('identity_pool_roles')
  245. self.assertIsNot(id_pool_roles, None)
  246. self.assertEqual(result.get('error', 'key_should_not_be_there'), 'key_should_not_be_there')
  247. self.assertEqual(len(id_pool_roles), 2)
  248. self.assertEqual(id_pool_roles[0], first_pool_role_ret)
  249. self.assertEqual(id_pool_roles[1], third_pool_role_ret)
  250. def test_that_when_get_identity_pool_roles_with_no_matching_pool_names_the_get_identity_pool_roles_method_returns_none(self):
  251. '''
  252. Tests that the given no pool id's matching the given pool name, the results returned is
  253. None and get_identity_pool_roles should never be called
  254. '''
  255. self.conn.list_identity_pools.return_value = identity_pools_ret
  256. result = boto_cognitoidentity.get_identity_pool_roles(IdentityPoolName="no_such_pool_name", **conn_parameters)
  257. mock_calls = self.conn.mock_calls
  258. self.assertIs(result.get('identity_pool_roles', 'key_should_be_there'), None)
  259. self.assertEqual(len(mock_calls), 1)
  260. self.assertEqual(result.get('error', 'key_should_not_be_there'), 'key_should_not_be_there')
  261. def test_that_when_get_identity_pool_roles_and_error_thrown_due_to_invalid_pool_id_the_get_identity_pool_roles_method_returns_error(self):
  262. '''
  263. Tests that given an invalid pool id, we properly handle error generated from get_identity_pool_roles
  264. '''
  265. self.conn.get_identity_pool_roles.side_effect = ClientError(error_content, 'get_identity_pool_roles')
  266. # try to delete an unexistent pool id "no_such_pool_id"
  267. result = boto_cognitoidentity.get_identity_pool_roles(IdentityPoolName='', IdentityPoolId='no_such_pool_id', **conn_parameters)
  268. mock_calls = self.conn.mock_calls
  269. self.assertEqual(result.get('error', {}).get('message'), error_message.format('get_identity_pool_roles'))
  270. self.assertEqual(len(mock_calls), 1)
  271. def test_that_when_set_identity_pool_roles_with_invalid_pool_id_the_set_identity_pool_roles_method_returns_set_false_and_error(self):
  272. '''
  273. Tests that given an invalid pool id, we properly handle error generated from set_identity_pool_roles
  274. '''
  275. self.conn.set_identity_pool_roles.side_effect = ClientError(error_content, 'set_identity_pool_roles')
  276. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='no_such_pool_id', **conn_parameters)
  277. mock_calls = self.conn.mock_calls
  278. self.assertIs(result.get('set'), False)
  279. self.assertEqual(result.get('error', {}).get('message'), error_message.format('set_identity_pool_roles'))
  280. self.assertEqual(len(mock_calls), 1)
  281. def test_that_when_set_identity_pool_roles_with_no_roles_specified_the_set_identity_pool_roles_method_unset_the_roles(self):
  282. '''
  283. Tests that given a valid pool id, and no other roles given, the role for the pool is cleared.
  284. '''
  285. self.conn.set_identity_pool_roles.return_value = None
  286. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', **conn_parameters)
  287. mock_calls = self.conn.mock_calls
  288. self.assertTrue(result.get('set'))
  289. self.assertEqual(len(mock_calls), 1)
  290. self.assertEqual(mock_calls[0][2].get('Roles'), {})
  291. def test_that_when_set_identity_pool_roles_with_only_auth_role_specified_the_set_identity_pool_roles_method_only_set_the_auth_role(self):
  292. '''
  293. Tests that given a valid pool id, and only other given role is the AuthenticatedRole, the auth role for the
  294. pool is set and unauth role is cleared.
  295. '''
  296. self.conn.set_identity_pool_roles.return_value = None
  297. with patch.dict(boto_cognitoidentity.__salt__, {'boto_iam.describe_role': MagicMock(return_value={'arn': 'my_auth_role_arn'})}):
  298. expected_roles = dict(authenticated='my_auth_role_arn')
  299. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', AuthenticatedRole='my_auth_role', **conn_parameters)
  300. mock_calls = self.conn.mock_calls
  301. self.assertTrue(result.get('set'))
  302. self.assertEqual(len(mock_calls), 1)
  303. self.assertEqual(mock_calls[0][2].get('Roles'), expected_roles)
  304. def test_that_when_set_identity_pool_roles_with_only_unauth_role_specified_the_set_identity_pool_roles_method_only_set_the_unauth_role(self):
  305. '''
  306. Tests that given a valid pool id, and only other given role is the UnauthenticatedRole, the unauth role for the
  307. pool is set and the auth role is cleared.
  308. '''
  309. self.conn.set_identity_pool_roles.return_value = None
  310. with patch.dict(boto_cognitoidentity.__salt__, {'boto_iam.describe_role': MagicMock(return_value={'arn': 'my_unauth_role_arn'})}):
  311. expected_roles = dict(unauthenticated='my_unauth_role_arn')
  312. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', UnauthenticatedRole='my_unauth_role', **conn_parameters)
  313. mock_calls = self.conn.mock_calls
  314. self.assertTrue(result.get('set'))
  315. self.assertEqual(len(mock_calls), 1)
  316. self.assertEqual(mock_calls[0][2].get('Roles'), expected_roles)
  317. def test_that_when_set_identity_pool_roles_with_both_roles_specified_the_set_identity_pool_role_method_set_both_roles(self):
  318. '''
  319. Tests setting of both roles to valid given roles
  320. '''
  321. self.conn.set_identity_pool_roles.return_value = None
  322. with patch.dict(boto_cognitoidentity.__salt__, {'boto_iam.describe_role': MagicMock(return_value={'arn': 'my_unauth_role_arn'})}):
  323. expected_roles = dict(authenticated='arn:aws:iam:my_auth_role',
  324. unauthenticated='my_unauth_role_arn')
  325. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', AuthenticatedRole='arn:aws:iam:my_auth_role', UnauthenticatedRole='my_unauth_role', **conn_parameters)
  326. mock_calls = self.conn.mock_calls
  327. self.assertTrue(result.get('set'))
  328. self.assertEqual(len(mock_calls), 1)
  329. self.assertEqual(mock_calls[0][2].get('Roles'), expected_roles)
  330. def test_that_when_set_identity_pool_roles_given_invalid_auth_role_the_set_identity_pool_method_returns_set_false_and_error(self):
  331. '''
  332. Tests error handling for invalid auth role
  333. '''
  334. with patch.dict(boto_cognitoidentity.__salt__, {'boto_iam.describe_role': MagicMock(return_value=False)}):
  335. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', AuthenticatedRole='no_such_auth_role', **conn_parameters)
  336. mock_calls = self.conn.mock_calls
  337. self.assertIs(result.get('set'), False)
  338. self.assertIn('no_such_auth_role', result.get('error', ''))
  339. self.assertEqual(len(mock_calls), 0)
  340. def test_that_when_set_identity_pool_roles_given_invalid_unauth_role_the_set_identity_pool_method_returns_set_false_and_error(self):
  341. '''
  342. Tests error handling for invalid unauth role
  343. '''
  344. with patch.dict(boto_cognitoidentity.__salt__, {'boto_iam.describe_role': MagicMock(return_value=False)}):
  345. result = boto_cognitoidentity.set_identity_pool_roles(IdentityPoolId='some_id', AuthenticatedRole='arn:aws:iam:my_auth_role', UnauthenticatedRole='no_such_unauth_role', **conn_parameters)
  346. mock_calls = self.conn.mock_calls
  347. self.assertIs(result.get('set'), False)
  348. self.assertIn('no_such_unauth_role', result.get('error', ''))
  349. self.assertEqual(len(mock_calls), 0)
  350. def test_that_when_update_identity_pool_given_invalid_pool_id_the_update_identity_pool_method_returns_updated_false_and_error(self):
  351. '''
  352. Tests error handling for invalid pool id
  353. '''
  354. self.conn.describe_identity_pool.side_effect = ClientError(error_content, 'error on describe identity pool with pool id')
  355. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId='no_such_pool_id', **conn_parameters)
  356. self.assertIs(result.get('updated'), False)
  357. self.assertEqual(result.get('error', {}).get('message'), error_message.format('error on describe identity pool with pool id'))
  358. def test_that_when_update_identity_pool_given_only_valid_pool_id_the_update_identity_pool_method_returns_udpated_identity(self):
  359. '''
  360. Tests the base case of calling update_identity_pool with only the pool id, verify
  361. that the passed parameters into boto3 update_identity_pool has at least all the
  362. expected required parameters in IdentityPoolId, IdentityPoolName and AllowUnauthenticatedIdentities
  363. '''
  364. self.conn.describe_identity_pool.return_value = second_pool_ret
  365. self.conn.update_identity_pool.return_value = second_pool_ret
  366. expected_params = second_pool_ret
  367. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=second_pool_id, **conn_parameters)
  368. self.assertTrue(result.get('updated'))
  369. self.assertEqual(result.get('identity_pool'), second_pool_ret)
  370. self.conn.update_identity_pool.assert_called_with(**expected_params)
  371. def test_that_when_update_identity_pool_given_valid_pool_id_and_pool_name_the_update_identity_pool_method_returns_updated_identity_pool(self):
  372. '''
  373. Tests successful update of a pool's name
  374. '''
  375. self.conn.describe_identity_pool.return_value = second_pool_ret
  376. second_pool_updated_ret = second_pool_ret.copy()
  377. second_pool_updated_ret['IdentityPoolName'] = second_pool_name_updated
  378. self.conn.update_identity_pool.return_value = second_pool_updated_ret
  379. expected_params = second_pool_updated_ret
  380. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=second_pool_id, IdentityPoolName=second_pool_name_updated, **conn_parameters)
  381. self.assertTrue(result.get('updated'))
  382. self.assertEqual(result.get('identity_pool'), second_pool_updated_ret)
  383. self.conn.update_identity_pool.assert_called_with(**expected_params)
  384. def test_that_when_update_identity_pool_given_empty_dictionary_for_supported_login_providers_the_update_identity_pool_method_is_called_with_proper_request_params(self):
  385. '''
  386. Tests the request parameters to boto3 update_identity_pool's AllowUnauthenticatedIdentities is {}
  387. '''
  388. self.conn.describe_identity_pool.return_value = first_pool_ret
  389. first_pool_updated_ret = first_pool_ret.copy()
  390. first_pool_updated_ret.pop('SupportedLoginProviders')
  391. self.conn.update_identity_pool.return_value = first_pool_updated_ret
  392. expected_params = first_pool_ret.copy()
  393. expected_params['SupportedLoginProviders'] = {}
  394. expected_params.pop('DeveloperProviderName')
  395. expected_params.pop('OpenIdConnectProviderARNs')
  396. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=first_pool_id, SupportedLoginProviders={}, **conn_parameters)
  397. self.assertTrue(result.get('updated'))
  398. self.assertEqual(result.get('identity_pool'), first_pool_updated_ret)
  399. self.conn.update_identity_pool.assert_called_with(**expected_params)
  400. def test_that_when_update_identity_pool_given_empty_list_for_openid_connect_provider_arns_the_update_identity_pool_method_is_called_with_proper_request_params(self):
  401. '''
  402. Tests the request parameters to boto3 update_identity_pool's OpenIdConnectProviderARNs is []
  403. '''
  404. self.conn.describe_identity_pool.return_value = first_pool_ret
  405. first_pool_updated_ret = first_pool_ret.copy()
  406. first_pool_updated_ret.pop('OpenIdConnectProviderARNs')
  407. self.conn.update_identity_pool.return_value = first_pool_updated_ret
  408. expected_params = first_pool_ret.copy()
  409. expected_params.pop('SupportedLoginProviders')
  410. expected_params.pop('DeveloperProviderName')
  411. expected_params['OpenIdConnectProviderARNs'] = []
  412. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=first_pool_id, OpenIdConnectProviderARNs=[], **conn_parameters)
  413. self.assertTrue(result.get('updated'))
  414. self.assertEqual(result.get('identity_pool'), first_pool_updated_ret)
  415. self.conn.update_identity_pool.assert_called_with(**expected_params)
  416. def test_that_when_update_identity_pool_given_developer_provider_name_when_developer_provider_name_was_set_previously_the_udpate_identity_pool_method_is_called_without_developer_provider_name_param(self):
  417. '''
  418. Tests the request parameters do not include 'DeveloperProviderName' if this was previously set
  419. for the given pool id
  420. '''
  421. self.conn.describe_identity_pool.return_value = first_pool_ret
  422. self.conn.update_identity_pool.return_value = first_pool_ret
  423. expected_params = first_pool_ret.copy()
  424. expected_params.pop('SupportedLoginProviders')
  425. expected_params.pop('DeveloperProviderName')
  426. expected_params.pop('OpenIdConnectProviderARNs')
  427. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=first_pool_id, DeveloperProviderName='this should not change', **conn_parameters)
  428. self.assertTrue(result.get('updated'))
  429. self.assertEqual(result.get('identity_pool'), first_pool_ret)
  430. self.conn.update_identity_pool.assert_called_with(**expected_params)
  431. def test_that_when_update_identity_pool_given_developer_provider_name_is_included_in_the_params_when_associated_for_the_first_time(self):
  432. '''
  433. Tests the request parameters include 'DeveloperProviderName' when the pool did not have this
  434. property set previously.
  435. '''
  436. self.conn.describe_identity_pool.return_value = second_pool_ret
  437. second_pool_updated_ret = second_pool_ret.copy()
  438. second_pool_updated_ret['DeveloperProviderName'] = 'added_developer_provider'
  439. self.conn.update_identity_pool.return_value = second_pool_updated_ret
  440. expected_params = second_pool_updated_ret.copy()
  441. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=second_pool_id, DeveloperProviderName='added_developer_provider', **conn_parameters)
  442. self.assertTrue(result.get('updated'))
  443. self.assertEqual(result.get('identity_pool'), second_pool_updated_ret)
  444. self.conn.update_identity_pool.assert_called_with(**expected_params)
  445. def test_that_the_update_identity_pool_method_handles_exception_from_boto3(self):
  446. '''
  447. Tests the error handling of exception generated by boto3 update_identity_pool
  448. '''
  449. self.conn.describe_identity_pool.return_value = second_pool_ret
  450. second_pool_updated_ret = second_pool_ret.copy()
  451. second_pool_updated_ret['DeveloperProviderName'] = 'added_developer_provider'
  452. self.conn.update_identity_pool.side_effect = ClientError(error_content, 'update_identity_pool')
  453. result = boto_cognitoidentity.update_identity_pool(IdentityPoolId=second_pool_id, DeveloperProviderName='added_developer_provider', **conn_parameters)
  454. self.assertIs(result.get('updated'), False)
  455. self.assertEqual(result.get('error', {}).get('message'), error_message.format('update_identity_pool'))