From 5db986f697d3bce0485e4b43d4f058058bc18de3 Mon Sep 17 00:00:00 2001 From: Nathan Swain Date: Wed, 2 Feb 2022 11:41:33 -0700 Subject: [PATCH] Fix for Spatial Dataset Service created w/ CLI does not work until manually saved in admin pages #763 --- .../test_tethys_cli/test_services_commands.py | 113 ++++++++++++++++-- tethys_cli/services_commands.py | 21 +++- 2 files changed, 119 insertions(+), 15 deletions(-) diff --git a/tests/unit_tests/test_tethys_cli/test_services_commands.py b/tests/unit_tests/test_tethys_cli/test_services_commands.py index e5a2d6a80..e7d19672b 100644 --- a/tests/unit_tests/test_tethys_cli/test_services_commands.py +++ b/tests/unit_tests/test_tethys_cli/test_services_commands.py @@ -259,6 +259,7 @@ def test_services_create_spatial_command_IndexError(self, mock_service, mock_pre """ mock_args = mock.MagicMock() mock_args.connection = 'IndexError:9876@IndexError' # No 'http' or '://' + mock_args.type = 'GeoServer' services_create_spatial_command(mock_args) @@ -282,6 +283,7 @@ def test_services_create_spatial_command_FormatError(self, mock_service, mock_pr mock_args = mock.MagicMock() mock_args.connection = 'foo:pass@http:://foo:1234' mock_args.public_endpoint = 'foo@foo:foo' # No 'http' or '://' + mock_args.type = 'GeoServer' services_create_spatial_command(mock_args) @@ -305,6 +307,7 @@ def test_services_create_spatial_command_IntegrityError(self, mock_service, mock mock_args = mock.MagicMock() mock_args.connection = 'foo:pass@http:://foo:1234' mock_args.public_endpoint = 'http://foo:1234' + mock_args.type = 'GeoServer' mock_service.side_effect = IntegrityError services_create_spatial_command(mock_args) @@ -318,7 +321,7 @@ def test_services_create_spatial_command_IntegrityError(self, mock_service, mock @mock.patch('tethys_cli.services_commands.pretty_output') @mock.patch('tethys_services.models.SpatialDatasetService') - def test_services_create_spatial_command(self, mock_service, mock_pretty_output): + def test_services_create_spatial_command_geoserver(self, mock_service, mock_pretty_output): """ Test for services_create_spatial_command For going through the function and saving @@ -326,10 +329,48 @@ def test_services_create_spatial_command(self, mock_service, mock_pretty_output) :param mock_pretty_output: mock for pretty_output text :return: """ - mock_args = mock.MagicMock() - mock_args.connection = 'foo:pass@http:://foo:1234' - mock_args.public_endpoint = 'http://foo:1234' - mock_service.return_value = mock.MagicMock() + mock_args = mock.MagicMock( + connection='foo:pass@http://localhost:8181/geoserver/rest/', + public_endpoint='https://www.example.com:443/geoserver/rest/', + apikey='apikey123', + type='GeoServer' + ) + mock_args.name = 'test_geoserver' + + services_create_spatial_command(mock_args) + + mock_service.assert_called() + + po_call_args = mock_pretty_output().__enter__().write.call_args_list + self.assertEqual(1, len(po_call_args)) + self.assertEqual('Successfully created new Spatial Dataset Service!', po_call_args[0][0][0]) + mock_service.assert_called_with( + name='test_geoserver', + endpoint='http://localhost:8181/geoserver/rest/', + public_endpoint='https://www.example.com:443/geoserver/rest/', + apikey='apikey123', + username='foo', + password='pass', + engine=mock_service.GEOSERVER + ) + + @mock.patch('tethys_cli.services_commands.pretty_output') + @mock.patch('tethys_services.models.SpatialDatasetService') + def test_services_create_spatial_command_thredds(self, mock_service, mock_pretty_output): + """ + Test for services_create_spatial_command + For going through the function and saving + :param mock_service: mock for SpatialDatasetService + :param mock_pretty_output: mock for pretty_output text + :return: + """ + mock_args = mock.MagicMock( + connection='foo:pass@http://localhost:8181/thredds/catalog.xml', + public_endpoint='https://www.example.com:443/thredds/catalog.xml', + apikey='apikey123', + type='THREDDS' + ) + mock_args.name = 'test_thredds' services_create_spatial_command(mock_args) @@ -338,6 +379,15 @@ def test_services_create_spatial_command(self, mock_service, mock_pretty_output) po_call_args = mock_pretty_output().__enter__().write.call_args_list self.assertEqual(1, len(po_call_args)) self.assertEqual('Successfully created new Spatial Dataset Service!', po_call_args[0][0][0]) + mock_service.assert_called_with( + name='test_thredds', + endpoint='http://localhost:8181/thredds/catalog.xml', + public_endpoint='https://www.example.com:443/thredds/catalog.xml', + apikey='apikey123', + username='foo', + password='pass', + engine=mock_service.THREDDS + ) @mock.patch('tethys_cli.services_commands.pretty_output') @mock.patch('tethys_cli.services_commands.exit') @@ -724,6 +774,7 @@ def test_services_create_dataset_command_IndexError(self, mock_service, mock_pre mock_args = mock.MagicMock() mock_args.connection = 'IndexError:9876@IndexError' # No 'http' or '://' + mock_args.type = 'HydroShare' services_create_dataset_command(mock_args) @@ -741,6 +792,7 @@ def test_services_create_dataset_command_FormatError(self, mock_service, mock_pr mock_args = mock.MagicMock() mock_args.connection = 'foo:pass@http:://foo:1234' mock_args.public_endpoint = 'foo@foo:foo' # No 'http' or '://' + mock_args.type = 'HydroShare' services_create_dataset_command(mock_args) @@ -758,6 +810,7 @@ def test_services_create_dataset_command_IntegrityError(self, mock_service, mock mock_args = mock.MagicMock() mock_args.connection = 'foo:pass@http:://foo:1234' mock_args.public_endpoint = 'http://foo:1234' + mock_args.type = 'HydroShare' mock_service.side_effect = IntegrityError services_create_dataset_command(mock_args) @@ -771,11 +824,42 @@ def test_services_create_dataset_command_IntegrityError(self, mock_service, mock @mock.patch('tethys_cli.services_commands.pretty_output') @mock.patch('tethys_services.models.DatasetService') - def test_services_create_dataset_command(self, mock_service, mock_pretty_output): - mock_args = mock.MagicMock() - mock_args.connection = 'foo:pass@http:://foo:1234' - mock_args.public_endpoint = 'http://foo:1234' - mock_service.return_value = mock.MagicMock() + def test_services_create_dataset_command_hydroshare(self, mock_service, mock_pretty_output): + mock_args = mock.MagicMock( + connection='foo:pass@http://localhost:80', + public_endpoint='http://www.example.com:80', + apikey='apikey123', + type='HydroShare' + ) + mock_args.name = 'test_hydroshare' + + services_create_dataset_command(mock_args) + + mock_service.assert_called() + + po_call_args = mock_pretty_output().__enter__().write.call_args_list + self.assertEqual(1, len(po_call_args)) + self.assertEqual('Successfully created new Dataset Service!', po_call_args[0][0][0]) + mock_service.assert_called_with( + name='test_hydroshare', + endpoint='http://localhost:80', + public_endpoint='http://www.example.com:80', + apikey='apikey123', + username='foo', + password='pass', + engine=mock_service.HYDROSHARE + ) + + @mock.patch('tethys_cli.services_commands.pretty_output') + @mock.patch('tethys_services.models.DatasetService') + def test_services_create_dataset_command_ckan(self, mock_service, mock_pretty_output): + mock_args = mock.MagicMock( + connection='foo:pass@http://localhost:80', + public_endpoint='http://www.example.com:80', + apikey='apikey123', + type='CKAN' + ) + mock_args.name = 'test_ckan' services_create_dataset_command(mock_args) @@ -784,6 +868,15 @@ def test_services_create_dataset_command(self, mock_service, mock_pretty_output) po_call_args = mock_pretty_output().__enter__().write.call_args_list self.assertEqual(1, len(po_call_args)) self.assertEqual('Successfully created new Dataset Service!', po_call_args[0][0][0]) + mock_service.assert_called_with( + name='test_ckan', + endpoint='http://localhost:80', + public_endpoint='http://www.example.com:80', + apikey='apikey123', + username='foo', + password='pass', + engine=mock_service.CKAN + ) @mock.patch('tethys_cli.services_commands.input') @mock.patch('tethys_cli.services_commands.pretty_output') diff --git a/tethys_cli/services_commands.py b/tethys_cli/services_commands.py index cba0d325b..3b427966a 100644 --- a/tethys_cli/services_commands.py +++ b/tethys_cli/services_commands.py @@ -183,18 +183,24 @@ def services_create_spatial_command(args): apikey = args.apikey or '' service_type = args.type + engines = { + 'GeoServer': SpatialDatasetService.GEOSERVER, + 'THREDDS': SpatialDatasetService.THREDDS + } + if 'http' not in endpoint or '://' not in endpoint: raise IndexError() if public_endpoint and 'http' not in public_endpoint or '://' not in public_endpoint: raise FormatError() - endpoint = add_geoserver_rest_to_endpoint(endpoint) - if public_endpoint: - public_endpoint = add_geoserver_rest_to_endpoint(public_endpoint) + if service_type == 'GeoServer': + endpoint = add_geoserver_rest_to_endpoint(endpoint) + if public_endpoint: + public_endpoint = add_geoserver_rest_to_endpoint(public_endpoint) new_persistent_service = SpatialDatasetService(name=name, endpoint=endpoint, public_endpoint=public_endpoint, apikey=apikey, username=service_username, - password=service_password, engine=service_type) + password=service_password, engine=engines[service_type]) new_persistent_service.save() with pretty_output(FG_GREEN) as p: @@ -230,6 +236,11 @@ def services_create_dataset_command(args): apikey = args.apikey or '' service_type = args.type + engines = { + 'CKAN': DatasetService.CKAN, + 'HydroShare': DatasetService.HYDROSHARE + } + if 'http' not in endpoint or '://' not in endpoint: raise IndexError() @@ -239,7 +250,7 @@ def services_create_dataset_command(args): new_persistent_service = DatasetService(name=name, endpoint=endpoint, public_endpoint=public_endpoint, apikey=apikey, username=service_username, - password=service_password, engine=service_type) + password=service_password, engine=engines[service_type]) new_persistent_service.save() with pretty_output(FG_GREEN) as p: