From 78ffebcf40348503a8685f3766c937f2dc3881f0 Mon Sep 17 00:00:00 2001 From: Alexander J Sheehan Date: Sun, 22 Oct 2023 15:23:50 +0000 Subject: [PATCH] chore: returning SP metadata url from the sso orchestrator to the API caller --- CHANGELOG.rst | 4 ++++ enterprise/__init__.py | 2 +- .../views/enterprise_customer_sso_configuration.py | 14 ++++++++------ enterprise/api_client/sso_orchestrator.py | 5 +++-- enterprise/models.py | 3 ++- tests/test_enterprise/api/test_views.py | 10 +++++----- .../api_client/test_sso_orchestrator.py | 4 ++-- 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 82839a0534..624c9800be 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,10 @@ Change Log Unreleased ---------- +[4.6.9] +------- +chore: returning SP metadata url from the sso orchestrator to the API caller + [4.6.8] ------- feat: truncate API Response before writing to the APIResponseRecord diff --git a/enterprise/__init__.py b/enterprise/__init__.py index 9f9aa82975..d878e19306 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,4 +2,4 @@ Your project description goes here. """ -__version__ = "4.6.8" +__version__ = "4.6.9" diff --git a/enterprise/api/v1/views/enterprise_customer_sso_configuration.py b/enterprise/api/v1/views/enterprise_customer_sso_configuration.py index 252f7aed1f..f123e136e9 100644 --- a/enterprise/api/v1/views/enterprise_customer_sso_configuration.py +++ b/enterprise/api/v1/views/enterprise_customer_sso_configuration.py @@ -259,12 +259,12 @@ def create(self, request, *args, **kwargs): try: with transaction.atomic(): new_record = EnterpriseCustomerSsoConfiguration.objects.create(**request_data) - new_record.submit_for_configuration() + sp_metadata_url = new_record.submit_for_configuration() except (TypeError, SsoOrchestratorClientError) as e: LOGGER.error(f'{CONFIG_CREATE_ERROR} {e}') return Response({'error': f'{CONFIG_CREATE_ERROR} {e}'}, status=HTTP_400_BAD_REQUEST) - return Response({'data': new_record.pk}, status=HTTP_201_CREATED) + return Response({'record': new_record.pk, 'sp_metadata_url': sp_metadata_url}, status=HTTP_201_CREATED) @permission_required( 'enterprise.can_access_admin_dashboard', @@ -321,12 +321,14 @@ def update(self, request, *args, **kwargs): try: with transaction.atomic(): sso_configuration_record.update(**request_data) - sso_configuration_record.first().submit_for_configuration(updating_existing_record=True) + sp_metadata_url = sso_configuration_record.first().submit_for_configuration( + updating_existing_record=True + ) except (TypeError, FieldDoesNotExist, ValidationError, SsoOrchestratorClientError) as e: - LOGGER.error(f'{CONFIG_UPDATE_ERROR}{e}') - return Response({'error': f'{CONFIG_UPDATE_ERROR}{e}'}, status=HTTP_400_BAD_REQUEST) + LOGGER.error(f'{CONFIG_UPDATE_ERROR} {e}') + return Response({'error': f'{CONFIG_UPDATE_ERROR} {e}'}, status=HTTP_400_BAD_REQUEST) serializer = self.serializer_class(sso_configuration_record.first()) - return Response(serializer.data, status=HTTP_200_OK) + return Response({'record': serializer.data, 'sp_metadata_url': sp_metadata_url}, status=HTTP_200_OK) @permission_required( 'enterprise.can_access_admin_dashboard', diff --git a/enterprise/api_client/sso_orchestrator.py b/enterprise/api_client/sso_orchestrator.py index 38df893dd7..693987aa2c 100644 --- a/enterprise/api_client/sso_orchestrator.py +++ b/enterprise/api_client/sso_orchestrator.py @@ -102,7 +102,7 @@ def _post(self, url, data=None): f"Failed to make SSO Orchestrator API request: {response.status_code}", response=response, ) - return response.status_code + return response.json() def configure_sso_orchestration_record( self, @@ -131,4 +131,5 @@ def configure_sso_orchestration_record( if is_sap or sap_config_data: request_data['sapsfConfiguration'] = sap_config_data - return self._post(self._get_orchestrator_configure_url(), data=request_data) + response = self._post(self._get_orchestrator_configure_url(), data=request_data) + return response.get('samlServiceProviderInformation', {}).get('spMetadataUrl', {}) diff --git a/enterprise/models.py b/enterprise/models.py index 4cd6fba832..9ab6485395 100644 --- a/enterprise/models.py +++ b/enterprise/models.py @@ -4095,7 +4095,7 @@ def submit_for_configuration(self, updating_existing_record=False): elif field_value := getattr(self, field): config_data[utils.camelCase(field)] = field_value - EnterpriseSSOOrchestratorApiClient().configure_sso_orchestration_record( + sp_metadata_url = EnterpriseSSOOrchestratorApiClient().configure_sso_orchestration_record( config_data=config_data, config_pk=self.pk, enterprise_data={ @@ -4109,3 +4109,4 @@ def submit_for_configuration(self, updating_existing_record=False): ) self.submitted_at = localized_utcnow() self.save() + return sp_metadata_url diff --git a/tests/test_enterprise/api/test_views.py b/tests/test_enterprise/api/test_views.py index 375298d904..95b77d5007 100644 --- a/tests/test_enterprise/api/test_views.py +++ b/tests/test_enterprise/api/test_views.py @@ -7529,8 +7529,8 @@ def test_sso_configuration_create(self): response = self.post_new_sso_configuration(data) assert response.status_code == status.HTTP_201_CREATED assert len(EnterpriseCustomerSsoConfiguration.objects.all()) == 1 - created_record = EnterpriseCustomerSsoConfiguration.objects.all().first().uuid - assert response.data['data'] == created_record + created_record_uuid = EnterpriseCustomerSsoConfiguration.objects.all().first().uuid + assert response.data['record'] == created_record_uuid def test_sso_configuration_create_permissioning(self): """ @@ -7766,7 +7766,7 @@ def test_sso_configurations_update_submitted_config(self): assert sent_body_params['requestIdentifier'] == str(config_pk) @responses.activate - def test_sso_configuration_update_x(self): + def test_sso_configuration_update_success(self): """ Test expected response when successfully updating an existing sso configuration. """ @@ -7797,8 +7797,8 @@ def test_sso_configuration_update_x(self): } response = self.update_sso_configuration(config_pk, data) assert response.status_code == status.HTTP_200_OK - assert response.json()['uuid'] == str(enterprise_sso_orchestration_config.uuid) - assert response.json()['metadata_url'] == "https://example.com/metadata_update.xml" + assert response.json()['record']['uuid'] == str(enterprise_sso_orchestration_config.uuid) + assert response.json()['record']['metadata_url'] == "https://example.com/metadata_update.xml" enterprise_sso_orchestration_config.refresh_from_db() assert enterprise_sso_orchestration_config.metadata_url == "https://example.com/metadata_update.xml" diff --git a/tests/test_enterprise/api_client/test_sso_orchestrator.py b/tests/test_enterprise/api_client/test_sso_orchestrator.py index 88894451b5..683fe2fec6 100644 --- a/tests/test_enterprise/api_client/test_sso_orchestrator.py +++ b/tests/test_enterprise/api_client/test_sso_orchestrator.py @@ -28,7 +28,7 @@ def test_post_sso_configuration(): responses.add( responses.POST, SSO_ORCHESTRATOR_CONFIGURE_URL, - json={}, + json={'samlServiceProviderInformation': {'spMetadataUrl': 'https://example.com'}}, ) client = sso_orchestrator.EnterpriseSSOOrchestratorApiClient() actual_response = client.configure_sso_orchestration_record( @@ -36,7 +36,7 @@ def test_post_sso_configuration(): config_pk=TEST_ENTERPRISE_SSO_CONFIG_UUID, enterprise_data={'uuid': TEST_ENTERPRISE_ID, 'name': TEST_ENTERPRISE_NAME, 'slug': TEST_ENTERPRISE_NAME}, ) - assert actual_response == 200 + assert actual_response == 'https://example.com' responses.assert_call_count(count=1, url=SSO_ORCHESTRATOR_CONFIGURE_URL) sent_body_params = json.loads(responses.calls[0].request.body)