2
0

test_endpoints.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Copyright 2023 Cloudbase Solutions Srl
  2. # All Rights Reserved.
  3. from unittest import mock
  4. from webob import exc
  5. from coriolis.api.v1 import endpoints
  6. from coriolis.api.v1.views import endpoint_view
  7. from coriolis.endpoints import api
  8. from coriolis import exception
  9. from coriolis.tests import test_base
  10. from coriolis.tests import testutils
  11. class EndpointControllerTestCase(test_base.CoriolisBaseTestCase):
  12. """Test suite for the Coriolis Endpoint v1 api"""
  13. def setUp(self):
  14. super(EndpointControllerTestCase, self).setUp()
  15. self.endpoint_api = endpoints.EndpointController()
  16. @mock.patch.object(endpoint_view, 'single')
  17. @mock.patch.object(api.API, 'get_endpoint')
  18. def test_show(
  19. self,
  20. mock_get_endpoint,
  21. mock_single,
  22. ):
  23. mock_req = mock.Mock()
  24. mock_context = mock.Mock()
  25. mock_req.environ = {'coriolis.context': mock_context}
  26. id = mock.sentinel.id
  27. result = self.endpoint_api.show(mock_req, id)
  28. mock_context.can.assert_called_once_with('migration:endpoints:show')
  29. mock_get_endpoint.assert_called_once_with(mock_context, id)
  30. mock_single.assert_called_once_with(
  31. mock_get_endpoint.return_value
  32. )
  33. self.assertEqual(
  34. mock_single.return_value,
  35. result
  36. )
  37. @mock.patch.object(api.API, 'get_endpoint')
  38. def test_show_no_endpoint(
  39. self,
  40. mock_get_endpoint,
  41. ):
  42. mock_req = mock.Mock()
  43. mock_context = mock.Mock()
  44. mock_req.environ = {'coriolis.context': mock_context}
  45. id = mock.sentinel.id
  46. mock_get_endpoint.return_value = None
  47. self.assertRaises(
  48. exc.HTTPNotFound,
  49. self.endpoint_api.show,
  50. mock_req,
  51. id
  52. )
  53. mock_context.can.assert_called_once_with('migration:endpoints:show')
  54. mock_get_endpoint.assert_called_once_with(mock_context, id)
  55. @mock.patch.object(endpoint_view, 'collection')
  56. @mock.patch.object(api.API, 'get_endpoints')
  57. def test_index(
  58. self,
  59. mock_get_endpoints,
  60. mock_collection,
  61. ):
  62. mock_req = mock.Mock()
  63. mock_context = mock.Mock()
  64. mock_req.environ = {'coriolis.context': mock_context}
  65. result = self.endpoint_api.index(mock_req)
  66. mock_context.can.assert_called_once_with('migration:endpoints:list')
  67. mock_get_endpoints.assert_called_once_with(mock_context)
  68. mock_collection.assert_called_once_with(
  69. mock_get_endpoints.return_value
  70. )
  71. self.assertEqual(
  72. mock_collection.return_value,
  73. result
  74. )
  75. def test__validate_create_body(self):
  76. mock_body = {
  77. 'endpoint':
  78. {
  79. 'name': 'mock_name',
  80. 'type': 'mock_type',
  81. 'connection_info': 'mock_connection_info',
  82. }
  83. }
  84. endpoint = testutils.get_wrapped_function(
  85. self.endpoint_api._validate_create_body)(
  86. self.endpoint_api,
  87. body=mock_body, # type: ignore
  88. )
  89. self.assertEqual(
  90. ('mock_name', 'mock_type', None, 'mock_connection_info', []),
  91. endpoint
  92. )
  93. def test__validate_create_body_all_keys(self):
  94. mock_body = {
  95. 'endpoint':
  96. {
  97. 'name': 'mock_name',
  98. 'type': 'mock_type',
  99. 'description': 'mock_description',
  100. 'connection_info': 'mock_connection_info',
  101. 'mapped_regions': ['mapped_region_1', 'mapped_region_1'],
  102. }
  103. }
  104. endpoint = testutils.get_wrapped_function(
  105. self.endpoint_api._validate_create_body)(
  106. self.endpoint_api,
  107. body=mock_body, # type: ignore
  108. )
  109. self.assertEqual(
  110. ('mock_name', 'mock_type', 'mock_description',
  111. 'mock_connection_info', ['mapped_region_1', 'mapped_region_1']),
  112. endpoint
  113. )
  114. def test__validate_create_body_no_endpoint(self):
  115. mock_body = {}
  116. self.assertRaises(
  117. KeyError,
  118. testutils.get_wrapped_function(
  119. self.endpoint_api._validate_create_body),
  120. self.endpoint_api,
  121. body=mock_body, # type: ignore
  122. )
  123. @mock.patch.object(endpoint_view, 'single')
  124. @mock.patch.object(api.API, 'create')
  125. @mock.patch.object(endpoints.EndpointController, '_validate_create_body')
  126. def test_create(
  127. self,
  128. mock__validate_create_body,
  129. mock_create,
  130. mock_single
  131. ):
  132. mock_req = mock.Mock()
  133. mock_context = mock.Mock()
  134. mock_req.environ = {'coriolis.context': mock_context}
  135. body = mock.sentinel.body
  136. mock__validate_create_body.return_value = (
  137. mock.sentinel.name,
  138. mock.sentinel.endpoint_type,
  139. mock.sentinel.description,
  140. mock.sentinel.connection_info,
  141. mock.sentinel.mapped_regions
  142. )
  143. result = self.endpoint_api.create(mock_req, body)
  144. mock_context.can.assert_called_once_with('migration:endpoints:create')
  145. mock_create.assert_called_once_with(
  146. mock_context,
  147. mock.sentinel.name,
  148. mock.sentinel.endpoint_type,
  149. mock.sentinel.description,
  150. mock.sentinel.connection_info,
  151. mock.sentinel.mapped_regions
  152. )
  153. self.assertEqual(
  154. mock_single.return_value,
  155. result
  156. )
  157. def test__validate_update_body(self):
  158. mock_body = {
  159. 'endpoint':
  160. {
  161. 'name': 'mock_name',
  162. 'description': 'mock_description',
  163. 'connection_info': 'mock_connection_info',
  164. 'mapped_regions': ['mapped_region_1', 'mapped_region_1'],
  165. }
  166. }
  167. endpoint = testutils.get_wrapped_function(
  168. self.endpoint_api._validate_update_body)(
  169. self.endpoint_api,
  170. body=mock_body, # type: ignore
  171. )
  172. self.assertEqual(
  173. mock_body['endpoint'],
  174. endpoint
  175. )
  176. def test__validate_update_body_no_keys(self):
  177. mock_body = {
  178. 'endpoint': {}
  179. }
  180. endpoint = testutils.get_wrapped_function(
  181. self.endpoint_api._validate_update_body)(
  182. self.endpoint_api,
  183. body=mock_body, # type: ignore
  184. )
  185. self.assertEqual(
  186. {},
  187. endpoint
  188. )
  189. def test__validate_update_body_no_endpoint(self):
  190. mock_body = {}
  191. self.assertRaises(
  192. KeyError,
  193. testutils.get_wrapped_function(
  194. self.endpoint_api._validate_update_body),
  195. self.endpoint_api,
  196. body=mock_body, # type: ignore
  197. )
  198. @mock.patch.object(endpoint_view, 'single')
  199. @mock.patch.object(api.API, 'update')
  200. @mock.patch.object(endpoints.EndpointController, '_validate_update_body')
  201. def test_update(
  202. self,
  203. mock__validate_update_body,
  204. mock_update,
  205. mock_single
  206. ):
  207. mock_req = mock.Mock()
  208. mock_context = mock.Mock()
  209. mock_req.environ = {'coriolis.context': mock_context}
  210. id = mock.sentinel.id
  211. body = mock.sentinel.body
  212. result = self.endpoint_api.update(mock_req, id, body)
  213. mock_context.can.assert_called_once_with('migration:endpoints:update')
  214. mock__validate_update_body.assert_called_once_with(body)
  215. mock_update.assert_called_once_with(
  216. mock_context, id,
  217. mock__validate_update_body.return_value
  218. )
  219. self.assertEqual(
  220. mock_single.return_value,
  221. result
  222. )
  223. @mock.patch.object(api.API, 'delete')
  224. def test_delete(
  225. self,
  226. mock_delete
  227. ):
  228. mock_req = mock.Mock()
  229. mock_context = mock.Mock()
  230. mock_req.environ = {'coriolis.context': mock_context}
  231. id = mock.sentinel.id
  232. self.assertRaises(
  233. exc.HTTPNoContent,
  234. self.endpoint_api.delete,
  235. mock_req,
  236. id
  237. )
  238. mock_context.can.assert_called_once_with('migration:endpoints:delete')
  239. mock_delete.assert_called_once_with(mock_context, id)
  240. @mock.patch.object(api.API, 'delete')
  241. def test_delete_not_found(
  242. self,
  243. mock_delete
  244. ):
  245. mock_req = mock.Mock()
  246. mock_context = mock.Mock()
  247. mock_req.environ = {'coriolis.context': mock_context}
  248. id = mock.sentinel.id
  249. mock_delete.side_effect = exception.NotFound()
  250. self.assertRaises(
  251. exc.HTTPNotFound,
  252. self.endpoint_api.delete,
  253. mock_req,
  254. id
  255. )
  256. mock_context.can.assert_called_once_with('migration:endpoints:delete')
  257. mock_delete.assert_called_once_with(mock_context, id)