| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- # Copyright 2023 Cloudbase Solutions Srl
- # All Rights Reserved.
- from unittest import mock
- from webob import exc
- from coriolis.api.v1 import endpoints
- from coriolis.api.v1.views import endpoint_view
- from coriolis.endpoints import api
- from coriolis import exception
- from coriolis.tests import test_base
- from coriolis.tests import testutils
- class EndpointControllerTestCase(test_base.CoriolisBaseTestCase):
- """Test suite for the Coriolis Endpoint v1 api"""
- def setUp(self):
- super(EndpointControllerTestCase, self).setUp()
- self.endpoint_api = endpoints.EndpointController()
- @mock.patch.object(endpoint_view, 'single')
- @mock.patch.object(api.API, 'get_endpoint')
- def test_show(
- self,
- mock_get_endpoint,
- mock_single,
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- id = mock.sentinel.id
- result = self.endpoint_api.show(mock_req, id)
- mock_context.can.assert_called_once_with('migration:endpoints:show')
- mock_get_endpoint.assert_called_once_with(mock_context, id)
- mock_single.assert_called_once_with(
- mock_get_endpoint.return_value
- )
- self.assertEqual(
- mock_single.return_value,
- result
- )
- @mock.patch.object(api.API, 'get_endpoint')
- def test_show_no_endpoint(
- self,
- mock_get_endpoint,
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- id = mock.sentinel.id
- mock_get_endpoint.return_value = None
- self.assertRaises(
- exc.HTTPNotFound,
- self.endpoint_api.show,
- mock_req,
- id
- )
- mock_context.can.assert_called_once_with('migration:endpoints:show')
- mock_get_endpoint.assert_called_once_with(mock_context, id)
- @mock.patch.object(endpoint_view, 'collection')
- @mock.patch.object(api.API, 'get_endpoints')
- def test_index(
- self,
- mock_get_endpoints,
- mock_collection,
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- result = self.endpoint_api.index(mock_req)
- mock_context.can.assert_called_once_with('migration:endpoints:list')
- mock_get_endpoints.assert_called_once_with(mock_context)
- mock_collection.assert_called_once_with(
- mock_get_endpoints.return_value
- )
- self.assertEqual(
- mock_collection.return_value,
- result
- )
- def test__validate_create_body(self):
- mock_body = {
- 'endpoint':
- {
- 'name': 'mock_name',
- 'type': 'mock_type',
- 'connection_info': 'mock_connection_info',
- }
- }
- endpoint = testutils.get_wrapped_function(
- self.endpoint_api._validate_create_body)(
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- self.assertEqual(
- ('mock_name', 'mock_type', None, 'mock_connection_info', []),
- endpoint
- )
- def test__validate_create_body_all_keys(self):
- mock_body = {
- 'endpoint':
- {
- 'name': 'mock_name',
- 'type': 'mock_type',
- 'description': 'mock_description',
- 'connection_info': 'mock_connection_info',
- 'mapped_regions': ['mapped_region_1', 'mapped_region_1'],
- }
- }
- endpoint = testutils.get_wrapped_function(
- self.endpoint_api._validate_create_body)(
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- self.assertEqual(
- ('mock_name', 'mock_type', 'mock_description',
- 'mock_connection_info', ['mapped_region_1', 'mapped_region_1']),
- endpoint
- )
- def test__validate_create_body_no_endpoint(self):
- mock_body = {}
- self.assertRaises(
- KeyError,
- testutils.get_wrapped_function(
- self.endpoint_api._validate_create_body),
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- @mock.patch.object(endpoint_view, 'single')
- @mock.patch.object(api.API, 'create')
- @mock.patch.object(endpoints.EndpointController, '_validate_create_body')
- def test_create(
- self,
- mock__validate_create_body,
- mock_create,
- mock_single
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- body = mock.sentinel.body
- mock__validate_create_body.return_value = (
- mock.sentinel.name,
- mock.sentinel.endpoint_type,
- mock.sentinel.description,
- mock.sentinel.connection_info,
- mock.sentinel.mapped_regions
- )
- result = self.endpoint_api.create(mock_req, body)
- mock_context.can.assert_called_once_with('migration:endpoints:create')
- mock_create.assert_called_once_with(
- mock_context,
- mock.sentinel.name,
- mock.sentinel.endpoint_type,
- mock.sentinel.description,
- mock.sentinel.connection_info,
- mock.sentinel.mapped_regions
- )
- self.assertEqual(
- mock_single.return_value,
- result
- )
- def test__validate_update_body(self):
- mock_body = {
- 'endpoint':
- {
- 'name': 'mock_name',
- 'description': 'mock_description',
- 'connection_info': 'mock_connection_info',
- 'mapped_regions': ['mapped_region_1', 'mapped_region_1'],
- }
- }
- endpoint = testutils.get_wrapped_function(
- self.endpoint_api._validate_update_body)(
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- self.assertEqual(
- mock_body['endpoint'],
- endpoint
- )
- def test__validate_update_body_no_keys(self):
- mock_body = {
- 'endpoint': {}
- }
- endpoint = testutils.get_wrapped_function(
- self.endpoint_api._validate_update_body)(
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- self.assertEqual(
- {},
- endpoint
- )
- def test__validate_update_body_no_endpoint(self):
- mock_body = {}
- self.assertRaises(
- KeyError,
- testutils.get_wrapped_function(
- self.endpoint_api._validate_update_body),
- self.endpoint_api,
- body=mock_body, # type: ignore
- )
- @mock.patch.object(endpoint_view, 'single')
- @mock.patch.object(api.API, 'update')
- @mock.patch.object(endpoints.EndpointController, '_validate_update_body')
- def test_update(
- self,
- mock__validate_update_body,
- mock_update,
- mock_single
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- id = mock.sentinel.id
- body = mock.sentinel.body
- result = self.endpoint_api.update(mock_req, id, body)
- mock_context.can.assert_called_once_with('migration:endpoints:update')
- mock__validate_update_body.assert_called_once_with(body)
- mock_update.assert_called_once_with(
- mock_context, id,
- mock__validate_update_body.return_value
- )
- self.assertEqual(
- mock_single.return_value,
- result
- )
- @mock.patch.object(api.API, 'delete')
- def test_delete(
- self,
- mock_delete
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- id = mock.sentinel.id
- self.assertRaises(
- exc.HTTPNoContent,
- self.endpoint_api.delete,
- mock_req,
- id
- )
- mock_context.can.assert_called_once_with('migration:endpoints:delete')
- mock_delete.assert_called_once_with(mock_context, id)
- @mock.patch.object(api.API, 'delete')
- def test_delete_not_found(
- self,
- mock_delete
- ):
- mock_req = mock.Mock()
- mock_context = mock.Mock()
- mock_req.environ = {'coriolis.context': mock_context}
- id = mock.sentinel.id
- mock_delete.side_effect = exception.NotFound()
- self.assertRaises(
- exc.HTTPNotFound,
- self.endpoint_api.delete,
- mock_req,
- id
- )
- mock_context.can.assert_called_once_with('migration:endpoints:delete')
- mock_delete.assert_called_once_with(mock_context, id)
|